PageRenderTime 50ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/zii/widgets/CBaseListView.php

https://github.com/balor/yiicms
PHP | 266 lines | 129 code | 21 blank | 116 comment | 21 complexity | eadb539a06cfae478726235d8a4c8082 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * CBaseListView class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2010 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CBaseListView is the base class for {@link CListView} and {@link CGridView}.
  12. *
  13. * CBaseListView implements the common features needed by a view wiget for rendering multiple models.
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @version $Id: CBaseListView.php 118 2010-01-21 17:42:01Z qiang.xue $
  17. * @package zii.widgets
  18. * @since 1.1
  19. */
  20. abstract class CBaseListView extends CWidget
  21. {
  22. /**
  23. * @var IDataProvider the data provider for the view.
  24. */
  25. public $dataProvider;
  26. /**
  27. * @var string the tag name for the view container. Defaults to 'div'.
  28. */
  29. public $tagName='div';
  30. /**
  31. * @var array the HTML options for the view container tag.
  32. */
  33. public $htmlOptions=array();
  34. /**
  35. * @var boolean whether to enable sorting. Note that if the {@link IDataProvider::sort} property
  36. * of {@link dataProvider} is false, this will be treated as false as well. When sorting is enabled,
  37. * sortable columns will have their headers clickable to trigger sorting along that column.
  38. * Defaults to true.
  39. * @see sortableAttributes
  40. */
  41. public $enableSorting=true;
  42. /**
  43. * @var boolean whether to enable pagination. Note that if the {@link IDataProvider::pagination} property
  44. * of {@link dataProvider} is false, this will be treated as false as well. When pagination is enabled,
  45. * a pager will be displayed in the view so that it can trigger pagination of the data display.
  46. * Defaults to true.
  47. */
  48. public $enablePagination=true;
  49. /**
  50. * @var array the configuration for the pager. Defaults to <code>array('class'=>'CLinkPager')</code>.
  51. * @see enablePagination
  52. */
  53. public $pager=array('class'=>'CLinkPager');
  54. /**
  55. * @var string the template to be used to control the layout of various sections in the view.
  56. * These tokens are recognized: {summary}, {items} and {pager}. They will be replaced with the
  57. * summary text, the items, and the pager.
  58. */
  59. public $template="{summary}\n{items}\n{pager}";
  60. /**
  61. * @var string the summary text template for the view. These tokens are recognized:
  62. * {start}, {end} and {count}. They will be replaced with the starting row number, ending row number
  63. * and total number of data records.
  64. */
  65. public $summaryText;
  66. /**
  67. * @var string the message to be displayed when {@link dataProvider} does not have any data.
  68. */
  69. public $emptyText;
  70. /**
  71. * @var string the CSS class name for the container of all data item display. Defaults to 'items'.
  72. */
  73. public $itemsCssClass='items';
  74. /**
  75. * @var string the CSS class name for the summary text container. Defaults to 'summary'.
  76. */
  77. public $summaryCssClass='summary';
  78. /**
  79. * @var string the CSS class name for the pager container. Defaults to 'pager'.
  80. */
  81. public $pagerCssClass='pager';
  82. /**
  83. * @var string the CSS class name that will be assigned to the widget container element
  84. * when the widget is updating its content via AJAX. Defaults to 'loading'.
  85. * @since 1.1.1
  86. */
  87. public $loadingCssClass='loading';
  88. /**
  89. * Initializes the view.
  90. * This method will initialize required property values and instantiate {@link columns} objects.
  91. */
  92. public function init()
  93. {
  94. if($this->dataProvider===null)
  95. throw new CException(Yii::t('zii','The "dataProvider" property cannot be empty.'));
  96. $this->dataProvider->getData();
  97. $this->htmlOptions['id']=$this->getId();
  98. if($this->enableSorting && $this->dataProvider->getSort()===false)
  99. $this->enableSorting=false;
  100. if($this->enablePagination && $this->dataProvider->getPagination()===false)
  101. $this->enablePagination=false;
  102. }
  103. /**
  104. * Renders the view.
  105. * This is the main entry of the whole view rendering.
  106. * Child classes should mainly override {@link renderContent} method.
  107. */
  108. public function run()
  109. {
  110. $this->registerClientScript();
  111. echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
  112. $this->renderKeys();
  113. $this->renderContent();
  114. echo CHtml::closeTag($this->tagName);
  115. }
  116. /**
  117. * Renders the main content of the view.
  118. * The content is divided into sections, such as summary, items, pager.
  119. * Each section is rendered by a method named as "renderXyz", where "Xyz" is the section name.
  120. * The rendering results will replace the corresponding placeholders in {@link template}.
  121. */
  122. public function renderContent()
  123. {
  124. ob_start();
  125. echo preg_replace_callback("/{(\w+)}/",array($this,'renderSection'),$this->template);
  126. ob_end_flush();
  127. }
  128. /**
  129. * Renders a section.
  130. * This method is invoked by {@link renderContent} for every placeholder found in {@link template}.
  131. * It should return the rendering result that would replace the placeholder.
  132. * @param array the matches, where $matches[0] represents the whole placeholder,
  133. * while $matches[1] contains the name of the matched placeholder.
  134. * @return string the rendering result of the section
  135. */
  136. protected function renderSection($matches)
  137. {
  138. $method='render'.$matches[1];
  139. if(method_exists($this,$method))
  140. {
  141. $this->$method();
  142. $html=ob_get_contents();
  143. ob_clean();
  144. return $html;
  145. }
  146. else
  147. return $matches[0];
  148. }
  149. /**
  150. * Renders the empty message when there is no data.
  151. */
  152. public function renderEmptyText()
  153. {
  154. $emptyText=$this->emptyText===null ? Yii::t('zii','No results found.') : $this->emptyText;
  155. echo CHtml::tag('span', array('class'=>'empty'), $emptyText);
  156. }
  157. /**
  158. * Renders the key values of the data in a hidden tag.
  159. */
  160. public function renderKeys()
  161. {
  162. echo CHtml::openTag('div',array(
  163. 'class'=>'keys',
  164. 'style'=>'display:none',
  165. 'title'=>Yii::app()->getRequest()->getUrl(),
  166. ));
  167. foreach($this->dataProvider->getKeys() as $key)
  168. echo "<span>$key</span>";
  169. echo "</div>\n";
  170. }
  171. /**
  172. * Renders the summary text.
  173. */
  174. public function renderSummary()
  175. {
  176. if(($count=$this->dataProvider->getItemCount())<=0)
  177. return;
  178. echo '<div class="'.$this->summaryCssClass.'">';
  179. if($this->enablePagination)
  180. {
  181. if(($summaryText=$this->summaryText)===null)
  182. $summaryText=Yii::t('zii','Displaying {start}-{end} of {count} result(s).');
  183. $pagination=$this->dataProvider->getPagination();
  184. $start=$pagination->currentPage*$pagination->pageSize+1;
  185. echo strtr($summaryText,array(
  186. '{start}'=>$start,
  187. '{end}'=>$start+$count-1,
  188. '{count}'=>$this->dataProvider->getTotalItemCount(),
  189. ));
  190. }
  191. else
  192. {
  193. if(($summaryText=$this->summaryText)===null)
  194. $summaryText=Yii::t('zii','Total {count} result(s).');
  195. echo strtr($summaryText,array('{count}'=>$count));
  196. }
  197. echo '</div>';
  198. }
  199. /**
  200. * Renders the pager.
  201. */
  202. public function renderPager()
  203. {
  204. $pager=array();
  205. $class='CLinkPager';
  206. if(is_string($this->pager))
  207. $class=$this->pager;
  208. else if(is_array($this->pager))
  209. {
  210. $pager=$this->pager;
  211. if(isset($pager['class']))
  212. {
  213. $class=$pager['class'];
  214. unset($pager['class']);
  215. }
  216. }
  217. if($this->enablePagination && $class==='CLinkPager')
  218. {
  219. if(!isset($pager['cssFile']))
  220. CLinkPager::registerCssFile();
  221. else if($pager['cssFile']!==false)
  222. CLinkPager::registerCssFile($pager['cssFile']);
  223. }
  224. if($this->dataProvider->getItemCount()<=0 || !$this->enablePagination)
  225. return;
  226. $pager['pages']=$this->dataProvider->getPagination();
  227. echo '<div class="'.$this->pagerCssClass.'">';
  228. $this->widget($class,$pager);
  229. echo '</div>';
  230. }
  231. /**
  232. * Registers necessary client scripts.
  233. * This method is invoked by {@link run}.
  234. * Child classes may override this method to register customized client scripts.
  235. */
  236. public function registerClientScript()
  237. {
  238. }
  239. /**
  240. * Renders the data items for the view.
  241. * Each item is corresponding to a single data model instance.
  242. * Child classes should override this method to provide the actual item rendering logic.
  243. */
  244. abstract public function renderItems();
  245. }