Commit e7a5e8ab by Qiang Xue

...

parent 6f528bac
...@@ -218,6 +218,7 @@ class YiiBase ...@@ -218,6 +218,7 @@ class YiiBase
* - a URL (e.g. `http://www.yiiframework.com`) * - a URL (e.g. `http://www.yiiframework.com`)
* - a path alias (e.g. `@yii/base`). In this case, the path alias will be converted into the * - a path alias (e.g. `@yii/base`). In this case, the path alias will be converted into the
* actual path first by calling [[getAlias]]. * actual path first by calling [[getAlias]].
* @throws \yii\base\Exception if $path is an invalid alias
* @see getAlias * @see getAlias
*/ */
public static function setAlias($alias, $path) public static function setAlias($alias, $path)
......
...@@ -118,7 +118,9 @@ class Application extends Module ...@@ -118,7 +118,9 @@ class Application extends Module
\Yii::$application = $this; \Yii::$application = $this;
$this->id = $id; $this->id = $id;
$this->setBasePath($basePath); $this->setBasePath($basePath);
\Yii::setAlias('application', $this->getBasePath()); \Yii::$aliases['@application'] = $this->getBasePath();
\Yii::$aliases['@entry'] = dirname($_SERVER['SCRIPT_FILENAME']);
\Yii::$aliases['@www'] = '';
$this->registerCoreComponents(); $this->registerCoreComponents();
} }
......
<?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
{
private $_name;
private $_basePath;
private $_baseUrl;
/**
* Constructor.
* @param string $name name of the theme
* @param string $basePath base theme path
* @param string $baseUrl base theme URL
*/
public function __construct($name, $basePath, $baseUrl)
{
$this->_name = $name;
$this->_baseUrl = $baseUrl;
$this->_basePath = $basePath;
}
/**
* @return string theme name
*/
public function getName()
{
return $this->_name;
}
/**
* @return string the relative URL to the theme folder (without ending slash)
*/
public function getBaseUrl()
{
return $this->_baseUrl;
}
/**
* @return string the file path to the theme folder
*/
public function getBasePath()
{
return $this->_basePath;
}
/**
* @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);
}
}
<?php
/**
* ThemeManager class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* ThemeManager manages the themes for the Web application.
*
* A theme is a collection of view/layout files and resource files
* (e.g. css, image, js files). When a theme is active, {@link CController}
* will look for the specified view/layout under the theme folder first.
* The corresponding view/layout files will be used if the theme provides them.
* Otherwise, the default view/layout files will be used.
*
* By default, each theme is organized as a directory whose name is the theme name.
* All themes are located under the "WebRootPath/themes" directory.
*
* To activate a theme, set the {@link CWebApplication::setTheme theme} property
* to be the name of that theme.
*
* Since a self-contained theme often contains resource files that are made
* Web accessible, please make sure the view/layout files are protected from Web access.
*
* @property array $themeNames List of available theme names.
* @property string $basePath The base path for all themes. Defaults to "WebRootPath/themes".
* @property string $baseUrl The base URL for all themes. Defaults to "/WebRoot/themes".
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ThemeManager extends ApplicationComponent
{
/**
* default themes base path
*/
const DEFAULT_BASEPATH = 'themes';
/**
* @var string the name of the theme class for representing a theme.
* Defaults to {@link Theme}. This can also be a class name in dot syntax.
*/
public $themeClass = 'Theme';
/**
* @var string the base path containing all themes. Defaults to '@entry/themes'.
*/
public $basePath = '@entry/themes';
/**
* @var string the base URL for all themes. Defaults to "@www/themes".
*/
public $baseUrl = '@www/themes';
/**
* @param string $name name of the theme to be retrieved
* @return Theme the theme retrieved. Null if the theme does not exist.
*/
public function getTheme($name)
{
$themePath = $this->getBasePath() . DIRECTORY_SEPARATOR . $name;
if (is_dir($themePath)) {
$class = Yii::import($this->themeClass, true);
return new $class($name, $themePath, $this->getBaseUrl() . '/' . $name);
} else {
return null;
}
}
/**
* @return array list of available theme names
*/
public function getThemeNames()
{
static $themes;
if ($themes === null) {
$themes = array();
$basePath = $this->getBasePath();
$folder = @opendir($basePath);
while (($file = @readdir($folder)) !== false) {
if ($file !== '.' && $file !== '..' && $file !== '.svn' && $file !== '.gitignore' && is_dir($basePath . DIRECTORY_SEPARATOR . $file)) {
$themes[] = $file;
}
}
closedir($folder);
sort($themes);
}
return $themes;
}
}
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
namespace yii\base; namespace yii\base;
use yii\util\FileHelper; use yii\util\FileHelper;
use yii\util\ArrayHelper;
/** /**
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
...@@ -47,7 +46,7 @@ class View extends Component ...@@ -47,7 +46,7 @@ class View extends Component
/** /**
* Renders a view. * Renders a view.
* *
* The method first identifies the actual view file corresponding to the specified view. * The method first finds the actual view file corresponding to the specified view.
* It then calls [[renderFile()]] to render the view file. The rendering result is returned * It then calls [[renderFile()]] to render the view file. The rendering result is returned
* as a string. If the view file does not exist, an exception will be thrown. * as a string. If the view file does not exist, an exception will be thrown.
* *
...@@ -58,36 +57,57 @@ class View extends Component ...@@ -58,36 +57,57 @@ class View extends Component
* or a path relative to [[basePath]]. The file suffix is optional and defaults to `.php` if not given * or a path relative to [[basePath]]. The file suffix is optional and defaults to `.php` if not given
* in the view name. * in the view name.
* *
* @param string $view the view to be rendered. This can be a path alias or a path relative to [[basePath]]. * @param string $view the view to be rendered. This can be either a path alias or a path relative to [[basePath]].
* @param array $params the parameters that should be made available in the view. The PHP function `extract()` * @param array $params the parameters that should be made available in the view. The PHP function `extract()`
* will be called on this variable to extract the variables from this parameter. * will be called on this variable to extract the variables from this parameter.
* @return string the rendering result * @return string the rendering result
* @throws Exception if the view file cannot be found * @throws Exception if the view file cannot be found
*/ */
public function render($view, $params = array()) public function render($view, $params = array())
{ {
$file = $this->findViewFile($view); $file = $this->findViewFile($view);
if ($file !== false) { if ($file !== false) {
$this->renderFile($file, $params); return $this->renderFile($file, $params);
} else { } else {
throw new Exception("Unable to find the view file for view '$view'."); throw new Exception("Unable to find the view file for view '$view'.");
} }
} }
/**
* Renders a view file.
* @param string $file the view file path
* @param array $params the parameters to be extracted and made available in the view file
* @return string the rendering result
*/
public function renderFile($file, $params = array()) public function renderFile($file, $params = array())
{ {
$this->renderFileInternal($file, $params); return $this->renderFileInternal($file, $params);
}
public function createWidget($class, $properties = array())
{
$properties['class'] = $class;
return \Yii::createObject($properties, $this->owner);
} }
public function widget($class, $properties = array()) public function widget($class, $properties = array())
{ {
$widget = $this->createWidget($class, $properties); $widget = $this->createWidget($class, $properties);
$widget->run(); echo $widget->run();
return $widget; return $widget;
} }
/**
* @var Widget[] the widgets that are currently not ended
*/
private $_widgetStack = array(); private $_widgetStack = array();
/**
* Begins a widget.
* @param string $class the widget class
* @param array $properties the initial property values of the widget
* @return Widget the widget instance
*/
public function beginWidget($class, $properties = array()) public function beginWidget($class, $properties = array())
{ {
$widget = $this->createWidget($class, $properties); $widget = $this->createWidget($class, $properties);
...@@ -95,43 +115,34 @@ class View extends Component ...@@ -95,43 +115,34 @@ class View extends Component
return $widget; return $widget;
} }
/**
* Ends a widget.
* Note that the rendering result of the widget is directly echoed out.
* If you want to capture the rendering result of a widget, you may use
* [[createWidget()]] and [[Widget::run()]].
* @return Widget the widget instance
* @throws Exception if [[beginWidget()]] and [[endWidget()]] calls are not properly nested
*/
public function endWidget() public function endWidget()
{ {
if (($widget = array_pop($this->_widgetStack)) !== null) { if (($widget = array_pop($this->_widgetStack)) !== null) {
$widget->run(); echo $widget->run();
return $widget; return $widget;
} else { } else {
throw new Exception("Unmatched beginWidget() and endWidget() calls."); throw new Exception("Unmatched beginWidget() and endWidget() calls.");
} }
} }
public function createWidget($class, $properties = array())
{
$properties['class'] = $class;
// todo: widget skin should be something global, similar to theme
if ($this->enableSkin) {
if ($this->skinnableWidgets === null || in_array($class, $this->skinnableWidgets)) {
$skinName = isset($properties['skin']) ? $properties['skin'] : 'default';
if ($skinName !== false && ($skin = $this->getSkin($class, $skinName)) !== array()) {
$properties = $properties === array() ? $skin : ArrayHelper::merge($skin, $properties);
}
}
}
return \Yii::createObject($properties, $this->owner);
}
/** /**
* Begins recording a clip. * Begins recording a clip.
* This method is a shortcut to beginning [[yii\web\widgets\ClipWidget]] * This method is a shortcut to beginning [[yii\widgets\Clip]]
* @param string $id the clip ID. * @param string $id the clip ID.
* @param array $properties initial property values for [[yii\web\widgets\ClipWidget]] * @param array $properties initial property values for [[yii\widgets\Clip]]
*/ */
public function beginClip($id, $properties = array()) public function beginClip($id, $properties = array())
{ {
$properties['id'] = $id; $properties['id'] = $id;
$this->beginWidget('yii\web\widgets\ClipWidget', $properties); $this->beginWidget('yii\widgets\Clip', $properties);
} }
/** /**
...@@ -158,14 +169,14 @@ class View extends Component ...@@ -158,14 +169,14 @@ class View extends Component
* ~~~ * ~~~
* *
* @param string $id a unique ID identifying the fragment to be cached. * @param string $id a unique ID identifying the fragment to be cached.
* @param array $properties initial property values for [[yii\web\widgets\OutputCache]] * @param array $properties initial property values for [[yii\widgets\OutputCache]]
* @return boolean whether we need to generate content for caching. False if cached version is available. * @return boolean whether we need to generate content for caching. False if cached version is available.
* @see endCache * @see endCache
*/ */
public function beginCache($id, $properties = array()) public function beginCache($id, $properties = array())
{ {
$properties['id'] = $id; $properties['id'] = $id;
$cache = $this->beginWidget('yii\web\widgets\OutputCache', $properties); $cache = $this->beginWidget('yii\widgets\OutputCache', $properties);
if ($cache->getIsContentCached()) { if ($cache->getIsContentCached()) {
$this->endCache(); $this->endCache();
return false; return false;
...@@ -195,11 +206,11 @@ class View extends Component ...@@ -195,11 +206,11 @@ class View extends Component
* {@link CWebModule::layout default layout}. * {@link CWebModule::layout default layout}.
* @param array $params the variables (name=>value) to be extracted and made available in the decorative view. * @param array $params the variables (name=>value) to be extracted and made available in the decorative view.
* @see endContent * @see endContent
* @see yii\web\widgets\ContentDecorator * @see yii\widgets\ContentDecorator
*/ */
public function beginContent($view = null, $params = array()) public function beginContent($view, $params = array())
{ {
$this->beginWidget('yii\web\widgets\ContentDecorator', array( $this->beginWidget('yii\widgets\ContentDecorator', array(
'view' => $view, 'view' => $view,
'params' => $params, 'params' => $params,
)); ));
...@@ -214,17 +225,32 @@ class View extends Component ...@@ -214,17 +225,32 @@ class View extends Component
$this->endWidget(); $this->endWidget();
} }
/**
* Renders a view file.
* This method will extract the given parameters and include the view file.
* It captures the output of the included view file and returns it as a string.
* @param string $_file_ the view file.
* @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file.
* @return string the rendering result
*/
protected function renderFileInternal($_file_, $_params_ = array()) protected function renderFileInternal($_file_, $_params_ = array())
{ {
ob_start();
ob_implicit_flush(false);
extract($_params_, EXTR_OVERWRITE); extract($_params_, EXTR_OVERWRITE);
require($_file_); require($_file_);
return ob_get_clean();
} }
/**
* Finds the view file based on the given view name.
* @param string $view the view name or path alias. If the view name does not specify
* the view file extension name, it will use `.php` as the extension name.
* @return string|boolean the view file if it exists. False if the view file cannot be found.
*/
public function findViewFile($view) public function findViewFile($view)
{ {
if ($view[0] === '/') { $view = ltrim($view, '/');
throw new Exception('The view name "$view" should not start with a slash "/".');
}
if (($extension = FileHelper::getExtension($view)) === '') { if (($extension = FileHelper::getExtension($view)) === '') {
$view .= '.php'; $view .= '.php';
......
<?php
/**
* Widget class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* Widget is the base class for widgets.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Widget extends Component implements Initable
{
/**
* @var mixed the name of the skin to be used by this widget. Defaults to 'default'.
* If this is set as false, no skin will be applied to this widget.
*/
public $skin = 'default';
/**
* @var Widget|Controller the owner/creator of this widget. It could be either a widget or a controller.
*/
public $owner;
/**
* @var string id of the widget.
*/
private $_id;
/**
* @var integer a counter used to generate IDs for widgets.
*/
private static $_counter = 0;
/**
* Constructor.
* @param Widget|Controller $owner owner/creator of this widget.
*/
public function __construct($owner)
{
$this->owner = $owner;
}
/**
* Returns the ID of the widget.
* @param boolean $autoGenerate whether to generate an ID if it is not set previously
* @return string ID of the widget.
*/
public function getId($autoGenerate = true)
{
if ($autoGenerate && $this->_id === null) {
$this->_id = 'yw' . self::$_counter++;
}
return $this->_id;
}
/**
* Sets the ID of the widget.
* @param string $value id of the widget.
*/
public function setId($value)
{
$this->_id = $value;
}
/**
* Initializes the widget.
*/
public function init()
{
}
/**
* Executes the widget.
* @return string the rendering result of the widget
*/
public function run()
{
}
/**
* Renders a view.
*
* The method first finds the actual view file corresponding to the specified view.
* It then calls [[renderFile()]] to render the view file. The rendering result is returned
* as a string. If the view file does not exist, an exception will be thrown.
*
* To determine which view file should be rendered, the method calls [[findViewFile()]] which
* will search in the directories as specified by [[basePath]].
*
* View name can be a path alias representing an absolute file path (e.g. `@app/views/layout/index`),
* or a path relative to [[basePath]]. The file suffix is optional and defaults to `.php` if not given
* in the view name.
*
* @param string $view the view to be rendered. This can be either a path alias or a path relative to [[basePath]].
* @param array $params the parameters that should be made available in the view. The PHP function `extract()`
* will be called on this variable to extract the variables from this parameter.
* @return string the rendering result
* @throws Exception if the view file cannot be found
*/
public function render($view, $params = array())
{
return $this->createView()->render($view, $params);
}
/**
* @return View
*/
public function createView()
{
$view = new View;
if (($theme = \Yii::$application->getTheme()) !== null) {
$view->basePath[] = $theme->getViewPath() . DIRECTORY_SEPARATOR . str_replace('\\', '_', get_class($this));
}
$class = new \ReflectionClass($this);
$view->basePath[] = dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
return $view;
}
}
\ No newline at end of file
...@@ -103,6 +103,7 @@ class Command extends \yii\base\Component ...@@ -103,6 +103,7 @@ class Command extends \yii\base\Component
* this may improve performance. * this may improve performance.
* For SQL statement with binding parameters, this method is invoked * For SQL statement with binding parameters, this method is invoked
* automatically. * automatically.
* @throws Exception if there is any DB error
*/ */
public function prepare() public function prepare()
{ {
...@@ -221,7 +222,7 @@ class Command extends \yii\base\Component ...@@ -221,7 +222,7 @@ class Command extends \yii\base\Component
} }
\Yii::trace("Executing SQL: {$sql}{$paramLog}", __CLASS__); \Yii::trace("Executing SQL: {$sql}{$paramLog}", __CLASS__);
//echo $sql . "\n\n";
try { try {
if ($this->connection->enableProfiling) { if ($this->connection->enableProfiling) {
\Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__); \Yii::beginProfile(__METHOD__ . "($sql)", __CLASS__);
......
<?php
/**
* Application class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
/**
* Application is the base class for all application classes.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Application extends \yii\base\Application
{
/**
* Processes the request.
* @return integer the exit status of the controller action (0 means normal, non-zero values mean abnormal)
*/
public function processRequest()
{
$route = $this->resolveRequest();
return $this->runController($route, null);
}
protected function resolveRequest()
{
return array();
}
}
<?php
/**
* Controller class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\web;
use yii\base\Action;
use yii\base\Exception;
use yii\base\HttpException;
/**
* Controller is the base class of Web controllers.
*
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Controller extends \yii\base\Controller
{
/**
* Returns the request parameters that will be used for action parameter binding.
* Default implementation simply returns an empty array.
* Child classes may override this method to customize the parameters to be provided
* for action parameter binding (e.g. `$_GET`).
* @return array the request parameters (name-value pairs) to be used for action parameter binding
*/
public function getActionParams()
{
return $_GET;
}
/**
* This method is invoked when the request parameters do not satisfy the requirement of the specified action.
* The default implementation will throw an exception.
* @param Action $action the action being executed
* @param Exception $exception the exception about the invalid parameters
* @throws HttpException $exception a 400 HTTP exception
*/
public function invalidActionParams($action, $exception)
{
throw new HttpException(400, \Yii::t('yii', 'Your request is invalid.'));
}
}
\ No newline at end of file
<?php
/**
* CHttpCookie class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* A CHttpCookie instance stores a single cookie, including the cookie name, value, domain, path, expire, and secure.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.web
* @since 1.0
*/
class CHttpCookie extends CComponent
{
/**
* @var string name of the cookie
*/
public $name;
/**
* @var string value of the cookie
*/
public $value='';
/**
* @var string domain of the cookie
*/
public $domain='';
/**
* @var integer the timestamp at which the cookie expires. This is the server timestamp. Defaults to 0, meaning "until the browser is closed".
*/
public $expire=0;
/**
* @var string the path on the server in which the cookie will be available on. The default is '/'.
*/
public $path='/';
/**
* @var boolean whether cookie should be sent via secure connection
*/
public $secure=false;
/**
* @var boolean whether the cookie should be accessible only through the HTTP protocol.
* By setting this property to true, the cookie will not be accessible by scripting languages,
* such as JavaScript, which can effectly help to reduce identity theft through XSS attacks.
* Note, this property is only effective for PHP 5.2.0 or above.
*/
public $httpOnly=false;
/**
* Constructor.
* @param string $name name of this cookie
* @param string $value value of this cookie
*/
public function __construct($name,$value)
{
$this->name=$name;
$this->value=$value;
}
}
<?php
/**
* CPagination class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CPagination represents information relevant to pagination.
*
* When data needs to be rendered in multiple pages, we can use CPagination to
* represent information such as {@link getItemCount total item count},
* {@link getPageSize page size}, {@link getCurrentPage current page}, etc.
* These information can be passed to {@link CBasePager pagers} to render
* pagination buttons or links.
*
* Example:
*
* Controller action:
* <pre>
* function actionIndex(){
* $criteria=new CDbCriteria();
* $count=Article::model()->count($criteria);
* $pages=new CPagination($count);
*
* // results per page
* $pages->pageSize=10;
* $pages->applyLimit($criteria);
* $models=Article::model()->findAll($criteria);
*
* $this->render('index', array(
* 'models' => $models,
* 'pages' => $pages
* ));
* }
* </pre>
*
* View:
* <pre>
* <?php foreach($models as $model): ?>
* // display a model
* <?php endforeach; ?>
*
* // display pagination
* <?php $this->widget('CLinkPager', array(
* 'pages' => $pages,
* )) ?>
* </pre>
*
* @property integer $pageSize Number of items in each page. Defaults to 10.
* @property integer $itemCount Total number of items. Defaults to 0.
* @property integer $pageCount Number of pages.
* @property integer $currentPage The zero-based index of the current page. Defaults to 0.
* @property integer $offset The offset of the data. This may be used to set the
* OFFSET value for a SQL statement for fetching the current page of data.
* @property integer $limit The limit of the data. This may be used to set the
* LIMIT value for a SQL statement for fetching the current page of data.
* This returns the same value as {@link pageSize}.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.web
* @since 1.0
*/
class CPagination extends CComponent
{
/**
* The default page size.
*/
const DEFAULT_PAGE_SIZE=10;
/**
* @var string name of the GET variable storing the current page index. Defaults to 'page'.
*/
public $pageVar='page';
/**
* @var string the route (controller ID and action ID) for displaying the paged contents.
* Defaults to empty string, meaning using the current route.
*/
public $route='';
/**
* @var array of parameters (name=>value) that should be used instead of GET when generating pagination URLs.
* Defaults to null, meaning using the currently available GET parameters.
*/
public $params;
/**
* @var boolean whether to ensure {@link currentPage} is returning a valid page number.
* When this property is true, the value returned by {@link currentPage} will always be between
* 0 and ({@link pageCount}-1). Because {@link pageCount} relies on the correct value of {@link itemCount},
* it means you must have knowledge about the total number of data items when you want to access {@link currentPage}.
* This is fine for SQL-based queries, but may not be feasible for other kinds of queries (e.g. MongoDB).
* In those cases, you may set this property to be false to skip the validation (you may need to validate yourself then).
* Defaults to true.
* @since 1.1.4
*/
public $validateCurrentPage=true;
private $_pageSize=self::DEFAULT_PAGE_SIZE;
private $_itemCount=0;
private $_currentPage;
/**
* Constructor.
* @param integer $itemCount total number of items.
*/
public function __construct($itemCount=0)
{
$this->setItemCount($itemCount);
}
/**
* @return integer number of items in each page. Defaults to 10.
*/
public function getPageSize()
{
return $this->_pageSize;
}
/**
* @param integer $value number of items in each page
*/
public function setPageSize($value)
{
if(($this->_pageSize=$value)<=0)
$this->_pageSize=self::DEFAULT_PAGE_SIZE;
}
/**
* @return integer total number of items. Defaults to 0.
*/
public function getItemCount()
{
return $this->_itemCount;
}
/**
* @param integer $value total number of items.
*/
public function setItemCount($value)
{
if(($this->_itemCount=$value)<0)
$this->_itemCount=0;
}
/**
* @return integer number of pages
*/
public function getPageCount()
{
return (int)(($this->_itemCount+$this->_pageSize-1)/$this->_pageSize);
}
/**
* @param boolean $recalculate whether to recalculate the current page based on the page size and item count.
* @return integer the zero-based index of the current page. Defaults to 0.
*/
public function getCurrentPage($recalculate=true)
{
if($this->_currentPage===null || $recalculate)
{
if(isset($_GET[$this->pageVar]))
{
$this->_currentPage=(int)$_GET[$this->pageVar]-1;
if($this->validateCurrentPage)
{
$pageCount=$this->getPageCount();
if($this->_currentPage>=$pageCount)
$this->_currentPage=$pageCount-1;
}
if($this->_currentPage<0)
$this->_currentPage=0;
}
else
$this->_currentPage=0;
}
return $this->_currentPage;
}
/**
* @param integer $value the zero-based index of the current page.
*/
public function setCurrentPage($value)
{
$this->_currentPage=$value;
$_GET[$this->pageVar]=$value+1;
}
/**
* Creates the URL suitable for pagination.
* This method is mainly called by pagers when creating URLs used to
* perform pagination. The default implementation is to call
* the controller's createUrl method with the page information.
* You may override this method if your URL scheme is not the same as
* the one supported by the controller's createUrl method.
* @param CController $controller the controller that will create the actual URL
* @param integer $page the page that the URL should point to. This is a zero-based index.
* @return string the created URL
*/
public function createPageUrl($controller,$page)
{
$params=$this->params===null ? $_GET : $this->params;
if($page>0) // page 0 is the default
$params[$this->pageVar]=$page+1;
else
unset($params[$this->pageVar]);
return $controller->createUrl($this->route,$params);
}
/**
* Applies LIMIT and OFFSET to the specified query criteria.
* @param CDbCriteria $criteria the query criteria that should be applied with the limit
*/
public function applyLimit($criteria)
{
$criteria->limit=$this->getLimit();
$criteria->offset=$this->getOffset();
}
/**
* @return integer the offset of the data. This may be used to set the
* OFFSET value for a SQL statement for fetching the current page of data.
* @since 1.1.0
*/
public function getOffset()
{
return $this->getCurrentPage()*$this->getPageSize();
}
/**
* @return integer the limit of the data. This may be used to set the
* LIMIT value for a SQL statement for fetching the current page of data.
* This returns the same value as {@link pageSize}.
* @since 1.1.0
*/
public function getLimit()
{
return $this->getPageSize();
}
}
\ No newline at end of file
<?php
/**
* CTheme class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CTheme represents an application theme.
*
* @property string $name Theme name.
* @property string $baseUrl The relative URL to the theme folder (without ending slash).
* @property string $basePath The file path to the theme folder.
* @property string $viewPath The path for controller views. Defaults to 'ThemeRoot/views'.
* @property string $systemViewPath The path for system views. Defaults to 'ThemeRoot/views/system'.
* @property string $skinPath The path for widget skins. Defaults to 'ThemeRoot/views/skins'.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.web
* @since 1.0
*/
class CTheme extends CComponent
{
private $_name;
private $_basePath;
private $_baseUrl;
/**
* Constructor.
* @param string $name name of the theme
* @param string $basePath base theme path
* @param string $baseUrl base theme URL
*/
public function __construct($name,$basePath,$baseUrl)
{
$this->_name=$name;
$this->_baseUrl=$baseUrl;
$this->_basePath=$basePath;
}
/**
* @return string theme name
*/
public function getName()
{
return $this->_name;
}
/**
* @return string the relative URL to the theme folder (without ending slash)
*/
public function getBaseUrl()
{
return $this->_baseUrl;
}
/**
* @return string the file path to the theme folder
*/
public function getBasePath()
{
return $this->_basePath;
}
/**
* @return string the path for controller views. Defaults to 'ThemeRoot/views'.
*/
public function getViewPath()
{
return $this->_basePath.DIRECTORY_SEPARATOR.'views';
}
/**
* @return string the path for system views. Defaults to 'ThemeRoot/views/system'.
*/
public function getSystemViewPath()
{
return $this->getViewPath().DIRECTORY_SEPARATOR.'system';
}
/**
* @return string the path for widget skins. Defaults to 'ThemeRoot/views/skins'.
* @since 1.1
*/
public function getSkinPath()
{
return $this->getViewPath().DIRECTORY_SEPARATOR.'skins';
}
/**
* 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);
}
}
<?php
/**
* CThemeManager class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CThemeManager manages the themes for the Web application.
*
* A theme is a collection of view/layout files and resource files
* (e.g. css, image, js files). When a theme is active, {@link CController}
* will look for the specified view/layout under the theme folder first.
* The corresponding view/layout files will be used if the theme provides them.
* Otherwise, the default view/layout files will be used.
*
* By default, each theme is organized as a directory whose name is the theme name.
* All themes are located under the "WebRootPath/themes" directory.
*
* To activate a theme, set the {@link CWebApplication::setTheme theme} property
* to be the name of that theme.
*
* Since a self-contained theme often contains resource files that are made
* Web accessible, please make sure the view/layout files are protected from Web access.
*
* @property array $themeNames List of available theme names.
* @property string $basePath The base path for all themes. Defaults to "WebRootPath/themes".
* @property string $baseUrl The base URL for all themes. Defaults to "/WebRoot/themes".
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.web
* @since 1.0
*/
class CThemeManager extends CApplicationComponent
{
/**
* default themes base path
*/
const DEFAULT_BASEPATH='themes';
/**
* @var string the name of the theme class for representing a theme.
* Defaults to {@link CTheme}. This can also be a class name in dot syntax.
*/
public $themeClass='CTheme';
private $_basePath=null;
private $_baseUrl=null;
/**
* @param string $name name of the theme to be retrieved
* @return CTheme the theme retrieved. Null if the theme does not exist.
*/
public function getTheme($name)
{
$themePath=$this->getBasePath().DIRECTORY_SEPARATOR.$name;
if(is_dir($themePath))
{
$class=Yii::import($this->themeClass, true);
return new $class($name,$themePath,$this->getBaseUrl().'/'.$name);
}
else
return null;
}
/**
* @return array list of available theme names
*/
public function getThemeNames()
{
static $themes;
if($themes===null)
{
$themes=array();
$basePath=$this->getBasePath();
$folder=@opendir($basePath);
while(($file=@readdir($folder))!==false)
{
if($file!=='.' && $file!=='..' && $file!=='.svn' && $file!=='.gitignore' && is_dir($basePath.DIRECTORY_SEPARATOR.$file))
$themes[]=$file;
}
closedir($folder);
sort($themes);
}
return $themes;
}
/**
* @return string the base path for all themes. Defaults to "WebRootPath/themes".
*/
public function getBasePath()
{
if($this->_basePath===null)
$this->setBasePath(dirname(Yii::app()->getRequest()->getScriptFile()).DIRECTORY_SEPARATOR.self::DEFAULT_BASEPATH);
return $this->_basePath;
}
/**
* @param string $value the base path for all themes.
* @throws CException if the base path does not exist
*/
public function setBasePath($value)
{
$this->_basePath=realpath($value);
if($this->_basePath===false || !is_dir($this->_basePath))
throw new CException(Yii::t('yii','Theme directory "{directory}" does not exist.',array('{directory}'=>$value)));
}
/**
* @return string the base URL for all themes. Defaults to "/WebRoot/themes".
*/
public function getBaseUrl()
{
if($this->_baseUrl===null)
$this->_baseUrl=Yii::app()->getBaseUrl().'/'.self::DEFAULT_BASEPATH;
return $this->_baseUrl;
}
/**
* @param string $value the base URL for all themes.
*/
public function setBaseUrl($value)
{
$this->_baseUrl=rtrim($value,'/');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment