Merge branch 'master' into next
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / form / builder / field / WysiwygFormField.class.php
CommitLineData
5158aa21
MS
1<?php
2namespace wcf\system\form\builder\field;
76d761f6 3use wcf\system\form\builder\field\data\processor\CustomFormFieldDataProcessor;
5158aa21
MS
4use wcf\system\form\builder\field\validation\FormFieldValidationError;
5use wcf\system\form\builder\IFormDocument;
5158aa21
MS
6use wcf\system\html\input\HtmlInputProcessor;
7use wcf\util\StringUtil;
8
9/**
10 * Implementation of a form field for wysiwyg editors.
11 *
12 * @author Matthias Schmidt
13 * @copyright 2001-2018 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package WoltLabSuite\Core\System\Form\Builder\Field
16 * @since 3.2
17 */
18class WysiwygFormField extends AbstractFormField implements IMaximumLengthFormField, IMinimumLengthFormField, IObjectTypeFormField {
19 use TMaximumLengthFormField;
20 use TMinimumLengthFormField;
21 use TObjectTypeFormField;
22
23 /**
24 * identifier used to autosave the field value; if empty, autosave is disabled
25 * @var string
26 */
27 protected $__autosaveId = '';
28
29 /**
30 * last time the field has been edited; if `0`, the last edit time is unknown
31 * @var int
32 */
33 protected $__lastEditTime = 0;
34
35 /**
36 * input processor containing the wysiwyg text
37 * @var HtmlInputProcessor
38 */
39 protected $htmlInputProcessor;
40
41 /**
42 * @inheritDoc
43 */
44 protected $templateName = '__wysiwygFormField';
45
46 /**
47 * Sets the identifier used to autosave the field value and returns this field.
48 *
49 * @param string $autosaveId identifier used to autosave field value
50 * @return WysiwygFormField this field
51 */
020e6570 52 public function autosaveId($autosaveId) {
5158aa21
MS
53 $this->__autosaveId = $autosaveId;
54
55 return $this;
56 }
57
58 /**
59 * Returns the identifier used to autosave the field value. If autosave is disabled,
9d68d5b2 60 * an empty string is returned.
5158aa21
MS
61 *
62 * @return string
63 */
7b43decd 64 public function getAutosaveId() {
5158aa21
MS
65 return $this->__autosaveId;
66 }
67
68 /**
69 * @inheritDoc
70 */
7b43decd 71 public function getObjectTypeDefinition() {
5158aa21
MS
72 return 'com.woltlab.wcf.message';
73 }
74
75 /**
76 * Returns the last time the field has been edited. If no last edit time has
77 * been set, `0` is returned.
78 *
79 * @return int
80 */
7b43decd 81 public function getLastEditTime() {
5158aa21
MS
82 return $this->__lastEditTime;
83 }
84
85 /**
86 * @inheritDoc
87 */
7b43decd 88 public function hasSaveValue() {
071fff89 89 return false;
5158aa21
MS
90 }
91
92 /**
93 * Sets the last time this field has been edited and returns this field.
94 *
95 * @param int $lastEditTime last time field has been edited
96 * @return WysiwygFormField this field
97 */
020e6570 98 public function lastEditTime($lastEditTime) {
5158aa21
MS
99 $this->__lastEditTime = $lastEditTime;
100
101 return $this;
102 }
103
104 /**
105 * @inheritDoc
106 */
7b43decd 107 public function populate() {
5158aa21
MS
108 parent::populate();
109
110 $this->getDocument()->getDataHandler()->add(new CustomFormFieldDataProcessor('wysiwyg', function(IFormDocument $document, array $parameters) {
8bdbb251 111 if ($this->checkDependencies()) {
f3a825e9 112 $parameters[$this->getObjectProperty() . '_htmlInputProcessor'] = $this->htmlInputProcessor;
8bdbb251 113 }
5158aa21
MS
114
115 return $parameters;
116 }));
117
118 return $this;
119 }
120
121 /**
122 * @inheritDoc
123 */
7b43decd 124 public function readValue() {
4b683ecd
MS
125 if ($this->getDocument()->hasRequestData($this->getPrefixedId())) {
126 $value = $this->getDocument()->getRequestData($this->getPrefixedId());
127
128 if (is_string($value)) {
129 $this->__value = StringUtil::trim($value);
130 }
5158aa21
MS
131 }
132
133 return $this;
134 }
135
136 /**
137 * @inheritDoc
138 */
139 public function validate() {
140 if ($this->isRequired() && $this->getValue() === '') {
141 $this->addValidationError(new FormFieldValidationError('empty'));
142 }
143 else {
144 $this->validateMinimumLength($this->getValue());
145 $this->validateMaximumLength($this->getValue());
146 }
147
148 $this->htmlInputProcessor = new HtmlInputProcessor();
149 $this->htmlInputProcessor->process($this->getValue(), $this->getObjectType()->objectType);
150
151 parent::validate();
152 }
153}