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>
23 * @package WoltLabSuite\Core\Data
25 trait TMultiCategoryObject
28 * list of the object's categories
29 * @var AbstractDecoratedCategory[]
31 protected $categories;
34 * ids of the object's categories
37 protected $categoryIDs = [];
40 * list of the object's leaf categories
41 * @var AbstractDecoratedCategory[]
43 protected $leafCategories;
46 * Returns the list of category ids.
50 public function getCategoryIDs()
52 return $this->categoryIDs
;
56 * Returns the categories of the object.
58 * @return AbstractDecoratedCategory[]
59 * @throws ParentClassException
61 public function getCategories()
63 if ($this->categories
=== null) {
64 $this->categories
= [];
66 $className = static::getCategoryClassName();
67 if (!\
is_subclass_of($className, AbstractDecoratedCategory
::class)) {
68 throw new ParentClassException($className, AbstractDecoratedCategory
::class);
71 if (!empty($this->categoryIDs
)) {
72 foreach ($this->categoryIDs
as $categoryID) {
73 /** @noinspection PhpUndefinedMethodInspection */
74 $this->categories
[$categoryID] = $className::getCategory($categoryID);
77 $sql = "SELECT categoryID
78 FROM wcf" . WCF_N
. "_category
81 FROM " . static::getCategoryMappingDatabaseTableName() . "
82 WHERE " . static::getDatabaseTableIndexName() . " = ?
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);
94 return $this->categories
;
98 * Returns the list of all selected categories unless a child category is selected.
100 * @return AbstractDecoratedCategory[]
102 public function getLeafCategories()
104 if ($this->leafCategories
=== null) {
105 $this->leafCategories
= $categories = $this->getCategories();
107 foreach ($categories as $category) {
108 if ($category->parentCategoryID
&& isset($this->leafCategories
[$category->parentCategoryID
])) {
109 unset($this->leafCategories
[$category->parentCategoryID
]);
114 return $this->leafCategories
;
118 * @see DatabaseObject::getObjectID()
120 abstract public function getObjectID();
123 * Sets a category id.
125 * @param int $categoryID
127 public function setCategoryID($categoryID)
129 $this->categoryIDs
[] = $categoryID;
133 * Sets a category ids.
135 * @param int[] $categoryIDs
137 public function setCategoryIDs(array $categoryIDs)
139 $this->categoryIDs
= $categoryIDs;