Merge branch '6.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / condition / ConditionHandler.class.php
... / ...
CommitLineData
1<?php
2
3namespace wcf\system\condition;
4
5use wcf\data\condition\Condition;
6use wcf\data\condition\ConditionAction;
7use wcf\data\condition\ConditionList;
8use wcf\data\object\type\ObjectType;
9use wcf\data\object\type\ObjectTypeCache;
10use wcf\system\cache\builder\ConditionCacheBuilder;
11use wcf\system\exception\SystemException;
12use wcf\system\SingletonFactory;
13
14/**
15 * Handles general condition-related matters.
16 *
17 * @author Matthias Schmidt
18 * @copyright 2001-2019 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 */
21class ConditionHandler extends SingletonFactory
22{
23 /**
24 * list of available conditions grouped by the id of the related condition
25 * object type definition
26 * @var array
27 */
28 protected $conditions = [];
29
30 /**
31 * Creates condition objects for the object with the given id and based
32 * on the given condition object types.
33 *
34 * @param int $objectID
35 * @param ObjectType[] $conditionObjectTypes
36 */
37 public function createConditions($objectID, array $conditionObjectTypes)
38 {
39 foreach ($conditionObjectTypes as $objectType) {
40 $conditionData = $objectType->getProcessor()->getData();
41 if ($conditionData !== null) {
42 $conditionAction = new ConditionAction([], 'create', [
43 'data' => [
44 'conditionData' => \serialize($conditionData),
45 'objectID' => $objectID,
46 'objectTypeID' => $objectType->objectTypeID,
47 ],
48 ]);
49 $conditionAction->executeAction();
50 }
51 }
52 }
53
54 /**
55 * Deletes all conditions of the objects with the given ids.
56 *
57 * @param string $definitionName
58 * @param int[] $objectIDs
59 * @throws SystemException
60 */
61 public function deleteConditions($definitionName, array $objectIDs)
62 {
63 if (empty($objectIDs)) {
64 return;
65 }
66
67 $definition = ObjectTypeCache::getInstance()->getDefinitionByName($definitionName);
68 if ($definition === null) {
69 throw new SystemException("Unknown object type definition with name '" . $definitionName . "'");
70 }
71
72 $objectTypes = ObjectTypeCache::getInstance()->getObjectTypes($definitionName);
73 $objectTypeIDs = [];
74 foreach ($objectTypes as $objectType) {
75 $objectTypeIDs[] = $objectType->objectTypeID;
76 }
77
78 if (empty($objectTypeIDs)) {
79 return;
80 }
81
82 $conditionList = new ConditionList();
83 $conditionList->getConditionBuilder()->add('objectTypeID IN (?)', [$objectTypeIDs]);
84 $conditionList->getConditionBuilder()->add('objectID IN (?)', [$objectIDs]);
85 $conditionList->readObjects();
86
87 if (\count($conditionList)) {
88 $conditionAction = new ConditionAction($conditionList->getObjects(), 'delete');
89 $conditionAction->executeAction();
90 }
91 }
92
93 /**
94 * Returns the conditions for the conditioned object with the given condition
95 * object type definition and object id.
96 *
97 * @param string $definitionName
98 * @param int $objectID
99 * @return Condition[]
100 * @throws SystemException
101 */
102 public function getConditions($definitionName, $objectID)
103 {
104 // validate definition
105 $definition = ObjectTypeCache::getInstance()->getDefinitionByName($definitionName);
106 if ($definition === null) {
107 throw new SystemException("Unknown object type definition with name '" . $definitionName . "'");
108 }
109
110 if (!isset($this->conditions[$definition->definitionID])) {
111 $this->conditions[$definition->definitionID] = ConditionCacheBuilder::getInstance()->getData([
112 'definitionID' => $definition->definitionID,
113 ]);
114 }
115
116 if (isset($this->conditions[$definition->definitionID][$objectID])) {
117 return $this->conditions[$definition->definitionID][$objectID];
118 }
119
120 return [];
121 }
122
123 /**
124 * Updates the conditions for the object with the given object id.
125 *
126 * @param int $objectID
127 * @param Condition[] $oldConditions
128 * @param ObjectType[] $conditionObjectTypes
129 */
130 public function updateConditions($objectID, array $oldConditions, array $conditionObjectTypes)
131 {
132 // delete old conditions first
133 $conditionAction = new ConditionAction($oldConditions, 'delete');
134 $conditionAction->executeAction();
135
136 // create new conditions
137 $this->createConditions($objectID, $conditionObjectTypes);
138 }
139}