Completed
Push — master ( 6f12d8...786ff2 )
by
unknown
11s
created

ElementalArea   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 200
rs 10
c 0
b 0
f 0
wmc 30

7 Methods

Rating   Name   Duplication   Size   Complexity  
A forTemplate() 0 3 1
A Breadcrumbs() 0 13 2
A ElementControllers() 0 20 4
A supportedPageTypes() 0 11 3
A canEdit() 0 12 3
A canView() 0 12 3
C getOwnerPage() 0 49 14
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
    /**
133
     * @return null|DataObject
134
     * @throws \Psr\Container\NotFoundExceptionInterface
135
     * @throws \SilverStripe\ORM\ValidationException
136
     */
137
    public function getOwnerPage()
138
    {
139
        if ($this->OwnerClassName) {
140
            $class = $this->OwnerClassName;
141
            $instance = Injector::inst()->get($class);
142
            if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) {
143
                return null;
144
            }
145
            $elementalAreaRelations = $instance->getElementalRelations();
146
147
            foreach ($elementalAreaRelations as $eaRelationship) {
148
                $areaID = $eaRelationship . 'ID';
149
150
                $currentStage = Versioned::get_stage() ?: Versioned::DRAFT;
151
                $page = Versioned::get_by_stage($class, $currentStage)->filter($areaID, $this->ID);
152
153
154
                if ($page && $page->exists()) {
155
                    return $page->first();
156
                }
157
            }
158
        }
159
160
        foreach ($this->supportedPageTypes() as $class) {
161
            $instance = Injector::inst()->get($class);
162
            if (!ClassInfo::hasMethod($instance, 'getElementalRelations')) {
163
                return null;
164
            }
165
            $elementalAreaRelations = $instance->getElementalRelations();
166
167
            foreach ($elementalAreaRelations as $eaRelationship) {
168
                $areaID = $eaRelationship . 'ID';
169
                $page = Versioned::get_by_stage($class, Versioned::DRAFT)->filter($areaID, $this->ID);
170
171
                if ($page && $page->exists()) {
172
                    if ($this->OwnerClassName !== $class) {
173
                        $this->OwnerClassName = $class;
174
175
                        // Avoid recursion: only write if it's already in the database
176
                        if ($this->isInDB()) {
177
                            $this->write();
178
                        }
179
                    }
180
                    return $page->first();
181
                }
182
            }
183
        }
184
185
        return null;
186
    }
187
188
    /**
189
     * @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...
190
     * @return bool
191
     * @throws \Psr\Container\NotFoundExceptionInterface
192
     * @throws \SilverStripe\ORM\ValidationException
193
     */
194
    public function canEdit($member = null)
195
    {
196
        if (parent::canEdit($member)) {
197
            return true;
198
        }
199
200
        $ownerPage = $this->getOwnerPage();
201
        if ($ownerPage !== null) {
202
            return $this->getOwnerPage()->canEdit($member);
203
        }
204
205
        return false;
206
    }
207
208
    /**
209
     * @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...
210
     * @return bool
211
     * @throws \Psr\Container\NotFoundExceptionInterface
212
     * @throws \SilverStripe\ORM\ValidationException
213
     */
214
    public function canView($member = null)
215
    {
216
        if (parent::canEdit($member)) {
217
            return true;
218
        }
219
220
        $ownerPage = $this->getOwnerPage();
221
        if ($ownerPage !== null) {
222
            return $this->getOwnerPage()->canView($member);
223
        }
224
225
        return false;
226
    }
227
}
228