5 use wcf\data\category\AbstractDecoratedCategory
;
6 use wcf\system\exception\ParentClassException
;
10 * Provides category-related methods for an object with multiple categories.
12 * Requires the following static methods:
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()
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>
24 trait TMultiCategoryObject
27 * list of the object's categories
28 * @var AbstractDecoratedCategory[]
30 protected $categories;
33 * ids of the object's categories
36 protected $categoryIDs = [];
39 * list of the object's leaf categories
40 * @var AbstractDecoratedCategory[]
42 protected $leafCategories;
45 * Returns the list of category ids.
49 public function getCategoryIDs()
51 return $this->categoryIDs
;
55 * Returns the categories of the object.
57 * @return AbstractDecoratedCategory[]
58 * @throws ParentClassException
60 public function getCategories()
62 if ($this->categories
=== null) {
63 $this->categories
= [];
65 $className = static::getCategoryClassName();
66 if (!\
is_subclass_of($className, AbstractDecoratedCategory
::class)) {
67 throw new ParentClassException($className, AbstractDecoratedCategory
::class);
70 if (!empty($this->categoryIDs
)) {
71 foreach ($this->categoryIDs
as $categoryID) {
72 /** @noinspection PhpUndefinedMethodInspection */
73 $this->categories
[$categoryID] = $className::getCategory($categoryID);
76 $sql = "SELECT categoryID
77 FROM wcf" . WCF_N
. "_category
80 FROM " . static::getCategoryMappingDatabaseTableName() . "
81 WHERE " . static::getDatabaseTableIndexName() . " = ?
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);
93 return $this->categories
;
97 * Returns the list of all selected categories unless a child category is selected.
99 * @return AbstractDecoratedCategory[]
101 public function getLeafCategories()
103 if ($this->leafCategories
=== null) {
104 $this->leafCategories
= $categories = $this->getCategories();
106 foreach ($categories as $category) {
107 if ($category->parentCategoryID
&& isset($this->leafCategories
[$category->parentCategoryID
])) {
108 unset($this->leafCategories
[$category->parentCategoryID
]);
113 return $this->leafCategories
;
117 * @see DatabaseObject::getObjectID()
119 abstract public function getObjectID();
122 * Sets a category id.
124 * @param int $categoryID
126 public function setCategoryID($categoryID)
128 $this->categoryIDs
[] = $categoryID;
132 * Sets a category ids.
134 * @param int[] $categoryIDs
136 public function setCategoryIDs(array $categoryIDs)
138 $this->categoryIDs
= $categoryIDs;