-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
Copy pathmodelement.class.php
994 lines (920 loc) · 37.2 KB
/
modelement.class.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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
<?php
/*
* This file is part of MODX Revolution.
*
* Copyright (c) MODX, LLC. All Rights Reserved.
*
* For complete copyright and license information, see the COPYRIGHT and LICENSE
* files found in the top-level directory of this distribution.
*/
use xPDO\Om\xPDOCriteria;
use xPDO\xPDO;
/**
* Represents an element of source content managed by MODX.
*
* These elements are defined by some type of source content that when processed
* will provide output or some type of logical result based on mutable
* properties.
*
* This class creates an instance of a modElement object. This should not be
* called directly, but rather extended for derivative modElement classes.
*
* @property modMediaSource $Source The associated Media Source, if any.
*
* @package modx
* @abstract Implement a derivative of this class to represent an element which
* can be processed within the MODX framework.
* @extends modAccessibleSimpleObject
*/
class modElement extends modAccessibleSimpleObject {
/**
* The property value array for the element.
* @var array
*/
public $_properties= null;
/**
* The string representation of the element properties.
* @var string
*/
public $_propertyString= '';
/**
* The source content of the element.
* @var string
*/
public $_content= '';
/**
* The source of the element.
* @var string
*/
public $_source= null;
/**
* The output of the element.
* @var string
*/
public $_output= '';
/**
* The boolean result of the element.
*
* This is typically only applicable to elements that use PHP source content.
* @var boolean
*/
public $_result= true;
/**
* The tag signature of the element instance.
* @var string
*/
public $_tag= null;
/**
* The character token which helps identify the element class in tag string.
* @var string
*/
public $_token= '';
/**
* @var boolean If the element is cacheable or not.
*/
public $_cacheable= true;
/**
* @var boolean Indicates if the element was processed already.
*/
public $_processed= false;
/**
* @var array Optional filters that can be used during processing.
*/
public $_filters= array('input' => null, 'output' => null);
/**
* @var string Path to source file location when modElement->isStatic() === true.
*/
protected $_sourcePath= "";
/**
* @var string Source file name when modElement->isStatic() === true.
*/
protected $_sourceFile= "";
/**
* @var array A list of invalid characters in the name of an Element.
*/
protected $_invalidCharacters = array('!','@','#','$','%','^','&','*',
'(',')','+','=','[',']','{','}','\'','"',';',':','\\','/','<','>','?'
,' ',',','`','~');
/**
* Provides custom handling for retrieving the properties field of an Element.
*
* {@inheritdoc}
*/
public function get($k, $format= null, $formatTemplate= null) {
$value = parent :: get($k, $format, $formatTemplate);
if ($k === 'properties' && $this->xpdo instanceof modX && $this->xpdo->getParser() && empty($value)) {
$value = !empty($this->properties) && is_string($this->properties)
? $this->xpdo->parser->parsePropertyString($this->properties)
: null;
}
/* automatically translate lexicon descriptions */
if ($k == 'properties' && !empty($value) && is_array($value)
&& is_object($this->xpdo) && $this->xpdo instanceof modX && $this->xpdo->lexicon) {
foreach ($value as &$property) {
if (!empty($property['lexicon'])) {
if (strpos($property['lexicon'],':') !== false) {
$this->xpdo->lexicon->load('en:'.$property['lexicon']);
} else {
$this->xpdo->lexicon->load('en:core:'.$property['lexicon']);
}
$this->xpdo->lexicon->load($property['lexicon']);
}
$property['desc_trans'] = $this->xpdo->lexicon($property['desc']);
$property['area'] = !empty($property['area']) ? $property['area'] : '';
$property['area_trans'] = !empty($property['area']) ? $this->xpdo->lexicon($property['area']) : '';
if (!empty($property['options'])) {
foreach ($property['options'] as &$option) {
if (empty($option['text']) && !empty($option['name'])) {
$option['text'] = $option['name'];
unset($option['name']);
}
if (empty($option['value']) && !empty($option[0])) {
$option['value'] = $option[0];
unset($option[0]);
}
$option['name'] = $this->xpdo->lexicon($option['text']);
}
}
}
}
return $value;
}
/**
* Overridden to handle changes to content managed in an external file.
*
* {@inheritdoc}
*/
public function save($cacheFlag = null) {
if (!$this->getOption(xPDO::OPT_SETUP)) {
if ($this->staticSourceChanged()) {
$staticContent = $this->getFileContent();
if ($staticContent !== $this->get('content')) {
if ($this->isStaticSourceMutable() && $staticContent === '') {
$this->setDirty('content');
} else {
$this->setContent($staticContent);
}
}
unset($staticContent);
}
$staticContentChanged = $this->staticContentChanged();
if ($staticContentChanged && !$this->isStaticSourceMutable()) {
$this->setContent($this->getFileContent());
$staticContentChanged = false;
}
/* If element is empty, set to true in order to create an empty static file. */
$content = $this->get('content');
if (empty($content) && $this->isStatic()) {
$staticContentChanged = true;
}
}
/* Set oldPath before saving object. */
$oldPath = $this->getOldStaticFilePath();
$saved = parent::save($cacheFlag);
if (!$this->getOption(xPDO::OPT_SETUP)) {
if ($saved && $staticContentChanged) {
$saved = $this->setFileContent($this->get('content'));
}
}
/* Removing old static file when succesfull saved and oldPath has been set. */
if ($saved && $oldPath && $this->isStaticFilesAutomated()) {
if (@unlink($oldPath)) {
$pathinfo = pathinfo($oldPath);
$this->cleanupStaticFileDirectories($pathinfo['dirname']);
}
}
return $saved;
}
/**
* Determine if static files should be automated for current element class.
*
* @return bool
*/
protected function isStaticFilesAutomated()
{
$elements = array(
'modTemplate' => 'templates',
'modTemplateVar' => 'tvs',
'modChunk' => 'chunks',
'modSnippet' => 'snippets',
'modPlugin' => 'plugins'
);
if (!array_key_exists($this->_class, $elements)) {
return false;
}
return (bool) $this->xpdo->getOption('static_elements_automate_' . $elements[$this->_class], null, false);
}
/**
* Constructs a valid tag representation of the element.
*
* @return string A tag representation of the element.
*/
public function getTag() {
if (empty($this->_tag)) {
$propTemp = array();
if (empty($this->_propertyString) && !empty($this->_properties)) {
foreach ($this->_properties as $key => $value) {
if (is_scalar($value)) {
$propTemp[] = trim($key) . '=`' . $value . '`';
}
else {
$propTemp[] = trim($key) . '=`' . md5(uniqid(rand(), true)) . '`';
}
}
if (!empty($propTemp)) {
$this->_propertyString = '?' . implode('&', $propTemp);
}
}
$tag = '[[';
$tag.= $this->getToken();
$tag.= $this->get('name');
if (!empty($this->_propertyString)) {
$tag.= $this->_propertyString;
}
$tag.= ']]';
$this->setTag($tag);
}
if (empty($this->_tag)) {
$this->xpdo->log(xPDO::LOG_LEVEL_ERROR, 'Instance of ' . get_class($this) . ' produced an empty tag!');
}
return $this->_tag;
}
/**
* Accessor method for the token class var.
*
* @return string The token for this element tag.
*/
public function getToken() {
return $this->_token;
}
/**
* Setter method for the token class var.
*
* @param string $token The token to use for this element tag.
*/
public function setToken($token) {
$this->_token = $token;
}
/**
* Setter method for the tag class var.
*
* @param string $tag The tag to use for this element.
*/
public function setTag($tag) {
$this->_tag = $tag;
}
/**
* Process the element source content to produce a result.
*
* @abstract Implement this to define behavior for a MODX content element.
* @param array|string $properties A set of configuration properties for the
* element.
* @param string $content Optional content to use in place of any persistent
* content associated with the element.
* @return mixed The result of processing.
*/
public function process($properties= null, $content= null) {
$this->xpdo->getParser();
$this->xpdo->parser->setProcessingElement(true);
$this->getProperties($properties);
$this->getTag();
if ($this->xpdo->getDebug() === true) $this->xpdo->log(xPDO::LOG_LEVEL_DEBUG, "Processing Element: " . $this->get('name') . ($this->_tag ? "\nTag: {$this->_tag}" : "\n") . "\nProperties: " . print_r($this->_properties, true));
if ($this->isCacheable() && isset ($this->xpdo->elementCache[$this->_tag])) {
$this->_output = $this->xpdo->elementCache[$this->_tag];
$this->_processed = true;
} else {
$this->filterInput();
$this->getContent(is_string($content) ? array('content' => $content) : array());
}
return $this->_result;
}
/**
* Cache the current output of this element instance by tag signature.
*/
public function cache() {
if ($this->isCacheable()) {
$this->xpdo->elementCache[$this->_tag]= $this->_output;
}
}
/**
* Get an input filter instance configured for this Element.
*
* @return modInputFilter|null An input filter instance (or null if one cannot be loaded).
*/
public function & getInputFilter() {
if (!isset ($this->_filters['input']) || !($this->_filters['input'] instanceof modInputFilter)) {
if (!$inputFilterClass= $this->get('input_filter')) {
$inputFilterClass = $this->xpdo->getOption('input_filter',null,'filters.modInputFilter');
}
if ($filterClass= $this->xpdo->loadClass($inputFilterClass, '', false, true)) {
if ($filter= new $filterClass($this->xpdo)) {
$this->_filters['input']= $filter;
}
}
}
return $this->_filters['input'];
}
/**
* Get an output filter instance configured for this Element.
*
* @return modOutputFilter|null An output filter instance (or null if one cannot be loaded).
*/
public function & getOutputFilter() {
if (!isset ($this->_filters['output']) || !($this->_filters['output'] instanceof modOutputFilter)) {
if (!$outputFilterClass= $this->get('output_filter')) {
$outputFilterClass = $this->xpdo->getOption('output_filter',null,'filters.modOutputFilter');
}
if ($filterClass= $this->xpdo->loadClass($outputFilterClass, '', false, true)) {
if ($filter= new $filterClass($this->xpdo)) {
$this->_filters['output']= $filter;
}
}
}
return $this->_filters['output'];
}
/**
* Apply an input filter to an element.
*
* This is called by default in {@link modElement::process()} after the
* element properties have been parsed.
*/
public function filterInput() {
$filter = $this->getInputFilter();
if ($filter !== null && $filter instanceof modInputFilter) {
$filter->filter($this);
}
}
/**
* Apply an output filter to an element.
*
* Call this method in your {modElement::process()} implementation when it
* is appropriate, typically once all processing has been completed, but
* before any caching takes place.
*/
public function filterOutput() {
$filter = $this->getOutputFilter();
if ($filter !== null && $filter instanceof modOutputFilter) {
$filter->filter($this);
}
}
/**
* Loads the access control policies applicable to this element.
*
* {@inheritdoc}
*/
public function findPolicy($context = '') {
$policy = array();
$enabled = true;
$context = !empty($context) ? $context : $this->xpdo->context->get('key');
if ($context === $this->xpdo->context->get('key')) {
$enabled = (boolean) $this->xpdo->getOption('access_category_enabled', null, true);
} elseif ($this->xpdo->getContext($context)) {
$enabled = (boolean) $this->xpdo->contexts[$context]->getOption('access_category_enabled', true);
}
if ($enabled) {
if (empty($this->_policies) || !isset($this->_policies[$context])) {
$accessTable = $this->xpdo->getTableName('modAccessCategory');
$policyTable = $this->xpdo->getTableName('modAccessPolicy');
$categoryClosureTable = $this->xpdo->getTableName('modCategoryClosure');
$sql = "SELECT Acl.target, Acl.principal, Acl.authority, Acl.policy, Policy.data FROM {$accessTable} Acl " .
"LEFT JOIN {$policyTable} Policy ON Policy.id = Acl.policy " .
"JOIN {$categoryClosureTable} CategoryClosure ON CategoryClosure.descendant = :category " .
"AND Acl.principal_class = 'modUserGroup' " .
"AND CategoryClosure.ancestor = Acl.target " .
"AND (Acl.context_key = :context OR Acl.context_key IS NULL OR Acl.context_key = '') " .
"ORDER BY CategoryClosure.depth DESC, target, principal, authority ASC";
$bindings = array(
':category' => $this->get('category'),
':context' => $context,
);
$query = new xPDOCriteria($this->xpdo, $sql, $bindings);
if ($query->stmt && $query->stmt->execute()) {
while ($row = $query->stmt->fetch(PDO::FETCH_ASSOC)) {
$policy['modAccessCategory'][$row['target']][] = array(
'principal' => $row['principal'],
'authority' => $row['authority'],
'policy' => $row['data'] ? $this->xpdo->fromJSON($row['data'], true) : array(),
);
}
}
$this->_policies[$context] = $policy;
} else {
$policy = $this->_policies[$context];
}
}
return $policy;
}
/**
* Gets the raw, unprocessed source content for this element.
*
* @param array $options An array of options implementations can use to
* accept language, revision identifiers, or other information to alter the
* behavior of the method.
* @return string The raw source content for the element.
*/
public function getContent(array $options = array()) {
if (!is_string($this->_content) || $this->_content === '') {
if (isset($options['content'])) {
$this->_content = $options['content'];
} elseif ($this->isStatic()) {
$this->_content = $this->getFileContent($options);
if ($this->_content !== $this->_fields['content']) {
$this->setContent($this->_content);
if (!$this->isNew()) {
$this->save();
}
}
} else {
$this->_content = $this->get('content');
}
}
return $this->_content;
}
/**
* Set the raw source content for this element.
*
* @param mixed $content The source content; implementations can decide if
* it can only be a string, or some other source from which to retrieve it.
* @param array $options An array of options implementations can use to
* accept language, revision identifiers, or other information to alter the
* behavior of the method.
* @return boolean True indicates the content was set.
*/
public function setContent($content, array $options = array()) {
return $this->set('content', $content);
}
/**
* Get the absolute path to the static source file for this instance.
*
* @param array $options An array of options.
* @return string|boolean The absolute path to the static source file or false if not static.
*/
public function getSourceFile(array $options = [])
{
if ($this->isStatic() && (empty($this->_sourceFile) || $this->getOption('recalculate_source_file', $options, $this->staticSourceChanged()))) {
$filename = $this->get('static_file');
if (!empty($filename)) {
$array = [];
if ($this->xpdo->getParser() && $this->xpdo->parser->collectElementTags($filename, $array)) {
$this->xpdo->parser->processElementTags('', $filename);
}
}
/** @var modMediaSource $source */
if ($source = $this->getSource()) {
$sourceBasePath = $source->getBasePath();
$checkFilename = strpos($filename, $sourceBasePath) === 0 ? str_replace($sourceBasePath, '', $filename) : $filename;
$metaData = $source->getMetaData($checkFilename);
$this->_sourceFile = empty($metaData) && $this->get('source') < 1
? $this->getSourcePath($options) . $filename
: $filename;
} else {
return false;
}
}
return $this->isStatic() ? $this->_sourceFile : false;
}
/**
* Get the absolute path location the source file is located relative to.
*
* @param array $options An array of options.
* @return string The path to file.
*/
public function getSourcePath(array $options = [])
{
$array = [];
$this->_sourcePath = $this->xpdo->getOption('element_static_path', $options, $this->xpdo->getOption('components_path', $options, 'components/'));
if ($this->xpdo->getParser() && $this->xpdo->parser->collectElementTags($this->_sourcePath, $array)) {
$this->xpdo->parser->processElementTags('', $this->_sourcePath);
}
return str_replace(MODX_CORE_PATH, '', $this->_sourcePath);
}
/**
* Get the content stored in an external file for this instance.
*
* @param array $options An array of options.
* @return bool|string The content or false if the content could not be retrieved.
*/
public function getFileContent(array $options = [])
{
$content = false;
if ($this->isStatic() && $sourceFile = $this->getSourceFile($options)) {
if ($source = $this->getSource()) {
if ($file = $source->getObjectContents($sourceFile)) {
return $file['content'];
}
}
}
return $content;
}
/**
* Set external file content from this instance.
*
* @param string $content The content to set.
* @param array $options An array of options.
* @return bool
*/
public function setFileContent($content, array $options = [])
{
$set = false;
if ($this->isStatic() && $sourceFile = $this->getSourceFile($options)) {
if ($source = $this->getSource()) {
if ($source->getMetaData($sourceFile)) {
$set = (bool)$source->updateObject($sourceFile, $content);
} else {
$path = explode(DIRECTORY_SEPARATOR, trim($sourceFile, DIRECTORY_SEPARATOR));
$file = array_pop($path);
$set = (bool)$source->createObject(implode(DIRECTORY_SEPARATOR, $path), $file, $content);
}
}
}
return $set;
}
/**
* Get the properties for this element instance for processing.
*
* @param array|string $properties An array or string of properties to
* apply.
* @return array A simple array of properties ready to use for processing.
*/
public function getProperties($properties = null) {
$this->xpdo->getParser();
$this->_properties= $this->xpdo->parser->parseProperties($this->get('properties'));
$set= $this->getPropertySet();
if (!empty($set)) {
$this->_properties= array_merge($this->_properties, $set);
}
if ($this->get('property_preprocess')) {
foreach ($this->_properties as $pKey => $pValue) {
if ($this->xpdo->parser->processElementTags('', $pValue, $this->xpdo->parser->isProcessingUncacheable())) {
$this->_properties[$pKey]= $pValue;
}
}
}
if (!empty($properties)) {
$this->_properties= array_merge($this->_properties, $this->xpdo->parser->parseProperties($properties));
}
return $this->_properties;
}
/**
* Gets a named property set related to this element instance.
*
* If a setName parameter is not provided, this function will attempt to
* extract a setName from the element name using the @ symbol to delimit the
* name of the property set.
*
* Here is an example of an element tag using the @ modifier to specify a
* property set name:
* [[ElementName@PropertySetName:FilterCommand=`FilterModifier`?
* &PropertyKey1=`PropertyValue1`
* &PropertyKey2=`PropertyValue2`
* ]]
*
* @access public
* @param string|null $setName An explicit property set name to search for.
* @return array|null An array of properties or null if no set is found.
*/
public function getPropertySet($setName = null) {
$propertySet= null;
$name = $this->get('name');
if (strpos($name, '@') !== false) {
$psName= '';
$split= xPDO :: escSplit('@', $name);
if ($split && isset($split[1])) {
$name= $split[0];
$psName= $split[1];
$filters= xPDO :: escSplit(':', $setName);
if ($filters && isset($filters[1]) && !empty($filters[1])) {
$psName= $filters[0];
$name.= ':' . $filters[1];
}
$this->set('name', $name);
}
if (!empty($psName)) {
$psObj= $this->xpdo->getObjectGraph('modPropertySet', '{"Elements":{}}', array(
'Elements.element' => $this->id,
'Elements.element_class' => $this->_class,
'modPropertySet.name' => $psName
));
if ($psObj) {
$propertySet= $this->xpdo->parser->parseProperties($psObj->get('properties'));
}
}
}
if (!empty($setName)) {
$propertySetObj= $this->xpdo->getObjectGraph('modPropertySet', '{"Elements":{}}', array(
'Elements.element' => $this->id,
'Elements.element_class' => $this->_class,
'modPropertySet.name' => $setName
));
if ($propertySetObj) {
if (is_array($propertySet)) {
$propertySet= array_merge($propertySet, $this->xpdo->parser->parseProperties($propertySetObj->get('properties')));
} else {
$propertySet= $this->xpdo->parser->parseProperties($propertySetObj->get('properties'));
}
}
}
return $propertySet;
}
/**
* Set default properties for this element instance.
*
* @access public
* @param array|string $properties A property array or property string.
* @param boolean $merge Indicates if properties should be merged with
* existing ones.
* @return boolean true if the properties are set.
*/
public function setProperties($properties, $merge = false) {
$set = false;
$propertiesArray = array();
if (is_string($properties)) {
$properties = $this->xpdo->parser->parsePropertyString($properties);
}
if (is_array($properties)) {
foreach ($properties as $propKey => $property) {
if (is_array($property) && isset($property[5])) {
$key = $property[0];
$propertyArray = array(
'name' => $property[0],
'desc' => $property[1],
'type' => $property[2],
'options' => $property[3],
'value' => $property[4],
'lexicon' => !empty($property[5]) ? $property[5] : null,
'area' => !empty($property[6]) ? $property[6] : '',
);
} elseif (is_array($property) && isset($property['value'])) {
$key = $property['name'];
$propertyArray = array(
'name' => $property['name'],
'desc' => isset($property['description']) ? $property['description'] : (isset($property['desc']) ? $property['desc'] : ''),
'type' => isset($property['xtype']) ? $property['xtype'] : (isset($property['type']) ? $property['type'] : 'textfield'),
'options' => isset($property['options']) ? $property['options'] : array(),
'value' => $property['value'],
'lexicon' => !empty($property['lexicon']) ? $property['lexicon'] : null,
'area' => !empty($property['area']) ? $property['area'] : '',
);
} else {
$key = $propKey;
$propertyArray = array(
'name' => $propKey,
'desc' => '',
'type' => 'textfield',
'options' => array(),
'value' => $property,
'lexicon' => null,
'area' => '',
);
}
if (!empty($propertyArray['options'])) {
foreach ($propertyArray['options'] as $optionKey => &$option) {
if (empty($option['text']) && !empty($option['name'])) $option['text'] = $option['name'];
unset($option['menu'],$option['name']);
}
}
if ($propertyArray['type'] == 'combo-boolean' && is_numeric($propertyArray['value'])) {
$propertyArray['value'] = (boolean)$propertyArray['value'];
}
$propertiesArray[$key] = $propertyArray;
}
if ($merge && !empty($propertiesArray)) {
$existing = $this->get('properties');
if (is_array($existing) && !empty($existing)) {
$propertiesArray = array_merge($existing, $propertiesArray);
}
}
$set = $this->set('properties', $propertiesArray);
}
return $set;
}
/**
* Add a property set to this element, making it available for use.
*
* @access public
* @param string|modPropertySet $propertySet A modPropertySet object or the
* name of a modPropertySet object to create a relationship with.
* @return boolean True if a relationship was created or already exists.
*/
public function addPropertySet($propertySet) {
$added= false;
if (!empty($propertySet)) {
if (is_string($propertySet)) {
$propertySet = $this->xpdo->getObject('modPropertySet', array('name' => $propertySet));
}
if (is_object($propertySet) && $propertySet instanceof modPropertySet) {
if (!$this->isNew() && !$propertySet->isNew() && $this->xpdo->getCount('modElementPropertySet', array('element' => $this->get('id'), 'element_class' => $this->_class, 'property_set' => $propertySet->get('id')))) {
$added = true;
} else {
if ($propertySet->isNew()) $propertySet->save();
/** @var modElementPropertySet $link */
$link= $this->xpdo->newObject('modElementPropertySet');
$link->set('element', $this->get('id'));
$link->set('element_class', $this->_class);
$link->set('property_set', $propertySet->get('id'));
$added = $link->save();
}
}
}
return $added;
}
/**
* Remove a property set from this element, making it unavailable for use.
*
* @access public
* @param string|modPropertySet $propertySet A modPropertySet object or the
* name of a modPropertySet object to dissociate from.
* @return boolean True if a relationship was destroyed.
*/
public function removePropertySet($propertySet) {
$removed = false;
if (!empty($propertySet)) {
if (is_string($propertySet)) {
$propertySet = $this->xpdo->getObject('modPropertySet', array('name' => $propertySet));
}
if (is_object($propertySet) && $propertySet instanceof modPropertySet) {
$removed = $this->xpdo->removeObject('modElementPropertySet', array('element' => $this->get('id'), 'element_class' => $this->_class, 'property_set' => $propertySet->get('id')));
}
}
return $removed;
}
/**
* Indicates if the element is cacheable.
*
* @access public
* @return boolean True if the element can be stored to or retrieved from
* the element cache.
*/
public function isCacheable() {
return $this->_cacheable;
}
/**
* Sets the runtime cacheability of the element.
*
* @access public
* @param boolean $cacheable Indicates the value to set for cacheability of
* this element.
*/
public function setCacheable($cacheable = true) {
$this->_cacheable = (boolean) $cacheable;
}
/**
* Get the Source for this Element
*
* @param string $contextKey
* @param boolean $fallbackToDefault
* @return modMediaSource|null
*/
public function getSource($contextKey = '',$fallbackToDefault = true) {
if (empty($contextKey)) {
$contextKey = $this->xpdo->context->get('key');
}
$source = $this->_source;
if (empty($source)) {
$c = $this->xpdo->newQuery('sources.modMediaSource');
$c->innerJoin('sources.modMediaSourceElement','SourceElement');
$c->where(array(
'SourceElement.object' => $this->get('id'),
'SourceElement.object_class' => $this->_class,
'SourceElement.context_key' => $contextKey,
));
$source = $this->xpdo->getObject('sources.modMediaSource',$c);
if (!$source && $fallbackToDefault) {
$source = modMediaSource::getDefaultSource($this->xpdo, $this->get('source'));
}
if ($source) {
$this->setSource($source);
}
}
return $source;
}
/**
* Setter method for the source class var.
*
* @param modMediaSourceInterface $source The source to use for this element.
*/
public function setSource(modMediaSourceInterface $source) {
$source->initialize();
$this->_source = $source;
}
/**
* Get the stored sourceCache for a context
*
* @param string $contextKey
* @param array $options
* @return array
*/
public function getSourceCache($contextKey = '',array $options = array()) {
/** @var modCacheManager $cacheManager */
$cacheManager = $this->xpdo->getCacheManager();
if (!$cacheManager || !($cacheManager instanceof modCacheManager)) return array();
if (empty($contextKey)) $contextKey = $this->xpdo->context->get('key');
return $cacheManager->getElementMediaSourceCache($this,$contextKey,$options);
}
/**
* Indicates if the instance has content in an external file.
*
* @return boolean True if the instance has content stored in an external file.
*/
public function isStatic() {
return $this->get('static');
}
/**
* Indicates if the content has changed and the Element has a mutable static source.
*
* @return boolean
*/
public function staticContentChanged() {
return $this->isStatic() && $this->isDirty('content');
}
/**
* Check if directories are empty after moving a static element and remove empty directories.
*
* @param $dirname
*/
public function cleanupStaticFileDirectories($dirname) {
$contents = array_diff(scandir($dirname), array('..', '.', '.DS_Store'));
@unlink($dirname .'/.DS_Store');
if (count($contents) === 0) {
if (is_dir($dirname)) {
if (rmdir($dirname)) {
/* Check if parent directory is also empty. */
$this->cleanupStaticFileDirectories(dirname($dirname));
}
}
}
}
/**
* Returns static file path if the file path or source has changed.
*/
public function getOldStaticFilePath() {
$oldFilePath = '';
$sourceId = 0;
$result = $this->xpdo->getObject($this->_class, array('id' => $this->_fields['id']));
if ($result) {
$staticFilePath = $result->get('static_file');
$sourceId = $result->get('source');
if ($staticFilePath !== $this->_fields['static_file'] || $sourceId !== $this->_fields['source']) {
$oldFilePath = $staticFilePath;
}
}
if (!empty($oldFilePath)) {
if ($sourceId > 0) {
/** @var modMediaSource $source */
$source = $this->xpdo->getObject('sources.modFileMediaSource', array('id' => $sourceId));
if ($source && $source->get('is_stream')) {
$source->initialize();
$oldFilePath = $source->getBasePath() . $oldFilePath;
}
}
if (!file_exists($oldFilePath) && $this->get('source') < 1) {
$this->getSourcePath();
$oldFilePath = $this->_sourcePath . $oldFilePath;
}
}
return $oldFilePath;
}
/**
* Indicates if the static source has changed.
*
* @return boolean
*/
public function staticSourceChanged() {
return $this->isStatic() && ($this->isDirty('static') || $this->isDirty('static_file') || $this->isDirty('source'));
}
/**
* Return if the static source is mutable.
*
* @return boolean True if the source file is mutable.
*/
public function isStaticSourceMutable()
{
$isMutable = false;
$sourceFile = $this->getSourceFile();
if ($sourceFile && $source = $this->getSource()) {
if (!$isMutable = (bool)$source->getMetaData($sourceFile)) {
$path = explode(DIRECTORY_SEPARATOR, trim($sourceFile, DIRECTORY_SEPARATOR));
$file = array_pop($path);
$isMutable = (bool)$source->createObject(implode(DIRECTORY_SEPARATOR, $path), $file, '');
}
}
return $isMutable;
}
/**
* Ensure the static source cannot browse the protected configuration directory
*
* @return boolean True if is a valid source path
*/
public function isStaticSourceValidPath() {
$isValid = true;
$sourceFile = $this->getSourceFile();
if ($sourceFile) {
$sourceDirectory = rtrim(dirname($sourceFile),'/');
$configDirectory = rtrim($this->xpdo->getOption('core_path',null,MODX_CORE_PATH).'config/','/');
if ($sourceDirectory == $configDirectory) {
$isValid = false;
}
}
return $isValid;
}
}