Commit | Line | Data |
---|---|---|
3b75466f | 1 | <?php |
a9229942 | 2 | |
3b75466f | 3 | namespace wcf\data\label; |
a9229942 | 4 | |
3b75466f MW |
5 | use wcf\data\DatabaseObjectEditor; |
6 | use wcf\data\IEditableCachedObject; | |
7 | use wcf\system\cache\builder\LabelCacheBuilder; | |
d8475f48 | 8 | use wcf\system\WCF; |
3b75466f MW |
9 | |
10 | /** | |
11 | * Provides functions to edit labels. | |
a9229942 TD |
12 | * |
13 | * @author Alexander Ebert | |
14 | * @copyright 2001-2019 WoltLab GmbH | |
15 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
a9229942 TD |
16 | * |
17 | * @method static Label create(array $parameters = []) | |
18 | * @method Label getDecoratedObject() | |
19 | * @mixin Label | |
3b75466f | 20 | */ |
a9229942 TD |
21 | class LabelEditor extends DatabaseObjectEditor implements IEditableCachedObject |
22 | { | |
23 | /** | |
24 | * @inheritDoc | |
25 | */ | |
26 | protected static $baseClass = Label::class; | |
27 | ||
28 | /** | |
29 | * @inheritDoc | |
30 | */ | |
31 | public static function resetCache() | |
32 | { | |
33 | LabelCacheBuilder::getInstance()->reset(); | |
34 | } | |
35 | ||
36 | /** | |
37 | * Adds the label to a specific position in the label group. | |
38 | * | |
39 | * @param int $groupID | |
40 | * @param int $showOrder | |
41 | */ | |
42 | public function setShowOrder($groupID, $showOrder = 0) | |
43 | { | |
44 | // shift back labels in old label group with higher showOrder | |
45 | if ($this->showOrder) { | |
46 | $sql = "UPDATE wcf" . WCF_N . "_label | |
47 | SET showOrder = showOrder - 1 | |
48 | WHERE groupID = ? | |
49 | AND showOrder >= ?"; | |
50 | $statement = WCF::getDB()->prepareStatement($sql); | |
51 | $statement->execute([$this->groupID, $this->showOrder]); | |
52 | } | |
53 | ||
54 | // shift labels in new label group with higher showOrder | |
55 | if ($showOrder) { | |
56 | $sql = "UPDATE wcf" . WCF_N . "_label | |
57 | SET showOrder = showOrder + 1 | |
58 | WHERE groupID = ? | |
59 | AND showOrder >= ?"; | |
60 | $statement = WCF::getDB()->prepareStatement($sql); | |
61 | $statement->execute([$groupID, $showOrder]); | |
62 | } | |
63 | ||
64 | // get maximum existing show order | |
65 | $sql = "SELECT MAX(showOrder) | |
66 | FROM wcf" . WCF_N . "_label | |
67 | WHERE groupID = ?"; | |
68 | $statement = WCF::getDB()->prepareStatement($sql); | |
69 | $statement->execute([$groupID]); | |
70 | $maxShowOrder = $statement->fetchSingleColumn() ?: 0; | |
71 | ||
72 | if (!$showOrder || $showOrder > $maxShowOrder) { | |
73 | $showOrder = $maxShowOrder + 1; | |
74 | } | |
75 | ||
76 | $this->update(['showOrder' => $showOrder]); | |
77 | } | |
3b75466f | 78 | } |