Commit | Line | Data |
---|---|---|
9785f7b8 | 1 | <?php |
a9229942 | 2 | |
9785f7b8 | 3 | namespace wcf\data; |
a9229942 | 4 | |
9785f7b8 | 5 | use wcf\data\category\AbstractDecoratedCategory; |
7b9ff46b | 6 | use wcf\system\exception\ParentClassException; |
9785f7b8 MS |
7 | use wcf\system\WCF; |
8 | ||
9 | /** | |
10 | * Provides category-related methods for an object with multiple categories. | |
a9229942 | 11 | * |
2cb7f828 | 12 | * Requires the following static methods: |
a9229942 TD |
13 | * - public static function getCategoryMappingDatabaseTableName() |
14 | * returns the name of the database table containing the mapping of the objects to their categories | |
15 | * - public static function getCategoryClassName() | |
16 | * returns the name of the used AbstractDecoratedCategory class | |
17 | * - public static function getDatabaseTableIndexName() | |
18 | * see IStorableObject::getDatabaseTableIndexName() | |
9785f7b8 | 19 | * |
a9229942 TD |
20 | * @author Matthias Schmidt, Marcel Werk |
21 | * @copyright 2001-2019 WoltLab GmbH | |
22 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
23 | * @package WoltLabSuite\Core\Data | |
9785f7b8 | 24 | */ |
a9229942 TD |
25 | trait TMultiCategoryObject |
26 | { | |
27 | /** | |
28 | * list of the object's categories | |
29 | * @var AbstractDecoratedCategory[] | |
30 | */ | |
31 | protected $categories; | |
32 | ||
33 | /** | |
34 | * ids of the object's categories | |
35 | * @var int[] | |
36 | */ | |
37 | protected $categoryIDs = []; | |
38 | ||
39 | /** | |
40 | * list of the object's leaf categories | |
41 | * @var AbstractDecoratedCategory[] | |
42 | */ | |
43 | protected $leafCategories; | |
44 | ||
45 | /** | |
46 | * Returns the list of category ids. | |
47 | * | |
48 | * @return int[] | |
49 | */ | |
50 | public function getCategoryIDs() | |
51 | { | |
52 | return $this->categoryIDs; | |
53 | } | |
54 | ||
55 | /** | |
56 | * Returns the categories of the object. | |
57 | * | |
58 | * @return AbstractDecoratedCategory[] | |
59 | * @throws ParentClassException | |
60 | */ | |
61 | public function getCategories() | |
62 | { | |
63 | if ($this->categories === null) { | |
64 | $this->categories = []; | |
65 | ||
66 | $className = static::getCategoryClassName(); | |
67 | if (!\is_subclass_of($className, AbstractDecoratedCategory::class)) { | |
68 | throw new ParentClassException($className, AbstractDecoratedCategory::class); | |
69 | } | |
70 | ||
71 | if (!empty($this->categoryIDs)) { | |
72 | foreach ($this->categoryIDs as $categoryID) { | |
73 | /** @noinspection PhpUndefinedMethodInspection */ | |
74 | $this->categories[$categoryID] = $className::getCategory($categoryID); | |
75 | } | |
76 | } else { | |
77 | $sql = "SELECT categoryID | |
78 | FROM wcf" . WCF_N . "_category | |
79 | WHERE categoryID IN ( | |
80 | SELECT categoryID | |
81 | FROM " . static::getCategoryMappingDatabaseTableName() . " | |
82 | WHERE " . static::getDatabaseTableIndexName() . " = ? | |
83 | ) | |
84 | ORDER BY parentCategoryID, showOrder"; | |
85 | $statement = WCF::getDB()->prepareStatement($sql); | |
86 | $statement->execute([$this->getObjectID()]); | |
87 | while ($categoryID = $statement->fetchColumn()) { | |
88 | /** @noinspection PhpUndefinedMethodInspection */ | |
89 | $this->categories[$categoryID] = $className::getCategory($categoryID); | |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | return $this->categories; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Returns the list of all selected categories unless a child category is selected. | |
99 | * | |
100 | * @return AbstractDecoratedCategory[] | |
101 | */ | |
102 | public function getLeafCategories() | |
103 | { | |
104 | if ($this->leafCategories === null) { | |
105 | $this->leafCategories = $categories = $this->getCategories(); | |
106 | ||
107 | foreach ($categories as $category) { | |
108 | if ($category->parentCategoryID && isset($this->leafCategories[$category->parentCategoryID])) { | |
109 | unset($this->leafCategories[$category->parentCategoryID]); | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
114 | return $this->leafCategories; | |
115 | } | |
116 | ||
117 | /** | |
118 | * @see DatabaseObject::getObjectID() | |
119 | */ | |
120 | abstract public function getObjectID(); | |
121 | ||
122 | /** | |
123 | * Sets a category id. | |
124 | * | |
125 | * @param int $categoryID | |
126 | */ | |
127 | public function setCategoryID($categoryID) | |
128 | { | |
129 | $this->categoryIDs[] = $categoryID; | |
130 | } | |
131 | ||
132 | /** | |
133 | * Sets a category ids. | |
134 | * | |
135 | * @param int[] $categoryIDs | |
136 | */ | |
137 | public function setCategoryIDs(array $categoryIDs) | |
138 | { | |
139 | $this->categoryIDs = $categoryIDs; | |
140 | } | |
9785f7b8 | 141 | } |