Commit | Line | Data |
---|---|---|
11ade432 AE |
1 | <?php |
2 | namespace wcf\data; | |
3 | ||
4 | use wcf\system\event\EventHandler; | |
5 | use wcf\system\exception\PermissionDeniedException; | |
6 | use wcf\system\exception\ValidateActionException; | |
7 | use wcf\system\WCF; | |
8 | use wcf\util\ClassUtil; | |
9 | use wcf\util\StringUtil; | |
10 | ||
11 | /** | |
12 | * Default implementation for DatabaseObject-related actions. | |
13 | * | |
14 | * @author Alexander Ebert | |
15 | * @copyright 2001-2011 WoltLab GmbH | |
16 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
17 | * @package com.woltlab.wcf | |
18 | * @subpackage data | |
19 | * @category Community Framework | |
20 | */ | |
21 | abstract class AbstractDatabaseObjectAction implements DatabaseObjectAction { | |
22 | /** | |
23 | * pending action | |
24 | * | |
25 | * @var string | |
26 | */ | |
27 | protected $action = ''; | |
28 | ||
29 | /** | |
30 | * object editor class name | |
31 | * | |
32 | * @var string | |
33 | */ | |
34 | protected $className = ''; | |
35 | ||
36 | /** | |
37 | * list of object ids | |
38 | * | |
39 | * @var array<integer> | |
40 | */ | |
41 | protected $objectIDs = array(); | |
42 | ||
43 | /** | |
44 | * list of objects | |
45 | * | |
46 | * @var array<DatabaseObjectEditor> | |
47 | */ | |
48 | protected $objects = array(); | |
49 | ||
50 | /** | |
51 | * multi-dimensional array of parameters required by an action | |
52 | * | |
53 | * @var array<array> | |
54 | */ | |
55 | protected $parameters = array(); | |
56 | ||
57 | /** | |
58 | * list of permissions required to create objects | |
59 | * | |
60 | * @var array | |
61 | */ | |
62 | protected $permissionsCreate = array(); | |
63 | ||
64 | /** | |
65 | * list of permissions required to delete objects | |
66 | * | |
67 | * @var array | |
68 | */ | |
69 | protected $permissionsDelete = array(); | |
70 | ||
71 | /** | |
72 | * list of permissions required to update objects | |
73 | * | |
74 | * @var array | |
75 | */ | |
76 | protected $permissionsUpdate = array(); | |
77 | ||
78 | /** | |
79 | * values returned by executed action | |
80 | * | |
81 | * @var mixed | |
82 | */ | |
83 | protected $returnValues = null; | |
84 | ||
85 | /** | |
86 | * Initialized a new DatabaseObject-related action. | |
87 | * | |
88 | * @param array $objectIDs | |
89 | * @param string $action | |
90 | * @param array $parameters | |
91 | */ | |
92 | public function __construct(array $objectIDs, $action, array $parameters = array()) { | |
93 | $this->objectIDs = $objectIDs; | |
94 | $this->action = $action; | |
95 | $this->parameters = $parameters; | |
96 | ||
97 | // fire event action | |
98 | EventHandler::getInstance()->fireAction($this, 'initializeAction'); | |
99 | } | |
100 | ||
101 | /** | |
102 | * @see DatabaseObjectAction::validateAction() | |
103 | */ | |
104 | public function validateAction() { | |
105 | // validate action name | |
106 | if (!method_exists($this, $this->getActionName())) { | |
107 | throw new ValidateActionException("unknown action '".$this->getActionName()."'"); | |
108 | } | |
109 | ||
110 | $actionName = 'validate'.StringUtil::firstCharToUpperCase($this->getActionName()); | |
111 | if (!method_exists($this, $actionName)) { | |
112 | throw new ValidateActionException("validation of action '".$this->getActionName()."' failed"); | |
113 | } | |
114 | ||
115 | // execute action | |
116 | call_user_func_array(array($this, $actionName), $this->getParameters()); | |
117 | } | |
118 | ||
119 | /** | |
120 | * @see DatabaseObjectAction::executeAction() | |
121 | */ | |
122 | public function executeAction() { | |
123 | // execute action | |
124 | if (method_exists($this, $this->getActionName())) { | |
125 | $this->returnValues = call_user_func_array(array($this, $this->getActionName()), $this->getParameters()); | |
126 | } | |
127 | ||
128 | // reset cache | |
129 | if (ClassUtil::isInstanceOf($this->className, 'wcf\data\EditableCachedObject')) { | |
130 | call_user_func(array($this->className, 'resetCache')); | |
131 | } | |
132 | ||
133 | // fire event action | |
134 | EventHandler::getInstance()->fireAction($this, 'finalizeAction'); | |
135 | ||
136 | return $this->getReturnValues(); | |
137 | } | |
138 | ||
139 | /** | |
140 | * @see DatabaseObjectAction::getActionName() | |
141 | */ | |
142 | public function getActionName() { | |
143 | return $this->action; | |
144 | } | |
145 | ||
146 | /** | |
147 | * @see DatabaseObjectAction::getObjectIDs() | |
148 | */ | |
149 | public function getObjectIDs() { | |
150 | return $this->objectIDs; | |
151 | } | |
152 | ||
153 | /** | |
154 | * @see DatabaseObjectAction::getParameters() | |
155 | */ | |
156 | public function getParameters() { | |
157 | return $this->parameters; | |
158 | } | |
159 | ||
160 | /** | |
161 | * @see DatabaseObjectAction::getReturnValues() | |
162 | */ | |
163 | public function getReturnValues() { | |
164 | return array( | |
165 | 'objectIDs' => $this->getObjectIDs(), | |
166 | 'returnValues' => $this->returnValues | |
167 | ); | |
168 | } | |
169 | ||
170 | /** | |
171 | * Validates permissions and parameters. | |
172 | */ | |
173 | public function validateCreate() { | |
174 | // validate permissions | |
175 | if (is_array($this->permissionsCreate) && count($this->permissionsCreate)) { | |
176 | try { | |
177 | WCF::getSession()->checkPermission($this->permissionsCreate); | |
178 | } | |
179 | catch (PermissionDeniedException $e) { | |
180 | throw new ValidateActionException('Insufficient permissions'); | |
181 | } | |
182 | } | |
183 | else { | |
184 | throw new ValidateActionException('Insufficient permissions'); | |
185 | } | |
186 | } | |
187 | ||
188 | /** | |
189 | * Validates permissions and parameters. | |
190 | */ | |
191 | public function validateDelete() { | |
192 | // validate permissions | |
193 | if (is_array($this->permissionsDelete) && count($this->permissionsDelete)) { | |
194 | try { | |
195 | WCF::getSession()->checkPermission($this->permissionsDelete); | |
196 | } | |
197 | catch (PermissionDeniedException $e) { | |
198 | throw new ValidateActionException('Insufficient permissions'); | |
199 | } | |
200 | } | |
201 | else { | |
202 | throw new ValidateActionException('Insufficient permissions'); | |
203 | } | |
204 | ||
205 | // read data | |
206 | $this->readObjects(); | |
207 | ||
208 | if (!count($this->objects)) { | |
209 | throw new ValidateActionException('Invalid object id'); | |
210 | } | |
211 | } | |
212 | ||
213 | /** | |
214 | * Validates permissions and parameters. | |
215 | */ | |
216 | public function validateUpdate() { | |
217 | // validate permissions | |
218 | if (is_array($this->permissionsUpdate) && count($this->permissionsUpdate)) { | |
219 | try { | |
220 | WCF::getSession()->checkPermission($this->permissionsUpdate); | |
221 | } | |
222 | catch (PermissionDeniedException $e) { | |
223 | throw new ValidateActionException('Insufficient permissions'); | |
224 | } | |
225 | } | |
226 | else { | |
227 | throw new ValidateActionException('Insufficient permissions'); | |
228 | } | |
229 | ||
230 | // read data | |
231 | $this->readObjects(); | |
232 | ||
233 | if (!count($this->objects)) { | |
234 | throw new ValidateActionException('Invalid object id'); | |
235 | } | |
236 | } | |
237 | ||
238 | /** | |
239 | * Creates a new data. | |
240 | * | |
241 | * @return DatabaseObject | |
242 | */ | |
243 | public function create() { | |
244 | // create data | |
245 | return call_user_func(array($this->className, 'create'), $this->parameters['data']); | |
246 | } | |
247 | ||
248 | /** | |
249 | * Deletes data. | |
250 | * Returns the number of deleted data. | |
251 | * | |
252 | * @return integer | |
253 | */ | |
254 | public function delete() { | |
255 | if (!count($this->objects)) { | |
256 | $this->readObjects(); | |
257 | } | |
258 | ||
259 | // get index name | |
260 | $indexName = call_user_func(array($this->className, 'getDatabaseTableIndexName')); | |
261 | ||
262 | // get ids | |
263 | $objectIDs = array(); | |
264 | foreach ($this->objects as $object) { | |
265 | $objectIDs[] = $object->__get($indexName); | |
266 | } | |
267 | ||
268 | // execute action | |
269 | return call_user_func(array($this->className, 'deleteAll'), $objectIDs); | |
270 | } | |
271 | ||
272 | /** | |
273 | * Updates data. | |
274 | */ | |
275 | public function update() { | |
276 | if (!count($this->objects)) { | |
277 | $this->readObjects(); | |
278 | } | |
279 | ||
280 | foreach ($this->objects as $object) { | |
281 | $object->update($this->parameters['data']); | |
282 | } | |
283 | } | |
284 | ||
285 | /** | |
286 | * Reads data by data id. | |
287 | */ | |
288 | protected function readObjects() { | |
289 | if (!count($this->objectIDs)) { | |
290 | return; | |
291 | } | |
292 | ||
293 | // get base class | |
294 | $baseClass = call_user_func(array($this->className, 'getBaseClass')); | |
295 | ||
296 | // get db information | |
297 | $tableName = call_user_func(array($this->className, 'getDatabaseTableName')); | |
298 | $indexName = call_user_func(array($this->className, 'getDatabaseTableIndexName')); | |
299 | ||
300 | // get objects | |
301 | $sql = "SELECT * | |
302 | FROM ".$tableName." | |
303 | WHERE ".$indexName." IN (".str_repeat('?,', count($this->objectIDs) - 1)."?)"; | |
304 | $statement = WCF::getDB()->prepareStatement($sql); | |
305 | $statement->execute($this->objectIDs); | |
306 | while ($object = $statement->fetchObject($baseClass)) { | |
307 | $this->objects[] = new $this->className($object); | |
308 | } | |
309 | } | |
310 | } | |
311 | ?> |