PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/yii/framework/zii/widgets/CBaseListView.php

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