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