Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / option / TextOptionType.class.php
1 <?php
2 namespace wcf\system\option;
3 use wcf\data\option\Option;
4 use wcf\system\database\util\PreparedStatementConditionBuilder;
5 use wcf\system\exception\UserInputException;
6 use wcf\system\WCF;
7 use wcf\util\StringUtil;
8
9 /**
10 * Option type implementation for textual input fields.
11 *
12 * @author Marcel Werk
13 * @copyright 2001-2014 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package com.woltlab.wcf
16 * @subpackage system.option
17 * @category Community Framework
18 */
19 class TextOptionType extends AbstractOptionType implements ISearchableUserOption {
20 /**
21 * input type
22 * @var string
23 */
24 protected $inputType = 'text';
25
26 /**
27 * input css class
28 * @var string
29 */
30 protected $inputClass = 'long';
31
32 /**
33 * @see \wcf\system\option\IOptionType::getFormElement()
34 */
35 public function getFormElement(Option $option, $value) {
36 WCF::getTPL()->assign(array(
37 'option' => $option,
38 'inputType' => $this->inputType,
39 'inputClass' => $this->inputClass,
40 'value' => $value
41 ));
42 return WCF::getTPL()->fetch('textOptionType');
43 }
44
45 /**
46 * @see \wcf\system\option\ISearchableUserOption::getSearchFormElement()
47 */
48 public function getSearchFormElement(Option $option, $value) {
49 WCF::getTPL()->assign(array(
50 'option' => $option,
51 'inputType' => $this->inputType,
52 'inputClass' => $this->inputClass,
53 'searchOption' => isset($_POST['searchOptions'][$option->optionName]),
54 'value' => $value
55 ));
56 return WCF::getTPL()->fetch('textSearchableOptionType');
57 }
58
59 /**
60 * @see \wcf\system\option\ISearchableUserOption::getCondition()
61 */
62 public function getCondition(PreparedStatementConditionBuilder &$conditions, Option $option, $value) {
63 if (!isset($_POST['searchOptions'][$option->optionName])) return false;
64
65 $value = StringUtil::trim($value);
66 if ($value == '') {
67 $conditions->add("option_value.userOption".$option->optionID." = ?", array(''));
68 }
69 else {
70 $conditions->add("option_value.userOption".$option->optionID." LIKE ?", array('%'.addcslashes($value, '_%').'%'));
71 }
72
73 return true;
74 }
75
76 /**
77 * @see \wcf\system\option\IOptionType::validate()
78 */
79 public function validate(Option $option, $newValue) {
80 $newValue = $this->getContent($option, $newValue);
81
82 if ($option->minlength !== null && $option->minlength > mb_strlen($newValue)) {
83 throw new UserInputException($option->optionName, 'tooShort');
84 }
85 if ($option->maxlength !== null && $option->maxlength < mb_strlen($newValue)) {
86 throw new UserInputException($option->optionName, 'tooLong');
87 }
88 }
89
90 /**
91 * @see \wcf\system\option\IOptionType::getData()
92 */
93 public function getData(Option $option, $newValue) {
94 return $this->getContent($option, $newValue);
95 }
96
97 /**
98 * Tries to extract content from value.
99 *
100 * @param \wcf\data\option\Option $option
101 * @param string $newValue
102 * @return string
103 */
104 protected function getContent(Option $option, $newValue) {
105 if ($option->contentpattern) {
106 if (preg_match('~'.$option->contentpattern.'~', $newValue, $matches)) {
107 unset($matches[0]);
108 $newValue = implode('', $matches);
109 }
110 }
111
112 return $newValue;
113 }
114 }