PageRenderTime 164ms CodeModel.GetById 100ms app.highlight 8ms RepoModel.GetById 53ms app.codeStats 0ms

/yii/zii/widgets/CBaseListView.php

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