Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / option / MultiSelectOptionType.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\ArrayUtil;
8
9 /**
10 * Option type implementation for multiple select lists.
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 MultiSelectOptionType extends SelectOptionType {
20 /**
21 * @see \wcf\system\option\IOptionType::getFormElement()
22 */
23 public function getFormElement(Option $option, $value) {
24 WCF::getTPL()->assign(array(
25 'option' => $option,
26 'selectOptions' => $option->parseSelectOptions(),
27 'value' => (!is_array($value) ? explode("\n", $value) : $value)
28 ));
29 return WCF::getTPL()->fetch('multiSelectOptionType');
30 }
31
32 /**
33 * @see \wcf\system\option\ISearchableUserOption::getSearchFormElement()
34 */
35 public function getSearchFormElement(Option $option, $value) {
36 WCF::getTPL()->assign(array(
37 'option' => $option,
38 'searchOption' => isset($_POST['searchOptions'][$option->optionName]),
39 'selectOptions' => $option->parseSelectOptions(),
40 'value' => (!is_array($value) ? explode("\n", $value) : $value)
41 ));
42 return WCF::getTPL()->fetch('multiSelectSearchableOptionType');
43 }
44
45 /**
46 * @see \wcf\system\option\IOptionType::validate()
47 */
48 public function validate(Option $option, $newValue) {
49 if (!is_array($newValue)) $newValue = array();
50 $options = $option->parseSelectOptions();
51 foreach ($newValue as $value) {
52 if (!isset($options[$value])) {
53 throw new UserInputException($option->optionName, 'validationFailed');
54 }
55 }
56 }
57
58 /**
59 * @see \wcf\system\option\IOptionType::getData()
60 */
61 public function getData(Option $option, $newValue) {
62 if (!is_array($newValue)) $newValue = array();
63 return implode("\n", $newValue);
64 }
65
66 /**
67 * @see \wcf\system\option\ISearchableUserOption::getCondition()
68 */
69 public function getCondition(PreparedStatementConditionBuilder &$conditions, Option $option, $value) {
70 if (!isset($_POST['searchOptions'][$option->optionName])) return false;
71
72 if (!is_array($value) || empty($value)) return false;
73 $value = ArrayUtil::trim($value);
74
75 $conditions->add("option_value.userOption".$option->optionID." REGEXP '".'(^|\n)'.implode('\n([^\n]*\n)*', array_map('escapeString', $value)).'($|\n)'."'");
76 return true;
77 }
78 }