Commit | Line | Data |
---|---|---|
5158aa21 | 1 | <?php |
71212588 MS |
2 | namespace wcf\system\form\builder\field\wysiwyg; |
3 | use wcf\system\form\builder\field\AbstractFormField; | |
76d761f6 | 4 | use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor; |
71212588 MS |
5 | use wcf\system\form\builder\field\IMaximumLengthFormField; |
6 | use wcf\system\form\builder\field\IMinimumLengthFormField; | |
7 | use wcf\system\form\builder\field\IObjectTypeFormNode; | |
8 | use wcf\system\form\builder\field\TMaximumLengthFormField; | |
9 | use wcf\system\form\builder\field\TMinimumLengthFormField; | |
10 | use wcf\system\form\builder\field\TObjectTypeFormNode; | |
5158aa21 MS |
11 | use wcf\system\form\builder\field\validation\FormFieldValidationError; |
12 | use wcf\system\form\builder\IFormDocument; | |
5158aa21 MS |
13 | use wcf\system\html\input\HtmlInputProcessor; |
14 | use wcf\util\StringUtil; | |
15 | ||
16 | /** | |
17 | * Implementation of a form field for wysiwyg editors. | |
18 | * | |
19 | * @author Matthias Schmidt | |
7b7b9764 | 20 | * @copyright 2001-2019 WoltLab GmbH |
5158aa21 MS |
21 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
22 | * @package WoltLabSuite\Core\System\Form\Builder\Field | |
dd2d8c0c | 23 | * @since 5.2 |
5158aa21 | 24 | */ |
71212588 | 25 | class WysiwygFormField extends AbstractFormField implements IMaximumLengthFormField, IMinimumLengthFormField, IObjectTypeFormNode { |
5158aa21 MS |
26 | use TMaximumLengthFormField; |
27 | use TMinimumLengthFormField; | |
71212588 | 28 | use TObjectTypeFormNode; |
5158aa21 MS |
29 | |
30 | /** | |
31 | * identifier used to autosave the field value; if empty, autosave is disabled | |
32 | * @var string | |
33 | */ | |
9299c8e5 | 34 | protected $autosaveId = ''; |
5158aa21 | 35 | |
71212588 MS |
36 | /** |
37 | * input processor containing the wysiwyg text | |
38 | * @var HtmlInputProcessor | |
39 | */ | |
40 | protected $htmlInputProcessor; | |
41 | ||
5158aa21 MS |
42 | /** |
43 | * last time the field has been edited; if `0`, the last edit time is unknown | |
44 | * @var int | |
45 | */ | |
9299c8e5 | 46 | protected $lastEditTime = 0; |
5158aa21 MS |
47 | |
48 | /** | |
71212588 MS |
49 | * is `true` if this form field should support attachments, otherwise `false` |
50 | * @var boolean | |
5158aa21 | 51 | */ |
71212588 MS |
52 | protected $supportAttachments = false; |
53 | ||
54 | /** | |
55 | * is `true` if this form field should support mentions, otherwise `false` | |
56 | * @var boolean | |
57 | */ | |
58 | protected $supportMentions = false; | |
5158aa21 MS |
59 | |
60 | /** | |
61 | * @inheritDoc | |
62 | */ | |
63 | protected $templateName = '__wysiwygFormField'; | |
64 | ||
65 | /** | |
66 | * Sets the identifier used to autosave the field value and returns this field. | |
67 | * | |
68 | * @param string $autosaveId identifier used to autosave field value | |
71212588 MS |
69 | * |
70 | * @return WysiwygFormNode this field | |
5158aa21 | 71 | */ |
020e6570 | 72 | public function autosaveId($autosaveId) { |
9299c8e5 | 73 | $this->autosaveId = $autosaveId; |
5158aa21 MS |
74 | |
75 | return $this; | |
76 | } | |
77 | ||
78 | /** | |
79 | * Returns the identifier used to autosave the field value. If autosave is disabled, | |
9d68d5b2 | 80 | * an empty string is returned. |
5158aa21 MS |
81 | * |
82 | * @return string | |
83 | */ | |
7b43decd | 84 | public function getAutosaveId() { |
9299c8e5 | 85 | return $this->autosaveId; |
5158aa21 MS |
86 | } |
87 | ||
88 | /** | |
89 | * @inheritDoc | |
90 | */ | |
7b43decd | 91 | public function getObjectTypeDefinition() { |
5158aa21 MS |
92 | return 'com.woltlab.wcf.message'; |
93 | } | |
94 | ||
95 | /** | |
96 | * Returns the last time the field has been edited. If no last edit time has | |
97 | * been set, `0` is returned. | |
98 | * | |
99 | * @return int | |
100 | */ | |
7b43decd | 101 | public function getLastEditTime() { |
9299c8e5 | 102 | return $this->lastEditTime; |
5158aa21 MS |
103 | } |
104 | ||
105 | /** | |
106 | * @inheritDoc | |
107 | */ | |
7b43decd | 108 | public function hasSaveValue() { |
071fff89 | 109 | return false; |
5158aa21 MS |
110 | } |
111 | ||
112 | /** | |
113 | * Sets the last time this field has been edited and returns this field. | |
114 | * | |
115 | * @param int $lastEditTime last time field has been edited | |
71212588 MS |
116 | * |
117 | * @return WysiwygFormNode this field | |
5158aa21 | 118 | */ |
020e6570 | 119 | public function lastEditTime($lastEditTime) { |
9299c8e5 | 120 | $this->lastEditTime = $lastEditTime; |
5158aa21 MS |
121 | |
122 | return $this; | |
123 | } | |
124 | ||
125 | /** | |
126 | * @inheritDoc | |
127 | */ | |
7b43decd | 128 | public function populate() { |
5158aa21 MS |
129 | parent::populate(); |
130 | ||
131 | $this->getDocument()->getDataHandler()->add(new CustomFormFieldDataProcessor('wysiwyg', function(IFormDocument $document, array $parameters) { | |
8bdbb251 | 132 | if ($this->checkDependencies()) { |
f3a825e9 | 133 | $parameters[$this->getObjectProperty() . '_htmlInputProcessor'] = $this->htmlInputProcessor; |
8bdbb251 | 134 | } |
5158aa21 MS |
135 | |
136 | return $parameters; | |
137 | })); | |
138 | ||
139 | return $this; | |
140 | } | |
141 | ||
142 | /** | |
143 | * @inheritDoc | |
144 | */ | |
7b43decd | 145 | public function readValue() { |
4b683ecd MS |
146 | if ($this->getDocument()->hasRequestData($this->getPrefixedId())) { |
147 | $value = $this->getDocument()->getRequestData($this->getPrefixedId()); | |
148 | ||
149 | if (is_string($value)) { | |
9299c8e5 | 150 | $this->value = StringUtil::trim($value); |
4b683ecd | 151 | } |
5158aa21 MS |
152 | } |
153 | ||
154 | return $this; | |
155 | } | |
156 | ||
71212588 MS |
157 | /** |
158 | * Sets if the form field supports attachments and returns this field. | |
159 | * | |
160 | * @param boolean $supportAttachments | |
161 | * | |
162 | * @return WysiwygFormNode | |
163 | */ | |
164 | public function supportAttachments($supportAttachments = true) { | |
165 | $this->supportAttachments = $supportAttachments; | |
166 | ||
167 | return $this; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Sets if the form field supports mentions and returns this field. | |
172 | * | |
173 | * @param boolean $supportMentions | |
174 | * | |
175 | * @return WysiwygFormNode | |
176 | */ | |
177 | public function supportMentions($supportMentions = true) { | |
178 | $this->supportMentions = $supportMentions; | |
179 | ||
180 | return $this; | |
181 | } | |
182 | ||
183 | /** | |
184 | * Returns `true` if the form field supports attachments and returns `false` otherwise. | |
185 | * | |
186 | * Important: If this method returns `true`, it does not necessarily mean that attachment | |
187 | * support will also work as that is the task of `WysiwygAttachmentFormField`. This method | |
188 | * is primarily relevant to inform the JavaScript API that the field supports attachments | |
189 | * so that the relevant editor plugin is loaded. | |
190 | * | |
191 | * By default, attachments are not supported. | |
192 | * | |
193 | * @return boolean | |
194 | */ | |
195 | public function supportsAttachments() { | |
196 | return $this->supportAttachments; | |
197 | } | |
198 | ||
199 | /** | |
200 | * Returns `true` if the form field supports mentions and returns `false` otherwise. | |
201 | * | |
202 | * By default, mentions are not supported. | |
203 | * | |
204 | * @return boolean | |
205 | */ | |
206 | public function supportsMentions() { | |
207 | return $this->supportMentions; | |
208 | } | |
209 | ||
5158aa21 MS |
210 | /** |
211 | * @inheritDoc | |
212 | */ | |
213 | public function validate() { | |
214 | if ($this->isRequired() && $this->getValue() === '') { | |
215 | $this->addValidationError(new FormFieldValidationError('empty')); | |
216 | } | |
217 | else { | |
218 | $this->validateMinimumLength($this->getValue()); | |
219 | $this->validateMaximumLength($this->getValue()); | |
220 | } | |
221 | ||
222 | $this->htmlInputProcessor = new HtmlInputProcessor(); | |
223 | $this->htmlInputProcessor->process($this->getValue(), $this->getObjectType()->objectType); | |
224 | ||
225 | parent::validate(); | |
226 | } | |
227 | } |