|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Advanced Grid for Yii2. |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/yii2-higrid |
|
6
|
|
|
* @package yii2-higrid |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\higrid; |
|
12
|
|
|
|
|
13
|
|
|
use Yii; |
|
14
|
|
|
use yii\base\InvalidConfigException; |
|
15
|
|
|
use yii\data\ArrayDataProvider; |
|
16
|
|
|
use yii\helpers\ArrayHelper; |
|
17
|
|
|
use yii\helpers\Html; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* DetailView displays the detail of a single data [[Model]]. |
|
21
|
|
|
* |
|
22
|
|
|
* [[hiqdev\higrid\DetailView]] is similar to [[yii\widgets\DetailView]] but uses [[Column]] |
|
23
|
|
|
* to show header and data part of given [[Model]]. |
|
24
|
|
|
* |
|
25
|
|
|
* The contents of the detail table are configured in terms of [[Column]] |
|
26
|
|
|
* classes, which are configured via $columns. |
|
27
|
|
|
* Helps reuse your code by defining [[Column]]s once and reusing them for |
|
28
|
|
|
* both GridView and DetailView. Filter and footer part of [[Column]] are |
|
29
|
|
|
* not used. |
|
30
|
|
|
* |
|
31
|
|
|
* A typical usage of DetailView is as follows: |
|
32
|
|
|
* |
|
33
|
|
|
* ```php |
|
34
|
|
|
* echo DetailView::widget([ |
|
35
|
|
|
* 'model' => $model, |
|
36
|
|
|
* 'columns' => [ |
|
37
|
|
|
* 'title', // title attribute (in plain text) |
|
38
|
|
|
* 'description:html', // description attribute in HTML |
|
39
|
|
|
* [ // the owner name of the model |
|
40
|
|
|
* 'label' => 'Owner', |
|
41
|
|
|
* 'value' => $model->owner->name, |
|
42
|
|
|
* ], |
|
43
|
|
|
* 'created_at:datetime', // creation date formatted as datetime |
|
44
|
|
|
* [ |
|
45
|
|
|
* 'class' => ActionColumn::class, |
|
46
|
|
|
* ], |
|
47
|
|
|
* ], |
|
48
|
|
|
* ]); |
|
49
|
|
|
* ``` |
|
50
|
|
|
* |
|
51
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
52
|
|
|
*/ |
|
53
|
|
|
class DetailView extends GridView |
|
54
|
|
|
{ |
|
55
|
|
|
/** |
|
56
|
|
|
* @var array|object the data model whose details are to be displayed. This can be a [[Model]] instance, |
|
57
|
|
|
* an associative array, an object that implements [[Arrayable]] interface or simply an object with defined |
|
58
|
|
|
* public accessible non-static properties. |
|
59
|
|
|
*/ |
|
60
|
|
|
public $model; |
|
61
|
|
|
/** |
|
62
|
|
|
* @var string|callable the template used to render a single attribute. If a string, the token `{label}` |
|
63
|
|
|
* and `{value}` will be replaced with the label and the value of the corresponding attribute. |
|
64
|
|
|
* If a callback (e.g. an anonymous function), the signature must be as follows: |
|
65
|
|
|
* |
|
66
|
|
|
* ```php |
|
67
|
|
|
* function ($attribute, $index, $widget) |
|
68
|
|
|
* ``` |
|
69
|
|
|
* |
|
70
|
|
|
* where `$attribute` refer to the specification of the attribute being rendered, `$index` is the zero-based |
|
71
|
|
|
* index of the attribute in the [[attributes]] array, and `$widget` refers to this widget instance. |
|
72
|
|
|
*/ |
|
73
|
|
|
public $template = '<tr>{label}{value}</tr>'; |
|
74
|
|
|
/** |
|
75
|
|
|
* @var array the HTML attributes for the container tag of this widget. The "tag" option specifies |
|
76
|
|
|
* what container tag should be used. It defaults to "table" if not set. |
|
77
|
|
|
* |
|
78
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
|
79
|
|
|
*/ |
|
80
|
|
|
public $options = ['class' => 'table table-striped table-bordered table-condensed detail-view']; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Initializes dataProvider. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function init() |
|
86
|
|
|
{ |
|
87
|
|
|
if ($this->model === null) { |
|
88
|
|
|
throw new InvalidConfigException('Please specify the "model" property.'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->dataProvider = new ArrayDataProvider(['allModels' => [$this->model]]); |
|
92
|
|
|
parent::init(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Renders the detail view. |
|
97
|
|
|
* This is the main entry of the whole detail view rendering. |
|
98
|
|
|
*/ |
|
99
|
|
|
public function run() |
|
100
|
|
|
{ |
|
101
|
|
|
$rows = []; |
|
102
|
|
|
$i = 0; |
|
103
|
|
|
foreach ($this->columns as $column) { |
|
104
|
|
|
$rows[] = $this->renderColumn($column, $i++); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$tag = ArrayHelper::remove($this->options, 'tag', 'table'); |
|
108
|
|
|
return Html::tag($tag, implode("\n", $rows), $this->options); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Renders a single column. |
|
113
|
|
|
* |
|
114
|
|
|
* @param DataColumn $column the specification of the column to be rendered |
|
115
|
|
|
* @param int $index the zero-based index of the column in the [[columns]] array |
|
116
|
|
|
* |
|
117
|
|
|
* @return string the rendering result |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function renderColumn($column, $index) |
|
120
|
|
|
{ |
|
121
|
|
|
if (is_string($this->template)) { |
|
122
|
|
|
return strtr($this->template, [ |
|
123
|
|
|
'{label}' => $column->renderHeaderCell(), |
|
124
|
|
|
'{value}' => $column->renderDataCell($this->model, $this->model->getPrimaryKey(), 0), |
|
125
|
|
|
]); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return call_user_func($this->template, $column, $index, $this); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @var GridView object to be used for DataColumn creation |
|
133
|
|
|
*/ |
|
134
|
|
|
public $grid; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* {@inheritdoc} |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function createDataColumn($text) |
|
140
|
|
|
{ |
|
141
|
|
|
if ($this->grid && method_exists($this->grid, 'createDataColumn')) { |
|
142
|
|
|
return call_user_func([$this->grid, 'createDataColumn'], $text); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return parent::createDataColumn($text); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|