Passed
Pull Request — master (#182)
by Andrew
07:01
created

ElementalArea::canView()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace DNADesign\Elemental\Models;
4
5
use DNADesign\Elemental\Extensions\ElementalAreasExtension;
6
use DNADesign\Elemental\Models\BaseElement;
7
8
use SilverStripe\CMS\Model\SiteTree;
9
use SilverStripe\Core\ClassInfo;
10
use SilverStripe\Core\Convert;
11
use SilverStripe\Core\Extensible;
12
use SilverStripe\ORM\ArrayList;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\ORM\HasManyList;
15
use SilverStripe\ORM\UnsavedRelationList;
16
use SilverStripe\Security\Permission;
17
use SilverStripe\Versioned\Versioned;
18
use SilverStripe\View\Requirements;
19
use SilverStripe\View\SSViewer;
20
use SilverStripe\Core\Injector\Injector;
21
use Page;
0 ignored issues
show
Bug introduced by Julian Thomson
The type Page 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...
22
23
class ElementalArea extends DataObject
24
{
25
    /**
26
     * @var array $db
27
     */
28
    private static $db = [
0 ignored issues
show
introduced by Will Rossiter
The private property $db is not used, and could be removed.
Loading history...
29
        'OwnerClassName' => 'Varchar(255)',
30
    ];
31
32
    /**
33
     * @var array $has_many
34
     */
35
    private static $has_many = [
0 ignored issues
show
introduced by Will Rossiter
The private property $has_many is not used, and could be removed.
Loading history...
36
        'Elements' => BaseElement::class
37
    ];
38
39
    /**
40
     * @var array
41
     */
42
    private static $extensions = [
0 ignored issues
show
introduced by Will Rossiter
The private property $extensions is not used, and could be removed.
Loading history...
43
        Versioned::class
44
    ];
45
46
    /**
47
     * @var array
48
     */
49
    private static $owns = [
0 ignored issues
show
introduced by Will Rossiter
The private property $owns is not used, and could be removed.
Loading history...
50
        'Elements'
51
    ];
52
53
    /**
54
     * @var array
55
     */
56
    private static $cascade_deletes = [
0 ignored issues
show
introduced by Will Rossiter
The private property $cascade_deletes is not used, and could be removed.
Loading history...
57
        'Elements'
58
    ];
59
60
    /**
61
     * @var array
62
     */
63
    private static $summary_fields = [
0 ignored issues
show
introduced by Will Rossiter
The private property $summary_fields is not used, and could be removed.
Loading history...
64
        'Title' => 'Title'
65
    ];
66
67
    /**
68
     * @var string
69
     */
70
    private static $table_name = 'ElementalArea';
0 ignored issues
show
introduced by John Milmine
The private property $table_name is not used, and could be removed.
Loading history...
71
72
    /**
73
     * @return array
74
     */
75
    public function supportedPageTypes()
76
    {
77
        $elementalClasses = [];
78
79
        foreach (ClassInfo::getValidSubClasses(SiteTree::class) as $class) {
80
            if (Extensible::has_extension($class, ElementalAreasExtension::class)) {
81
                $elementalClasses[] = $class;
82
            }
83
        }
84
85
        return $elementalClasses;
86
    }
87
88
    /**
89
     * @return HTMLText
0 ignored issues
show
Bug introduced by Will Rossiter
The type DNADesign\Elemental\Models\HTMLText 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...
90
     */
91
    public function forTemplate()
92
    {
93
        return $this->renderWith(static::class);
0 ignored issues
show
Bug Best Practice introduced by Will Rossiter
The expression return $this->renderWith(static::class) returns the type SilverStripe\ORM\FieldType\DBHTMLText which is incompatible with the documented return type DNADesign\Elemental\Models\HTMLText.
Loading history...
94
    }
95
96
    /**
97
     * Necessary to display results in CMS site search.
98
     *
99
     * @return HTMLText
100
     */
101
    public function Breadcrumbs()
102
    {
103
        $ownerClassName = $this->OwnerClassName;
104
105
        if ($owner = $ownerClassName::get()->filter('ElementalAreaID', $this->ID)->first()) {
106
            return DBField::create_field('HTMLText', sprintf(
0 ignored issues
show
Bug introduced by Will Rossiter
The type DNADesign\Elemental\Models\DBField 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...
107
                '<a href="%s">%s</a>',
108
                $owner->CMSEditLink(),
109
                $owner->Title
110
            ));
111
        }
112
    }
113
114
    /**
115
     * Used in template instead of {@link Elements()} to wrap each element in
116
     * its' controller, making it easier to access and process form logic and
117
     * actions stored in {@link ElementController}.
118
     *
119
     * @return ArrayList
120
     */
121
    public function ElementControllers()
122
    {
123
        $controllers = new ArrayList();
124
        $items = $this->Elements();
0 ignored issues
show
Bug introduced by Will Rossiter
The method Elements() does not exist on DNADesign\Elemental\Models\ElementalArea. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

124
        /** @scrutinizer ignore-call */ 
125
        $items = $this->Elements();
Loading history...
125
126
        if (!is_null($items)) {
127
            foreach ($items as $element) {
128
                $controller = $element->getController();
129
                $controllers->push($controller);
130
            }
131
        }
132
133
        return $controllers;
134
    }
135
136
    public function getOwnerPage()
137
    {
138
        if ($this->OwnerClassName) {
139
            $class = $this->OwnerClassName;
140
            $instance = Injector::inst()->get($class);
141
            if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) {
142
                return;
143
            }
144
            $elementalAreaRelations = $instance->getElementalRelations();
145
146
            foreach ($elementalAreaRelations as $eaRelationship) {
147
                $areaID = $eaRelationship . 'ID';
148
149
                $page = Versioned::get_by_stage($class, Versioned::get_stage())->filter($areaID, $this->ID);
150
151
                if ($page && $page->exists()) {
152
                    return $page->first();
153
                }
154
            }
155
        }
156
157
        foreach ($this->supportedPageTypes() as $class) {
158
            $instance = Injector::inst()->get($class);
159
            if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) {
160
                return;
161
            }
162
            $elementalAreaRelations = $instance->getElementalRelations();
163
164
            foreach ($elementalAreaRelations as $eaRelationship) {
165
                $areaID = $eaRelationship . 'ID';
166
                $page = Versioned::get_by_stage($class, Versioned::DRAFT)->filter($areaID, $this->ID);
167
168
                if ($page && $page->exists()) {
169
                    $this->OwnerClassName = $class;
0 ignored issues
show
Bug Best Practice introduced by Will Rossiter
The property OwnerClassName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
170
                    $this->write();
171
172
                    return $page->first();
173
                }
174
            }
175
        }
176
177
        return false;
178
    }
179
180
    public function canEdit($member = null)
181
    {
182
        if (Permission::check('ADMIN')) {
183
            return true;
184
        }
185
        return $this->getOwnerPage()->canEdit($member);
186
    }
187
188
    public function canView($member = null)
189
    {
190
        if (Permission::check('ADMIN')) {
191
            return true;
192
        }
193
        return $this->getOwnerPage()->canView($member);
194
    }
195
}
196