PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/a10/lib/yii-1.1.10/web/CBaseController.php

http://chenjin.googlecode.com/
PHP | 303 lines | 111 code | 16 blank | 176 comment | 8 complexity | 2a7d3bb30345b18410fd127098ab61dd MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * CBaseController class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CBaseController is the base class for {@link CController} and {@link CWidget}.
  12. *
  13. * It provides the common functionalities shared by controllers who need to render views.
  14. *
  15. * CBaseController also implements the support for the following features:
  16. * <ul>
  17. * <li>{@link CClipWidget Clips} : a clip is a piece of captured output that can be inserted elsewhere.</li>
  18. * <li>{@link CWidget Widgets} : a widget is a self-contained sub-controller with its own view and model.</li>
  19. * <li>{@link COutputCache Fragment cache} : fragment cache selectively caches a portion of the output.</li>
  20. * </ul>
  21. *
  22. * To use a widget in a view, use the following in the view:
  23. * <pre>
  24. * $this->widget('path.to.widgetClass',array('property1'=>'value1',...));
  25. * </pre>
  26. * or
  27. * <pre>
  28. * $this->beginWidget('path.to.widgetClass',array('property1'=>'value1',...));
  29. * // ... display other contents here
  30. * $this->endWidget();
  31. * </pre>
  32. *
  33. * To create a clip, use the following:
  34. * <pre>
  35. * $this->beginClip('clipID');
  36. * // ... display the clip contents
  37. * $this->endClip();
  38. * </pre>
  39. * Then, in a different view or place, the captured clip can be inserted as:
  40. * <pre>
  41. * echo $this->clips['clipID'];
  42. * </pre>
  43. *
  44. * Note that $this in the code above refers to current controller so, for example,
  45. * if you need to access clip from a widget where $this refers to widget itself
  46. * you need to do it the following way:
  47. *
  48. * <pre>
  49. * echo $this->getController()->clips['clipID'];
  50. * </pre>
  51. *
  52. * To use fragment cache, do as follows,
  53. * <pre>
  54. * if($this->beginCache('cacheID',array('property1'=>'value1',...))
  55. * {
  56. * // ... display the content to be cached here
  57. * $this->endCache();
  58. * }
  59. * </pre>
  60. *
  61. * @author Qiang Xue <qiang.xue@gmail.com>
  62. * @version $Id: CBaseController.php 242 2012-03-29 15:18:01Z mole1230 $
  63. * @package system.web
  64. * @since 1.0
  65. */
  66. abstract class CBaseController extends CComponent
  67. {
  68. private $_widgetStack=array();
  69. /**
  70. * Returns the view script file according to the specified view name.
  71. * This method must be implemented by child classes.
  72. * @param string $viewName view name
  73. * @return string the file path for the named view. False if the view cannot be found.
  74. */
  75. abstract public function getViewFile($viewName);
  76. /**
  77. * Renders a view file.
  78. *
  79. * @param string $viewFile view file path
  80. * @param array $data data to be extracted and made available to the view
  81. * @param boolean $return whether the rendering result should be returned instead of being echoed
  82. * @return string the rendering result. Null if the rendering result is not required.
  83. * @throws CException if the view file does not exist
  84. */
  85. public function renderFile($viewFile,$data=null,$return=false)
  86. {
  87. $widgetCount=count($this->_widgetStack);
  88. if(($renderer=Yii::app()->getViewRenderer())!==null && $renderer->fileExtension==='.'.CFileHelper::getExtension($viewFile))
  89. $content=$renderer->renderFile($this,$viewFile,$data,$return);
  90. else
  91. $content=$this->renderInternal($viewFile,$data,$return);
  92. if(count($this->_widgetStack)===$widgetCount)
  93. return $content;
  94. else
  95. {
  96. $widget=end($this->_widgetStack);
  97. throw new CException(Yii::t('yii','{controller} contains improperly nested widget tags in its view "{view}". A {widget} widget does not have an endWidget() call.',
  98. array('{controller}'=>get_class($this), '{view}'=>$viewFile, '{widget}'=>get_class($widget))));
  99. }
  100. }
  101. /**
  102. * Renders a view file.
  103. * This method includes the view file as a PHP script
  104. * and captures the display result if required.
  105. * @param string $_viewFile_ view file
  106. * @param array $_data_ data to be extracted and made available to the view file
  107. * @param boolean $_return_ whether the rendering result should be returned as a string
  108. * @return string the rendering result. Null if the rendering result is not required.
  109. */
  110. public function renderInternal($_viewFile_,$_data_=null,$_return_=false)
  111. {
  112. // we use special variable names here to avoid conflict when extracting data
  113. if(is_array($_data_))
  114. extract($_data_,EXTR_PREFIX_SAME,'data');
  115. else
  116. $data=$_data_;
  117. if($_return_)
  118. {
  119. ob_start();
  120. ob_implicit_flush(false);
  121. require($_viewFile_);
  122. return ob_get_clean();
  123. }
  124. else
  125. require($_viewFile_);
  126. }
  127. /**
  128. * Creates a widget and initializes it.
  129. * This method first creates the specified widget instance.
  130. * It then configures the widget's properties with the given initial values.
  131. * At the end it calls {@link CWidget::init} to initialize the widget.
  132. * Starting from version 1.1, if a {@link CWidgetFactory widget factory} is enabled,
  133. * this method will use the factory to create the widget, instead.
  134. * @param string $className class name (can be in path alias format)
  135. * @param array $properties initial property values
  136. * @return CWidget the fully initialized widget instance.
  137. */
  138. public function createWidget($className,$properties=array())
  139. {
  140. $widget=Yii::app()->getWidgetFactory()->createWidget($this,$className,$properties);
  141. $widget->init();
  142. return $widget;
  143. }
  144. /**
  145. * Creates a widget and executes it.
  146. * @param string $className the widget class name or class in dot syntax (e.g. application.widgets.MyWidget)
  147. * @param array $properties list of initial property values for the widget (Property Name => Property Value)
  148. * @param boolean $captureOutput whether to capture the output of the widget. If true, the method will capture
  149. * and return the output generated by the widget. If false, the output will be directly sent for display
  150. * and the widget object will be returned. This parameter is available since version 1.1.2.
  151. * @return mixed the widget instance when $captureOutput is false, or the widget output when $captureOutput is true.
  152. */
  153. public function widget($className,$properties=array(),$captureOutput=false)
  154. {
  155. if($captureOutput)
  156. {
  157. ob_start();
  158. ob_implicit_flush(false);
  159. $widget=$this->createWidget($className,$properties);
  160. $widget->run();
  161. return ob_get_clean();
  162. }
  163. else
  164. {
  165. $widget=$this->createWidget($className,$properties);
  166. $widget->run();
  167. return $widget;
  168. }
  169. }
  170. /**
  171. * Creates a widget and executes it.
  172. * This method is similar to {@link widget()} except that it is expecting
  173. * a {@link endWidget()} call to end the execution.
  174. * @param string $className the widget class name or class in dot syntax (e.g. application.widgets.MyWidget)
  175. * @param array $properties list of initial property values for the widget (Property Name => Property Value)
  176. * @return CWidget the widget created to run
  177. * @see endWidget
  178. */
  179. public function beginWidget($className,$properties=array())
  180. {
  181. $widget=$this->createWidget($className,$properties);
  182. $this->_widgetStack[]=$widget;
  183. return $widget;
  184. }
  185. /**
  186. * Ends the execution of the named widget.
  187. * This method is used together with {@link beginWidget()}.
  188. * @param string $id optional tag identifying the method call for debugging purpose.
  189. * @return CWidget the widget just ended running
  190. * @throws CException if an extra endWidget call is made
  191. * @see beginWidget
  192. */
  193. public function endWidget($id='')
  194. {
  195. if(($widget=array_pop($this->_widgetStack))!==null)
  196. {
  197. $widget->run();
  198. return $widget;
  199. }
  200. else
  201. throw new CException(Yii::t('yii','{controller} has an extra endWidget({id}) call in its view.',
  202. array('{controller}'=>get_class($this),'{id}'=>$id)));
  203. }
  204. /**
  205. * Begins recording a clip.
  206. * This method is a shortcut to beginning {@link CClipWidget}.
  207. * @param string $id the clip ID.
  208. * @param array $properties initial property values for {@link CClipWidget}.
  209. */
  210. public function beginClip($id,$properties=array())
  211. {
  212. $properties['id']=$id;
  213. $this->beginWidget('CClipWidget',$properties);
  214. }
  215. /**
  216. * Ends recording a clip.
  217. * This method is an alias to {@link endWidget}.
  218. */
  219. public function endClip()
  220. {
  221. $this->endWidget('CClipWidget');
  222. }
  223. /**
  224. * Begins fragment caching.
  225. * This method will display cached content if it is availabe.
  226. * If not, it will start caching and would expect a {@link endCache()}
  227. * call to end the cache and save the content into cache.
  228. * A typical usage of fragment caching is as follows,
  229. * <pre>
  230. * if($this->beginCache($id))
  231. * {
  232. * // ...generate content here
  233. * $this->endCache();
  234. * }
  235. * </pre>
  236. * @param string $id a unique ID identifying the fragment to be cached.
  237. * @param array $properties initial property values for {@link COutputCache}.
  238. * @return boolean whether we need to generate content for caching. False if cached version is available.
  239. * @see endCache
  240. */
  241. public function beginCache($id,$properties=array())
  242. {
  243. $properties['id']=$id;
  244. $cache=$this->beginWidget('COutputCache',$properties);
  245. if($cache->getIsContentCached())
  246. {
  247. $this->endCache();
  248. return false;
  249. }
  250. else
  251. return true;
  252. }
  253. /**
  254. * Ends fragment caching.
  255. * This is an alias to {@link endWidget}.
  256. * @see beginCache
  257. */
  258. public function endCache()
  259. {
  260. $this->endWidget('COutputCache');
  261. }
  262. /**
  263. * Begins the rendering of content that is to be decorated by the specified view.
  264. * @param mixed $view the name of the view that will be used to decorate the content. The actual view script
  265. * is resolved via {@link getViewFile}. If this parameter is null (default),
  266. * the default layout will be used as the decorative view.
  267. * Note that if the current controller does not belong to
  268. * any module, the default layout refers to the application's {@link CWebApplication::layout default layout};
  269. * If the controller belongs to a module, the default layout refers to the module's
  270. * {@link CWebModule::layout default layout}.
  271. * @param array $data the variables (name=>value) to be extracted and made available in the decorative view.
  272. * @see endContent
  273. * @see CContentDecorator
  274. */
  275. public function beginContent($view=null,$data=array())
  276. {
  277. $this->beginWidget('CContentDecorator',array('view'=>$view, 'data'=>$data));
  278. }
  279. /**
  280. * Ends the rendering of content.
  281. * @see beginContent
  282. */
  283. public function endContent()
  284. {
  285. $this->endWidget('CContentDecorator');
  286. }
  287. }