Resource::getFieldHidemenu()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jgulledge
5
 * Date: 10/6/2017
6
 * Time: 9:01 AM
7
 */
8
9
namespace LCI\Blend\Blendable;
10
use LCI\Blend\Blender;
11
12
/**
13
 * Class Resource
14
 * @package LCI\Blend\Blendable
15
 */
16
class Resource extends Blendable
17
{
18
    use BlendableProperties;
19
20
    /** @var string  */
21
    protected $opt_cache_key = 'resources/web';
22
23
    /** @var string ex: modResource */
24
    protected $xpdo_simple_object_class = 'modResource';
25
26
    /** @var string  */
27
    protected $unique_key_column = 'alias';
28
29
    /** @var array ~ this should match data to be inserted via xPDO, ex [column_name => value, ...] */
30
    protected $blendable_xpdo_simple_object_data = [
31
        'alias' => '',
32
        'alias_visible' => true,
33
        'cacheable' => true,
34
        'class_key' => 'modDocument',
35
        'content' => '',
36
        'contentType' => 'text/html',
37
        'content_dispo' => false,
38
        'context_key' => 'web',
39
        'createdby' => 0,
40
        'deleted' => false,
41
        'description' => '',
42
        'donthit' => false,
43
        'hidemenu' => false,
44
        'hide_children_in_tree' => false,
45
        'introtext' => '',
46
        'isfolder' => false,
47
        'link_attributes' => '',
48
        'longtitle' => '',
49
        'menuindex' => 0,
50
        'menutitle' => '',
51
        'pagetitle' => '',
52
        'parent' => 0,
53
        'privatemgr' => false,
54
        'privateweb' => false,
55
        'properties' => '',
56
        'published' => false,
57
        'publishedby' => 0,
58
        'publishedon' => 0,
59
        'pub_date' => 0,
60
        'richtext' => true,
61
        'searchable' => true,
62
        'show_in_tree' => true,
63
        'template' => 0,
64
        'type' => 'document',
65
        'unpub_date' => 0,
66
        'uri' => '',
67
        'uri_override' => false,
68
    ];
69
70
    /** @var array ~ ['setMethodName' => 'setMethodActualName', 'setDoNotUseMethod' => false] overwrite in child classes */
71
    protected $load_from_array_aliases = [
72
        'setProperties' => 'mergePropertiesFromArray'
73
    ];
74
75
    /** @var array  */
76
    protected $portable_template_variables = [];
77
78
    /**
79
     * Resource constructor.
80
     *
81
     * @param \modx $modx
82
     * @param Blender $blender
83
     * @param string $alias ~ alias as string
84
     * @param string $context ~ will default to web context
85
     */
86
    public function __construct(\modx $modx, Blender $blender, $alias, $context = 'web')
0 ignored issues
show
Bug introduced by
The type modx was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
87
    {
88
        $this->setFieldAlias($alias);
89
        $this->setFieldContextKey($context);
90
        parent::__construct($modx, $blender, ['alias' => $alias, 'context_key' => $context]);
91
92
        $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.mediaSources'));
93
        if (count($additional) > 0) {
94
            foreach ($additional as $tv_name) {
95
                $this->portable_template_variables[$tv_name] = 'media_source';
96
            }
97
        }
98
99
        $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.resources'));
100
        if (count($additional) > 0) {
101
            foreach ($additional as $tv_name) {
102
                $this->portable_template_variables[$tv_name] = 'resource';
103
            }
104
        }
105
106
        $additional = explode(',', $this->modx->getOption('blend.portable.templateVariables.templates'));
107
        if (count($additional) > 0) {
108
            foreach ($additional as $tv_name) {
109
                $this->portable_template_variables[$tv_name] = 'template';
110
            }
111
        }
112
    }
113
114
115
    // Column Getters:
116
    /**
117
     * @return string
118
     */
119
    public function getFieldAlias()
120
    {
121
        return $this->blendable_xpdo_simple_object_data['alias'];
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function getFieldAliasVisible()
128
    {
129
        return $this->blendable_xpdo_simple_object_data['alias_visible'];
130
    }
131
132
    /**
133
     * @return bool
134
     */
135
    public function getFieldCacheable()
136
    {
137
        return $this->blendable_xpdo_simple_object_data['cacheable'];
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getFieldClassKey()
144
    {
145
        return $this->blendable_xpdo_simple_object_data['class_key'];
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getFieldContent()
152
    {
153
        return $this->blendable_xpdo_simple_object_data['content'];
154
    }
155
156
    /**
157
     * @return string
158
     */
159
    public function getFieldContentType()
160
    {
161
        return $this->blendable_xpdo_simple_object_data['contentType'];
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    public function getFieldContentDispo()
168
    {
169
        return $this->blendable_xpdo_simple_object_data['content_dispo'];
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    public function getFieldContextKey()
176
    {
177
        return $this->blendable_xpdo_simple_object_data['context_key'];
178
    }
179
180
    /**
181
     * @return int
182
     */
183
    public function getFieldCreatedby()
184
    {
185
        return $this->blendable_xpdo_simple_object_data['createdby'];
186
    }
187
188
    /**
189
     * @return bool
190
     */
191
    public function getFieldDeleted()
192
    {
193
        return $this->blendable_xpdo_simple_object_data['deleted'];
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function getFieldDescription()
200
    {
201
        return $this->blendable_xpdo_simple_object_data['description'];
202
    }
203
204
    /**
205
     * @return bool
206
     */
207
    public function getFieldDonthit()
208
    {
209
        return $this->blendable_xpdo_simple_object_data['donthit'];
210
    }
211
212
    /**
213
     * @return bool
214
     */
215
    public function getFieldHidemenu()
216
    {
217
        return $this->blendable_xpdo_simple_object_data['hidemenu'];
218
    }
219
220
    /**
221
     * @return bool
222
     */
223
    public function getFieldHideChildrenInTree()
224
    {
225
        return $this->blendable_xpdo_simple_object_data['hide_children_in_tree'];
226
    }
227
228
    /**
229
     * @return string
230
     */
231
    public function getFieldIntrotext()
232
    {
233
        return $this->blendable_xpdo_simple_object_data['introtext'];
234
    }
235
236
    /**
237
     * @return bool
238
     */
239
    public function getFieldIsfolder()
240
    {
241
        return $this->blendable_xpdo_simple_object_data['isfolder'];
242
    }
243
244
    /**
245
     * @return string
246
     */
247
    public function getFieldLinkAttributes()
248
    {
249
        return $this->blendable_xpdo_simple_object_data['link_attributes'];
250
    }
251
252
    /**
253
     * @return string
254
     */
255
    public function getFieldLongtitle()
256
    {
257
        return $this->blendable_xpdo_simple_object_data['longtitle'];
258
    }
259
260
    /**
261
     * @return int
262
     */
263
    public function getFieldMenuindex()
264
    {
265
        return $this->blendable_xpdo_simple_object_data['menuindex'];
266
    }
267
268
    /**
269
     * @return string
270
     */
271
    public function getFieldMenutitle()
272
    {
273
        return $this->blendable_xpdo_simple_object_data['menutitle'];
274
    }
275
276
    /**
277
     * @return string
278
     */
279
    public function getFieldPagetitle()
280
    {
281
        return $this->blendable_xpdo_simple_object_data['pagetitle'];
282
    }
283
284
    /**
285
     * @return int
286
     */
287
    public function getFieldParent()
288
    {
289
        return $this->blendable_xpdo_simple_object_data['parent'];
290
    }
291
292
    /**
293
     * @return bool
294
     */
295
    public function getFieldPrivatemgr()
296
    {
297
        return $this->blendable_xpdo_simple_object_data['privatemgr'];
298
    }
299
300
    /**
301
     * @return bool
302
     */
303
    public function getFieldPrivateweb()
304
    {
305
        return $this->blendable_xpdo_simple_object_data['privateweb'];
306
    }
307
308
    /**
309
     * @return string
310
     */
311
    public function getFieldProperties()
312
    {
313
        return $this->blendable_xpdo_simple_object_data['properties'];
314
    }
315
316
    /**
317
     * @return bool
318
     */
319
    public function getFieldPublished()
320
    {
321
        return $this->blendable_xpdo_simple_object_data['published'];
322
    }
323
324
    /**
325
     * @return int
326
     */
327
    public function getFieldPublishedby()
328
    {
329
        return $this->blendable_xpdo_simple_object_data['publishedby'];
330
    }
331
332
    /**
333
     * @return int
334
     */
335
    public function getFieldPublishedon()
336
    {
337
        return $this->blendable_xpdo_simple_object_data['publishedon'];
338
    }
339
340
    /**
341
     * @return int
342
     */
343
    public function getFieldPubDate()
344
    {
345
        return $this->blendable_xpdo_simple_object_data['pub_date'];
346
    }
347
348
    /**
349
     * @return bool
350
     */
351
    public function getFieldRichtext()
352
    {
353
        return $this->blendable_xpdo_simple_object_data['richtext'];
354
    }
355
356
    /**
357
     * @return bool
358
     */
359
    public function getFieldSearchable()
360
    {
361
        return $this->blendable_xpdo_simple_object_data['searchable'];
362
    }
363
364
    /**
365
     * @return bool
366
     */
367
    public function getFieldShowInTree()
368
    {
369
        return $this->blendable_xpdo_simple_object_data['show_in_tree'];
370
    }
371
372
    /**
373
     * @return int
374
     */
375
    public function getFieldTemplate()
376
    {
377
        return $this->blendable_xpdo_simple_object_data['template'];
378
    }
379
380
    /**
381
     * @return string
382
     */
383
    public function getFieldType()
384
    {
385
        return $this->blendable_xpdo_simple_object_data['type'];
386
    }
387
388
    /**
389
     * @return int
390
     */
391
    public function getFieldUnpubDate()
392
    {
393
        return $this->blendable_xpdo_simple_object_data['unpub_date'];
394
    }
395
396
    /**
397
     * @return string
398
     */
399
    public function getFieldUri()
400
    {
401
        return $this->blendable_xpdo_simple_object_data['uri'];
402
    }
403
404
    /**
405
     * @return bool
406
     */
407
    public function getFieldUriOverride()
408
    {
409
        return $this->blendable_xpdo_simple_object_data['uri_override'];
410
    }
411
412
    // Column Setters:
413
    /**
414
     * @param string $value  max characters: 191
415
     * @return $this
416
     */
417
    public function setFieldAlias($value)
418
    {
419
        $this->blendable_xpdo_simple_object_data['alias'] = $value;
420
        return $this;
421
    }
422
423
    /**
424
     * @param bool $value
425
     * @return $this
426
     */
427
    public function setFieldAliasVisible($value)
428
    {
429
        $this->blendable_xpdo_simple_object_data['alias_visible'] = $value;
430
        return $this;
431
    }
432
433
    /**
434
     * @param bool $value
435
     * @return $this
436
     */
437
    public function setFieldCacheable($value)
438
    {
439
        $this->blendable_xpdo_simple_object_data['cacheable'] = $value;
440
        return $this;
441
    }
442
443
    /**
444
     * @param string $value  max characters: 100
445
     * @return $this
446
     */
447
    public function setFieldClassKey($value)
448
    {
449
        $this->blendable_xpdo_simple_object_data['class_key'] = $value;
450
        return $this;
451
    }
452
453
    /**
454
     * @param string $value
455
     * @return $this
456
     */
457
    public function setFieldContent($value)
458
    {
459
        $this->blendable_xpdo_simple_object_data['content'] = $value;
460
        return $this;
461
    }
462
463
    /**
464
     * @param string $value  max characters: 50
465
     * @return $this
466
     */
467
    public function setFieldContentType($value)
468
    {
469
        $this->blendable_xpdo_simple_object_data['contentType'] = $value;
470
        return $this;
471
    }
472
473
    /**
474
     * @param bool $value
475
     * @return $this
476
     */
477
    public function setFieldContentDispo($value)
478
    {
479
        $this->blendable_xpdo_simple_object_data['content_dispo'] = $value;
480
        return $this;
481
    }
482
483
    /**
484
     * @param string $value  max characters: 100
485
     * @return $this
486
     */
487
    public function setFieldContextKey($value)
488
    {
489
        $this->blendable_xpdo_simple_object_data['context_key'] = $value;
490
        $this->opt_cache_key = 'resources/'.$value;
491
        return $this;
492
    }
493
494
    /**
495
     * @param int $value
496
     * @return $this
497
     */
498
    public function setFieldCreatedby($value)
499
    {
500
        $this->blendable_xpdo_simple_object_data['createdby'] = $value;
501
        return $this;
502
    }
503
504
    /**
505
     * @param bool $value
506
     * @return $this
507
     */
508
    public function setFieldDeleted($value)
509
    {
510
        $this->blendable_xpdo_simple_object_data['deleted'] = $value;
511
        return $this;
512
    }
513
514
    /**
515
     * @param string $value  max characters: 191
516
     * @return $this
517
     */
518
    public function setFieldDescription($value)
519
    {
520
        $this->blendable_xpdo_simple_object_data['description'] = $value;
521
        return $this;
522
    }
523
524
    /**
525
     * @param bool $value
526
     * @return $this
527
     */
528
    public function setFieldDonthit($value)
529
    {
530
        $this->blendable_xpdo_simple_object_data['donthit'] = $value;
531
        return $this;
532
    }
533
534
    /**
535
     * @param bool $value
536
     * @return $this
537
     */
538
    public function setFieldHidemenu($value)
539
    {
540
        $this->blendable_xpdo_simple_object_data['hidemenu'] = $value;
541
        return $this;
542
    }
543
544
    /**
545
     * @param bool $value
546
     * @return $this
547
     */
548
    public function setFieldHideChildrenInTree($value)
549
    {
550
        $this->blendable_xpdo_simple_object_data['hide_children_in_tree'] = $value;
551
        return $this;
552
    }
553
554
    /**
555
     * @param string $value
556
     * @return $this
557
     */
558
    public function setFieldIntrotext($value)
559
    {
560
        $this->blendable_xpdo_simple_object_data['introtext'] = $value;
561
        return $this;
562
    }
563
564
    /**
565
     * @param bool $value
566
     * @return $this
567
     */
568
    public function setFieldIsfolder($value)
569
    {
570
        $this->blendable_xpdo_simple_object_data['isfolder'] = $value;
571
        return $this;
572
    }
573
574
    /**
575
     * @param string $value  max characters: 191
576
     * @return $this
577
     */
578
    public function setFieldLinkAttributes($value)
579
    {
580
        $this->blendable_xpdo_simple_object_data['link_attributes'] = $value;
581
        return $this;
582
    }
583
584
    /**
585
     * @param string $value  max characters: 191
586
     * @return $this
587
     */
588
    public function setFieldLongtitle($value)
589
    {
590
        $this->blendable_xpdo_simple_object_data['longtitle'] = $value;
591
        return $this;
592
    }
593
594
    /**
595
     * @param int $value
596
     * @return $this
597
     */
598
    public function setFieldMenuindex($value)
599
    {
600
        $this->blendable_xpdo_simple_object_data['menuindex'] = $value;
601
        return $this;
602
    }
603
604
    /**
605
     * @param string $value  max characters: 191
606
     * @return $this
607
     */
608
    public function setFieldMenutitle($value)
609
    {
610
        $this->blendable_xpdo_simple_object_data['menutitle'] = $value;
611
        return $this;
612
    }
613
614
    /**
615
     * @param string $value  max characters: 191
616
     * @return $this
617
     */
618
    public function setFieldPagetitle($value)
619
    {
620
        $this->blendable_xpdo_simple_object_data['pagetitle'] = $value;
621
        return $this;
622
    }
623
624
    /**
625
     * @deprecated will be removed, use setFieldParentFromAlias
626
     * @param int $value
627
     * @return $this
628
     */
629
    public function setFieldParent($value)
630
    {
631
        $this->blendable_xpdo_simple_object_data['parent'] = $value;
632
        return $this;
633
    }
634
635
    /**
636
     * @param string $alias
637
     * @param string $context
638
     * @return $this
639
     */
640
    public function setFieldParentFromAlias($alias, $context='web')
641
    {
642
        $this->blendable_xpdo_simple_object_data['parent'] = [
643
            'seed_key' => $this->blender->getSeedKeyFromAlias($alias),
644
            'context' => $context
645
        ];
646
        return $this;
647
    }
648
649
    /**
650
     * @param bool $value
651
     * @return $this
652
     */
653
    public function setFieldPrivatemgr($value)
654
    {
655
        $this->blendable_xpdo_simple_object_data['privatemgr'] = $value;
656
        return $this;
657
    }
658
659
    /**
660
     * @param bool $value
661
     * @return $this
662
     */
663
    public function setFieldPrivateweb($value)
664
    {
665
        $this->blendable_xpdo_simple_object_data['privateweb'] = $value;
666
        return $this;
667
    }
668
669
    /**
670
     * @param string $value
671
     * @return $this
672
     */
673
    public function setFieldProperties($value)
674
    {
675
        $this->blendable_xpdo_simple_object_data['properties'] = $value;
676
        return $this;
677
    }
678
679
    /**
680
     * @param bool $value
681
     * @return $this
682
     */
683
    public function setFieldPublished($value)
684
    {
685
        $this->blendable_xpdo_simple_object_data['published'] = $value;
686
        return $this;
687
    }
688
689
    /**
690
     * @param int $value
691
     * @return $this
692
     */
693
    public function setFieldPublishedby($value)
694
    {
695
        $this->blendable_xpdo_simple_object_data['publishedby'] = $value;
696
        return $this;
697
    }
698
699
    /**
700
     * @param int $value
701
     * @return $this
702
     */
703
    public function setFieldPublishedon($value)
704
    {
705
        $this->blendable_xpdo_simple_object_data['publishedon'] = $value;
706
        return $this;
707
    }
708
709
    /**
710
     * @param int $value
711
     * @return $this
712
     */
713
    public function setFieldPubDate($value)
714
    {
715
        $this->blendable_xpdo_simple_object_data['pub_date'] = $value;
716
        return $this;
717
    }
718
719
    /**
720
     * @param bool $value
721
     * @return $this
722
     */
723
    public function setFieldRichtext($value)
724
    {
725
        $this->blendable_xpdo_simple_object_data['richtext'] = $value;
726
        return $this;
727
    }
728
729
    /**
730
     * @param bool $value
731
     * @return $this
732
     */
733
    public function setFieldSearchable($value)
734
    {
735
        $this->blendable_xpdo_simple_object_data['searchable'] = $value;
736
        return $this;
737
    }
738
739
    /**
740
     * @param bool $value
741
     * @return $this
742
     */
743
    public function setFieldShowInTree($value)
744
    {
745
        $this->blendable_xpdo_simple_object_data['show_in_tree'] = $value;
746
        return $this;
747
    }
748
749
    /**
750
     * @param string|int $value ~ the template name
751
     * @return $this
752
     */
753
    public function setFieldTemplate($value)
754
    {
755
        $this->blendable_xpdo_simple_object_data['template'] = $value;
756
        return $this;
757
    }
758
759
    /**
760
     * @param string $name
761
     * @param string|array $value
762
     * @return $this
763
     */
764
    public function setTVValue($name, $value)
765
    {
766
        if (!isset($this->related_data['tvs'])) {
767
            $this->related_data['tvs'] = [];
768
        }
769
770
        $this->related_data['tvs'][$name] = $value;
771
772
        return $this;
773
    }
774
775
    /**
776
     * @param string $tv_name
777
     * @param string $alias
778
     * @param string $context
779
     * @return $this
780
     */
781
    public function setTVValueResourceIDFromAlias($tv_name, $alias, $context='web')
782
    {
783
        $value = $this->blender->getResourceIDFromLocalAlias($alias, $context);
784
785
        return $this->setTVValue($tv_name, $value);
786
    }
787
788
    /**
789
     * @param string $tv_name
790
     * @param string $media_source_name
791
     * @return $this
792
     */
793
    public function setTVValueMediaSourceIDFromName($tv_name, $media_source_name)
794
    {
795
        $portable = [
796
            'value' => $media_source_name,
797
            'portable_type' => 'media_source',
798
            'portable_value' => $media_source_name
799
        ];
800
801
        return $this->setTVValue($tv_name, $portable);
802
    }
803
804
    /**
805
     * @param string $tv_name
806
     * @param string $template_name
807
     * @return $this
808
     */
809
    public function setTVValueTemplateIDFromName($tv_name, $template_name)
810
    {
811
        $portable = [
812
            'value' => $template_name,
813
            'portable_type' => 'template',
814
            'portable_value' => $template_name
815
        ];
816
817
        return $this->setTVValue($tv_name, $portable);
818
    }
819
820
    /**
821
     * @param string $value  max characters: 20
822
     * @return $this
823
     */
824
    public function setFieldType($value)
825
    {
826
        $this->blendable_xpdo_simple_object_data['type'] = $value;
827
        return $this;
828
    }
829
830
    /**
831
     * @param int $value
832
     * @return $this
833
     */
834
    public function setFieldUnpubDate($value)
835
    {
836
        $this->blendable_xpdo_simple_object_data['unpub_date'] = $value;
837
        return $this;
838
    }
839
840
    /**
841
     * @param string $value
842
     * @return $this
843
     */
844
    public function setFieldUri($value)
845
    {
846
        $this->blendable_xpdo_simple_object_data['uri'] = $value;
847
        return $this;
848
    }
849
850
    /**
851
     * @param bool $value
852
     * @return $this
853
     */
854
    public function setFieldUriOverride($value)
855
    {
856
        $this->blendable_xpdo_simple_object_data['uri_override'] = $value;
857
        return $this;
858
    }
859
860
861
    /**
862
     * @return array
863
     */
864
    protected function getUniqueCriteria()
865
    {
866
        return [
867
            $this->unique_key_column => $this->blendable_xpdo_simple_object_data[$this->unique_key_column],
868
            'context_key' => $this->getFieldContextKey()
869
        ];
870
    }
871
872
    /**
873
     * @param string|array $criteria ~ alias as string will default to web context or ['alias' => 'my-page.html', 'context_key' => 'web']
874
     */
875
    protected function setUniqueCriteria($criteria)
876
    {
877
        if (is_string($criteria)) {
878
            $this->setFieldAlias($criteria);
879
        } else {
880
            $this->setFieldAlias($criteria['alias']);
881
            $this->setFieldContextKey($criteria['context_key']);
882
        }
883
    }
884
885
    /**
886
     *
887
     */
888
    protected function assignResourceGroups()
889
    {
890
        $new_groups = (isset($this->related_data['resource_groups']) ? $this->related_data['resource_groups'] : []);
891
892
        // resource groups
893
        $current_groups = $this->xPDOSimpleObject->getResourceGroupNames();
0 ignored issues
show
Bug introduced by
The method getResourceGroupNames() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

893
        /** @scrutinizer ignore-call */ 
894
        $current_groups = $this->xPDOSimpleObject->getResourceGroupNames();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
894
        foreach ($current_groups as $group) {
895
            if (!in_array($group, $new_groups)) {
896
                $this->xPDOSimpleObject->leaveGroup($group);
897
            }
898
        }
899
900
        foreach ($new_groups as $group) {
901
            /** @var \modResourceGroup $resourceGroup */
902
            $resourceGroup = $this->modx->getObject('modResourceGroup', ['name' => $group]);
903
            if (!$resourceGroup || !$resourceGroup instanceof \modResourceGroup) {
0 ignored issues
show
Bug introduced by
The type modResourceGroup was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
904
                // create the resource group if it does not exist
905
                $this->blender->out('Attempting to create a new Resource Group: '.$group);
906
                $resourceGroup = $this->modx->newObject('modResourceGroup');
907
                $resourceGroup->set('name', $group);
908
                $resourceGroup->save();
909
            }
910
911
            if (!in_array($group, $current_groups)) {
912
                $this->xPDOSimpleObject->joinGroup($group);
913
            }
914
        }
915
    }
916
917
    /**
918
     * @param string $seed_key
919
     * @param string $context
920
     *
921
     * @return bool|\modResource
0 ignored issues
show
Bug introduced by
The type modResource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
922
     */
923
    public function getResourceFromSeedKey($seed_key, $context = 'web')
924
    {
925
        // get the alias:
926
        $alias = $this->blender->getAliasFromSeedKey($seed_key);
927
928
        return $this->modx->getObject('modResource', ['alias' => $alias, 'context_key' => $context]);
929
    }
930
931
    /**
932
     * @param string $type ~ seed or revert
933
     * @return string
934
     */
935
    public function getSeedKey($type = 'seed')
936
    {
937
        $key = $this->blender->getSeedKeyFromAlias($this->blendable_xpdo_simple_object_data['alias']);
938
939
        switch ($type) {
940
            case 'revert':
941
                $seed_key = 'revert-'.$key;
942
                break;
943
944
            case 'seed':
945
                // no break
946
            default:
947
                $seed_key = $key;
948
        }
949
950
        return $seed_key;
951
    }
952
953
954
    /**
955
     * @param array $resource_data
956
     *
957
     * @return $this
958
     */
959
    public function setResourceData(array $resource_data)
960
    {
961
        $this->blendable_xpdo_simple_object_data = $resource_data;
962
        return $this;
963
    }
964
965
966
    /**
967
     * @return Blendable
968
     */
969
    public function getCurrentVersion()
970
    {
971
        /** @var \LCI\Blend\Blendable\Resource $resource */
972
        $resource = new self($this->modx, $this->blender, $this->getFieldAlias(), $this->getFieldContextKey());
973
        return $resource
974
            ->setSeedsDir($this->getSeedsDir());
975
    }
976
977
    /**
978
     * Override in child classes
979
     */
980
    protected function loadRelatedData()
981
    {
982
        /** @var \modResource $this->xPDOSimpleObject */
983
        $this->xPDOSimpleObject;
984
        // no IDs only TV name
985
        $tvs = []; // TemplateVarResources modTemplateVarResource
986
987
        $template = false;
988
        $resource_groups = [];
989
        if (is_object($this->xPDOSimpleObject)) {
990
            /** @var \modTemplate $template */
991
            $template = $this->xPDOSimpleObject->getOne('Template');
992
            $resource_groups = $this->xPDOSimpleObject->getResourceGroupNames();
993
        }
994
        if (is_object($template)) {
995
996
            // get all TemplateValues
997
            $tvTemplates = $template->getMany('TemplateVarTemplates');
998
            /** @var \modTemplateVarTemplates $tvTemplate */
999
            foreach ($tvTemplates as $tvTemplate) {
1000
                /** @var \modTemplateVar $tv */
1001
                $tv = $tvTemplate->getOne('TemplateVar');
1002
                $tv_name = $tv->get('name');
1003
1004
                // Raw TV value:
1005
                $tvResource = $this->modx->getObject(
1006
                    'modTemplateVarResource',
1007
                    ['tmplvarid' => $tv->get('id'), 'contentid' => $this->xPDOSimpleObject->get('id')]
1008
                );
1009
1010
                if ($tvResource instanceof \modTemplateVarResource) {
0 ignored issues
show
Bug introduced by
The type modTemplateVarResource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
1011
                    $tvs[$tv_name] = $this->makePortableTVData($tv_name, $tv->get('type'), $tvResource->get('value'));
1012
                }
1013
            }
1014
        }
1015
1016
        $extras = [];
1017
        // tagger:
1018
        $tagger = $this->blender->getTagger();
1019
        if ($tagger instanceof \Tagger) {
0 ignored issues
show
Bug introduced by
The type Tagger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
1020
            $extras['tagger'] = $this->getResourceTags();
1021
        }
1022
1023
        $this->related_data = [
1024
            'extras' => $extras,
1025
            'tvs' => $tvs,
1026
            'resource_groups' => $resource_groups
1027
        ];
1028
1029
        // Calls on the event: OnBlendLoadRelatedData
1030
        parent::loadRelatedData();
1031
    }
1032
1033
    /**
1034
     * @param string $tv_name
1035
     * @return bool|string
1036
     */
1037
    protected function getPortableTVType($tv_name, $tv_type)
1038
    {
1039
        $type = false;
1040
1041
        // These are set via system settings:
1042
        if (isset($this->portable_template_variables[$tv_name])) {
1043
            return $this->portable_template_variables[$tv_name];
1044
        }
1045
1046
        switch ($tv_type) {
1047
            case 'resourcelist':
1048
                $type = 'resource';
1049
                break;
1050
        }
1051
1052
        return $type;
1053
    }
1054
1055
    /**
1056
     * @param string $tv_name
1057
     * @param string $tv_type
1058
     * @param mixed $value
1059
     * @return array
1060
     */
1061
    protected function makePortableTVData($tv_name, $tv_type, $value)
1062
    {
1063
        $portable = [
1064
            'type' => $tv_type,
1065
            'value' => $value
1066
        ];
1067
1068
        $type = $this->getPortableTVType($tv_name, $tv_type);
1069
1070
        switch ($type) {
1071
            case 'media_source':
1072
                $mediaSource = $this->modx->getObject('modMediaSource', $value);
1073
                if (is_object($mediaSource)) {
1074
                    $portable['portable_type'] = 'media_source';
1075
                    $portable['portable_value'] = $mediaSource->get('name');
1076
                }
1077
                break;
1078
1079
            case 'resource':
1080
                $portable['portable_type'] = 'resource';
1081
                $portable['portable_value'] = $this->blender->getResourceSeedKeyFromID($value);
1082
                break;
1083
1084
            case 'template':
1085
                $template = $this->modx->getObject('modTemplate', $value);
1086
                if (is_object($template)) {
1087
                    $portable['portable_type'] = 'media_source';
1088
                    $portable['portable_value'] = $template->get('templatename');
1089
                }
1090
                break;
1091
        }
1092
1093
        return $portable;
1094
    }
1095
1096
    protected function uniqueCriteria()
1097
    {
1098
        return [
1099
            $this->unique_key_column => $this->blendable_xpdo_simple_object_data[$this->unique_key_column],
1100
            'context' => $this->getFieldContextKey()
1101
        ];
1102
    }
1103
1104
    /**
1105
     * Create convert methods for any portable data column that needs to be converted to an int for a related primary key
1106
     */
1107
    /**
1108
     * @param array $parent
1109
     * @return int
1110
     */
1111
    protected function convertParent($parent)
1112
    {
1113
        if (!is_array($parent)) {
0 ignored issues
show
introduced by
The condition is_array($parent) is always true.
Loading history...
1114
            // @TODO throw exception
1115
            return 0;
1116
        }
1117
        return (int)$this->blender->getResourceIDFromSeedKey(
1118
            $parent['seed_key'],
1119
            $parent['context']
1120
        );
1121
    }
1122
    /**
1123
     * @param string $name
1124
     * @return int
1125
     */
1126
    protected function convertTemplate($name)
1127
    {
1128
        $id = 0;
1129
1130
        // Should there be an event fired here? Allowing to alter the name or returned ID?
1131
        $template = $this->modx->getObject('modTemplate', ['templatename' => $name]);
1132
        if ($template) {
1133
            $id = $template->get('id');
1134
            $this->blender->out(' Template ID set: '.$id);
1135
        } else {
1136
            $this->blender->out(' Template not found: '.$name, true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $verbose of LCI\Blend\Blender::out(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1136
            $this->blender->out(' Template not found: '.$name, /** @scrutinizer ignore-type */ true);
Loading history...
1137
        }
1138
        return $id;
1139
    }
1140
1141
    /**
1142
     * @param array $tv_data
1143
     * @return string|int|mixed $value
1144
     */
1145
    protected function convertToLocalTVData($tv_data)
1146
    {
1147
        $value = $tv_data;
1148
1149
        if (is_array($tv_data)) {
0 ignored issues
show
introduced by
The condition is_array($tv_data) is always true.
Loading history...
1150
            $value = $tv_data['value'];
1151
            if (is_array($tv_data) && isset($tv_data['portable_type']) && isset($tv_data['portable_value'])) {
1152
                switch ($tv_data['portable_type']) {
1153
                    case 'media_source':
1154
                        $mediaSource = $this->modx->getObject('modMediaSource', ['name' => $tv_data['portable_value']]);
1155
                        if (is_object($mediaSource)) {
1156
                            $value = $mediaSource->get('id');
1157
                        }
1158
                        break;
1159
1160
                    case 'resource':
1161
                        $value = $this->blender->getResourceIDFromSeedKey($tv_data['portable_value']['seed_key'], $tv_data['portable_value']['context']);
1162
                        break;
1163
1164
                    case 'template':
1165
                        $template = $this->modx->getObject('modTemplate', ['templatename' => $tv_data['portable_value']]);
1166
                        if (is_object($template)) {
1167
                            $value = $template->get('id');
1168
                        }
1169
                        break;
1170
                }
1171
            }
1172
        }
1173
1174
        return $value;
1175
    }
1176
1177
    /**
1178
     * Create seed methods for any column that needs be portable, from an int to string|array
1179
     */
1180
    /**
1181
     * @param int $parent_id
1182
     * @return array
1183
     */
1184
    protected function seedParent($parent_id)
1185
    {
1186
        return $this->blender->getResourceSeedKeyFromID($parent_id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->blender->g...edKeyFromID($parent_id) also could return the type boolean which is incompatible with the documented return type array.
Loading history...
1187
    }
1188
1189
    /**
1190
     * @param int $template_id
1191
     * @return string
1192
     */
1193
    protected function seedTemplate($template_id)
0 ignored issues
show
Unused Code introduced by
The parameter $template_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

1193
    protected function seedTemplate(/** @scrutinizer ignore-unused */ $template_id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1194
    {
1195
        $template = $this->xPDOSimpleObject->getOne('Template');
1196
1197
        if (is_object($template)) {
1198
            return $template->get('templatename');
1199
        }
1200
        return '';
1201
    }
1202
1203
1204
    /**
1205
     * This method is called just before blend/save()
1206
     */
1207
    protected function attachRelatedPieces()
1208
    {
1209
1210
    }
1211
1212
    /**
1213
     * This method is called just after a successful blend/save()
1214
     */
1215
    protected function attachRelatedPiecesAfterSave()
1216
    {
1217
        if (isset($this->related_data['tvs'])) {
1218
            $tvs = $this->related_data['tvs'];
1219
            if (is_array($tvs) && count($tvs) > 0) {
1220
                foreach ($tvs as $tv_name => $tv_data) {
1221
                    $value = $this->convertToLocalTVData($tv_data);
1222
1223
                    $this->xPDOSimpleObject->setTVValue($tv_name, $value);
1224
                }
1225
            }
1226
        }
1227
1228
        $this->assignResourceGroups();
1229
1230
        // extras
1231
        $tagger = $this->blender->getTagger();
1232
        if ($tagger instanceof \Tagger) {
1233
            $this->setResourceTags($this->xPDOSimpleObject, (isset($this->related_data['extras']['tagger']) ? $this->related_data['extras']['tagger'] : []));
0 ignored issues
show
Bug introduced by
It seems like $this->xPDOSimpleObject can also be of type null; however, parameter $resource of LCI\Blend\Blendable\Resource::setResourceTags() does only seem to accept modResource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

1233
            $this->setResourceTags(/** @scrutinizer ignore-type */ $this->xPDOSimpleObject, (isset($this->related_data['extras']['tagger']) ? $this->related_data['extras']['tagger'] : []));
Loading history...
1234
        }
1235
    }
1236
1237
    // current_xpdo_simple_object_data for seeds: and seedRelated
1238
1239
1240
    // Extras, to be removed
1241
    /**
1242
     * @return array
1243
     */
1244
    protected function getResourceTags()
1245
    {
1246
        $tags = [];
1247
        [
1248
            'group-alias' => [
1249
                'tags' => [],
1250
                'columns' => []
1251
            ]
1252
        ];
1253
        // get resource Group
1254
        // Get all tags for resource:
1255
1256
        $query = $this->modx->newQuery('TaggerTag');
1257
1258
        $query->leftJoin('TaggerTagResource', 'Resources');
1259
        $query->leftJoin('TaggerGroup', 'Group');
1260
        $query->leftJoin('modResource', 'Resource', ['Resources.resource = Resource.id']);
1261
1262
        $query->select($this->modx->getSelectColumns('TaggerTag', 'TaggerTag'));
1263
        $query->select($this->modx->getSelectColumns('TaggerGroup', 'Group', 'group_'));
1264
1265
        $query->where(['Resources.resource' => $this->xPDOSimpleObject->get('id')]);
1266
1267
        $query->prepare();
1268
        $sql = $query->toSql();
1269
1270
        $results = $this->modx->query($sql);
1271
1272
        while ($tag = $results->fetch(\PDO::FETCH_ASSOC)) {
1273
1274
            $tag_columns = $group_columns = [];
1275
            foreach ($tag as $name => $value) {
1276
                if (strpos(' '.$name, 'group_') === 1) {
1277
                    $group_columns[substr($name, strlen('group_'))] = $value;
1278
                } else {
1279
                    $tag_columns[$name] = $value;
1280
                }
1281
            }
1282
            if (!isset($tags[$tag['group_name']])) {
1283
                $tags[$tag['group_alias']] = [
1284
                    'columns' => $group_columns,
1285
                    'tags' => []
1286
                ];
1287
            }
1288
1289
            $tags[$tag['group_alias']]['tags'][$tag['alias']] = $tag_columns;
1290
        }
1291
1292
        return $tags;
1293
    }
1294
1295
    /**
1296
     * @param \modResource $resource
1297
     * @param array $tags
1298
     */
1299
    protected function setResourceTags(\modResource $resource, $tags = [])
1300
    {
1301
        $tagger_groups = [];
1302
1303
        $existing_tags = $this->getResourceTags();
1304
        foreach ($existing_tags as $tag_group_alias => $data) {
1305
1306
            $tagger_groups[$tag_group_alias] = $data['columns']['id'];
1307
1308
            $remove = false;
1309
            if (!isset($tags[$tag_group_alias])) {
1310
                // remove all related group tags as
1311
                $remove = true;
1312
            }
1313
            if (isset($data['tags'])) {
1314
                foreach ($data['tags'] as $tag_alias => $tag) {
1315
                    $not_current = false;
1316
                    if (!isset($tags[$tag_group_alias]) || !isset($tags[$tag_group_alias]['tags']) || !isset($tags[$tag_group_alias]['tags'][$tag_alias])) {
1317
                        $not_current = true;
1318
                    } else {
1319
                        // it already exists so remove from the save list
1320
                        unset($tags[$tag_group_alias]['tags'][$tag_alias]);
1321
                    }
1322
                    if ($remove || $not_current) {
1323
                        $resourceTag = $this->modx->getObject('TaggerTagResource', ['tag' => $tag['id'], 'resource' => $resource->get('id')]);
1324
                        if ($resourceTag) {
1325
                            $resourceTag->remove();
1326
                        }
1327
                    }
1328
                }
1329
            }
1330
        }
1331
1332
        // now save any remaining tags/groups
1333
        foreach ($tags as $tag_group_alias => $data) {
1334
            // does the tagger group exist?
1335
            if (!$tagger_groups[$tag_group_alias]) {
1336
                $taggerGroup = $this->modx->getObject('TaggerGroup', ['alias' => $tag_group_alias]);
1337
                if (!$taggerGroup) {
1338
                    $taggerGroup = $this->modx->newObject('TaggerGroup');
1339
                    unset($data['columns']['id']);
1340
                    $taggerGroup->fromArray($data['columns']);
1341
                    // @TODO show for templates column
1342
                    $taggerGroup->save();
1343
                }
1344
                $tagger_groups[$tag_group_alias] = $taggerGroup->get('id');
1345
            }
1346
1347
1348
            if (isset($data['tags'])) {
1349
                foreach ($data['tags'] as $tag_alias => $tag) {
1350
                    // does the tag exist?
1351
                    $taggerTag = $this->modx->getObject('TaggerTag', ['alias' => $tag_alias]);
1352
                    if (!$taggerTag) {
1353
                        $taggerTag = $this->modx->newObject('TaggerTag');
1354
                        unset($tag['id']);
1355
                        $taggerTag->fromArray($tag);
1356
                        $taggerTag->save();
1357
                    }
1358
1359
                    $resourceTag = $this->modx->newObject('TaggerTagResource');
1360
                    $resourceTag->set('tag', $taggerTag->get('id'));
1361
                    $resourceTag->set('resource', $resource->get('id'));
1362
                    $resourceTag->save();
1363
                }
1364
            }
1365
        }
1366
1367
    }
1368
}
1369