3 namespace wcf\system\condition
;
5 use wcf\data\condition\Condition
;
6 use wcf\data\condition\ConditionAction
;
7 use wcf\data\condition\ConditionList
;
8 use wcf\data\
object\type\ObjectType
;
9 use wcf\data\
object\type\ObjectTypeCache
;
10 use wcf\system\cache\builder\ConditionCacheBuilder
;
11 use wcf\system\exception\SystemException
;
12 use wcf\system\SingletonFactory
;
15 * Handles general condition-related matters.
17 * @author Matthias Schmidt
18 * @copyright 2001-2019 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
21 class ConditionHandler
extends SingletonFactory
24 * list of available conditions grouped by the id of the related condition
25 * object type definition
28 protected $conditions = [];
31 * Creates condition objects for the object with the given id and based
32 * on the given condition object types.
34 * @param int $objectID
35 * @param ObjectType[] $conditionObjectTypes
37 public function createConditions($objectID, array $conditionObjectTypes)
39 foreach ($conditionObjectTypes as $objectType) {
40 $conditionData = $objectType->getProcessor()->getData();
41 if ($conditionData !== null) {
42 $conditionAction = new ConditionAction([], 'create', [
44 'conditionData' => \
serialize($conditionData),
45 'objectID' => $objectID,
46 'objectTypeID' => $objectType->objectTypeID
,
49 $conditionAction->executeAction();
55 * Deletes all conditions of the objects with the given ids.
57 * @param string $definitionName
58 * @param int[] $objectIDs
59 * @throws SystemException
61 public function deleteConditions($definitionName, array $objectIDs)
63 if (empty($objectIDs)) {
67 $definition = ObjectTypeCache
::getInstance()->getDefinitionByName($definitionName);
68 if ($definition === null) {
69 throw new SystemException("Unknown object type definition with name '" . $definitionName . "'");
72 $objectTypes = ObjectTypeCache
::getInstance()->getObjectTypes($definitionName);
74 foreach ($objectTypes as $objectType) {
75 $objectTypeIDs[] = $objectType->objectTypeID
;
78 if (empty($objectTypeIDs)) {
82 $conditionList = new ConditionList();
83 $conditionList->getConditionBuilder()->add('objectTypeID IN (?)', [$objectTypeIDs]);
84 $conditionList->getConditionBuilder()->add('objectID IN (?)', [$objectIDs]);
85 $conditionList->readObjects();
87 if (\
count($conditionList)) {
88 $conditionAction = new ConditionAction($conditionList->getObjects(), 'delete');
89 $conditionAction->executeAction();
94 * Returns the conditions for the conditioned object with the given condition
95 * object type definition and object id.
97 * @param string $definitionName
98 * @param int $objectID
100 * @throws SystemException
102 public function getConditions($definitionName, $objectID)
104 // validate definition
105 $definition = ObjectTypeCache
::getInstance()->getDefinitionByName($definitionName);
106 if ($definition === null) {
107 throw new SystemException("Unknown object type definition with name '" . $definitionName . "'");
110 if (!isset($this->conditions
[$definition->definitionID
])) {
111 $this->conditions
[$definition->definitionID
] = ConditionCacheBuilder
::getInstance()->getData([
112 'definitionID' => $definition->definitionID
,
116 if (isset($this->conditions
[$definition->definitionID
][$objectID])) {
117 return $this->conditions
[$definition->definitionID
][$objectID];
124 * Updates the conditions for the object with the given object id.
126 * @param int $objectID
127 * @param Condition[] $oldConditions
128 * @param ObjectType[] $conditionObjectTypes
130 public function updateConditions($objectID, array $oldConditions, array $conditionObjectTypes)
132 // delete old conditions first
133 $conditionAction = new ConditionAction($oldConditions, 'delete');
134 $conditionAction->executeAction();
136 // create new conditions
137 $this->createConditions($objectID, $conditionObjectTypes);