CDataColumn.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * CDataColumn class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. Yii::import('zii.widgets.grid.CGridColumn');
  11. /**
  12. * CDataColumn represents a grid view column that is associated with a data attribute or PHP expression.
  13. *
  14. * Either {@link name} or {@link value} should be specified. The former specifies
  15. * a data attribute name, while the latter a PHP expression whose value should be rendered instead.
  16. *
  17. * The property {@link sortable} determines whether the grid view can be sorted according to this column.
  18. * Note that the {@link name} should always be set if the column needs to be sortable. The {@link name}
  19. * value will be used by {@link CSort} to render a clickable link in the header cell to trigger the sorting.
  20. *
  21. * @author Qiang Xue <qiang.xue@gmail.com>
  22. * @package zii.widgets.grid
  23. * @since 1.1
  24. */
  25. class CDataColumn extends CGridColumn
  26. {
  27. /**
  28. * @var string the attribute name of the data model. Used for column sorting, filtering and to render the corresponding
  29. * attribute value in each data cell. If {@link value} is specified it will be used to rendered the data cell instead of the attribute value.
  30. * @see value
  31. * @see sortable
  32. */
  33. public $name;
  34. /**
  35. * @var string a PHP expression that will be evaluated for every data cell using {@link evaluateExpression} and whose result will be rendered
  36. * as the content of the data cell.
  37. * In this expression, you can use the following variables:
  38. * <ul>
  39. * <li><code>$row</code> the row number (zero-based).</li>
  40. * <li><code>$data</code> the data model for the row.</li>
  41. * <li><code>$this</code> the column object.</li>
  42. * </ul>
  43. * A PHP expression can be any PHP code that has a value. To learn more about what an expression is,
  44. * please refer to the {@link http://www.php.net/manual/en/language.expressions.php php manual}.
  45. */
  46. public $value;
  47. /**
  48. * @var string the type of the attribute value. This determines how the attribute value is formatted for display.
  49. * Valid values include those recognizable by {@link CGridView::formatter}, such as: raw, text, ntext, html, date, time,
  50. * datetime, boolean, number, email, image, url. For more details, please refer to {@link CFormatter}.
  51. * Defaults to 'text' which means the attribute value will be HTML-encoded.
  52. */
  53. public $type='text';
  54. /**
  55. * @var boolean whether the column is sortable. If so, the header cell will contain a link that may trigger the sorting.
  56. * Defaults to true. Note that if {@link name} is not set, or if {@link name} is not allowed by {@link CSort},
  57. * this property will be treated as false.
  58. * @see name
  59. */
  60. public $sortable=true;
  61. /**
  62. * @var mixed the HTML code representing a filter input (eg a text field, a dropdown list)
  63. * that is used for this data column. This property is effective only when
  64. * {@link CGridView::filter} is set.
  65. * If this property is not set, a text field will be generated as the filter input;
  66. * If this property is an array, a dropdown list will be generated that uses this property value as
  67. * the list options.
  68. * If you don't want a filter for this data column, set this value to false.
  69. * @since 1.1.1
  70. */
  71. public $filter;
  72. /**
  73. * Initializes the column.
  74. */
  75. public function init()
  76. {
  77. parent::init();
  78. if($this->name===null)
  79. $this->sortable=false;
  80. if($this->name===null && $this->value===null)
  81. throw new CException(Yii::t('zii','Either "name" or "value" must be specified for CDataColumn.'));
  82. }
  83. /**
  84. * Returns the filter cell content.
  85. * This method will return the {@link filter} as is if it is a string.
  86. * If {@link filter} is an array, it is assumed to be a list of options, and a dropdown selector will be rendered.
  87. * Otherwise if {@link filter} is not false, a text field is rendered.
  88. * @return string the filter cell content
  89. * @since 1.1.16
  90. */
  91. public function getFilterCellContent()
  92. {
  93. if(is_string($this->filter))
  94. return $this->filter;
  95. elseif($this->filter!==false && $this->grid->filter!==null && $this->name!==null && strpos($this->name,'.')===false)
  96. {
  97. if(is_array($this->filter))
  98. return CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id'=>false,'prompt'=>''));
  99. elseif($this->filter===null)
  100. return CHtml::activeTextField($this->grid->filter, $this->name, array('id'=>false));
  101. }
  102. else
  103. return parent::getFilterCellContent();
  104. }
  105. /**
  106. * Returns the header cell content.
  107. * This method will render a link that can trigger the sorting if the column is sortable.
  108. * @return string the header cell content.
  109. * @since 1.1.16
  110. */
  111. public function getHeaderCellContent()
  112. {
  113. if($this->grid->enableSorting && $this->sortable && $this->name!==null)
  114. return $this->grid->dataProvider->getSort()->link($this->name,$this->header,array('class'=>'sort-link'));
  115. elseif($this->name!==null && $this->header===null)
  116. {
  117. if($this->grid->dataProvider instanceof CActiveDataProvider)
  118. return CHtml::encode($this->grid->dataProvider->model->getAttributeLabel($this->name));
  119. else
  120. return CHtml::encode($this->name);
  121. }
  122. else
  123. return parent::getHeaderCellContent();
  124. }
  125. /**
  126. * Returns the data cell content.
  127. * This method evaluates {@link value} or {@link name} and renders the result.
  128. * @param integer $row the row number (zero-based)
  129. * @return string the data cell content.
  130. * @since 1.1.16
  131. */
  132. public function getDataCellContent($row)
  133. {
  134. $data=$this->grid->dataProvider->data[$row];
  135. if($this->value!==null)
  136. $value=$this->evaluateExpression($this->value,array('data'=>$data,'row'=>$row));
  137. elseif($this->name!==null)
  138. $value=CHtml::value($data,$this->name);
  139. return $value===null ? $this->grid->nullDisplay : $this->grid->getFormatter()->format($value,$this->type);
  140. }
  141. }