Commit | Line | Data |
---|---|---|
158bd3ca | 1 | <?php |
a9229942 | 2 | |
158bd3ca | 3 | namespace wcf\system\option; |
a9229942 | 4 | |
158bd3ca | 5 | use wcf\data\option\Option; |
87d3a054 MS |
6 | use wcf\data\user\User; |
7 | use wcf\data\user\UserList; | |
ec1b1daf | 8 | use wcf\system\database\util\PreparedStatementConditionBuilder; |
8e5a1d4c | 9 | use wcf\system\exception\UserInputException; |
2bc9f31d | 10 | use wcf\system\WCF; |
158bd3ca TD |
11 | use wcf\util\StringUtil; |
12 | ||
13 | /** | |
a17de04e | 14 | * Option type implementation for textual input fields. |
a9229942 TD |
15 | * |
16 | * @author Marcel Werk | |
17 | * @copyright 2001-2019 WoltLab GmbH | |
18 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
158bd3ca | 19 | */ |
a9229942 TD |
20 | class TextOptionType extends AbstractOptionType implements ISearchableConditionUserOption |
21 | { | |
22 | /** | |
23 | * input type | |
24 | * @var string | |
25 | */ | |
26 | protected $inputType = 'text'; | |
27 | ||
28 | /** | |
29 | * input css class | |
30 | * @var string | |
31 | */ | |
32 | protected $inputClass = 'long'; | |
33 | ||
34 | /** | |
35 | * if `true`, the option is considered as being searched when generating the form element | |
36 | * @var bool | |
37 | */ | |
38 | public $forceSearchOption = false; | |
39 | ||
40 | /** | |
41 | * @inheritDoc | |
42 | */ | |
43 | public function getFormElement(Option $option, $value) | |
44 | { | |
45 | WCF::getTPL()->assign([ | |
46 | 'option' => $option, | |
47 | 'inputType' => $this->inputType, | |
48 | 'inputClass' => $this->inputClass, | |
49 | 'value' => $value, | |
50 | ]); | |
51 | ||
52 | return WCF::getTPL()->fetch('textOptionType'); | |
53 | } | |
54 | ||
55 | /** | |
56 | * @inheritDoc | |
57 | */ | |
58 | public function getSearchFormElement(Option $option, $value) | |
59 | { | |
60 | WCF::getTPL()->assign([ | |
61 | 'option' => $option, | |
62 | 'inputType' => $this->inputType, | |
63 | 'inputClass' => $this->inputClass, | |
64 | 'searchOption' => $this->forceSearchOption || ($value !== null && $value !== $option->defaultValue) || isset($_POST['searchOptions'][$option->optionName]), | |
65 | 'value' => $value, | |
66 | ]); | |
67 | ||
68 | return WCF::getTPL()->fetch('textSearchableOptionType'); | |
69 | } | |
70 | ||
71 | /** | |
72 | * @inheritDoc | |
73 | */ | |
74 | public function getCondition(PreparedStatementConditionBuilder &$conditions, Option $option, $value) | |
75 | { | |
76 | if (!isset($_POST['searchOptions'][$option->optionName])) { | |
77 | return false; | |
78 | } | |
79 | ||
a945e985 | 80 | $value = StringUtil::trim($value ?: ''); |
a9229942 TD |
81 | if ($value == '') { |
82 | $conditions->add("option_value.userOption" . $option->optionID . " = ?", ['']); | |
83 | } else { | |
84 | $conditions->add( | |
85 | "option_value.userOption" . $option->optionID . " LIKE ?", | |
86 | ['%' . \addcslashes($value, '_%') . '%'] | |
87 | ); | |
88 | } | |
89 | ||
90 | return true; | |
91 | } | |
92 | ||
93 | /** | |
94 | * @inheritDoc | |
95 | */ | |
96 | public function validate(Option $option, $newValue) | |
97 | { | |
98 | $newValue = $this->getContent($option, $newValue); | |
99 | ||
100 | if ($option->minlength !== null && $option->minlength > \mb_strlen($newValue)) { | |
101 | throw new UserInputException($option->optionName, 'tooShort'); | |
102 | } | |
103 | if ($option->maxlength !== null && $option->maxlength < \mb_strlen($newValue)) { | |
104 | throw new UserInputException($option->optionName, 'tooLong'); | |
105 | } | |
106 | } | |
107 | ||
108 | /** | |
109 | * @inheritDoc | |
110 | */ | |
111 | public function getData(Option $option, $newValue) | |
112 | { | |
113 | return $this->getContent($option, $newValue); | |
114 | } | |
115 | ||
116 | /** | |
117 | * Tries to extract content from value. | |
118 | * | |
119 | * @param Option $option | |
120 | * @param string $newValue | |
121 | * @return string | |
122 | */ | |
123 | protected function getContent(Option $option, $newValue) | |
124 | { | |
125 | if ($option->contentpattern) { | |
126 | if (\preg_match('~' . $option->contentpattern . '~', $newValue, $matches)) { | |
127 | unset($matches[0]); | |
128 | $newValue = \implode('', $matches); | |
129 | } | |
130 | } | |
131 | ||
132 | return $newValue; | |
133 | } | |
134 | ||
135 | /** | |
136 | * @inheritDoc | |
137 | */ | |
138 | public function addCondition(UserList $userList, Option $option, $value) | |
139 | { | |
140 | $value = StringUtil::trim($value); | |
141 | if ($value == '') { | |
142 | $userList->getConditionBuilder()->add('user_option_value.userOption' . $option->optionID . ' = ?', ['']); | |
143 | } else { | |
144 | $userList->getConditionBuilder()->add( | |
145 | 'user_option_value.userOption' . $option->optionID . ' LIKE ?', | |
146 | ['%' . \addcslashes($value, '_%') . '%'] | |
147 | ); | |
148 | } | |
149 | } | |
150 | ||
151 | /** | |
152 | * @inheritDoc | |
153 | */ | |
154 | public function checkUser(User $user, Option $option, $value) | |
155 | { | |
156 | $value = StringUtil::trim($value); | |
157 | if ($value == '') { | |
158 | return $user->getUserOption($option->optionName) == ''; | |
159 | } else { | |
160 | return \mb_stripos($user->getUserOption($option->optionName), $value) !== false; | |
161 | } | |
162 | } | |
163 | ||
164 | /** | |
165 | * @inheritDoc | |
166 | */ | |
167 | public function getConditionData(Option $option, $newValue) | |
168 | { | |
169 | return $newValue; | |
170 | } | |
171 | ||
172 | /** | |
173 | * @inheritDoc | |
174 | */ | |
175 | public function hideLabelInSearch() | |
176 | { | |
177 | return true; | |
178 | } | |
dcb3a44c | 179 | } |