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> | |
9785f7b8 | 23 | */ |
a9229942 TD |
24 | trait TMultiCategoryObject |
25 | { | |
26 | /** | |
27 | * list of the object's categories | |
28 | * @var AbstractDecoratedCategory[] | |
29 | */ | |
30 | protected $categories; | |
31 | ||
32 | /** | |
33 | * ids of the object's categories | |
34 | * @var int[] | |
35 | */ | |
36 | protected $categoryIDs = []; | |
37 | ||
38 | /** | |
39 | * list of the object's leaf categories | |
40 | * @var AbstractDecoratedCategory[] | |
41 | */ | |
42 | protected $leafCategories; | |
43 | ||
44 | /** | |
45 | * Returns the list of category ids. | |
46 | * | |
47 | * @return int[] | |
48 | */ | |
49 | public function getCategoryIDs() | |
50 | { | |
51 | return $this->categoryIDs; | |
52 | } | |
53 | ||
54 | /** | |
55 | * Returns the categories of the object. | |
56 | * | |
57 | * @return AbstractDecoratedCategory[] | |
58 | * @throws ParentClassException | |
59 | */ | |
60 | public function getCategories() | |
61 | { | |
62 | if ($this->categories === null) { | |
63 | $this->categories = []; | |
64 | ||
65 | $className = static::getCategoryClassName(); | |
66 | if (!\is_subclass_of($className, AbstractDecoratedCategory::class)) { | |
67 | throw new ParentClassException($className, AbstractDecoratedCategory::class); | |
68 | } | |
69 | ||
70 | if (!empty($this->categoryIDs)) { | |
71 | foreach ($this->categoryIDs as $categoryID) { | |
72 | /** @noinspection PhpUndefinedMethodInspection */ | |
73 | $this->categories[$categoryID] = $className::getCategory($categoryID); | |
74 | } | |
75 | } else { | |
76 | $sql = "SELECT categoryID | |
77 | FROM wcf" . WCF_N . "_category | |
78 | WHERE categoryID IN ( | |
79 | SELECT categoryID | |
80 | FROM " . static::getCategoryMappingDatabaseTableName() . " | |
81 | WHERE " . static::getDatabaseTableIndexName() . " = ? | |
82 | ) | |
83 | ORDER BY parentCategoryID, showOrder"; | |
84 | $statement = WCF::getDB()->prepareStatement($sql); | |
85 | $statement->execute([$this->getObjectID()]); | |
86 | while ($categoryID = $statement->fetchColumn()) { | |
87 | /** @noinspection PhpUndefinedMethodInspection */ | |
88 | $this->categories[$categoryID] = $className::getCategory($categoryID); | |
89 | } | |
90 | } | |
91 | } | |
92 | ||
93 | return $this->categories; | |
94 | } | |
95 | ||
96 | /** | |
97 | * Returns the list of all selected categories unless a child category is selected. | |
98 | * | |
99 | * @return AbstractDecoratedCategory[] | |
100 | */ | |
101 | public function getLeafCategories() | |
102 | { | |
103 | if ($this->leafCategories === null) { | |
104 | $this->leafCategories = $categories = $this->getCategories(); | |
105 | ||
106 | foreach ($categories as $category) { | |
107 | if ($category->parentCategoryID && isset($this->leafCategories[$category->parentCategoryID])) { | |
108 | unset($this->leafCategories[$category->parentCategoryID]); | |
109 | } | |
110 | } | |
111 | } | |
112 | ||
113 | return $this->leafCategories; | |
114 | } | |
115 | ||
116 | /** | |
117 | * @see DatabaseObject::getObjectID() | |
118 | */ | |
119 | abstract public function getObjectID(); | |
120 | ||
121 | /** | |
122 | * Sets a category id. | |
123 | * | |
124 | * @param int $categoryID | |
125 | */ | |
126 | public function setCategoryID($categoryID) | |
127 | { | |
128 | $this->categoryIDs[] = $categoryID; | |
129 | } | |
130 | ||
131 | /** | |
132 | * Sets a category ids. | |
133 | * | |
134 | * @param int[] $categoryIDs | |
135 | */ | |
136 | public function setCategoryIDs(array $categoryIDs) | |
137 | { | |
138 | $this->categoryIDs = $categoryIDs; | |
139 | } | |
9785f7b8 | 140 | } |