Merge branch '2.1' into 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-2017 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 * @return string[]
63 */
64 protected function loadBBCodeSelection() {
65 $this->bbCodes = array_keys(BBCodeCache::getInstance()->getBBCodes());
66 $this->bbCodes = array_diff($this->bbCodes, self::$alwaysAvailable);
67
68 asort($this->bbCodes);
69 }
70
71 /**
72 * @inheritDoc
73 */
74 public function merge($defaultValue, $groupValue) {
75 if ($this->bbCodes === null) {
76 $this->loadBBCodeSelection();
77 }
78
79 if (empty($defaultValue)) {
80 $defaultValue = [];
81 }
82 else {
83 $defaultValue = explode(',', StringUtil::unifyNewlines($defaultValue));
84 }
85
86 if (empty($groupValue)) {
87 $groupValue = [];
88 }
89 else {
90 $groupValue = explode(',', StringUtil::unifyNewlines($groupValue));
91 }
92
93 $newValue = array_intersect($defaultValue, $groupValue);
94 sort($newValue);
95
96 return implode(',', $newValue);
97 }
98
99 /**
100 * @inheritDoc
101 */
102 public function validate(Option $option, $newValue) {
103 if (!is_array($newValue)) {
104 $newValue = [];
105 }
106
107 if ($this->bbCodes === null) {
108 $this->loadBBCodeSelection();
109 }
110
111 foreach ($newValue as $tag) {
112 if (!in_array($tag, $this->bbCodes)) {
113 throw new UserInputException($option->optionName, 'validationFailed');
114 }
115 }
116 }
117
118 /**
119 * @inheritDoc
120 */
121 public function compare($value1, $value2) {
122 // handle special case where no disallowed BBCodes have been set
123 if (empty($value1)) {
124 if (empty($value2)) {
125 return 0;
126 }
127
128 return -1;
129 }
130 else if (empty($value2)) {
131 return 1;
132 }
133
134 $value1 = explode(',', $value1);
135 $value2 = explode(',', $value2);
136
137 // check if value1 disallows more BBCodes than value2
138 $diff = array_diff($value1, $value2);
139 if (!empty($diff)) {
140 return 1;
141 }
142
143 // check if value1 disallows less BBCodes than value2
144 $diff = array_diff($value2, $value1);
145 if (!empty($diff)) {
146 return -1;
147 }
148
149 // both lists of BBCodes are equal
150 return 0;
151 }
152 }