1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* X-editable extension for Yii2 |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-x-editable |
6
|
|
|
* @package yii2-x-editable |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\xeditable\traits; |
12
|
|
|
|
13
|
|
|
use hipanel\helpers\ArrayHelper; |
14
|
|
|
use hiqdev\xeditable\assets\XEditableAsset; |
15
|
|
|
use vova07\select2\Select2Asset; |
16
|
|
|
use Yii; |
17
|
|
|
use yii\helpers\Html; |
18
|
|
|
use yii\helpers\Json; |
19
|
|
|
use yii\helpers\Url; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class XEditableTrait. |
23
|
|
|
*/ |
24
|
|
|
trait XEditableTrait |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
public $pluginOptions = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string library to be used: bootstrap, jqueryui, plain jquery |
33
|
|
|
*/ |
34
|
|
|
public $library; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array will be passed to [[Html::a]] method as options |
38
|
|
|
*/ |
39
|
|
|
public $linkOptions = []; |
40
|
|
|
|
41
|
|
|
public function registerAssets() |
42
|
|
|
{ |
43
|
|
|
$asset = new XEditableAsset(); |
44
|
|
|
if ($this->library) { |
45
|
|
|
$asset->library = $this->library; |
46
|
|
|
} |
47
|
|
|
$asset->register(Yii::$app->view); |
48
|
|
|
if ($this->pluginOptions['type'] === 'select2') { |
49
|
|
|
Select2Asset::register(Yii::$app->view); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function init() |
54
|
|
|
{ |
55
|
|
|
parent::init(); |
56
|
|
|
$this->registerAssets(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function registerMyJs($data) |
60
|
|
|
{ |
61
|
|
|
if (!$this->pluginOptions['title']) { |
62
|
|
|
$this->pluginOptions['title'] = $data['model']->getAttributeLabel($data['attribute']); |
63
|
|
|
} |
64
|
|
|
if ($this->pluginOptions['url']) { |
65
|
|
|
$this->pluginOptions['url'] = Url::to($this->pluginOptions['url']); |
66
|
|
|
} |
67
|
|
|
$selector = ArrayHelper::remove($this->pluginOptions, 'selector', ".editable[data-pk={$data['model']->primaryKey}][data-name={$data['attribute']}]"); |
68
|
|
|
Yii::$app->view->registerJs('$("' . $selector . '").editable(' . Json::htmlEncode($this->pluginOptions) . ');'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function prepareValue($data) |
72
|
|
|
{ |
73
|
|
|
if ($data['value'] !== null) { |
74
|
|
|
if ($data['value'] instanceof \Closure) { |
75
|
|
|
$value = call_user_func($data['value'], $this->model, $this); |
|
|
|
|
76
|
|
|
} else { |
77
|
|
|
$value = $data['value']; |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
|
|
$value = $data['model']->getAttribute($data['attribute']); |
81
|
|
|
if (is_array($value)) { |
82
|
|
|
if (ArrayHelper::isAssociative($value)) { |
83
|
|
|
$value = array_keys($value); |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
|
|
$value = [$value]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->pluginOptions['value'] = $value; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function prepareDataValue($data) |
94
|
|
|
{ |
95
|
|
View Code Duplication |
if (isset($this->pluginOptions['data-value'])) { |
|
|
|
|
96
|
|
|
if ($this->pluginOptions['data-value'] instanceof \Closure) { |
97
|
|
|
$result = call_user_func($this->pluginOptions['data-value'], $this, $data); |
98
|
|
|
} else { |
99
|
|
|
$result = $this->pluginOptions['data-value']; |
100
|
|
|
} |
101
|
|
|
} else { |
102
|
|
|
$result = $this->pluginOptions['value']; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->pluginOptions['data-value'] = $result; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function prepareDisplayValue($data) |
109
|
|
|
{ |
110
|
|
View Code Duplication |
if (isset($this->pluginOptions['data-display-value'])) { |
|
|
|
|
111
|
|
|
if ($this->pluginOptions['data-display-value'] instanceof \Closure) { |
112
|
|
|
$result = call_user_func($this->pluginOptions['data-display-value'], $this, $data); |
113
|
|
|
} else { |
114
|
|
|
$result = $this->pluginOptions['data-display-value']; |
115
|
|
|
} |
116
|
|
|
} else { |
117
|
|
|
$result = $this->pluginOptions['value']; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (is_array($result)) { |
121
|
|
|
if ($this->pluginOptions['source']) { |
122
|
|
|
$result = ArrayHelper::getValues($this->pluginOptions['source'], $result); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$result = implode('<br />', $result); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$this->pluginOptions['data-display-value'] = $result; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function prepareHtml($data) |
132
|
|
|
{ |
133
|
|
|
$this->prepareValue($data); |
134
|
|
|
$this->prepareDataValue($data); |
135
|
|
|
$this->prepareDisplayValue($data); |
136
|
|
|
$this->registerMyJs($data); |
137
|
|
|
|
138
|
|
|
$params = ArrayHelper::merge([ |
139
|
|
|
'id' => $this->getId(), |
|
|
|
|
140
|
|
|
'class' => 'editable', |
141
|
|
|
'data-pk' => $data['model']->primaryKey, |
142
|
|
|
'data-name' => $data['attribute'], |
143
|
|
|
'data-value' => $this->pluginOptions['data-value'], |
144
|
|
|
], $this->linkOptions); |
145
|
|
|
|
146
|
|
|
return Html::a($this->pluginOptions['data-display-value'], '#', $params); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: