Merge remote-tracking branch 'refs/remotes/origin/3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / option / user / group / BBCodeSelectUserGroupOptionType.class.php
1 <?php
2 namespace wcf\system\option\user\group;
3 use wcf\data\bbcode\BBCodeCache;
4 use wcf\data\option\Option;
5 use wcf\system\exception\UserInputException;
6 use wcf\system\option\AbstractOptionType;
7 use wcf\system\WCF;
8 use wcf\util\StringUtil;
9
10 /**
11 * User group option type implementation for BBCode select lists.
12 *
13 * @author Matthias Schmidt
14 * @copyright 2001-2018 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package WoltLabSuite\Core\System\Option
17 */
18 class BBCodeSelectUserGroupOptionType extends AbstractOptionType implements IUserGroupOptionType {
19 /**
20 * available BBCodes
21 * @var string[]
22 */
23 protected $bbCodes;
24
25 /**
26 * list of bbcode tags that are always available
27 * @var string[]
28 */
29 protected static $alwaysAvailable = ['align', 'attach', 'b', 'code', 'i', 'list', 'quote', 's', 'sub', 'sup', 'table', 'td', 'tr', 'tt', 'u', 'user', 'wsm', 'wsmg', 'wsp'];
30
31 /**
32 * @inheritDoc
33 */
34 public function getData(Option $option, $newValue) {
35 if (!is_array($newValue)) {
36 $newValue = [];
37 }
38
39 return implode(',', $newValue);
40 }
41
42 /**
43 * @inheritDoc
44 */
45 public function getFormElement(Option $option, $value) {
46 if ($this->bbCodes === null) {
47 $this->loadBBCodeSelection();
48 }
49
50 WCF::getTPL()->assign([
51 'bbCodes' => $this->bbCodes,
52 'option' => $option,
53 'selectedBBCodes' => explode(',', $value)
54 ]);
55
56 return WCF::getTPL()->fetch('bbCodeSelectOptionType');
57 }
58
59 /**
60 * Loads the list of BBCodes for the HTML select element.
61 */
62 protected function loadBBCodeSelection() {
63 $this->bbCodes = array_keys(BBCodeCache::getInstance()->getBBCodes());
64 $this->bbCodes = array_diff($this->bbCodes, self::$alwaysAvailable);
65
66 asort($this->bbCodes);
67 }
68
69 /**
70 * @inheritDoc
71 */
72 public function merge($defaultValue, $groupValue) {
73 if ($this->bbCodes === null) {
74 $this->loadBBCodeSelection();
75 }
76
77 if (empty($defaultValue)) {
78 $defaultValue = [];
79 }
80 else {
81 $defaultValue = explode(',', StringUtil::unifyNewlines($defaultValue));
82 }
83
84 if (empty($groupValue)) {
85 $groupValue = [];
86 }
87 else {
88 $groupValue = explode(',', StringUtil::unifyNewlines($groupValue));
89 }
90
91 $newValue = array_intersect($defaultValue, $groupValue);
92 sort($newValue);
93
94 return implode(',', $newValue);
95 }
96
97 /**
98 * @inheritDoc
99 */
100 public function validate(Option $option, $newValue) {
101 if (!is_array($newValue)) {
102 $newValue = [];
103 }
104
105 if ($this->bbCodes === null) {
106 $this->loadBBCodeSelection();
107 }
108
109 foreach ($newValue as $tag) {
110 if (!in_array($tag, $this->bbCodes)) {
111 throw new UserInputException($option->optionName, 'validationFailed');
112 }
113 }
114 }
115
116 /**
117 * @inheritDoc
118 */
119 public function compare($value1, $value2) {
120 // handle special case where no disallowed BBCodes have been set
121 if (empty($value1)) {
122 if (empty($value2)) {
123 return 0;
124 }
125
126 return -1;
127 }
128 else if (empty($value2)) {
129 return 1;
130 }
131
132 $value1 = explode(',', $value1);
133 $value2 = explode(',', $value2);
134
135 // check if value1 disallows more BBCodes than value2
136 $diff = array_diff($value1, $value2);
137 if (!empty($diff)) {
138 return 1;
139 }
140
141 // check if value1 disallows less BBCodes than value2
142 $diff = array_diff($value2, $value1);
143 if (!empty($diff)) {
144 return -1;
145 }
146
147 // both lists of BBCodes are equal
148 return 0;
149 }
150 }