Merge branch '3.1' into 5.2
[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 $validSortFields = ['labelID', 'label', 'groupName', 'showOrder'];
40
41 /**
42 * @inheritDoc
43 */
44 public $neededPermissions = ['admin.content.label.canManageLabel'];
45
46 /**
47 * @inheritDoc
48 */
49 public $objectListClassName = LabelList::class;
50
51 /**
52 * filter for css class name
53 * @var string
54 */
55 public $cssClassName = '';
56
57 /**
58 * if of the label group to which the displayed labels belong
59 * @var integer
60 */
61 public $groupID = 0;
62
63 /**
64 * filter for label name
65 * @var string
66 */
67 public $label = '';
68
69 /**
70 * label group to which the displayed labels belong
71 * @var LabelGroup
72 */
73 public $labelGroup;
74
75 /**
76 * list with available label groups
77 * @var LabelGroupList
78 */
79 public $labelGroupList;
80
81 /**
82 * @inheritDoc
83 */
84 public function assignVariables() {
85 parent::assignVariables();
86
87 WCF::getTPL()->assign([
88 'cssClassName' => $this->cssClassName,
89 'groupID' => $this->groupID,
90 'labelSearch' => $this->label,
91 'labelGroup' => $this->labelGroup,
92 'labelGroupList' => $this->labelGroupList
93 ]);
94 }
95
96 /**
97 * @inheritDoc
98 */
99 protected function initObjectList() {
100 parent::initObjectList();
101
102 $this->objectList->sqlSelects = "label_group.groupName, label_group.groupDescription";
103 $this->objectList->sqlJoins = "LEFT JOIN wcf".WCF_N."_label_group label_group ON (label_group.groupID = label.groupID)";
104 if ($this->labelGroup) {
105 $this->objectList->getConditionBuilder()->add('label.groupID = ?', [$this->labelGroup->groupID]);
106
107 // Ramp up the limit to display all labels at once for easier
108 // drag & drop sorting. This isn't exactly infinite, but if
109 // you have a label group with more than 1k labels, being able
110 // to sort them is the least of your problems.
111 $this->itemsPerPage = 1000;
112 }
113 if ($this->cssClassName) {
114 $this->objectList->getConditionBuilder()->add('label.cssClassName LIKE ?', ['%'.addcslashes($this->cssClassName, '_%').'%']);
115 }
116
117 if ($this->label) {
118 $languageItemList = new LanguageItemList();
119 $languageItemList->getConditionBuilder()->add('languageCategoryID = ?', [LanguageFactory::getInstance()->getCategory('wcf.acp.label')->languageCategoryID]);
120 $languageItemList->getConditionBuilder()->add('languageID = ?', [WCF::getLanguage()->languageID]);
121 $languageItemList->getConditionBuilder()->add('languageItem LIKE ?', ['wcf.acp.label.label%']);
122 $languageItemList->getConditionBuilder()->add('languageItemValue LIKE ?', ['%'.addcslashes($this->label, '_%').'%']);
123 $languageItemList->readObjects();
124
125 $labelIDs = [];
126 foreach ($languageItemList as $languageItem) {
127 $labelIDs[] = str_replace('wcf.acp.label.label', '', $languageItem->languageItem);
128 }
129
130 if (!empty($labelIDs)) {
131 $this->objectList->getConditionBuilder()->add('(label LIKE ? OR labelID IN (?))', ['%'.addcslashes($this->label, '_%').'%', $labelIDs]);
132 }
133 else {
134 $this->objectList->getConditionBuilder()->add('label LIKE ?', ['%'.addcslashes($this->label, '_%').'%']);
135 }
136 }
137 }
138
139 /**
140 * @inheritDoc
141 */
142 public function readData() {
143 parent::readData();
144
145 $this->labelGroupList = new LabelGroupList();
146 $this->labelGroupList->readObjects();
147 }
148
149 /**
150 * @inheritDoc
151 */
152 public function readParameters() {
153 parent::readParameters();
154
155 if (!empty($_POST)) {
156 $parameters = [];
157 if (!empty($_POST['groupID'])) $parameters['id'] = intval($_POST['groupID']);
158 if (!empty($_POST['label'])) $parameters['label'] = StringUtil::trim($_POST['label']);
159 if (!empty($_POST['cssClassName'])) $parameters['cssClassName'] = StringUtil::trim($_POST['cssClassName']);
160
161 if (!empty($parameters)) {
162 HeaderUtil::redirect(LinkHandler::getInstance()->getLink('LabelList', $parameters));
163 exit;
164 }
165 }
166
167 if (isset($_REQUEST['id'])) $this->groupID = intval($_REQUEST['id']);
168 if (isset($_REQUEST['label'])) $this->label = StringUtil::trim($_REQUEST['label']);
169 if (isset($_REQUEST['cssClassName'])) $this->cssClassName = StringUtil::trim($_REQUEST['cssClassName']);
170
171 if ($this->groupID) {
172 $this->labelGroup = new LabelGroup($this->groupID);
173 if (!$this->labelGroup->groupID) {
174 throw new IllegalLinkException();
175 }
176
177 $this->defaultSortField = 'showOrder';
178 }
179 }
180 }