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