forked from pimcore/pimcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Service.php
687 lines (588 loc) · 19.8 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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<?php
/**
* Pimcore
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @category Pimcore
* @package Element
* @copyright Copyright (c) 2009-2015 pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GNU General Public License version 3 (GPLv3)
*/
namespace Pimcore\Model\Element;
use Pimcore\Model;
use Pimcore\Model\Document;
use Pimcore\Model\Asset;
use Pimcore\Model\Object;
use Pimcore\Model\Dependency;
use Pimcore\File;
class Service extends Model\AbstractModel {
/**
* @static
* @param $element
* @return string
*/
public static function getIdPath($element) {
$path = "";
if ($element instanceof ElementInterface) {
$elementType = self::getElementType($element);
$nid = $element->getParentId();
$ne = self::getElementById($elementType, $nid);
}
if ($ne) {
$path = self::getIdPath($ne, $path);
}
if ($element) {
$path = $path . "/" . $element->getId();
}
return $path;
}
/**
* @static
* @param $list array | \Pimcore\Model\Listing\AbstractListing
* @return array
*/
public static function getIdList($list,$idGetter = 'getId'){
$ids = array();
if(is_array($list)){
foreach($list as $entry){
if(is_object($entry) && method_exists($entry,$idGetter)){
$ids[] = $entry->$idGetter();
}elseif(is_scalar($entry)){
$ids[] = $entry;
}
}
}
if($list instanceof \Pimcore\Model\Listing\AbstractListing){
$ids = $list->loadIdList();
}
$ids = array_unique($ids);
return $ids;
}
/**
* @param Dependency $d
* @return array
*/
public static function getRequiredByDependenciesForFrontend(Dependency $d)
{
$dependencies["hasHidden"] = false;
$dependencies["requiredBy"] = array();
// requiredBy
foreach ($d->getRequiredBy() as $r) {
if ($e = self::getDependedElement($r)) {
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)
{
$dependencies["hasHidden"] = false;
$dependencies["requires"] = array();
// requires
foreach ($d->getRequires() as $r) {
if ($e = self::getDependedElement($r)) {
if ($e->isAllowed("list")) {
$dependencies["requires"][] = self::getDependencyForFrontend($e);
} else {
$dependencies["hasHidden"] = true;
}
}
}
return $dependencies;
}
/**
* @param Document|Asset|Object\AbstractObject $element
* @return array
*/
public static function getDependencyForFrontend($element)
{
if ($element instanceof ElementInterface) {
return array(
"id" => $element->getId(),
"path" => $element->getFullPath(),
"type" => self::getElementType($element),
"subtype" => $element->getType()
);
}
}
/**
* @param array $config
* @return Object\AbstractObject|Document|Asset
*/
public static function getDependedElement($config)
{
if ($config["type"] == "object") {
return Object::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 ElementInterface $element
* @return bool
*/
public static function isPublished($element = null)
{
if ($element instanceof ElementInterface) {
if (method_exists($element, "isPublished")) {
return $element->isPublished();
}
else {
return true;
}
}
return false;
}
/**
* @static
* @param string $type
* @param string $path
* @return ElementInterface
*/
public static function getElementByPath($type, $path)
{
if ($type == "asset") {
$element = Asset::getByPath($path);
} else if ($type == "object") {
$element = Object::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 ElementInterface|string
* @param string $type
* @param string $sourceKey
* @param ElementInterface $target
*/
public static function getSaveCopyName($type, $sourceKey, $target)
{
if (self::pathExists($target->getFullPath() . "/" . $sourceKey, $type)) {
// 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 = File::getFileExtension($sourceKey)) {
$sourceKey = str_replace("." . $fileExtension, "_copy." . $fileExtension, $sourceKey);
} else {
$sourceKey .= "_copy";
}
return self::getSaveCopyName($type, $sourceKey, $target);
}
return $sourceKey;
}
/**
* @static
* @param $type
* @param $path
* @return bool
*/
public static function pathExists ($path, $type = null) {
if($type == "asset") {
return Asset\Service::pathExists($path);
} else if ($type == "document") {
return Document\Service::pathExists($path);
} else if ($type == "object") {
return Object\Service::pathExists($path);
}
return;
}
/**
* @static
* @param string $type
* @param int $id
* @return ElementInterface
*/
public static function getElementById($type, $id)
{
$element = null;
if ($type == "asset") {
$element = Asset::getById($id);
} else if ($type == "object") {
$element = Object::getById($id);
} else if ($type == "document") {
$element = Document::getById($id);
}
return $element;
}
/**
* @static
* @param ElementInterface $element $element
* @return string
*/
public static function getElementType($element)
{
$type = null;
if ($element instanceof Object\AbstractObject) {
$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 ElementInterface $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 ElementInterface $element
* @return void
*/
public static function scheduleForSanityCheck($element)
{
$type = self::getElementType($element);
$sanityCheck = new Sanitycheck($element->getId(), $type);
$sanityCheck->save();
}
/**
*
*/
public static function runSanityCheck() {
$sanityCheck = Sanitycheck::getNext();
while ($sanityCheck) {
$element = self::getElementById($sanityCheck->getType(), $sanityCheck->getId());
if ($element) {
try {
self::performSanityCheck($element);
} 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 = Sanitycheck::getNext();
// reduce load on server
\Logger::debug("Now timeout for 3 seconds");
sleep(3);
}
}
/**
* @static
* @param ElementInterface $element
* @return void
*/
protected static function performSanityCheck($element)
{
if($latestVersion = $element->getLatestVersion()) {
if($latestVersion->getDate() > $element->getModificationDate()) {
return;
}
}
$element->setUserModification(0);
$element->save();
if($version = $element->getLatestVersion(true)) {
$version->setNote("Sanitycheck");
$version->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\AbstractObject) {
$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);
}
// add config from predefined properties
if ($p->getName() && $p->getType()) {
$predefined = Model\Property\Predefined::getByKey($p->getName());
if ($predefined && $predefined->getType() == $p->getType()) {
$properties[$key]["config"] = $predefined->getConfig();
}
}
}
return $properties;
}
/**
* @param ElementInterface $target the parent element
* @param ElementInterface $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 ElementInterface $element
* @return array
*/
public static function gridElementData(ElementInterface $element)
{
$data = array(
"id" => $element->getId(),
"fullpath" => $element->getFullPath(),
"type" => self::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 ElementInterface $element
* @return string
*/
public static function getFilename(ElementInterface $element)
{
if ($element instanceof Document || $element instanceof Object\AbstractObject) {
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)
{
if($user->isAdmin()) {
return array();
}
// get workspaces
$workspaces = $user->{"getWorkspaces".ucfirst($type)}();
foreach ($user->getRoles() as $roleId) {
$role = Model\User\Role::getById($roleId);
$workspaces = array_merge($workspaces, $role->{"getWorkspaces".ucfirst($type)}());
}
$forbidden = array();
if(count($workspaces) > 0) {
foreach ($workspaces as $workspace) {
if(!$workspace->getList()) {
$forbidden[] = $workspace->getCpath();
}
}
} else {
$forbidden[] = "/";
}
return $forbidden;
}
/**
* renews all references, for example after unserializing an ElementInterface
* @param Document|Asset|Object\AbstractObject $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 ElementInterface && !$initial) {
return self::getElementById(self::getElementType($data), $data->getId());
} else {
// if this is the initial element set the correct path and key
if ($data instanceof ElementInterface && $initial) {
$originalElement = self::getElementById(self::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\AbstractObject) {
$data->setKey($originalElement->getKey());
}
if (!Object\AbstractObject::doNotRestoreKeyAndPath()) {
$data->setPath($originalElement->getPath());
}
}
}
$properties = get_object_vars($data);
foreach ($properties as $name => $value) {
$data->$name = self::renewReferences($value, false);
}
return $data;
}
}
return $data;
}
/**
* @static
* @param string $path
* @return string
*/
public static function correctPath ($path) {
// remove trailing slash
if($path != "/") {
$path = rtrim($path,"/ ");
}
// correct wrong path (root-node problem)
$path = str_replace("//", "/", $path);
return $path;
}
/**
* @static
* @param ElementInterface $element
* @return ElementInterface
*/
public static function loadAllFields (ElementInterface $element) {
if($element instanceof Document) {
Document\Service::loadAllDocumentFields($element);
} else if ($element instanceof Object\Concrete) {
Object\Service::loadAllObjectFields($element);
} else if ($element instanceof Asset) {
Asset\Service::loadAllFields($element);
}
return $element;
}
/**
* clean up broken views which were generated by localized fields, ....
* when removing a language the view isn't valid anymore
*/
public function cleanupBrokenViews () {
$this->getDao()->cleanupBrokenViews();
}
/** Callback for array_filter function.
* @param $var value
* @return bool true if value is accepted
*/
private static function filterNullValues($var) {
return strlen($var) > 0;
}
/**
* @param $path
* @param array $options
* @return null
* @throws \Exception
*/
public static function createFolderByPath($path,$options = array()) {
$calledClass = get_called_class();
if($calledClass == __CLASS__){
throw new \Exception("This method must be called from a extended class. e.g Asset\\Service, Object\\Service, Document\\Service");
}
$type = str_replace('\Service','',$calledClass);
$type = "\\" . ltrim($type, "\\");
$folderType = $type . '\Folder';
$lastFolder = null;
$pathsArray = array();
$parts = explode('/', $path);
$parts = array_filter($parts, "\\Pimcore\\Model\\Element\\Service::filterNullValues");
$sanitizedPath = "/";
foreach($parts as $part) {
$sanitizedPath = $sanitizedPath . File::getValidFilename($part) . "/";
}
if (!($foundElement = $type::getByPath($sanitizedPath))) {
foreach ($parts as $part) {
$pathsArray[] = $pathsArray[count($pathsArray) - 1] . '/' . File::getValidFilename($part);
}
for ($i = 0; $i < count($pathsArray); $i++) {
$currentPath = $pathsArray[$i];
if (!($type::getByPath($currentPath) instanceof $type)) {
$parentFolderPath = ($i ==0) ? '/' : $pathsArray[$i - 1];
$parentFolder = $type::getByPath($parentFolderPath);
$folder = new $folderType();
$folder->setParent($parentFolder);
if ($parentFolder) {
$folder->setParentId($parentFolder->getId());
} else {
$folder->setParentId(1);
}
$key = substr($currentPath, strrpos($currentPath, '/') + 1, strlen($currentPath));
if (method_exists($folder, 'setKey')) {
$folder->setKey($key);
}
if (method_exists($folder, 'setFilename')) {
$folder->setFilename($key);
}
if (method_exists($folder, 'setType')) {
$folder->setType('folder');
}
$folder->setPath($currentPath);
$folder->setUserModification(0);
$folder->setUserOwner(1);
$folder->setCreationDate(time());
$folder->setModificationDate(time());
$folder->setValues($options);
$folder->save();
$lastFolder = $folder;
}
}
} else {
return $foundElement;
}
return $lastFolder;
}
}