Passed
Pull Request — master (#158)
by Robbie
01:54
created

ElementalArea::onBeforeWrite()   A

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

123
        /** @scrutinizer ignore-call */ 
124
        $items = $this->Elements();
Loading history...
124
125
        if (!is_null($items)) {
126
            foreach ($items as $element) {
127
                $controller = $element->getController();
128
                $controllers->push($controller);
129
            }
130
        }
131
132
        return $controllers;
133
    }
134
135
    public function getOwnerPage()
136
    {
137
        if ($this->OwnerClassName) {
138
            $class = $this->OwnerClassName;
139
            $elementalAreaRelations = Injector::inst()->get($class)->getElementalRelations();
140
141
            foreach ($elementalAreaRelations as $eaRelationship) {
142
                $areaID = $eaRelationship . 'ID';
143
144
                $page = $class::get()->filter($areaID, $this->ID);
145
146
                if ($page && $page->exists()) {
147
                    return $page->first();
148
                }
149
            }
150
        }
151
152
        $originalMode = Versioned::get_stage();
153
154
        if (!$originalMode) {
155
            $originalMode = Versioned::DRAFT;
156
        }
157
158
        Versioned::set_stage(Versioned::DRAFT);
159
160
        foreach ($this->supportedPageTypes() as $class) {
161
            $elementalAreaRelations = Injector::inst()->get($class)->getElementalRelations();
162
163
            foreach ($elementalAreaRelations as $eaRelationship) {
164
                $areaID = $eaRelationship . 'ID';
165
                $page = $class::get()->filter($areaID, $this->ID);
166
167
                if ($page && $page->exists()) {
168
                    Versioned::set_stage($originalMode);
169
                    $this->OwnerClassName = $class;
0 ignored issues
show
Bug Best Practice introduced by
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
        Versioned::set_stage($originalMode);
178
179
        return false;
180
    }
181
}
182