Added basic contact options
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / custom / option / CustomOption.class.php
1 <?php
2 namespace wcf\data\custom\option;
3 use wcf\data\option\Option;
4 use wcf\system\bbcode\MessageParser;
5 use wcf\system\bbcode\SimpleMessageParser;
6 use wcf\system\exception\NotImplementedException;
7 use wcf\system\WCF;
8 use wcf\util\DateUtil;
9 use wcf\util\OptionUtil;
10 use wcf\util\StringUtil;
11
12 /**
13 * Default implementation for custom options.
14 *
15 * @author Alexander Ebert
16 * @copyright 2001-2017 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package WoltLabSuite\Core\Data\Custom\Option
19 * @since 3.1
20 *
21 * @property-read integer $optionID unique id of the option
22 * @property-read string $optionTitle title of the option or name of language item which contains the title
23 * @property-read string $optionDescription description of the option or name of language item which contains the description
24 * @property-read string $optionType type of the option which determines its input and output
25 * @property-read string $defaultValue default value of the option
26 * @property-read string $validationPattern regular expression used to validate the value of the option
27 * @property-read string $selectOptions possible values of the option separated by newlines
28 * @property-read integer $required is `1` if the option has to be filled out, otherwise `0`
29 * @property-read integer $showOrder position of the option relation tp the other options
30 * @property-read integer $isDisabled is `1` if the option is disabled, otherwise `0`
31 * @property-read integer $originIsSystem is `1` if the option has been delivered by a package, otherwise `0` (i.e. the option has been created in the ACP)
32 */
33 abstract class CustomOption extends Option {
34 /**
35 * option value
36 * @var string
37 */
38 protected $optionValue = '';
39
40 /**
41 * Returns true if the option is visible
42 *
43 * @return boolean
44 */
45 public function isVisible() {
46 return !$this->isDisabled;
47 }
48
49 /**
50 * @inheritDoc
51 */
52 public static function getDatabaseTableAlias() {
53 throw new NotImplementedException();
54 }
55
56 /**
57 * Returns the value of this option.
58 *
59 * @return string
60 */
61 public function getOptionValue() {
62 return $this->optionValue;
63 }
64
65 /**
66 * Sets the value of this option.
67 *
68 * @param string $value
69 */
70 public function setOptionValue($value) {
71 $this->optionValue = $value;
72 }
73
74 /**
75 * Returns the formatted value of this option.
76 *
77 * @return string
78 */
79 public function getFormattedOptionValue() {
80 switch ($this->optionType) {
81 case 'boolean':
82 return WCF::getLanguage()->get('wcf.acp.option.optionType.boolean.'.($this->optionValue ? 'yes' : 'no'));
83
84 case 'date':
85 $year = $month = $day = 0;
86 $optionValue = explode('-', $this->optionValue);
87 if (isset($optionValue[0])) $year = intval($optionValue[0]);
88 if (isset($optionValue[1])) $month = intval($optionValue[1]);
89 if (isset($optionValue[2])) $day = intval($optionValue[2]);
90 return DateUtil::format(DateUtil::getDateTimeByTimestamp(gmmktime(12, 1, 1, $month, $day, $year)), DateUtil::DATE_FORMAT);
91
92 case 'float':
93 return StringUtil::formatDouble(intval($this->optionValue));
94
95 case 'integer':
96 return StringUtil::formatInteger(intval($this->optionValue));
97
98 case 'radioButton':
99 case 'select':
100 $selectOptions = OptionUtil::parseSelectOptions($this->selectOptions);
101 if (isset($selectOptions[$this->optionValue])) return WCF::getLanguage()->get($selectOptions[$this->optionValue]);
102 return '';
103
104 case 'multiSelect':
105 case 'checkboxes':
106 $selectOptions = OptionUtil::parseSelectOptions($this->selectOptions);
107 $values = explode("\n", $this->optionValue);
108 $result = '';
109 foreach ($values as $value) {
110 if (isset($selectOptions[$value])) {
111 if (!empty($result)) $result .= "<br>\n";
112 $result .= WCF::getLanguage()->get($selectOptions[$value]);
113 }
114 }
115 return $result;
116
117 case 'textarea':
118 return SimpleMessageParser::getInstance()->parse($this->optionValue);
119
120 case 'message':
121 return MessageParser::getInstance()->parse($this->optionValue);
122
123 case 'URL':
124 return StringUtil::getAnchorTag($this->optionValue);
125
126 default:
127 return StringUtil::encodeHTML($this->optionValue);
128 }
129 }
130
131 /**
132 * Returns true if this option can be deleted, defaults to false for
133 * options created through the package system.
134 *
135 * @return boolean
136 */
137 public function canDelete() {
138 return !$this->originIsSystem;
139 }
140 }