forked from pimcore/pimcore
/
Service.php
536 lines (465 loc) · 16.2 KB
/
Service.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
<?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 Element_Service
{
/**
* @param Dependency $d
* @return array
*/
public static function getRequiredByDependenciesForFrontend(Dependency $d)
{
$user = Zend_Registry::get("pimcore_user");
$dependencies["hasHidden"] = false;
$dependencies["requiredBy"] = array();
// requiredBy
foreach ($d->getRequiredBy() as $r) {
if ($e = self::getDependedElement($r)) {
$e->getUserPermissions($user);
if ($e->isAllowed("list")) {
$dependencies["requiredBy"][] = self::getDependencyForFrontend($e);
} else {
$dependencies["hasHidden"] = true;
}
}
}
return $dependencies;
}
/**
* @param Dependency $d
* @return array
*/
public static function getRequiresDependenciesForFrontend(Dependency $d)
{
$user = Zend_Registry::get("pimcore_user");
$dependencies["hasHidden"] = false;
$dependencies["requires"] = array();
// requires
foreach ($d->getRequires() as $r) {
if ($e = self::getDependedElement($r)) {
$e->getUserPermissions($user);
if ($e->isAllowed("list")) {
$dependencies["requires"][] = self::getDependencyForFrontend($e);
} else {
$dependencies["hasHidden"] = true;
}
}
}
return $dependencies;
}
/**
* @param Document|Asset|Object_Abstract $element
* @return array
*/
public static function getDependencyForFrontend($element)
{
if ($element instanceof Document) {
return array(
"id" => $element->getId(),
"path" => $element->getFullPath(),
"type" => "document",
"subtype" => $element->getType()
);
}
else if ($element instanceof Asset) {
return array(
"id" => $element->getId(),
"path" => $element->getFullPath(),
"type" => "asset",
"subtype" => $element->getType()
);
}
else if ($element instanceof Object_Abstract) {
return array(
"id" => $element->getId(),
"path" => $element->getFullPath(),
"type" => "object",
"subtype" => $element->geto_Type()
);
}
}
/**
* @param array $config
* @return Object_Abstract|Document|Asset
*/
public static function getDependedElement($config)
{
if ($config["type"] == "object") {
return Object_Abstract::getById($config["id"]);
}
else if ($config["type"] == "asset") {
return Asset::getById($config["id"]);
}
else if ($config["type"] == "document") {
return Document::getById($config["id"]);
}
return false;
}
/**
* determines whether an element is published
*
* @static
* @param Element_Interface $element
* @return bool
*/
public static function isPublished($element = null)
{
if ($element instanceof Element_Interface) {
if (method_exists($element, "isPublished")) {
return $element->isPublished();
}
else {
return true;
}
}
return false;
}
/**
* @static
* @param string $type
* @param string $path
* @return Element_Interface
*/
public static function getElementByPath($type, $path)
{
if ($type == "asset") {
$element = Asset::getByPath($path);
} else if ($type == "object") {
$element = Object_Abstract::getByPath($path);
} else if ($type == "document") {
$element = Document::getByPath($path);
}
return $element;
}
/**
* Returns a uniqe key for the element in the $target-Path (recursive)
* @static
* @return Element_Interface|string
* @param string $type
* @param string $sourceKey
* @param Element_Interface $target
*/
public static function getSaveCopyName($type, $sourceKey, $target)
{
$equalElement = self::getElementByPath($type, $target->getFullPath() . "/" . $sourceKey);
if ($equalElement) {
// only for assets: add the prefix _copy before the file extension (if exist) not after to that source.jpg will be source_copy.jpg and not source.jpg_copy
if($type == "asset" && $fileExtension = Pimcore_File::getFileExtension($sourceKey)) {
$sourceKey = str_replace("." . $fileExtension, "_copy." . $fileExtension, $sourceKey);
} else {
$sourceKey .= "_copy";
}
return self::getSaveCopyName($type, $sourceKey, $target);
}
return $sourceKey;
}
/**
* @static
* @param string $type
* @param int $id
* @return Element_Interface
*/
public static function getElementById($type, $id)
{
$element = null;
if ($type == "asset") {
$element = Asset::getById($id);
} else if ($type == "object") {
$element = Object_Abstract::getById($id);
} else if ($type == "document") {
$element = Document::getById($id);
}
return $element;
}
/**
* @static
* @param Element_Interface $element $element
* @return string
*/
public static function getElementType($element)
{
$type = null;
if ($element instanceof Object_Abstract) {
$type = "object";
} else if ($element instanceof Document) {
$type = "document";
} else if ($element instanceof Asset) {
$type = "asset";
}
return $type;
}
/**
* determines the type of an element (object,asset,document)
*
* @static
* @param Element_Interface $element
* @return string
*/
public static function getType($element)
{
return self::getElementType($element);
}
/**
* Schedules element with this id for sanity check to be cleaned of broken relations
*
* @static
* @param Element_Interface $element
* @return void
*/
public static function scheduleForSanityCheck($element)
{
$type = self::getElementType($element);
$sanityCheck = new Element_Sanitycheck($element->getId(), $type);
$sanityCheck->save();
}
public static function runSanityCheck($runDirtyCheck=true)
{
$sanityCheck = Element_Sanitycheck::getNext();
while ($sanityCheck) {
$element = self::getElementById($sanityCheck->getType(), $sanityCheck->getId());
if ($element) {
try {
self::performSanityCheck($element,$runDirtyCheck);
} catch (Exception $e) {
Logger::error("Element_Service: sanity check for element with id [ " . $element->getId() . " ] and type [ " . self::getType($element) . " ] failed");
}
$sanityCheck->delete();
} else {
$sanityCheck->delete();
}
$sanityCheck = Element_Sanitycheck::getNext();
}
}
/**
* @static
* @param Element_Interface $element
* @return void
*/
protected static function performSanityCheck($element, $runDirtyCheck = true)
{
if ($runDirtyCheck) {
$dirty = false;
if ($element instanceof Document_PageSnippet) {
$elements = $element->getElements();
if (is_array($elements)) {
foreach ($elements as $el) {
if (!$el->sanityCheck($element)) {
$dirty = true;
}
}
}
} else if ($element instanceof Object_Abstract) {
$fieldDefintions = $element->getO_class()->getFieldDefinitions();
foreach ($fieldDefintions as $fd) {
if (!$fd->sanityCheck($element)) {
$dirty = true;
}
}
}
$properties = $element->getProperties();
if (is_array($properties)) {
foreach ($properties as $p) {
$relTypes = array("object", "asset", "document");
if (in_array($p->getType(), $relTypes) and !$p->isInherited()) {
$relId = $p->getResource()->getRawData();
if ($relId) {
$el = self::getElementById($p->getType(), $relId);
if (!$el) {
$p->setData(null);
$dirty = true;
}
}
}
}
}
} else {
$dirty = true;
}
if ($dirty) {
$element->setUserModification(0);
$element->save();
}
}
/**
* @static
* @param $props
* @return array
*/
public static function minimizePropertiesForEditmode($props)
{
$properties = array();
foreach ($props as $key => $p) {
//$p = object2array($p);
$allowedProperties = array(
"key",
"o_key",
"filename",
"path",
"o_path",
"id",
"o_id",
"o_type",
"type"
);
if ($p->getData() instanceof Document || $p->getData() instanceof Asset || $p->getData() instanceof Object_Abstract) {
$pa = array();
$vars = get_object_vars($p->getData());
foreach ($vars as $k => $value) {
if (in_array($k, $allowedProperties)) {
$pa[$k] = $p->getData()->$k;
}
}
// clone it because of caching
$tmp = clone $p;
$tmp->setData($pa);
$properties[$key] = object2array($tmp);
}
else {
$properties[$key] = object2array($p);
}
}
return $properties;
}
/**
* @param Element_Interface $target the parent element
* @param Element_Interface $new the newly inserted child
* @return void
*/
protected function updateChilds($target, $new)
{
if (is_array($target->getChilds())) {
//check in case of recursion
$found = false;
foreach ($target->getChilds() as $child) {
if ($child->getId() == $new->getId()) {
$found = true;
}
}
if (!$found) {
$target->setChilds(array_merge($target->getChilds(), array($new)));
}
} else {
$target->setChilds(array($new));
}
}
/**
* @param Element_interface $element
* @return array
*/
public static function gridElementData(Element_Interface $element)
{
$data = array(
"id" => $element->getId(),
"fullpath" => $element->getFullPath(),
"type" => Element_Service::getType($element),
"subtype" => $element->getType(),
"filename" => self::getFilename($element),
"creationDate" => $element->getCreationDate(),
"modificationDate" => $element->getModificationDate()
);
if (method_exists($element, "isPublished")) {
$data["published"] = $element->isPublished();
} else {
$data["published"] = true;
}
return $data;
}
/**
* @param Element_Interface $element
* @return string
*/
public static function getFilename(Element_Interface $element)
{
if ($element instanceof Document || $element instanceof Object_Abstract) {
return $element->getKey();
} else if ($element instanceof Asset) {
return $element->getFilename();
}
}
/**
* find all elements which the user may not list and therefore may never be shown to the user
* @param string $type asset|object|document
* @return array
*/
public static function findForbiddenPaths($type, $user)
{
$parentIds = array();
$parent = $user->getParent();
while ($parent instanceof User) {
$parentIds[] = "userId = " . $parent->getId();
$parent = $parent->getParent();
}
$userCondition = "( userId= " . $user->getId();
if (count($parentIds) > 0) {
$userCondition .= " OR " . implode(" or ", $parentIds) . ")";
} else $userCondition .= ")";
$condition = "SELECT cpath FROM (SELECT cid,cpath,
sum(`list`) as `list`
FROM " . $type . "s_permissions
WHERE " . $userCondition . "
GROUP by cid
ORDER BY cpath DESC ) temp WHERE list = 0";
//Logger::log($condition);
$db = Pimcore_Resource::get();
$data = $db->fetchAll($condition);
$forbidden = array();
if (is_array($data)) {
foreach ($data as $row) {
$forbidden[] = $row["cpath"];
}
}
return $forbidden;
}
/**
* renews all references, for example after unserializing an Element_Interface
* @param Document|Asset|Object_Abstract $data
* @return mixed
*/
public static function renewReferences($data, $initial = true)
{
if (is_array($data)) {
foreach ($data as &$value) {
$value = self::renewReferences($value, false);
}
return $data;
} else if (is_object($data)) {
if ($data instanceof Element_Interface && !$initial) {
return Element_Service::getElementById(Element_Service::getElementType($data), $data->getId());
} else {
// if this is the initial element set the correct path and key
if ($data instanceof Element_Interface && $initial) {
$originalElement = Element_Service::getElementById(Element_Service::getElementType($data), $data->getId());
if ($originalElement) {
if ($data instanceof Asset) {
$data->setFilename($originalElement->getFilename());
} else if ($data instanceof Document) {
$data->setKey($originalElement->getKey());
} else if ($data instanceof Object_Abstract) {
$data->setKey($originalElement->getKey());
}
$data->setPath($originalElement->getPath());
}
}
$properties = get_object_vars($data);
foreach ($properties as $name => $value) {
$data->$name = self::renewReferences($value, false);
}
return $data;
}
}
return $data;
}
}