Completed
Push — master ( a14fd2...dd886f )
by Will
02:49
created

ElementalArea::ItemsToRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package elemental
5
 */
6
class ElementalArea extends WidgetArea
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * Returns all the {@link BaseElement} instances in this area, regardless if
10
     * they are enabled or not.
11
     *
12
     * @return HasManyList
13
     */
14
    public function AllElements()
15
    {
16
        $result = $this->getComponents('Widgets');
17
18
        if ($result instanceof UnsavedRelationList) {
19
            // Setup a proper UnsavedRelationList so that a page using this extension can be created
20
            // programmatically and have unsaved/saved BaseElement records attached to it.
21
22
            // NOTE(SilbinaryWolf): Able to set protected var 'dataClass' due to ViewableData using magic get/set for properties
23
            $result->dataClass = 'BaseElement'; // Change from 'Widget' to 'BaseElement'
24
            return $result;
25
        }
26
27
        $list = new HasManyList('BaseElement', $result->getForeignKey());
28
        $list->setDataModel($this->model);
29
        $list->sort('Sort ASC');
30
31
        $list = $list->forForeignID($this->ID);
32
33
        return $list;
34
    }
35
36
    /**
37
     * Returns the {@link BaseElement} instances that should be displayed to the
38
     * user.
39
     *
40
     * @return HasManyList
41
     */
42
    public function Elements()
43
    {
44
        $list = $this->AllElements();
45
46
        $list = $list->filter(array(
47
            'Enabled' => 1
48
        ));
49
50
        return $list;
51
    }
52
53
    /**
54
     * Override {@link WidgetArea::ItemsToRender}
55
     *
56
     * @return HasManyList
57
     */
58
    public function ItemsToRender()
59
    {
60
        return $this->Elements();
61
    }
62
63
    /**
64
    * Return an ArrayList of pages with the Element Page Extension
65
    *
66
    * @return ArrayList
67
    */
68
    public function getOwnerPage()
69
    {
70
        $originalMode = Versioned::current_stage();
71
        Versioned::reading_stage('Stage');
72
73
        foreach (get_declared_classes() as $class) {
74
            if (is_subclass_of($class, 'SiteTree')) {
75
                $object = singleton($class);
76
                $classes = ClassInfo::subclassesFor('ElementPageExtension');
77
                $isElemental = false;
78
79
                foreach ($classes as $extension) {
0 ignored issues
show
Bug introduced by
The expression $classes of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
80
                    if ($object->hasExtension($extension)) {
81
                        $isElemental = true;
82
                    }
83
                }
84
85
                if ($isElemental) {
86
                    $page = $class::get()->filter('ElementAreaID', $this->ID);
87
                    if ($page && $page->exists()) {
88
                        Versioned::reading_stage($originalMode);
89
                        return $page->first();
90
                    }
91
                }
92
            }
93
        }
94
95
        Versioned::reading_stage($originalMode);
96
        return false;
97
    }
98
}
99