You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 satır
9.7KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\grid;
  8. use yii\base\Model;
  9. use yii\data\ActiveDataProvider;
  10. use yii\data\ArrayDataProvider;
  11. use yii\db\ActiveQueryInterface;
  12. use yii\helpers\ArrayHelper;
  13. use yii\helpers\Html;
  14. use yii\helpers\Inflector;
  15. /**
  16. * DataColumn is the default column type for the [[GridView]] widget.
  17. *
  18. * It is used to show data columns and allows [[enableSorting|sorting]] and [[filter|filtering]] them.
  19. *
  20. * A simple data column definition refers to an attribute in the data model of the
  21. * GridView's data provider. The name of the attribute is specified by [[attribute]].
  22. *
  23. * By setting [[value]] and [[label]], the header and cell content can be customized.
  24. *
  25. * A data column differentiates between the [[getDataCellValue|data cell value]] and the
  26. * [[renderDataCellContent|data cell content]]. The cell value is an un-formatted value that
  27. * may be used for calculation, while the actual cell content is a [[format|formatted]] version of that
  28. * value which may contain HTML markup.
  29. *
  30. * @author Qiang Xue <qiang.xue@gmail.com>
  31. * @since 2.0
  32. */
  33. class DataColumn extends Column
  34. {
  35. /**
  36. * @var string the attribute name associated with this column. When neither [[content]] nor [[value]]
  37. * is specified, the value of the specified attribute will be retrieved from each data model and displayed.
  38. *
  39. * Also, if [[label]] is not specified, the label associated with the attribute will be displayed.
  40. */
  41. public $attribute;
  42. /**
  43. * @var string label to be displayed in the [[header|header cell]] and also to be used as the sorting
  44. * link label when sorting is enabled for this column.
  45. * If it is not set and the models provided by the GridViews data provider are instances
  46. * of [[\yii\db\ActiveRecord]], the label will be determined using [[\yii\db\ActiveRecord::getAttributeLabel()]].
  47. * Otherwise [[\yii\helpers\Inflector::camel2words()]] will be used to get a label.
  48. */
  49. public $label;
  50. /**
  51. * @var boolean whether the header label should be HTML-encoded.
  52. * @see label
  53. * @since 2.0.1
  54. */
  55. public $encodeLabel = true;
  56. /**
  57. * @var string|\Closure an anonymous function or a string that is used to determine the value to display in the current column.
  58. *
  59. * If this is an anonymous function, it will be called for each row and the return value will be used as the value to
  60. * display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`.
  61. * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
  62. * and `$column` is a reference to the [[DataColumn]] object.
  63. *
  64. * You may also set this property to a string representing the attribute name to be displayed in this column.
  65. * This can be used when the attribute to be displayed is different from the [[attribute]] that is used for
  66. * sorting and filtering.
  67. *
  68. * If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [[attribute]].
  69. */
  70. public $value;
  71. /**
  72. * @var string|array in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`,
  73. * `['date', 'php:Y-m-d']`). Supported formats are determined by the [[GridView::formatter|formatter]] used by
  74. * the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when
  75. * [[\yii\i18n\Formatter]] is used as the [[GridView::$formatter|formatter]] of the GridView.
  76. */
  77. public $format = 'text';
  78. /**
  79. * @var boolean whether to allow sorting by this column. If true and [[attribute]] is found in
  80. * the sort definition of [[GridView::dataProvider]], then the header cell of this column
  81. * will contain a link that may trigger the sorting when being clicked.
  82. */
  83. public $enableSorting = true;
  84. /**
  85. * @var array the HTML attributes for the link tag in the header cell
  86. * generated by [[\yii\data\Sort::link]] when sorting is enabled for this column.
  87. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  88. */
  89. public $sortLinkOptions = [];
  90. /**
  91. * @var string|array|null|false the HTML code representing a filter input (e.g. a text field, a dropdown list)
  92. * that is used for this data column. This property is effective only when [[GridView::filterModel]] is set.
  93. *
  94. * - If this property is not set, a text field will be generated as the filter input;
  95. * - If this property is an array, a dropdown list will be generated that uses this property value as
  96. * the list options.
  97. * - If you don't want a filter for this data column, set this value to be false.
  98. */
  99. public $filter;
  100. /**
  101. * @var array the HTML attributes for the filter input fields. This property is used in combination with
  102. * the [[filter]] property. When [[filter]] is not set or is an array, this property will be used to
  103. * render the HTML attributes for the generated filter input fields.
  104. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  105. */
  106. public $filterInputOptions = ['class' => 'form-control', 'id' => null];
  107. /**
  108. * @inheritdoc
  109. */
  110. protected function renderHeaderCellContent()
  111. {
  112. if ($this->header !== null || $this->label === null && $this->attribute === null) {
  113. return parent::renderHeaderCellContent();
  114. }
  115. $label = $this->getHeaderCellLabel();
  116. if ($this->encodeLabel) {
  117. $label = Html::encode($label);
  118. }
  119. if ($this->attribute !== null && $this->enableSorting &&
  120. ($sort = $this->grid->dataProvider->getSort()) !== false && $sort->hasAttribute($this->attribute)) {
  121. return $sort->link($this->attribute, array_merge($this->sortLinkOptions, ['label' => $label]));
  122. } else {
  123. return $label;
  124. }
  125. }
  126. /**
  127. * @inheritdoc
  128. * @since 2.0.8
  129. */
  130. protected function getHeaderCellLabel()
  131. {
  132. $provider = $this->grid->dataProvider;
  133. if ($this->label === null) {
  134. if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
  135. /* @var $model Model */
  136. $model = new $provider->query->modelClass;
  137. $label = $model->getAttributeLabel($this->attribute);
  138. } elseif ($provider instanceof ArrayDataProvider && $provider->modelClass !== null) {
  139. /* @var $model Model */
  140. $model = new $provider->modelClass;
  141. $label = $model->getAttributeLabel($this->attribute);
  142. } elseif ($this->grid->filterModel !== null && $this->grid->filterModel instanceof Model) {
  143. $label = $this->grid->filterModel->getAttributeLabel($this->attribute);
  144. } else {
  145. $models = $provider->getModels();
  146. if (($model = reset($models)) instanceof Model) {
  147. /* @var $model Model */
  148. $label = $model->getAttributeLabel($this->attribute);
  149. } else {
  150. $label = Inflector::camel2words($this->attribute);
  151. }
  152. }
  153. } else {
  154. $label = $this->label;
  155. }
  156. return $label;
  157. }
  158. /**
  159. * @inheritdoc
  160. */
  161. protected function renderFilterCellContent()
  162. {
  163. if (is_string($this->filter)) {
  164. return $this->filter;
  165. }
  166. $model = $this->grid->filterModel;
  167. if ($this->filter !== false && $model instanceof Model && $this->attribute !== null && $model->isAttributeActive($this->attribute)) {
  168. if ($model->hasErrors($this->attribute)) {
  169. Html::addCssClass($this->filterOptions, 'has-error');
  170. $error = ' ' . Html::error($model, $this->attribute, $this->grid->filterErrorOptions);
  171. } else {
  172. $error = '';
  173. }
  174. if (is_array($this->filter)) {
  175. $options = array_merge(['prompt' => ''], $this->filterInputOptions);
  176. return Html::activeDropDownList($model, $this->attribute, $this->filter, $options) . $error;
  177. } else {
  178. return Html::activeTextInput($model, $this->attribute, $this->filterInputOptions) . $error;
  179. }
  180. } else {
  181. return parent::renderFilterCellContent();
  182. }
  183. }
  184. /**
  185. * Returns the data cell value.
  186. * @param mixed $model the data model
  187. * @param mixed $key the key associated with the data model
  188. * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
  189. * @return string the data cell value
  190. */
  191. public function getDataCellValue($model, $key, $index)
  192. {
  193. if ($this->value !== null) {
  194. if (is_string($this->value)) {
  195. return ArrayHelper::getValue($model, $this->value);
  196. } else {
  197. return call_user_func($this->value, $model, $key, $index, $this);
  198. }
  199. } elseif ($this->attribute !== null) {
  200. return ArrayHelper::getValue($model, $this->attribute);
  201. }
  202. return null;
  203. }
  204. /**
  205. * @inheritdoc
  206. */
  207. protected function renderDataCellContent($model, $key, $index)
  208. {
  209. if ($this->content === null) {
  210. return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format);
  211. } else {
  212. return parent::renderDataCellContent($model, $key, $index);
  213. }
  214. }
  215. }