forked from clime/pimcore-custom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resource.php
404 lines (331 loc) · 13.3 KB
/
Resource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/**
* Pimcore
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.pimcore.org/license
*
* @category Pimcore
* @package Document
* @copyright Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
* @license http://www.pimcore.org/license New BSD License
*/
class Document_Resource extends Element_Resource {
/**
* Contains the valid database colums
*
* @var array
*/
protected $validColumnsDocument = array();
/**
* Get the valid database columns from database
*
* @return void
*/
public function init() {
$this->validColumnsDocument = $this->getValidTableColumns("documents");
}
/**
* Get the data for the object by the given id
*
* @param integer $id
* @return void
*/
public function getById($id) {
try {
$data = $this->db->fetchRow("SELECT * FROM documents WHERE id = ?", $id);
}
catch (Exception $e) {
}
if ($data["id"] > 0) {
$this->assignVariablesToModel($data);
$this->loadLocks();
}
else {
throw new Exception("Document with the ID " . $id . " doesn't exists");
}
}
/**
* Get the data for the document from database for the given path
*
* @param string $path
* @return void
*/
public function getByPath($path) {
// check for root node
$_path = $path != "/" ? dirname($path) : $path;
$_path = str_replace("\\", "/", $_path); // windows patch
$_key = basename($path);
$_path .= $_path != "/" ? "/" : "";
$data = $this->db->fetchRow("SELECT id FROM documents WHERE path = " . $this->db->quote($_path) . " and `key` = " . $this->db->quote($_key));
if ($data["id"]) {
$this->assignVariablesToModel($data);
}
else {
// try to find a page with a pretty URL (use the original $path)
$data = $this->db->fetchRow("SELECT id FROM documents_page WHERE prettyUrl = " . $this->db->quote($path));
if ($data["id"]) {
$this->assignVariablesToModel($data);
} else {
throw new Exception("document with path $path doesn't exist");
}
}
}
/**
* Create a new record for the object in the database
*
* @return void
*/
public function create() {
try {
$this->db->insert("documents", array(
"path" => $this->model->getRealPath(),
"parentId" => $this->model->getParentId()
));
$date = time();
$this->model->setId($this->db->lastInsertId());
if (!$this->model->getKey()) {
$this->model->setKey($this->model->getId());
}
$this->model->setCreationDate($date);
$this->model->setModificationDate($date);
}
catch (Exception $e) {
throw $e;
}
}
/**
* Updates the object's data to the database, it's an good idea to use save() instead
*
* @return void
*/
public function update() {
try {
$this->model->setModificationDate(time());
$document = get_object_vars($this->model);
foreach ($document as $key => $value) {
if (in_array($key, $this->validColumnsDocument)) {
// check if the getter exists
$getter = "get" . ucfirst($key);
if(!method_exists($this->model,$getter)) {
continue;
}
// get the value from the getter
$value = $this->model->$getter();
if (is_bool($value)) {
$value = (int) $value;
}
$data[$key] = $value;
}
}
// first try to insert a new record, this is because of the recyclebin restore
try {
$this->db->insert("documents", $data);
}
catch (Exception $e) {
$this->db->update("documents", $data, $this->db->quoteInto("id = ?", $this->model->getId() ));
}
$this->updateLocks();
}
catch (Exception $e) {
throw $e;
}
}
/**
* Deletes the object from database
*
* @return void
*/
public function delete() {
try {
$this->db->delete("documents", $this->db->quoteInto("id = ?", $this->model->getId() ));
}
catch (Exception $e) {
throw $e;
}
}
public function updateChildsPaths($oldPath) {
//get documents to empty their cache
$documents = $this->db->fetchAll("SELECT id,path FROM documents WHERE path LIKE ?", $oldPath . "%");
//update documents child paths
$this->db->query("update documents set path = replace(path," . $this->db->quote($oldPath) . "," . $this->db->quote($this->model->getRealFullPath()) . ") where path like " . $this->db->quote($oldPath . "/%") . ";");
//update documents child permission paths
$this->db->query("update users_workspaces_document set cpath = replace(cpath," . $this->db->quote($oldPath) . "," . $this->db->quote($this->model->getRealFullPath()) . ") where cpath like " . $this->db->quote($oldPath . "/%") .";");
//update documents child properties paths
$this->db->query("update properties set cpath = replace(cpath," . $this->db->quote($oldPath) . "," . $this->db->quote($this->model->getRealFullPath()) . ") where cpath like " . $this->db->quote($oldPath . "/%" ) . ";");
foreach ($documents as $document) {
// empty documents cache
try {
Pimcore_Model_Cache::clearTag("document_" . $document["id"]);
}
catch (Exception $e) {
}
}
}
/**
* @return string retrieves the current full document path from DB
*/
public function getCurrentFullPath() {
try {
$path = $this->db->fetchOne("SELECT CONCAT(path,`key`) as path FROM documents WHERE id = ?", $this->model->getId());
}
catch (Exception $e) {
Logger::error("could not get current document path from DB");
}
return $path;
}
/**
* Get the properties for the object from database and assign it
*
* @return void
*/
public function getProperties($onlyInherited = false, $onlyDirect = false) {
$properties = array();
if($onlyDirect) {
$propertiesRaw = $this->db->fetchAll("SELECT * FROM properties WHERE cid = ? AND ctype='document'", $this->model->getId());
} else {
$parentIds = $this->getParentIds();
$propertiesRaw = $this->db->fetchAll("SELECT * FROM properties WHERE ((cid IN (".implode(",",$parentIds).") AND inheritable = 1) OR cid = ? ) AND ctype='document'", $this->model->getId());
}
// because this should be faster than mysql
usort($propertiesRaw, function($left,$right) {
return strcmp($left["cpath"],$right["cpath"]);
});
foreach ($propertiesRaw as $propertyRaw) {
try {
$property = new Property();
$property->setType($propertyRaw["type"]);
$property->setCid($this->model->getId());
$property->setName($propertyRaw["name"]);
$property->setCtype("document");
$property->setDataFromResource($propertyRaw["data"]);
$property->setInherited(true);
if ($propertyRaw["cid"] == $this->model->getId()) {
$property->setInherited(false);
}
$property->setInheritable(false);
if ($propertyRaw["inheritable"]) {
$property->setInheritable(true);
}
if($onlyInherited && !$property->getInherited()) {
continue;
}
$properties[$propertyRaw["name"]] = $property;
}
catch (Exception $e) {
Logger::error("can't add property " . $propertyRaw["name"] . " to document " . $this->model->getRealFullPath());
}
}
// if only inherited then only return it and dont call the setter in the model
if($onlyInherited || $onlyDirect) {
return $properties;
}
$this->model->setProperties($properties);
return $properties;
}
/**
* deletes all properties for the object from database
*
* @return void
*/
public function deleteAllProperties() {
$this->db->delete("properties", $this->db->quoteInto("cid = ? AND ctype = 'document'", $this->model->getId()));
}
/**
* @return void
*/
public function deleteAllPermissions() {
$this->db->delete("users_workspaces_document", $this->db->quoteInto("cid = ?", $this->model->getId()));
}
/**
* @return void
*/
public function deleteAllTasks() {
$this->db->delete("schedule_tasks", $this->db->quoteInto("cid = ? AND ctype='document'", $this->model->getId()));
}
/**
* Quick test if there are childs
*
* @return boolean
*/
public function hasChilds() {
$c = $this->db->fetchOne("SELECT id FROM documents WHERE parentId = ? LIMIT 1", $this->model->getId());
return (bool) $c;
}
/**
* returns the amount of directly childs (not recursivly)
*
* @return integer
*/
public function getChildAmount() {
$c = $this->db->fetchOne("SELECT COUNT(*) AS count FROM documents WHERE parentId = ?", $this->model->getId());
return $c;
}
public function isLocked () {
// check for an locked element below this element
$belowLocks = $this->db->fetchOne("SELECT tree_locks.id FROM tree_locks INNER JOIN documents ON tree_locks.id = documents.id WHERE documents.path LIKE ? AND tree_locks.type = 'document' AND tree_locks.locked IS NOT NULL AND tree_locks.locked != '' LIMIT 1", $this->model->getFullpath() . "/%");
if($belowLocks > 0) {
return true;
}
$parentIds = $this->getParentIds();
$inhertitedLocks = $this->db->fetchOne("SELECT id FROM tree_locks WHERE id IN (".implode(",",$parentIds).") AND type='document' AND locked = 'propagate' LIMIT 1");
if($inhertitedLocks > 0) {
return true;
}
return false;
}
public function loadLocks() {
// add tree-lock
$this->model->setLocked($this->db->fetchOne("SELECT locked FROM tree_locks WHERE id = ? AND type = ?", array($this->model->getId(), "document")));
}
public function updateLocks() {
// tree_locks
$this->db->delete("tree_locks", "id = " . $this->model->getId() . " AND type = 'document'");
if($this->model->getLocked()) {
$this->db->insert("tree_locks", array(
"id" => $this->model->getId(),
"type" => "document",
"locked" => $this->model->getLocked()
));
}
}
public function isAllowed($type, $user) {
// collect properties via parent - ids
$parentIds = array(1);
$obj = $this->model->getParent();
if($obj) {
while($obj) {
$parentIds[] = $obj->getId();
$obj = $obj->getParent();
}
}
$parentIds[] = $this->model->getId();
$userIds = $user->getRoles();
$userIds[] = $user->getId();
try {
$permissionsParent = $this->db->fetchOne("SELECT `" . $type . "` FROM users_workspaces_document WHERE cid IN (".implode(",",$parentIds).") AND userId IN (" . implode(",",$userIds) . ") ORDER BY LENGTH(cpath) DESC LIMIT 1");
if($permissionsParent) {
return true;
}
// exception for list permission
if(empty($permissionsParent) && $type == "list") {
// check for childs with permissions
$path = $this->model->getFullPath() . "/";
if($this->model->getId() == 1) {
$path = "/";
}
$permissionsChilds = $this->db->fetchOne("SELECT list FROM users_workspaces_document WHERE cpath LIKE ? AND userId IN (" . implode(",",$userIds) . ") LIMIT 1", $path."%");
if($permissionsChilds) {
return true;
}
}
} catch (Exception $e) {
Logger::warn("Unable to get permission " . $type . " for document " . $this->model->getId());
}
return false;
}
}