Split additional joins into multiple lines
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / page / LabelListPage.class.php
1 <?php
2
3 namespace wcf\acp\page;
4
5 use wcf\data\label\group\LabelGroup;
6 use wcf\data\label\group\LabelGroupList;
7 use wcf\data\label\LabelList;
8 use wcf\data\language\item\LanguageItemList;
9 use wcf\page\SortablePage;
10 use wcf\system\exception\IllegalLinkException;
11 use wcf\system\language\LanguageFactory;
12 use wcf\system\request\LinkHandler;
13 use wcf\system\WCF;
14 use wcf\util\HeaderUtil;
15 use wcf\util\StringUtil;
16
17 /**
18 * Lists available labels
19 *
20 * @author Alexander Ebert
21 * @copyright 2001-2019 WoltLab GmbH
22 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
23 * @package WoltLabSuite\Core\Acp\Page
24 *
25 * @property LabelList $objectList
26 */
27 class LabelListPage extends SortablePage
28 {
29 /**
30 * @inheritDoc
31 */
32 public $activeMenuItem = 'wcf.acp.menu.link.label.list';
33
34 /**
35 * @inheritDoc
36 */
37 public $defaultSortField = 'label';
38
39 /**
40 * @inheritDoc
41 */
42 public $itemsPerPage = 50;
43
44 /**
45 * @inheritDoc
46 */
47 public $validSortFields = ['labelID', 'label', 'groupName', 'showOrder'];
48
49 /**
50 * @inheritDoc
51 */
52 public $neededPermissions = ['admin.content.label.canManageLabel'];
53
54 /**
55 * @inheritDoc
56 */
57 public $objectListClassName = LabelList::class;
58
59 /**
60 * filter for css class name
61 * @var string
62 */
63 public $cssClassName = '';
64
65 /**
66 * if of the label group to which the displayed labels belong
67 * @var int
68 */
69 public $groupID = 0;
70
71 /**
72 * filter for label name
73 * @var string
74 */
75 public $label = '';
76
77 /**
78 * label group to which the displayed labels belong
79 * @var LabelGroup
80 */
81 public $labelGroup;
82
83 /**
84 * list with available label groups
85 * @var LabelGroupList
86 */
87 public $labelGroupList;
88
89 /**
90 * @inheritDoc
91 */
92 public function assignVariables()
93 {
94 parent::assignVariables();
95
96 WCF::getTPL()->assign([
97 'cssClassName' => $this->cssClassName,
98 'groupID' => $this->groupID,
99 'labelSearch' => $this->label,
100 'labelGroup' => $this->labelGroup,
101 'labelGroupList' => $this->labelGroupList,
102 ]);
103 }
104
105 /**
106 * @inheritDoc
107 */
108 protected function initObjectList()
109 {
110 parent::initObjectList();
111
112 $this->objectList->sqlSelects = "label_group.groupName, label_group.groupDescription";
113 $this->objectList->sqlJoins = "
114 LEFT JOIN wcf" . WCF_N . "_label_group label_group
115 ON label_group.groupID = label.groupID";
116 if ($this->labelGroup) {
117 $this->objectList->getConditionBuilder()->add('label.groupID = ?', [$this->labelGroup->groupID]);
118
119 // Ramp up the limit to display all labels at once for easier
120 // drag & drop sorting. This isn't exactly infinite, but if
121 // you have a label group with more than 1k labels, being able
122 // to sort them is the least of your problems.
123 $this->itemsPerPage = 1000;
124 }
125 if ($this->cssClassName) {
126 $this->objectList->getConditionBuilder()->add(
127 'label.cssClassName LIKE ?',
128 ['%' . \addcslashes($this->cssClassName, '_%') . '%']
129 );
130 }
131
132 if ($this->label) {
133 $languageItemList = new LanguageItemList();
134 $languageItemList->getConditionBuilder()->add(
135 'languageCategoryID = ?',
136 [LanguageFactory::getInstance()->getCategory('wcf.acp.label')->languageCategoryID]
137 );
138 $languageItemList->getConditionBuilder()->add('languageID = ?', [WCF::getLanguage()->languageID]);
139 $languageItemList->getConditionBuilder()->add('languageItem LIKE ?', ['wcf.acp.label.label%']);
140 $languageItemList->getConditionBuilder()->add(
141 'languageItemValue LIKE ?',
142 ['%' . \addcslashes($this->label, '_%') . '%']
143 );
144 $languageItemList->readObjects();
145
146 $labelIDs = [];
147 foreach ($languageItemList as $languageItem) {
148 $labelIDs[] = \str_replace('wcf.acp.label.label', '', $languageItem->languageItem);
149 }
150
151 if (!empty($labelIDs)) {
152 $this->objectList->getConditionBuilder()->add(
153 '(label LIKE ? OR labelID IN (?))',
154 ['%' . \addcslashes($this->label, '_%') . '%', $labelIDs]
155 );
156 } else {
157 $this->objectList->getConditionBuilder()->add(
158 'label LIKE ?',
159 ['%' . \addcslashes($this->label, '_%') . '%']
160 );
161 }
162 }
163 }
164
165 /**
166 * @inheritDoc
167 */
168 public function readData()
169 {
170 parent::readData();
171
172 $this->labelGroupList = new LabelGroupList();
173 $this->labelGroupList->readObjects();
174 }
175
176 /**
177 * @inheritDoc
178 */
179 public function readParameters()
180 {
181 parent::readParameters();
182
183 if (!empty($_POST)) {
184 $parameters = [];
185 if (!empty($_POST['groupID'])) {
186 $parameters['id'] = \intval($_POST['groupID']);
187 }
188 if (!empty($_POST['label'])) {
189 $parameters['label'] = StringUtil::trim($_POST['label']);
190 }
191 if (!empty($_POST['cssClassName'])) {
192 $parameters['cssClassName'] = StringUtil::trim($_POST['cssClassName']);
193 }
194
195 if (!empty($parameters)) {
196 HeaderUtil::redirect(LinkHandler::getInstance()->getLink('LabelList', $parameters));
197
198 exit;
199 }
200 }
201
202 if (isset($_REQUEST['id'])) {
203 $this->groupID = \intval($_REQUEST['id']);
204 }
205 if (isset($_REQUEST['label'])) {
206 $this->label = StringUtil::trim($_REQUEST['label']);
207 }
208 if (isset($_REQUEST['cssClassName'])) {
209 $this->cssClassName = StringUtil::trim($_REQUEST['cssClassName']);
210 }
211
212 if ($this->groupID) {
213 $this->labelGroup = new LabelGroup($this->groupID);
214 if (!$this->labelGroup->groupID) {
215 throw new IllegalLinkException();
216 }
217
218 $this->defaultSortField = 'showOrder';
219 }
220 }
221 }