-
Notifications
You must be signed in to change notification settings - Fork 7
/
TabularWidget.php
229 lines (211 loc) · 6.6 KB
/
TabularWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
namespace mdm\widgets;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\widgets\ActiveForm;
/**
* Description of TabularWidget
*
* @author Misbahul D Munir <misbahuldmunir@gmail.com>
* @since 1.0
*/
abstract class TabularWidget extends Widget
{
public $tag = 'div';
/**
* @var array the HTML attributes for the container tag of the list view.
* The "tag" element specifies the tag name of the container element and defaults to "div".
* @see Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];
/**
* @var array the HTML attributes for the container of the rendering result of each data model.
* The "tag" element specifies the tag name of the container element and defaults to "div".
* If "tag" is false, it means no container element will be rendered.
* @see Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $itemOptions = [];
/**
* @var array the HTML attributes for the container of the rendering result of each data model.
* The "tag" element specifies the tag name of the container element and defaults to "div".
* If "tag" is false, it means no container element will be rendered.
* @see Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $containerOptions = [];
/**
* @var string the HTML code to be displayed between any two consecutive items.
*/
public $separator = "\n";
/**
* @var Model[]|array
*/
public $allModels = [];
/**
* @var mixed
*/
public $model;
/**
* @var ActiveForm
*/
public $form;
/**
* @var array Client option
*/
public $clientOptions = [];
/**
* @var string
*/
public $layout = "{header}\n{items}";
/**
* Header
* @var string
*/
public $header;
/**
* Footer
* @var string
*/
public $footer;
/**
* Part
* @var array
*/
public $sections = [];
protected $level;
private static $_level = 0;
/**
* Renders a single data model.
* @param mixed $model the data model to be rendered
* @param mixed $key the key value associated with the data model
* @param integer $index the zero-based index of the data model in the model array returned by [[dataProvider]].
* @return string the rendering result
*/
abstract public function renderItem($model, $key, $index);
/**
* @inheritdoc
*/
public function init()
{
$this->level = self::$_level === 0 ? '' : self::$_level;
self::$_level++;
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
if (!empty($this->model) && !is_object($this->model)) {
$this->model = Yii::createObject($this->model);
}
}
/**
* @inheritdoc
*/
public function run()
{
if (!empty($this->containerOptions['tag']) && empty($this->clientOptions['container'])) {
Html::addCssClass($this->containerOptions, 'mdm-container');
$this->clientOptions['container'] = "{$this->containerOptions['tag']}.mdm-container";
}
$this->registerClientScript();
$content = preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) {
$method = 'render' . $matches[1];
if (method_exists($this, $method)) {
return $this->$method();
} elseif (isset($this->sections[$matches[1]])) {
return $matches[1];
}
return $matches[0];
}, $this->layout);
self::$_level--;
echo Html::tag($this->tag, $content, $this->options);
}
/**
* Renders all data models.
* @return string the rendering result
*/
public function renderHeader()
{
return $this->header;
}
/**
* Renders all data models.
* @return string the rendering result
*/
public function renderItems()
{
$rows = [];
$index = 0;
foreach ($this->allModels as $key => $model) {
$rows[] = $this->renderItem($model, $key, $index++);
}
$content = implode($this->separator, $rows);
$options = $this->containerOptions;
if (empty($options['tag'])) {
return $content;
}
$tag = $options['tag'];
unset($options['tag']);
return Html::tag($tag, $content, $options);
}
public function renderFooter()
{
return $this->footer;
}
/**
* Register script
*/
protected function registerClientScript()
{
$id = $this->options['id'];
$options = Json::htmlEncode($this->getClientOptions());
$view = $this->getView();
TabularAsset::register($view);
$view->registerJs("jQuery('#$id').mdmTabularInput($options);");
}
/**
* Get client options
* @return array
*/
protected function getClientOptions()
{
$counter = count($this->allModels) ? max(array_keys($this->allModels)) + 1 : 0;
$clientOptions = $this->clientOptions;
$clientOptions['counter'] = $counter;
if (empty($clientOptions['itemSelector'])) {
throw new InvalidConfigException('Value of "clientOptions[\'itemSelector\']" must be specified.');
}
if ($this->form instanceof ActiveForm) {
$clientOptions['formSelector'] = '#' . $this->form->options['id'];
$oldAttrs = $this->form->attributes;
$this->form->attributes = [];
}
// template and js
$view = $this->getView();
$oldJs = $view->js;
$view->js = [];
$template = $this->renderItem($this->model, "_dkey{$this->level}_", "_dindex{$this->level}_");
if (isset($oldAttrs)) {
$clientOptions['validations'] = $this->form->attributes;
$this->form->attributes = $oldAttrs;
}
$js = [];
ksort($view->js);
foreach ($view->js as $pieces) {
$js[] = implode("\n", $pieces);
}
if (count($js)) {
$clientOptions['templateJs'] = implode("\n", $js);
}
$view->js = $oldJs;
// ***
$clientOptions['template'] = $template;
$clientOptions['replaces'] = [
'key' => new JsExpression("/_dkey{$this->level}_/g"),
'index' => new JsExpression("/_dindex{$this->level}_/g"),
];
return $clientOptions;
}
}