123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- namespace backend\widgets\grid;
- use Closure;
- use Yii;
- use yii\base\Model;
- use yii\base\BaseObject;
- use yii\data\ActiveDataProvider;
- use yii\db\ActiveQueryInterface;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Html;
- use yii\helpers\Inflector;
- class TreeColumn extends BaseObject
- {
-
- public $grid;
-
- public $header;
-
- public $footer;
-
- public $content;
-
- public $visible = true;
-
- public $options = [];
-
- public $headerOptions = [];
-
- public $contentOptions = [];
-
- public $footerOptions = [];
-
- public $attribute;
-
- public $label;
-
- public $encodeLabel = true;
-
- public $value;
-
- public $format = 'text';
-
- public function renderHeaderCell()
- {
- return Html::tag('th', $this->renderHeaderCellContent(), $this->headerOptions);
- }
-
- public function renderFooterCell()
- {
- return Html::tag('td', $this->renderFooterCellContent(), $this->footerOptions);
- }
-
- public function renderDataCell($model, $key, $index)
- {
- if ($this->contentOptions instanceof Closure) {
- $options = call_user_func($this->contentOptions, $model, $key, $index, $this);
- } else {
- $options = $this->contentOptions;
- }
- return Html::tag('td', $this->renderDataCellContent($model, $key, $index), $options);
- }
-
- protected function renderHeaderCellContent()
- {
- if ($this->header !== null || $this->label === null && $this->attribute === null) {
- return trim($this->header) !== '' ? $this->header : $this->grid->emptyCell;
- }
- $provider = $this->grid->dataProvider;
- if ($this->label === null) {
- if ($provider instanceof ActiveDataProvider && $provider->query instanceof ActiveQueryInterface) {
-
- $model = new $provider->query->modelClass;
- $label = $model->getAttributeLabel($this->attribute);
- } else {
- $models = $provider->getModels();
- if (($model = reset($models)) instanceof Model) {
-
- $label = $model->getAttributeLabel($this->attribute);
- } else {
- $label = Inflector::camel2words($this->attribute);
- }
- }
- } else {
- $label = $this->label;
- }
- return $this->encodeLabel ? Html::encode($label) : $label;
- }
-
- protected function renderFooterCellContent()
- {
- return trim($this->footer) !== '' ? $this->footer : $this->grid->emptyCell;
- }
-
- protected function renderDataCellContent($model, $key, $index)
- {
- if ($this->content === null) {
- return $this->grid->formatter->format($this->getDataCellValue($model, $key, $index), $this->format);
- } else {
- if ($this->content !== null) {
- return call_user_func($this->content, $model, $key, $index, $this);
- } else {
- return $this->grid->emptyCell;
- }
- }
- }
-
- public function getDataCellValue($model, $key, $index)
- {
- if ($this->value !== null) {
- if (is_string($this->value)) {
- return ArrayHelper::getValue($model, $this->value);
- } else {
- return call_user_func($this->value, $model, $key, $index, $this);
- }
- } elseif ($this->attribute !== null) {
- return ArrayHelper::getValue($model, $this->attribute);
- }
- return null;
- }
- }
|