Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / option / RadioButtonOptionType.class.php
CommitLineData
158bd3ca
TD
1<?php
2namespace wcf\system\option;
3use wcf\data\option\Option;
4use wcf\system\database\util\PreparedStatementConditionBuilder;
6286572b 5use wcf\system\exception\UserInputException;
158bd3ca 6use wcf\system\WCF;
158bd3ca
TD
7use wcf\util\StringUtil;
8
9/**
a17de04e
MS
10 * Option type implementation for radio buttons.
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 */
79d5661c 19class RadioButtonOptionType extends AbstractOptionType implements ISearchableUserOption {
245b5797
MS
20 /**
21 * name of the template that contains the form element of this option type
22 * @var string
23 */
79d5661c 24 public $templateName = 'radioButtonOptionType';
9f959ced 25
158bd3ca 26 /**
0ad90fc3 27 * @see \wcf\system\option\IOptionType::getFormElement()
158bd3ca
TD
28 */
29 public function getFormElement(Option $option, $value) {
30 // get options
31 $selectOptions = $option->parseSelectOptions();
9f959ced 32
158bd3ca
TD
33 $availableOptions = $option->parseMultipleEnableOptions();
34 $options = array(
35 'disableOptions' => array(),
36 'enableOptions' => array()
37 );
38
39 foreach ($availableOptions as $key => $enableOptions) {
40 $optionData = Option::parseEnableOptions($enableOptions);
41
42 $options['disableOptions'][$key] = $optionData['disableOptions'];
43 $options['enableOptions'][$key] = $optionData['enableOptions'];
44 }
45
46 WCF::getTPL()->assign(array(
47 'disableOptions' => $options['disableOptions'],
48 'enableOptions' => $options['enableOptions'],
49 'option' => $option,
50 'selectOptions' => $selectOptions,
51 'value' => $value
52 ));
53 return WCF::getTPL()->fetch($this->templateName);
54 }
55
56 /**
0ad90fc3 57 * @see \wcf\system\option\IOptionType::validate()
158bd3ca
TD
58 */
59 public function validate(Option $option, $newValue) {
60 if (!empty($newValue)) {
61 $options = $option->parseSelectOptions();
62 if (!isset($options[$newValue])) {
63 throw new UserInputException($option->optionName, 'validationFailed');
64 }
65 }
66 }
67
158bd3ca 68 /**
0ad90fc3 69 * @see \wcf\system\option\ISearchableUserOption::getSearchFormElement()
158bd3ca
TD
70 */
71 public function getSearchFormElement(Option $option, $value) {
567b90a3
MS
72 $this->templateName = 'radioButtonSearchableOptionType';
73 WCF::getTPL()->assign('searchOption', empty($_POST) || isset($_POST['searchOptions'][$option->optionName]));
74
6286572b 75 return $this->getFormElement($option, $value);
158bd3ca
TD
76 }
77
78 /**
0ad90fc3 79 * @see \wcf\system\option\ISearchableUserOption::getCondition()
158bd3ca
TD
80 */
81 public function getCondition(PreparedStatementConditionBuilder &$conditions, Option $option, $value) {
567b90a3 82 if (!isset($_POST['searchOptions'][$option->optionName])) return false;
158bd3ca 83
567b90a3 84 $conditions->add("option_value.userOption".$option->optionID." = ?", array(StringUtil::trim($value)));
158bd3ca
TD
85 return true;
86 }
dcb3a44c 87}