PageRenderTime 61ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

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

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