1 | <?php |
||
20 | |||
21 | class MainColumn extends DataColumn |
||
22 | { |
||
23 | /** |
||
24 | * @var true|string|array |
||
25 | * true - note editing is enabled in this column, target attribute name is `note` |
||
26 | * string - target atttribute name |
||
27 | * array - array of notes for an attribute |
||
28 | */ |
||
29 | public $note; |
||
30 | |||
31 | /** |
||
32 | * @var string name of attribute with extra data showed under main data |
||
33 | */ |
||
34 | public $extraAttribute; |
||
35 | |||
36 | /** |
||
37 | * @var array will be passed to the ```pluginOptions``` of [[XEditable]] plugin |
||
38 | */ |
||
39 | public $noteOptions = []; |
||
40 | |||
41 | /** |
||
42 | * @var string|Closure badges string or callback to render badges |
||
43 | */ |
||
44 | public $badges; |
||
45 | |||
46 | /** |
||
47 | * Builds url. |
||
48 | * @param string $url |
||
49 | * @return string |
||
50 | */ |
||
51 | protected function buildUrl($url) |
||
52 | { |
||
53 | if (strncmp($url, '/', 1) === 0) { |
||
54 | return $url; |
||
55 | } |
||
56 | $baseUrl = isset($this->grid->controllerUrl) ? $this->grid->controllerUrl : ''; |
||
57 | |||
58 | return $baseUrl ? Url::to($baseUrl . '/' . $url) : Url::to($url); |
||
59 | } |
||
60 | |||
61 | public function init() |
||
62 | { |
||
63 | parent::init(); |
||
64 | $this->noteOptions = ArrayHelper::merge([ |
||
65 | 'url' => $this->buildUrl('set-note'), |
||
66 | ], $this->noteOptions); |
||
67 | } |
||
68 | |||
69 | /** {@inheritdoc} */ |
||
70 | protected function renderDataCellContent($model, $key, $index) |
||
71 | { |
||
72 | $value = $this->renderValue($model, $key, $index); |
||
73 | $note = $this->renderNoteLink($model, $key, $index); |
||
74 | $extra = $this->renderExtra($model); |
||
75 | $badges = $this->renderBadges($model, $key, $index); |
||
76 | |||
77 | return $value . $extra . $badges . $note; |
||
78 | } |
||
79 | |||
80 | protected function renderValue($model, $key, $index) |
||
81 | { |
||
82 | if ($this->value !== null) { |
||
83 | if (is_string($this->value)) { |
||
84 | $value = ArrayHelper::getValue($model, $this->value); |
||
85 | } else { |
||
86 | $value = call_user_func($this->value, $model, $key, $index, $this); |
||
87 | } |
||
88 | } else { |
||
89 | $value = $this->renderViewLink($model, $key, $index); |
||
90 | } |
||
91 | return $value; |
||
92 | } |
||
93 | |||
94 | protected function renderBadges($model, $key, $index) |
||
95 | { |
||
96 | $badges = $this->badges instanceof Closure |
||
97 | ? call_user_func($this->badges, $model, $key, $index) |
||
98 | : $this->badges; |
||
99 | |||
100 | return $badges ? (' ' . $badges) : ''; |
||
101 | } |
||
102 | |||
103 | protected function renderExtra($model) |
||
104 | { |
||
105 | $value = $this->extraAttribute ? $model->{$this->extraAttribute} : null; |
||
106 | |||
107 | return $value ? "<br>$value" : ''; |
||
108 | } |
||
109 | |||
110 | protected function renderViewLink($model, $key, $index) |
||
111 | { |
||
112 | $value = parent::renderDataCellContent($model, $key, $index); |
||
113 | |||
114 | return Html::a($value, [$this->buildUrl('view'), 'id' => $model->id], ['class' => 'bold']); |
||
115 | } |
||
116 | |||
150 |