PageRenderTime 62ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/yii/framework/zii/widgets/grid/CGridView.php

https://github.com/joshuaswarren/weatherhub
PHP | 545 lines | 252 code | 28 blank | 265 comment | 49 complexity | eb91d0a5d2a74bf327d23e730ac5fcf5 MD5 | raw file
  1. <?php
  2. /**
  3. * CGridView 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. Yii::import('zii.widgets.CBaseListView');
  11. Yii::import('zii.widgets.grid.CDataColumn');
  12. Yii::import('zii.widgets.grid.CLinkColumn');
  13. Yii::import('zii.widgets.grid.CButtonColumn');
  14. Yii::import('zii.widgets.grid.CCheckBoxColumn');
  15. /**
  16. * CGridView displays a list of data items in terms of a table.
  17. *
  18. * Each row of the table represents the data of a single data item, and a column usually represents
  19. * an attribute of the item (some columns may correspond to complex expression of attributes or static text).
  20. *
  21. * CGridView supports both sorting and pagination of the data items. The sorting
  22. * and pagination can be done in AJAX mode or normal page request. A benefit of using CGridView is that
  23. * when the user browser disables JavaScript, the sorting and pagination automatically degenerate
  24. * to normal page requests and are still functioning as expected.
  25. *
  26. * CGridView should be used together with a {@link IDataProvider data provider}, preferrably a
  27. * {@link CActiveDataProvider}.
  28. *
  29. * The minimal code needed to use CGridView is as follows:
  30. *
  31. * <pre>
  32. * $dataProvider=new CActiveDataProvider('Post');
  33. *
  34. * $this->widget('zii.widgets.grid.CGridView', array(
  35. * 'dataProvider'=>$dataProvider,
  36. * ));
  37. * </pre>
  38. *
  39. * The above code first creates a data provider for the <code>Post</code> ActiveRecord class.
  40. * It then uses CGridView to display every attribute in every <code>Post</code> instance.
  41. * The displayed table is equiped with sorting and pagination functionality.
  42. *
  43. * In order to selectively display attributes with different formats, we may configure the
  44. * {@link CGridView::columns} property. For example, we may specify only the <code>title</code>
  45. * and <code>create_time</code> attributes to be displayed, and the <code>create_time</code>
  46. * should be properly formatted to show as a time. We may also display the attributes of the related
  47. * objects using the dot-syntax as shown below:
  48. *
  49. * <pre>
  50. * $this->widget('zii.widgets.grid.CGridView', array(
  51. * 'dataProvider'=>$dataProvider,
  52. * 'columns'=>array(
  53. * 'title', // display the 'title' attribute
  54. * 'category.name', // display the 'name' attribute of the 'category' relation
  55. * 'content:html', // display the 'content' attribute as purified HTML
  56. * array( // display 'create_time' using an expression
  57. * 'name'=>'create_time',
  58. * 'value'=>'date("M j, Y", $data->create_time)',
  59. * ),
  60. * array( // display 'author.username' using an expression
  61. * 'name'=>'authorName',
  62. * 'value'=>'$data->author->username',
  63. * ),
  64. * array( // display a column with "view", "update" and "delete" buttons
  65. * 'class'=>'CButtonColumn',
  66. * ),
  67. * ),
  68. * ));
  69. * </pre>
  70. *
  71. * Please refer to {@link columns} for more details about how to configure this property.
  72. *
  73. * @author Qiang Xue <qiang.xue@gmail.com>
  74. * @version $Id: CGridView.php 3286 2011-06-16 17:34:34Z qiang.xue $
  75. * @package zii.widgets.grid
  76. * @since 1.1
  77. */
  78. class CGridView extends CBaseListView
  79. {
  80. const FILTER_POS_HEADER='header';
  81. const FILTER_POS_FOOTER='footer';
  82. const FILTER_POS_BODY='body';
  83. private $_formatter;
  84. /**
  85. * @var array grid column configuration. Each array element represents the configuration
  86. * for one particular grid column which can be either a string or an array.
  87. *
  88. * When a column is specified as a string, it should be in the format of "name:type:header",
  89. * where "type" and "header" are optional. A {@link CDataColumn} instance will be created in this case,
  90. * whose {@link CDataColumn::name}, {@link CDataColumn::type} and {@link CDataColumn::header}
  91. * properties will be initialized accordingly.
  92. *
  93. * When a column is specified as an array, it will be used to create a grid column instance, where
  94. * the 'class' element specifies the column class name (defaults to {@link CDataColumn} if absent).
  95. * Currently, these official column classes are provided: {@link CDataColumn},
  96. * {@link CLinkColumn}, {@link CButtonColumn} and {@link CCheckBoxColumn}.
  97. */
  98. public $columns=array();
  99. /**
  100. * @var array the CSS class names for the table body rows. If multiple CSS class names are given,
  101. * they will be assigned to the rows sequentially and repeatedly. This property is ignored
  102. * if {@link rowCssClassExpression} is set. Defaults to <code>array('odd', 'even')</code>.
  103. * @see rowCssClassExpression
  104. */
  105. public $rowCssClass=array('odd','even');
  106. /**
  107. * @var string a PHP expression that is evaluated for every table body row and whose result
  108. * is used as the CSS class name for the row. In this expression, the variable <code>$row</code>
  109. * stands for the row number (zero-based), <code>$data</code> is the data model associated with
  110. * the row, and <code>$this</code> is the grid object.
  111. * @see rowCssClass
  112. */
  113. public $rowCssClassExpression;
  114. /**
  115. * @var boolean whether to display the table even when there is no data. Defaults to true.
  116. * The {@link emptyText} will be displayed to indicate there is no data.
  117. */
  118. public $showTableOnEmpty=true;
  119. /**
  120. * @var mixed the ID of the container whose content may be updated with an AJAX response.
  121. * Defaults to null, meaning the container for this grid view instance.
  122. * If it is set false, it means sorting and pagination will be performed in normal page requests
  123. * instead of AJAX requests. If the sorting and pagination should trigger the update of multiple
  124. * containers' content in AJAX fashion, these container IDs may be listed here (separated with comma).
  125. */
  126. public $ajaxUpdate;
  127. /**
  128. * @var string the jQuery selector of the HTML elements that may trigger AJAX updates when they are clicked.
  129. * If not set, the pagination links and the sorting links will trigger AJAX updates.
  130. * @since 1.1.7
  131. */
  132. public $updateSelector;
  133. /**
  134. * @var string a javascript function that will be invoked if an AJAX update error occurs.
  135. *
  136. * The function signature is <code>function(xhr, textStatus, errorThrown, errorMessage)</code>
  137. * <ul>
  138. * <li><code>xhr</code> is the XMLHttpRequest object.</li>
  139. * <li><code>textStatus</code> is a string describing the type of error that occurred.
  140. * Possible values (besides null) are "timeout", "error", "notmodified" and "parsererror"</li>
  141. * <li><code>errorThrown</code> is an optional exception object, if one occurred.</li>
  142. * <li><code>errorMessage</code> is the CGridView default error message derived from xhr and errorThrown.
  143. * Usefull if you just want to display this error differently. CGridView by default displays this error with an javascript.alert()</li>
  144. * </ul>
  145. * Note: This handler is not called for JSONP requests, because they do not use an XMLHttpRequest.
  146. *
  147. * Example (add in a call to CGridView):
  148. * <pre>
  149. * ...
  150. * 'ajaxUpdateError'=>'function(xhr,ts,et,err){ $("#myerrordiv").text(err); }',
  151. * ...
  152. * </pre>
  153. */
  154. public $ajaxUpdateError;
  155. /**
  156. * @var string the name of the GET variable that indicates the request is an AJAX request triggered
  157. * by this widget. Defaults to 'ajax'. This is effective only when {@link ajaxUpdate} is not false.
  158. */
  159. public $ajaxVar='ajax';
  160. /**
  161. * @var mixed the URL for the AJAX requests should be sent to. {@link CHtml::normalizeUrl()} will be
  162. * called on this property. If not set, the current page URL will be used for AJAX requests.
  163. * @since 1.1.8
  164. */
  165. public $ajaxUrl;
  166. /**
  167. * @var string a javascript function that will be invoked before an AJAX update occurs.
  168. * The function signature is <code>function(id,options)</code> where 'id' refers to the ID of the grid view,
  169. * 'options' the AJAX request options (see jQuery.ajax api manual).
  170. */
  171. public $beforeAjaxUpdate;
  172. /**
  173. * @var string a javascript function that will be invoked after a successful AJAX response is received.
  174. * The function signature is <code>function(id, data)</code> where 'id' refers to the ID of the grid view,
  175. * 'data' the received ajax response data.
  176. */
  177. public $afterAjaxUpdate;
  178. /**
  179. * @var string a javascript function that will be invoked after the row selection is changed.
  180. * The function signature is <code>function(id)</code> where 'id' refers to the ID of the grid view.
  181. * In this function, you may use <code>$.fn.yiiGridView.getSelection(id)</code> to get the key values
  182. * of the currently selected rows.
  183. * @see selectableRows
  184. */
  185. public $selectionChanged;
  186. /**
  187. * @var integer the number of table body rows that can be selected. If 0, it means rows cannot be selected.
  188. * If 1, only one row can be selected. If 2 or any other number, it means multiple rows can be selected.
  189. * A selected row will have a CSS class named 'selected'. You may also call the JavaScript function
  190. * <code>$.fn.yiiGridView.getSelection(containerID)</code> to retrieve the key values of the selected rows.
  191. */
  192. public $selectableRows=1;
  193. /**
  194. * @var string the base script URL for all grid view resources (e.g. javascript, CSS file, images).
  195. * Defaults to null, meaning using the integrated grid view resources (which are published as assets).
  196. */
  197. public $baseScriptUrl;
  198. /**
  199. * @var string the URL of the CSS file used by this grid view. Defaults to null, meaning using the integrated
  200. * CSS file. If this is set false, you are responsible to explicitly include the necessary CSS file in your page.
  201. */
  202. public $cssFile;
  203. /**
  204. * @var string the text to be displayed in a data cell when a data value is null. This property will NOT be HTML-encoded
  205. * when rendering. Defaults to an HTML blank.
  206. */
  207. public $nullDisplay='&nbsp;';
  208. /**
  209. * @var string the text to be displayed in an empty grid cell. This property will NOT be HTML-encoded when rendering. Defaults to an HTML blank.
  210. * This differs from {@link nullDisplay} in that {@link nullDisplay} is only used by {@link CDataColumn} to render
  211. * null data values.
  212. * @since 1.1.7
  213. */
  214. public $blankDisplay='&nbsp;';
  215. /**
  216. * @var string the CSS class name that will be assigned to the widget container element
  217. * when the widget is updating its content via AJAX. Defaults to 'grid-view-loading'.
  218. * @since 1.1.1
  219. */
  220. public $loadingCssClass='grid-view-loading';
  221. /**
  222. * @var string the CSS class name for the table row element containing all filter input fields. Defaults to 'filters'.
  223. * @see filter
  224. * @since 1.1.1
  225. */
  226. public $filterCssClass='filters';
  227. /**
  228. * @var string whether the filters should be displayed in the grid view. Valid values include:
  229. * <ul>
  230. * <li>header: the filters will be displayed on top of each column's header cell.</li>
  231. * <li>body: the filters will be displayed right below each column's header cell.</li>
  232. * <li>footer: the filters will be displayed below each column's footer cell.</li>
  233. * </ul>
  234. * @see filter
  235. * @since 1.1.1
  236. */
  237. public $filterPosition='body';
  238. /**
  239. * @var CModel the model instance that keeps the user-entered filter data. When this property is set,
  240. * the grid view will enable column-based filtering. Each data column by default will display a text field
  241. * at the top that users can fill in to filter the data.
  242. * Note that in order to show an input field for filtering, a column must have its {@link CDataColumn::name}
  243. * property set or have {@link CDataColumn::filter} as the HTML code for the input field.
  244. * @since 1.1.1
  245. */
  246. public $filter;
  247. /**
  248. * @var boolean whether to hide the header cells of the grid. When this is true, header cells
  249. * will not be rendered, which means the grid cannot be sorted anymore since the sort links are located
  250. * in the header. Defaults to false.
  251. * @since 1.1.1
  252. */
  253. public $hideHeader=false;
  254. /**
  255. * Initializes the grid view.
  256. * This method will initialize required property values and instantiate {@link columns} objects.
  257. */
  258. public function init()
  259. {
  260. parent::init();
  261. if(!isset($this->htmlOptions['class']))
  262. $this->htmlOptions['class']='grid-view';
  263. if($this->baseScriptUrl===null)
  264. $this->baseScriptUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets')).'/gridview';
  265. if($this->cssFile!==false)
  266. {
  267. if($this->cssFile===null)
  268. $this->cssFile=$this->baseScriptUrl.'/styles.css';
  269. Yii::app()->getClientScript()->registerCssFile($this->cssFile);
  270. }
  271. $this->initColumns();
  272. }
  273. /**
  274. * Creates column objects and initializes them.
  275. */
  276. protected function initColumns()
  277. {
  278. if($this->columns===array())
  279. {
  280. if($this->dataProvider instanceof CActiveDataProvider)
  281. $this->columns=$this->dataProvider->model->attributeNames();
  282. else if($this->dataProvider instanceof IDataProvider)
  283. {
  284. // use the keys of the first row of data as the default columns
  285. $data=$this->dataProvider->getData();
  286. if(isset($data[0]) && is_array($data[0]))
  287. $this->columns=array_keys($data[0]);
  288. }
  289. }
  290. $id=$this->getId();
  291. foreach($this->columns as $i=>$column)
  292. {
  293. if(is_string($column))
  294. $column=$this->createDataColumn($column);
  295. else
  296. {
  297. if(!isset($column['class']))
  298. $column['class']='CDataColumn';
  299. $column=Yii::createComponent($column, $this);
  300. }
  301. if(!$column->visible)
  302. {
  303. unset($this->columns[$i]);
  304. continue;
  305. }
  306. if($column->id===null)
  307. $column->id=$id.'_c'.$i;
  308. $this->columns[$i]=$column;
  309. }
  310. foreach($this->columns as $column)
  311. $column->init();
  312. }
  313. /**
  314. * Creates a {@link CDataColumn} based on a shortcut column specification string.
  315. * @param string $text the column specification string
  316. * @return CDataColumn the column instance
  317. */
  318. protected function createDataColumn($text)
  319. {
  320. if(!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/',$text,$matches))
  321. throw new CException(Yii::t('zii','The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));
  322. $column=new CDataColumn($this);
  323. $column->name=$matches[1];
  324. if(isset($matches[3]) && $matches[3]!=='')
  325. $column->type=$matches[3];
  326. if(isset($matches[5]))
  327. $column->header=$matches[5];
  328. return $column;
  329. }
  330. /**
  331. * Registers necessary client scripts.
  332. */
  333. public function registerClientScript()
  334. {
  335. $id=$this->getId();
  336. if($this->ajaxUpdate===false)
  337. $ajaxUpdate=false;
  338. else
  339. $ajaxUpdate=array_unique(preg_split('/\s*,\s*/',$this->ajaxUpdate.','.$id,-1,PREG_SPLIT_NO_EMPTY));
  340. $options=array(
  341. 'ajaxUpdate'=>$ajaxUpdate,
  342. 'ajaxVar'=>$this->ajaxVar,
  343. 'pagerClass'=>$this->pagerCssClass,
  344. 'loadingClass'=>$this->loadingCssClass,
  345. 'filterClass'=>$this->filterCssClass,
  346. 'tableClass'=>$this->itemsCssClass,
  347. 'selectableRows'=>$this->selectableRows,
  348. );
  349. if($this->ajaxUrl!==null)
  350. $options['url']=CHtml::normalizeUrl($this->ajaxUrl);
  351. if($this->updateSelector!==null)
  352. $options['updateSelector']=$this->updateSelector;
  353. if($this->enablePagination)
  354. $options['pageVar']=$this->dataProvider->getPagination()->pageVar;
  355. if($this->beforeAjaxUpdate!==null)
  356. $options['beforeAjaxUpdate']=(strpos($this->beforeAjaxUpdate,'js:')!==0 ? 'js:' : '').$this->beforeAjaxUpdate;
  357. if($this->afterAjaxUpdate!==null)
  358. $options['afterAjaxUpdate']=(strpos($this->afterAjaxUpdate,'js:')!==0 ? 'js:' : '').$this->afterAjaxUpdate;
  359. if($this->ajaxUpdateError!==null)
  360. $options['ajaxUpdateError']=(strpos($this->ajaxUpdateError,'js:')!==0 ? 'js:' : '').$this->ajaxUpdateError;
  361. if($this->selectionChanged!==null)
  362. $options['selectionChanged']=(strpos($this->selectionChanged,'js:')!==0 ? 'js:' : '').$this->selectionChanged;
  363. $options=CJavaScript::encode($options);
  364. $cs=Yii::app()->getClientScript();
  365. $cs->registerCoreScript('jquery');
  366. $cs->registerCoreScript('bbq');
  367. $cs->registerScriptFile($this->baseScriptUrl.'/jquery.yiigridview.js',CClientScript::POS_END);
  368. $cs->registerScript(__CLASS__.'#'.$id,"jQuery('#$id').yiiGridView($options);");
  369. }
  370. /**
  371. * Renders the data items for the grid view.
  372. */
  373. public function renderItems()
  374. {
  375. if($this->dataProvider->getItemCount()>0 || $this->showTableOnEmpty)
  376. {
  377. echo "<table class=\"{$this->itemsCssClass}\">\n";
  378. $this->renderTableHeader();
  379. ob_start();
  380. $this->renderTableBody();
  381. $body=ob_get_clean();
  382. $this->renderTableFooter();
  383. echo $body; // TFOOT must appear before TBODY according to the standard.
  384. echo "</table>";
  385. }
  386. else
  387. $this->renderEmptyText();
  388. }
  389. /**
  390. * Renders the table header.
  391. */
  392. public function renderTableHeader()
  393. {
  394. if(!$this->hideHeader)
  395. {
  396. echo "<thead>\n";
  397. if($this->filterPosition===self::FILTER_POS_HEADER)
  398. $this->renderFilter();
  399. echo "<tr>\n";
  400. foreach($this->columns as $column)
  401. $column->renderHeaderCell();
  402. echo "</tr>\n";
  403. if($this->filterPosition===self::FILTER_POS_BODY)
  404. $this->renderFilter();
  405. echo "</thead>\n";
  406. }
  407. else if($this->filter!==null && ($this->filterPosition===self::FILTER_POS_HEADER || $this->filterPosition===self::FILTER_POS_BODY))
  408. {
  409. echo "<thead>\n";
  410. $this->renderFilter();
  411. echo "</thead>\n";
  412. }
  413. }
  414. /**
  415. * Renders the filter.
  416. * @since 1.1.1
  417. */
  418. public function renderFilter()
  419. {
  420. if($this->filter!==null)
  421. {
  422. echo "<tr class=\"{$this->filterCssClass}\">\n";
  423. foreach($this->columns as $column)
  424. $column->renderFilterCell();
  425. echo "</tr>\n";
  426. }
  427. }
  428. /**
  429. * Renders the table footer.
  430. */
  431. public function renderTableFooter()
  432. {
  433. $hasFilter=$this->filter!==null && $this->filterPosition===self::FILTER_POS_FOOTER;
  434. $hasFooter=$this->getHasFooter();
  435. if($hasFilter || $hasFooter)
  436. {
  437. echo "<tfoot>\n";
  438. if($hasFooter)
  439. {
  440. echo "<tr>\n";
  441. foreach($this->columns as $column)
  442. $column->renderFooterCell();
  443. echo "</tr>\n";
  444. }
  445. if($hasFilter)
  446. $this->renderFilter();
  447. echo "</tfoot>\n";
  448. }
  449. }
  450. /**
  451. * Renders the table body.
  452. */
  453. public function renderTableBody()
  454. {
  455. $data=$this->dataProvider->getData();
  456. $n=count($data);
  457. echo "<tbody>\n";
  458. if($n>0)
  459. {
  460. for($row=0;$row<$n;++$row)
  461. $this->renderTableRow($row);
  462. }
  463. else
  464. {
  465. echo '<tr><td colspan="'.count($this->columns).'">';
  466. $this->renderEmptyText();
  467. echo "</td></tr>\n";
  468. }
  469. echo "</tbody>\n";
  470. }
  471. /**
  472. * Renders a table body row.
  473. * @param integer $row the row number (zero-based).
  474. */
  475. public function renderTableRow($row)
  476. {
  477. if($this->rowCssClassExpression!==null)
  478. {
  479. $data=$this->dataProvider->data[$row];
  480. echo '<tr class="'.$this->evaluateExpression($this->rowCssClassExpression,array('row'=>$row,'data'=>$data)).'">';
  481. }
  482. else if(is_array($this->rowCssClass) && ($n=count($this->rowCssClass))>0)
  483. echo '<tr class="'.$this->rowCssClass[$row%$n].'">';
  484. else
  485. echo '<tr>';
  486. foreach($this->columns as $column)
  487. $column->renderDataCell($row);
  488. echo "</tr>\n";
  489. }
  490. /**
  491. * @return boolean whether the table should render a footer.
  492. * This is true if any of the {@link columns} has a true {@link CGridColumn::hasFooter} value.
  493. */
  494. public function getHasFooter()
  495. {
  496. foreach($this->columns as $column)
  497. if($column->getHasFooter())
  498. return true;
  499. return false;
  500. }
  501. /**
  502. * @return CFormatter the formatter instance. Defaults to the 'format' application component.
  503. */
  504. public function getFormatter()
  505. {
  506. if($this->_formatter===null)
  507. $this->_formatter=Yii::app()->format;
  508. return $this->_formatter;
  509. }
  510. /**
  511. * @param CFormatter $value the formatter instance
  512. */
  513. public function setFormatter($value)
  514. {
  515. $this->_formatter=$value;
  516. }
  517. }