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