Theme.php 2.91 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
/**
 * Theme class file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

/**
 * Theme represents an application theme.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Theme extends ApplicationComponent
{
Qiang Xue committed
20 21
	public $basePath;
	public $baseUrl;
Qiang Xue committed
22

Qiang Xue committed
23
	public function init()
Qiang Xue committed
24
	{
Qiang Xue committed
25 26 27 28 29 30 31 32 33 34
		if ($this->basePath !== null) {
			$this->basePath = \Yii::getAlias($this->basePath);
		} else {
			throw new Exception("Theme.basePath must be set.");
		}
		if ($this->baseUrl !== null) {
			$this->baseUrl = \Yii::getAlias($this->baseUrl);
		} else {
			throw new Exception("Theme.baseUrl must be set.");
		}
Qiang Xue committed
35 36 37
	}

	/**
Qiang Xue committed
38 39
	 * @param Controller $controller
	 * @return string
Qiang Xue committed
40
	 */
Qiang Xue committed
41
	public function getViewPath($controller = null)
Qiang Xue committed
42
	{
Qiang Xue committed
43 44
		$path = $this->basePath . DIRECTORY_SEPARATOR . 'views';
		return $controller === null ? $path : $path . DIRECTORY_SEPARATOR . $controller->id;
Qiang Xue committed
45 46
	}

Qiang Xue committed
47
	public function getLayoutPath($module = null)
Qiang Xue committed
48
	{
Qiang Xue committed
49 50
		$path = $this->getViewPath($module);
		return $controller === null ? $path : $path . DIRECTORY_SEPARATOR . $controller->id;
Qiang Xue committed
51 52
	}

Qiang Xue committed
53
	public function getWidgetViewPath($widget)
Qiang Xue committed
54
	{
Qiang Xue committed
55

Qiang Xue committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	}

	/**
	 * @return string the path for controller views. Defaults to 'ThemeRoot/views'.
	 */
	public function getViewPath()
	{
		return $this->_basePath . DIRECTORY_SEPARATOR . 'views';
	}

	/**
	 * Finds the view file for the specified controller's view.
	 * @param CController $controller the controller
	 * @param string $viewName the view name
	 * @return string the view file path. False if the file does not exist.
	 */
	public function getViewFile($controller, $viewName)
	{
		$moduleViewPath = $this->getViewPath();
		if (($module = $controller->getModule()) !== null)
				{
					$moduleViewPath .= '/' . $module->getId();
				}
		return $controller->resolveViewFile($viewName, $this->getViewPath() . '/' . $controller->getUniqueId(), $this->getViewPath(), $moduleViewPath);
	}

	/**
	 * Finds the layout file for the specified controller's layout.
	 * @param CController $controller the controller
	 * @param string $layoutName the layout name
	 * @return string the layout file path. False if the file does not exist.
	 */
	public function getLayoutFile($controller, $layoutName)
	{
		$moduleViewPath = $basePath = $this->getViewPath();
		$module = $controller->getModule();
		if (empty($layoutName)) {
			while ($module !== null) {
				if ($module->layout === false)
					return false;
				if (!empty($module->layout))
					break;
				$module = $module->getParentModule();
			}
			if ($module === null)
				$layoutName = Yii::app()->layout;
			else {
				$layoutName = $module->layout;
				$moduleViewPath .= '/' . $module->getId();
			}
		}
		else if ($module !== null)
			$moduleViewPath .= '/' . $module->getId();

		return $controller->resolveViewFile($layoutName, $moduleViewPath . '/layouts', $basePath, $moduleViewPath);
	}
}