TreeColumn.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace backend\widgets\grid;
  3. use Closure;
  4. use Yii;
  5. use yii\base\Model;
  6. use yii\base\BaseObject;
  7. use yii\data\ActiveDataProvider;
  8. use yii\db\ActiveQueryInterface;
  9. use yii\helpers\ArrayHelper;
  10. use yii\helpers\Html;
  11. use yii\helpers\Inflector;
  12. /**
  13. * Column is the base class of all [[TreeGrid]] column classes.
  14. * The code was based in: https://github.com/yiisoft/yii2/blob/master/framework/grid/DataColumn.php
  15. *
  16. * @author Leandro Gehlen <leandrogehlen@gmail.com>
  17. */
  18. class TreeColumn extends BaseObject
  19. {
  20. /**
  21. * @var TreeGrid the grid view object that owns this column.
  22. */
  23. public $grid;
  24. /**
  25. * @var string the header cell content. Note that it will not be HTML-encoded.
  26. */
  27. public $header;
  28. /**
  29. * @var string the footer cell content. Note that it will not be HTML-encoded.
  30. */
  31. public $footer;
  32. /**
  33. * @var callable This is a callable that will be used to generate the content of each cell.
  34. * The signature of the function should be the following: `function ($model, $key, $index, $column)`.
  35. * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
  36. * and `$column` is a reference to the [[TreeColumn]] object.
  37. */
  38. public $content;
  39. /**
  40. * @var boolean whether this column is visible. Defaults to true.
  41. */
  42. public $visible = true;
  43. /**
  44. * @var array the HTML attributes for the column group tag.
  45. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  46. */
  47. public $options = [];
  48. /**
  49. * @var array the HTML attributes for the header cell tag.
  50. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  51. */
  52. public $headerOptions = [];
  53. /**
  54. * @var array|\Closure the HTML attributes for the data cell tag. This can either be an array of
  55. * attributes or an anonymous function ([[Closure]]) that returns such an array.
  56. * The signature of the function should be the following: `function ($model, $key, $index, $column)`.
  57. * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
  58. * and `$column` is a reference to the [[Column]] object.
  59. * A function may be used to assign different attributes to different rows based on the data in that row.
  60. *
  61. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  62. */
  63. public $contentOptions = [];
  64. /**
  65. * @var array the HTML attributes for the footer cell tag.
  66. * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
  67. */
  68. public $footerOptions = [];
  69. /**
  70. * @var string the attribute name associated with this column. When neither [[content]] nor [[value]]
  71. * is specified, the value of the specified attribute will be retrieved from each data model and displayed.
  72. *
  73. * Also, if [[label]] is not specified, the label associated with the attribute will be displayed.
  74. */
  75. public $attribute;
  76. /**
  77. * @var string label to be displayed in the [[header|header cell]] and also to be used as the sorting
  78. * link label when sorting is enabled for this column.
  79. * If it is not set and the models provided by the GridViews data provider are instances
  80. * of [[\yii\db\ActiveRecord]], the label will be determined using [[\yii\db\ActiveRecord::getAttributeLabel()]].
  81. * Otherwise [[\yii\helpers\Inflector::camel2words()]] will be used to get a label.
  82. */
  83. public $label;
  84. /**
  85. * @var boolean whether the header label should be HTML-encoded.
  86. * @see label
  87. */
  88. public $encodeLabel = true;
  89. /**
  90. * @var string|\Closure an anonymous function or a string that is used to determine the value to display in the current column.
  91. *
  92. * If this is an anonymous function, it will be called for each row and the return value will be used as the value to
  93. * display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`.
  94. * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
  95. * and `$column` is a reference to the [[DataColumn]] object.
  96. *
  97. * You may also set this property to a string representing the attribute name to be displayed in this column.
  98. * This can be used when the attribute to be displayed is different from the [[attribute]] that is used for
  99. * sorting and filtering.
  100. *
  101. * If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [[attribute]].
  102. */
  103. public $value;
  104. /**
  105. * @var string|array in which format should the value of each data model be displayed as (e.g. `"raw"`, `"text"`, `"html"`,
  106. * `['date', 'php:Y-m-d']`). Supported formats are determined by the [[GridView::formatter|formatter]] used by
  107. * the [[GridView]]. Default format is "text" which will format the value as an HTML-encoded plain text when
  108. * [[\yii\i18n\Formatter]] is used as the [[GridView::$formatter|formatter]] of the GridView.
  109. */
  110. public $format = 'text';
  111. /**
  112. * Renders the header cell.
  113. */
  114. public function renderHeaderCell()
  115. {
  116. return Html::tag('th', $this->renderHeaderCellContent(), $this->headerOptions);
  117. }
  118. /**
  119. * Renders the footer cell.
  120. */
  121. public function renderFooterCell()
  122. {
  123. return Html::tag('td', $this->renderFooterCellContent(), $this->footerOptions);
  124. }
  125. /**
  126. * Renders a data cell.
  127. * @param mixed $model the data model being rendered
  128. * @param mixed $key the key associated with the data model
  129. * @param integer $index the zero-based index of the data item among the item array returned by [[GridView::dataProvider]].
  130. * @return string the rendering result
  131. */
  132. public function renderDataCell($model, $key, $index)
  133. {
  134. if ($this->contentOptions instanceof Closure) {
  135. $options = call_user_func($this->contentOptions, $model, $key, $index, $this);
  136. } else {
  137. $options = $this->contentOptions;
  138. }
  139. return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
  140. }
  141. /**
  142. * Renders the header cell content.
  143. * The default implementation simply renders [[header]].
  144. * This method may be overridden to customize the rendering of the header cell.
  145. * @return string the rendering result
  146. */
  147. protected function renderHeaderCellContent()
  148. {
  149. if ($this->header !== null || $this->label === null && $this->attribute === null) {
  150. return trim($this->header) !== '' ? $this->header : $this->grid->emptyCell;
  151. }
  152. $provider = $this->grid->dataProvider;
  153. if ($this->label === null) {
  154. if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
  155. /* @var $model Model */
  156. $model = new $provider->query->modelClass;
  157. $label = $model->getAttributeLabel($this->attribute);
  158. } else {
  159. $models = $provider->getModels();
  160. if (($model = reset($models)) instanceof Model) {
  161. /* @var $model Model */
  162. $label = $model->getAttributeLabel($this->attribute);
  163. } else {
  164. $label = Inflector::camel2words($this->attribute);
  165. }
  166. }
  167. } else {
  168. $label = $this->label;
  169. }
  170. return $this->encodeLabel ? Html::encode($label) : $label;
  171. }
  172. /**
  173. * Renders the footer cell content.
  174. * The default implementation simply renders [[footer]].
  175. * This method may be overridden to customize the rendering of the footer cell.
  176. * @return string the rendering result
  177. */
  178. protected function renderFooterCellContent()
  179. {
  180. return trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
  181. }
  182. /**
  183. * Renders the data cell content.
  184. * @param mixed $model the data model
  185. * @param mixed $key the key associated with the data model
  186. * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
  187. * @return string the rendering result
  188. */
  189. protected function renderDataCellContent($model, $key, $index)
  190. {
  191. if ($this->content === null) {
  192. return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format);
  193. } else {
  194. if ($this->content !== null) {
  195. return call_user_func($this->content, $model, $key, $index, $this);
  196. } else {
  197. return $this->grid->emptyCell;
  198. }
  199. }
  200. }
  201. /**
  202. * Returns the data cell value.
  203. * @param mixed $model the data model
  204. * @param mixed $key the key associated with the data model
  205. * @param integer $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
  206. * @return string the data cell value
  207. */
  208. public function getDataCellValue($model, $key, $index)
  209. {
  210. if ($this->value !== null) {
  211. if (is_string($this->value)) {
  212. return ArrayHelper::getValue($model, $this->value);
  213. } else {
  214. return call_user_func($this->value, $model, $key, $index, $this);
  215. }
  216. } elseif ($this->attribute !== null) {
  217. return ArrayHelper::getValue($model, $this->attribute);
  218. }
  219. return null;
  220. }
  221. }