1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-grid-view-library project. |
5
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
6
|
|
|
* For the full copyright and license information, please view |
7
|
|
|
* the LICENSE file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace dosamigos\grid\columns; |
11
|
|
|
|
12
|
|
|
use dosamigos\editable\EditableAddressAsset; |
13
|
|
|
use dosamigos\editable\EditableBootstrapAsset; |
14
|
|
|
use dosamigos\editable\EditableComboDateAsset; |
15
|
|
|
use dosamigos\editable\EditableDatePickerAsset; |
16
|
|
|
use dosamigos\editable\EditableDateTimePickerAsset; |
17
|
|
|
use dosamigos\editable\EditableSelect2Asset; |
18
|
|
|
use dosamigos\editable\EditableWysiHtml5Asset; |
19
|
|
|
use dosamigos\grid\bundles\EditableColumnAsset; |
20
|
|
|
use yii\base\InvalidConfigException; |
21
|
|
|
use yii\helpers\Html; |
22
|
|
|
use yii\helpers\Url; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* EditableColumn adds X-Editable capabilities to a column |
26
|
|
|
*/ |
27
|
|
|
class EditableColumn extends DataColumn |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array the options for the X-editable.js plugin. |
31
|
|
|
* Please refer to the X-editable.js plugin web page for possible options. |
32
|
|
|
* @see http://vitalets.github.io/x-editable/docs.html#editable |
33
|
|
|
*/ |
34
|
|
|
public $editableOptions = []; |
35
|
|
|
/** |
36
|
|
|
* @var string suffix substituted to a name class of the tag <a> |
37
|
|
|
*/ |
38
|
|
|
public $classSuffix; |
39
|
|
|
/** |
40
|
|
|
* @var string the url to post |
41
|
|
|
*/ |
42
|
|
|
public $url; |
43
|
|
|
/** |
44
|
|
|
* @var string the type of editor |
45
|
|
|
*/ |
46
|
|
|
public $type = 'text'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string the language of editor |
50
|
|
|
*/ |
51
|
|
|
public $language = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
* @throws \yii\base\InvalidConfigException |
56
|
|
|
*/ |
57
|
|
|
public function init() |
58
|
|
|
{ |
59
|
|
|
if ($this->url === null) { |
60
|
|
|
throw new InvalidConfigException("'Url' property must be specified."); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
parent::init(); |
64
|
|
|
|
65
|
|
|
if (!$this->format) { |
66
|
|
|
$this->format = 'raw'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$rel = $this->attribute . '_editable' . $this->classSuffix; |
70
|
|
|
$this->options['pjax'] = '0'; |
71
|
|
|
$this->options['rel'] = $rel; |
72
|
|
|
|
73
|
|
|
$this->registerClientScript(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
protected function renderDataCellContent($model, $key, $index) |
80
|
|
|
{ |
81
|
|
|
$value = parent::renderDataCellContent($model, $key, $index); |
82
|
|
|
if (is_callable($this->editableOptions)) { |
83
|
|
|
$opts = call_user_func($this->editableOptions, $model, $key, $index); |
84
|
|
|
foreach ($opts as $prop => $v) { |
85
|
|
|
$this->options['data-' . $prop] = $v; |
86
|
|
|
} |
87
|
|
|
} elseif (is_array($this->editableOptions)) { |
88
|
|
|
foreach ($this->editableOptions as $prop => $v) { |
89
|
|
|
$this->options['data-' . $prop] = $v; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$url = (array)$this->url; |
94
|
|
|
$this->options['data-url'] = Url::to($url); |
95
|
|
|
$this->options['data-pk'] = base64_encode(serialize($key)); |
96
|
|
|
$this->options['data-name'] = $this->attribute; |
97
|
|
|
$this->options['data-type'] = $this->type; |
98
|
|
|
|
99
|
|
|
return Html::a($value, null, $this->options); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Registers required script to the columns work |
104
|
|
|
*/ |
105
|
|
|
protected function registerClientScript() |
106
|
|
|
{ |
107
|
|
|
$view = $this->grid->getView(); |
108
|
|
|
$language = $this->language; |
109
|
|
|
|
110
|
|
|
switch ($this->type) { |
111
|
|
|
case 'address': |
112
|
|
|
EditableAddressAsset::register($view); |
113
|
|
|
break; |
114
|
|
|
case 'combodate': |
115
|
|
|
EditableComboDateAsset::register($view); |
116
|
|
|
break; |
117
|
|
|
case 'date': |
118
|
|
|
if ($language) { |
119
|
|
|
EditableDatePickerAsset::register( |
120
|
|
|
$view |
121
|
|
|
)->js[] = 'vendor/js/locales/bootstrap-datetimepicker.' . $language . '.js'; |
122
|
|
|
} else { |
123
|
|
|
EditableDatePickerAsset::register($view); |
124
|
|
|
} |
125
|
|
|
break; |
126
|
|
|
case 'datetime': |
127
|
|
|
if ($language) { |
128
|
|
|
EditableDateTimePickerAsset::register( |
129
|
|
|
$view |
130
|
|
|
)->js[] = 'vendor/js/locales/bootstrap-datetimepicker.' . $language . '.js'; |
131
|
|
|
} else { |
132
|
|
|
EditableDateTimePickerAsset::register($view); |
133
|
|
|
} |
134
|
|
|
break; |
135
|
|
|
case 'select2': |
136
|
|
|
EditableSelect2Asset::register($view); |
137
|
|
|
break; |
138
|
|
|
case 'wysihtml5': |
139
|
|
|
$language = $language ?: 'en-US'; |
140
|
|
|
EditableWysiHtml5Asset::register( |
141
|
|
|
$view |
142
|
|
|
)->js[] = 'vendor/locales/bootstrap-wysihtml5.' . $language . '.js'; |
143
|
|
|
break; |
144
|
|
|
default: |
145
|
|
|
EditableBootstrapAsset::register($view); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
EditableColumnAsset::register($view); |
149
|
|
|
$rel = $this->options['rel']; |
150
|
|
|
$selector = "a[rel=\"$rel\"]"; |
151
|
|
|
$grid = "#{$this->grid->id}"; |
152
|
|
|
$script = ";jQuery('$selector').editable();"; |
153
|
|
|
$hash = hash('crc32', $script); |
154
|
|
|
$js = []; |
155
|
|
|
$js[] = $script; |
156
|
|
|
$js[] = "dosamigos.editableColumn.registerHandler('$grid', '$selector', '$hash');"; |
157
|
|
|
$view->registerJs(implode("\n", $js)); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|