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