CTheme.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * CTheme class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CTheme represents an application theme.
  12. *
  13. * @property string $name Theme name.
  14. * @property string $baseUrl The relative URL to the theme folder (without ending slash).
  15. * @property string $basePath The file path to the theme folder.
  16. * @property string $viewPath The path for controller views. Defaults to 'ThemeRoot/views'.
  17. * @property string $systemViewPath The path for system views. Defaults to 'ThemeRoot/views/system'.
  18. * @property string $skinPath The path for widget skins. Defaults to 'ThemeRoot/views/skins'.
  19. *
  20. * @author Qiang Xue <qiang.xue@gmail.com>
  21. * @package system.web
  22. * @since 1.0
  23. */
  24. class CTheme extends CComponent
  25. {
  26. private $_name;
  27. private $_basePath;
  28. private $_baseUrl;
  29. /**
  30. * Constructor.
  31. * @param string $name name of the theme
  32. * @param string $basePath base theme path
  33. * @param string $baseUrl base theme URL
  34. */
  35. public function __construct($name,$basePath,$baseUrl)
  36. {
  37. $this->_name=$name;
  38. $this->_baseUrl=$baseUrl;
  39. $this->_basePath=$basePath;
  40. }
  41. /**
  42. * @return string theme name
  43. */
  44. public function getName()
  45. {
  46. return $this->_name;
  47. }
  48. /**
  49. * @return string the relative URL to the theme folder (without ending slash)
  50. */
  51. public function getBaseUrl()
  52. {
  53. return $this->_baseUrl;
  54. }
  55. /**
  56. * @return string the file path to the theme folder
  57. */
  58. public function getBasePath()
  59. {
  60. return $this->_basePath;
  61. }
  62. /**
  63. * @return string the path for controller views. Defaults to 'ThemeRoot/views'.
  64. */
  65. public function getViewPath()
  66. {
  67. return $this->_basePath.DIRECTORY_SEPARATOR.'views';
  68. }
  69. /**
  70. * @return string the path for system views. Defaults to 'ThemeRoot/views/system'.
  71. */
  72. public function getSystemViewPath()
  73. {
  74. return $this->getViewPath().DIRECTORY_SEPARATOR.'system';
  75. }
  76. /**
  77. * @return string the path for widget skins. Defaults to 'ThemeRoot/views/skins'.
  78. * @since 1.1
  79. */
  80. public function getSkinPath()
  81. {
  82. return $this->getViewPath().DIRECTORY_SEPARATOR.'skins';
  83. }
  84. /**
  85. * Finds the view file for the specified controller's view.
  86. * @param CController $controller the controller
  87. * @param string $viewName the view name
  88. * @return string the view file path. False if the file does not exist.
  89. */
  90. public function getViewFile($controller,$viewName)
  91. {
  92. $moduleViewPath=$this->getViewPath();
  93. if(($module=$controller->getModule())!==null)
  94. $moduleViewPath.='/'.$module->getId();
  95. return $controller->resolveViewFile($viewName,$this->getViewPath().'/'.$controller->getUniqueId(),$this->getViewPath(),$moduleViewPath);
  96. }
  97. /**
  98. * Finds the layout file for the specified controller's layout.
  99. * @param CController $controller the controller
  100. * @param string $layoutName the layout name
  101. * @return string the layout file path. False if the file does not exist.
  102. */
  103. public function getLayoutFile($controller,$layoutName)
  104. {
  105. $moduleViewPath=$basePath=$this->getViewPath();
  106. $module=$controller->getModule();
  107. if(empty($layoutName))
  108. {
  109. while($module!==null)
  110. {
  111. if($module->layout===false)
  112. return false;
  113. if(!empty($module->layout))
  114. break;
  115. $module=$module->getParentModule();
  116. }
  117. if($module===null)
  118. $layoutName=Yii::app()->layout;
  119. else
  120. {
  121. $layoutName=$module->layout;
  122. $moduleViewPath.='/'.$module->getId();
  123. }
  124. }
  125. elseif($module!==null)
  126. $moduleViewPath.='/'.$module->getId();
  127. return $controller->resolveViewFile($layoutName,$moduleViewPath.'/layouts',$basePath,$moduleViewPath);
  128. }
  129. }