Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / option / TextOptionType.class.php
CommitLineData
158bd3ca
TD
1<?php
2namespace wcf\system\option;
3use wcf\data\option\Option;
ec1b1daf 4use wcf\system\database\util\PreparedStatementConditionBuilder;
8e5a1d4c 5use wcf\system\exception\UserInputException;
2bc9f31d 6use wcf\system\WCF;
158bd3ca
TD
7use wcf\util\StringUtil;
8
9/**
a17de04e 10 * Option type implementation for textual input fields.
9f959ced 11 *
158bd3ca 12 * @author Marcel Werk
ca4ba303 13 * @copyright 2001-2014 WoltLab GmbH
158bd3ca
TD
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package com.woltlab.wcf
16 * @subpackage system.option
9f959ced 17 * @category Community Framework
158bd3ca 18 */
245b5797 19class TextOptionType extends AbstractOptionType implements ISearchableUserOption {
158bd3ca
TD
20 /**
21 * input type
9f959ced 22 * @var string
158bd3ca
TD
23 */
24 protected $inputType = 'text';
25
8381f0d8
MW
26 /**
27 * input css class
9f959ced 28 * @var string
8381f0d8
MW
29 */
30 protected $inputClass = 'long';
31
158bd3ca 32 /**
0ad90fc3 33 * @see \wcf\system\option\IOptionType::getFormElement()
158bd3ca
TD
34 */
35 public function getFormElement(Option $option, $value) {
36 WCF::getTPL()->assign(array(
37 'option' => $option,
38 'inputType' => $this->inputType,
8381f0d8 39 'inputClass' => $this->inputClass,
158bd3ca
TD
40 'value' => $value
41 ));
79d5661c 42 return WCF::getTPL()->fetch('textOptionType');
158bd3ca
TD
43 }
44
158bd3ca 45 /**
0ad90fc3 46 * @see \wcf\system\option\ISearchableUserOption::getSearchFormElement()
158bd3ca
TD
47 */
48 public function getSearchFormElement(Option $option, $value) {
567b90a3
MS
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');
158bd3ca
TD
57 }
58
59 /**
0ad90fc3 60 * @see \wcf\system\option\ISearchableUserOption::getCondition()
158bd3ca
TD
61 */
62 public function getCondition(PreparedStatementConditionBuilder &$conditions, Option $option, $value) {
567b90a3
MS
63 if (!isset($_POST['searchOptions'][$option->optionName])) return false;
64
158bd3ca 65 $value = StringUtil::trim($value);
567b90a3
MS
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 }
158bd3ca 72
158bd3ca
TD
73 return true;
74 }
8e5a1d4c
MW
75
76 /**
0ad90fc3 77 * @see \wcf\system\option\IOptionType::validate()
8e5a1d4c
MW
78 */
79 public function validate(Option $option, $newValue) {
12348253
MW
80 $newValue = $this->getContent($option, $newValue);
81
838e315b 82 if ($option->minlength !== null && $option->minlength > mb_strlen($newValue)) {
8e5a1d4c
MW
83 throw new UserInputException($option->optionName, 'tooShort');
84 }
838e315b 85 if ($option->maxlength !== null && $option->maxlength < mb_strlen($newValue)) {
8e5a1d4c
MW
86 throw new UserInputException($option->optionName, 'tooLong');
87 }
88 }
12348253
MW
89
90 /**
0ad90fc3 91 * @see \wcf\system\option\IOptionType::getData()
12348253
MW
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 *
0ad90fc3 100 * @param \wcf\data\option\Option $option
12348253 101 * @param string $newValue
567b90a3 102 * @return string
12348253
MW
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 }
dcb3a44c 114}