Passed
Push — master ( b070db...6f12d8 )
by
unknown
02:35
created

ElementalArea::onBeforeWrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace DNADesign\Elemental\Models;
4
5
use DNADesign\Elemental\Extensions\ElementalAreasExtension;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Core\ClassInfo;
8
use SilverStripe\Core\Extensible;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\ORM\ArrayList;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\ORM\FieldType\DBField;
13
use SilverStripe\ORM\FieldType\DBHTMLText;
14
use SilverStripe\ORM\HasManyList;
15
use SilverStripe\ORM\UnsavedRelationList;
16
use SilverStripe\Versioned\Versioned;
17
18
/**
19
 * Class ElementalArea
20
 * @package DNADesign\Elemental\Models
21
 *
22
 * @property string $OwnerClassName
23
 *
24
 * @method HasManyList|BaseElement[] Elements()
25
 */
26
class ElementalArea extends DataObject
27
{
28
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
29
        'OwnerClassName' => 'Varchar(255)',
30
    ];
31
32
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
33
        'Elements' => BaseElement::class,
34
    ];
35
36
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
37
        Versioned::class,
38
    ];
39
40
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
41
        'Elements',
42
    ];
43
44
    private static $cascade_deletes = [
0 ignored issues
show
introduced by
The private property $cascade_deletes is not used, and could be removed.
Loading history...
45
        'Elements',
46
    ];
47
48
    private static $cascade_duplicates = [
0 ignored issues
show
introduced by
The private property $cascade_duplicates is not used, and could be removed.
Loading history...
49
        'Elements',
50
    ];
51
52
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
53
        'Title' => 'Title',
54
    ];
55
56
    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...
57
58
    /**
59
     * @return array
60
     */
61
    public function supportedPageTypes()
62
    {
63
        $elementalClasses = [];
64
65
        foreach (ClassInfo::getValidSubClasses(SiteTree::class) as $class) {
66
            if (Extensible::has_extension($class, ElementalAreasExtension::class)) {
67
                $elementalClasses[] = $class;
68
            }
69
        }
70
71
        return $elementalClasses;
72
    }
73
74
    /**
75
     * @return DBHTMLText
76
     */
77
    public function forTemplate()
78
    {
79
        return $this->renderWith(static::class);
80
    }
81
82
    /**
83
     * Necessary to display results in CMS site search.
84
     *
85
     * @return DBField
86
     */
87
    public function Breadcrumbs()
88
    {
89
        $ownerClassName = $this->OwnerClassName;
90
91
        if ($owner = $ownerClassName::get()->filter('ElementalAreaID', $this->ID)->first()) {
92
            return DBField::create_field('HTMLText', sprintf(
93
                '<a href="%s">%s</a>',
94
                $owner->CMSEditLink(),
95
                $owner->Title
96
            ));
97
        }
98
99
        return null;
100
    }
101
102
    /**
103
     * Used in template instead of {@link Elements()} to wrap each element in
104
     * its' controller, making it easier to access and process form logic and
105
     * actions stored in {@link ElementController}.
106
     *
107
     * @return ArrayList
108
     * @throws \Exception
109
     */
110
    public function ElementControllers()
111
    {
112
        // Don't try and process unsaved lists
113
        if ($this->Elements() instanceof UnsavedRelationList) {
114
            return ArrayList::create();
115
        }
116
117
        $controllers = ArrayList::create();
118
        $items = $this->Elements()->filterByCallback(function (BaseElement $item) {
119
            return $item->canView();
120
        });
121
122
        if (!is_null($items)) {
123
            foreach ($items as $element) {
124
                $controller = $element->getController();
125
                $controllers->push($controller);
126
            }
127
        }
128
129
        return $controllers;
130
    }
131
132
    public function onBeforeWrite()
133
    {
134
        $this->getOwnerPage();
135
        parent::onBeforeWrite();
136
    }
137
138
    /**
139
     * @return null|DataObject
140
     * @throws \Psr\Container\NotFoundExceptionInterface
141
     * @throws \SilverStripe\ORM\ValidationException
142
     */
143
    public function getOwnerPage()
144
    {
145
        if ($this->OwnerClassName) {
146
            $class = $this->OwnerClassName;
147
            $instance = Injector::inst()->get($class);
148
            if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) {
149
                return null;
150
            }
151
            $elementalAreaRelations = $instance->getElementalRelations();
152
153
            foreach ($elementalAreaRelations as $eaRelationship) {
154
                $areaID = $eaRelationship . 'ID';
155
156
                $currentStage = Versioned::get_stage() ?: Versioned::DRAFT;
157
                $page = Versioned::get_by_stage($class, $currentStage)->filter($areaID, $this->ID);
158
159
160
                if ($page && $page->exists()) {
161
                    return $page->first();
162
                }
163
            }
164
        }
165
166
        foreach ($this->supportedPageTypes() as $class) {
167
            $instance = Injector::inst()->get($class);
168
            if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) {
169
                return null;
170
            }
171
            $elementalAreaRelations = $instance->getElementalRelations();
172
173
            foreach ($elementalAreaRelations as $eaRelationship) {
174
                $areaID = $eaRelationship . 'ID';
175
                $page = Versioned::get_by_stage($class, Versioned::DRAFT)->filter($areaID, $this->ID);
176
177
                if ($page && $page->exists()) {
178
                    $this->OwnerClassName = $class;
179
                    return $page->first();
180
                }
181
            }
182
        }
183
184
        return null;
185
    }
186
187
    /**
188
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
189
     * @return bool
190
     * @throws \Psr\Container\NotFoundExceptionInterface
191
     * @throws \SilverStripe\ORM\ValidationException
192
     */
193
    public function canEdit($member = null)
194
    {
195
        if (parent::canEdit($member)) {
196
            return true;
197
        }
198
199
        $ownerPage = $this->getOwnerPage();
200
        if ($ownerPage !== null) {
201
            return $this->getOwnerPage()->canEdit($member);
202
        }
203
204
        return false;
205
    }
206
207
    /**
208
     * @param null $member
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $member is correct as it would always require null to be passed?
Loading history...
209
     * @return bool
210
     * @throws \Psr\Container\NotFoundExceptionInterface
211
     * @throws \SilverStripe\ORM\ValidationException
212
     */
213
    public function canView($member = null)
214
    {
215
        if (parent::canEdit($member)) {
216
            return true;
217
        }
218
219
        $ownerPage = $this->getOwnerPage();
220
        if ($ownerPage !== null) {
221
            return $this->getOwnerPage()->canView($member);
222
        }
223
224
        return false;
225
    }
226
}
227