|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Element; |
|
4
|
|
|
|
|
5
|
|
|
use SleepingOwl\Admin\Exceptions\WysiwygException; |
|
6
|
|
|
use SleepingOwl\Admin\Contracts\Wysiwyg\WysiwygEditorInterface; |
|
7
|
|
|
|
|
8
|
|
|
class Wysiwyg extends NamedFormElement |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string|null |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $editor; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var bool|null |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $collapsed; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string|null |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $filteredFieldKey; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $parameters = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $filterValue = true; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $view = 'form.element.wysiwyg'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Wysiwyg constructor. |
|
42
|
|
|
* @param $path |
|
43
|
|
|
* @param null $label |
|
|
|
|
|
|
44
|
|
|
* @param null $editor |
|
|
|
|
|
|
45
|
|
|
* @throws \SleepingOwl\Admin\Exceptions\Form\FormElementException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct($path, $label = null, $editor = null) |
|
48
|
|
|
{ |
|
49
|
|
|
parent::__construct($path, $label); |
|
50
|
|
|
|
|
51
|
|
|
if (is_null($editor)) { |
|
|
|
|
|
|
52
|
|
|
$editor = app('sleeping_owl.wysiwyg')->getDefaultEditorId(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->setEditor($editor); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @throws WysiwygException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function initialize() |
|
62
|
|
|
{ |
|
63
|
|
|
/** @var WysiwygEditorInterface $editor */ |
|
64
|
|
|
$editor = app('sleeping_owl.wysiwyg')->getEditor($this->getEditor()); |
|
65
|
|
|
|
|
66
|
|
|
if (is_null($editor)) { |
|
67
|
|
|
throw new WysiwygException("Wysiwyg editor [{$this->getEditor()}] is not defined."); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
app('sleeping_owl.wysiwyg')->loadEditor($this->getEditor()); |
|
71
|
|
|
|
|
72
|
|
|
$config = $editor->getConfig(); |
|
73
|
|
|
$config->set($this->parameters); |
|
74
|
|
|
|
|
75
|
|
|
if (! $this->hasHtmlAttribute('id')) { |
|
76
|
|
|
$this->setHtmlAttribute('id', $this->getName()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->parameters = (array) $config->all(); |
|
80
|
|
|
|
|
81
|
|
|
$params = collect($this->parameters); |
|
82
|
|
|
|
|
83
|
|
|
if (! $params->has('uploadUrl')) { |
|
84
|
|
|
$this->parameters['uploadUrl'] = route('admin.ckeditor.upload', ['_token' => csrf_token()]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (! $params->has('filebrowserUploadUrl')) { |
|
88
|
|
|
$this->parameters['filebrowserUploadUrl'] = route('admin.ckeditor.upload', ['_token' => csrf_token()]); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
|
|
public function disableFilter() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->filterValue = false; |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
public function canFilterValue() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->filterValue; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return null|string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getEditor() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->editor; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return null|string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getCollapsed() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->collapsed; |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param null|string $editor |
|
128
|
|
|
* |
|
129
|
|
|
* @return $this |
|
130
|
|
|
*/ |
|
131
|
|
|
public function setEditor($editor) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->editor = $editor; |
|
134
|
|
|
|
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param null|bool $collapsed |
|
140
|
|
|
* |
|
141
|
|
|
* @return $this |
|
142
|
|
|
*/ |
|
143
|
|
|
public function setCollapsed($collapsed) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->collapsed = $collapsed; |
|
146
|
|
|
|
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param int|null $height |
|
152
|
|
|
* |
|
153
|
|
|
* @return $this |
|
154
|
|
|
*/ |
|
155
|
|
|
public function setHeight($height) |
|
156
|
|
|
{ |
|
157
|
|
|
$this->parameters['height'] = (int) $height; |
|
158
|
|
|
|
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getParameters() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->parameters; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param array $parameters |
|
172
|
|
|
* |
|
173
|
|
|
* @return $this |
|
174
|
|
|
*/ |
|
175
|
|
|
public function setParameters(array $parameters) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->parameters = $parameters; |
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param string $field |
|
184
|
|
|
* |
|
185
|
|
|
* @return $this |
|
186
|
|
|
*/ |
|
187
|
|
|
public function setFilteredValueToField($field) |
|
188
|
|
|
{ |
|
189
|
|
|
$this->filteredFieldKey = $field; |
|
190
|
|
|
|
|
191
|
|
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return array |
|
196
|
|
|
*/ |
|
197
|
|
|
public function toArray() |
|
198
|
|
|
{ |
|
199
|
|
|
return ['attributes' => $this->getHtmlAttributes()] + parent::toArray() + [ |
|
200
|
|
|
'parameters' => json_encode($this->getParameters()), |
|
201
|
|
|
'editor' => $this->getEditor(), |
|
202
|
|
|
'collapsed' => $this->getCollapsed(), |
|
203
|
|
|
]; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @param mixed $value |
|
208
|
|
|
* |
|
209
|
|
|
* @return void |
|
210
|
|
|
*/ |
|
211
|
|
|
public function setModelAttribute($value) |
|
212
|
|
|
{ |
|
213
|
|
|
if (! empty($this->filteredFieldKey)) { |
|
214
|
|
|
parent::setModelAttribute($value); |
|
215
|
|
|
|
|
216
|
|
|
$this->setModelAttributeKey($this->filteredFieldKey); |
|
217
|
|
|
parent::setModelAttribute( |
|
218
|
|
|
$this->filterValue($value) |
|
219
|
|
|
); |
|
220
|
|
|
} else { |
|
221
|
|
|
parent::setModelAttribute( |
|
222
|
|
|
$this->filterValue($value) |
|
223
|
|
|
); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @param string $value |
|
229
|
|
|
* |
|
230
|
|
|
* @return string |
|
231
|
|
|
*/ |
|
232
|
|
|
protected function filterValue($value) |
|
233
|
|
|
{ |
|
234
|
|
|
if ($this->canFilterValue()) { |
|
235
|
|
|
return app('sleeping_owl.wysiwyg')->applyFilter($this->getEditor(), $value); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
return $value; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|