1: <?php
2: /**
3: * CThemeManager 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: /**
12: * CThemeManager manages the themes for the Web application.
13: *
14: * A theme is a collection of view/layout files and resource files
15: * (e.g. css, image, js files). When a theme is active, {@link CController}
16: * will look for the specified view/layout under the theme folder first.
17: * The corresponding view/layout files will be used if the theme provides them.
18: * Otherwise, the default view/layout files will be used.
19: *
20: * By default, each theme is organized as a directory whose name is the theme name.
21: * All themes are located under the "WebRootPath/themes" directory.
22: *
23: * To activate a theme, set the {@link CWebApplication::setTheme theme} property
24: * to be the name of that theme.
25: *
26: * Since a self-contained theme often contains resource files that are made
27: * Web accessible, please make sure the view/layout files are protected from Web access.
28: *
29: * @property array $themeNames List of available theme names.
30: * @property string $basePath The base path for all themes. Defaults to "WebRootPath/themes".
31: * @property string $baseUrl The base URL for all themes. Defaults to "/WebRoot/themes".
32: *
33: * @author Qiang Xue <qiang.xue@gmail.com>
34: * @package system.web
35: * @since 1.0
36: */
37: class CThemeManager extends CApplicationComponent
38: {
39: /**
40: * default themes base path
41: */
42: const DEFAULT_BASEPATH='themes';
43:
44: /**
45: * @var string the name of the theme class for representing a theme.
46: * Defaults to {@link CTheme}. This can also be a class name in dot syntax.
47: */
48: public $themeClass='CTheme';
49:
50: private $_basePath=null;
51: private $_baseUrl=null;
52:
53:
54: /**
55: * @param string $name name of the theme to be retrieved
56: * @return CTheme the theme retrieved. Null if the theme does not exist.
57: */
58: public function getTheme($name)
59: {
60: $themePath=$this->getBasePath().DIRECTORY_SEPARATOR.$name;
61: if(is_dir($themePath))
62: {
63: $class=Yii::import($this->themeClass, true);
64: return new $class($name,$themePath,$this->getBaseUrl().'/'.$name);
65: }
66: else
67: return null;
68: }
69:
70: /**
71: * @return array list of available theme names
72: */
73: public function getThemeNames()
74: {
75: static $themes;
76: if($themes===null)
77: {
78: $themes=array();
79: $basePath=$this->getBasePath();
80: $folder=@opendir($basePath);
81: while(($file=@readdir($folder))!==false)
82: {
83: if($file!=='.' && $file!=='..' && $file!=='.svn' && $file!=='.gitignore' && is_dir($basePath.DIRECTORY_SEPARATOR.$file))
84: $themes[]=$file;
85: }
86: closedir($folder);
87: sort($themes);
88: }
89: return $themes;
90: }
91:
92: /**
93: * @return string the base path for all themes. Defaults to "WebRootPath/themes".
94: */
95: public function getBasePath()
96: {
97: if($this->_basePath===null)
98: $this->setBasePath(dirname(Yii::app()->getRequest()->getScriptFile()).DIRECTORY_SEPARATOR.self::DEFAULT_BASEPATH);
99: return $this->_basePath;
100: }
101:
102: /**
103: * @param string $value the base path for all themes.
104: * @throws CException if the base path does not exist
105: */
106: public function setBasePath($value)
107: {
108: $this->_basePath=realpath($value);
109: if($this->_basePath===false || !is_dir($this->_basePath))
110: throw new CException(Yii::t('yii','Theme directory "{directory}" does not exist.',array('{directory}'=>$value)));
111: }
112:
113: /**
114: * @return string the base URL for all themes. Defaults to "/WebRoot/themes".
115: */
116: public function getBaseUrl()
117: {
118: if($this->_baseUrl===null)
119: $this->_baseUrl=Yii::app()->getBaseUrl().'/'.self::DEFAULT_BASEPATH;
120: return $this->_baseUrl;
121: }
122:
123: /**
124: * @param string $value the base URL for all themes.
125: */
126: public function setBaseUrl($value)
127: {
128: $this->_baseUrl=rtrim($value,'/');
129: }
130: }
131: