Passed
Pull Request — master (#38)
by
unknown
07:45
created

BaseElementExtension::onBeforeDelete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace DNADesign\ElementalVirtual\Extensions;
4
5
use DNADesign\ElementalVirtual\Forms\ElementalGridFieldDeleteAction;
6
use DNADesign\ElementalVirtual\Model\ElementVirtual;
7
use SilverStripe\ORM\DataExtension;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Versioned\Versioned;
10
use SilverStripe\Forms\LiteralField;
11
use SilverStripe\Forms\GridField\GridFieldConfig_Base;
12
use SilverStripe\Forms\GridField\GridFieldDataColumns;
13
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
14
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
15
use SilverStripe\Forms\GridField\GridFieldDetailForm;
16
use DNADesign\Elemental\Models\ElementalArea;
17
use SilverStripe\ORM\FieldType\DBHTMLText;
18
use SilverStripe\ORM\ArrayList;
19
20
class BaseElementExtension extends DataExtension
21
{
22
    /**
23
     * @var mixed
24
     */
25
    protected $virtualOwner;
26
27
    /**
28
     * @config
29
     *
30
     * @var boolean
31
     */
32
    private static $default_global_elements = true;
0 ignored issues
show
introduced by
The private property $default_global_elements is not used, and could be removed.
Loading history...
33
34
    /**
35
     * @var array
36
     */
37
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
38
        'AvailableGlobally' => 'Boolean(1)'
39
    ];
40
41
    /**
42
     * @var array $has_many
43
     */
44
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
45
        'VirtualClones' => ElementVirtual::class
46
    ];
47
48
    public function populateDefaults()
49
    {
50
        $default = $this->owner->config()->get('default_global_elements');
51
52
        $this->AvailableGlobally = $default;
0 ignored issues
show
Bug Best Practice introduced by
The property AvailableGlobally does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
    }
54
55
    /**
56
     * @param ElementVirtual
57
     *
58
     * @return $this
59
     */
60
    public function setVirtualOwner(ElementVirtual $owner)
61
    {
62
        $this->virtualOwner = $owner;
63
        return $this;
64
    }
65
66
    /**
67
     * @return ElementVirtual
68
     */
69
    public function getVirtualOwner()
70
    {
71
        return $this->virtualOwner;
72
    }
73
74
    /**
75
     * Finds and returns elements that are virtual elements which link to this
76
     * element.
77
     *
78
     * @return DataList
0 ignored issues
show
Bug introduced by
The type DNADesign\ElementalVirtual\Extensions\DataList 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...
79
     */
80
    public function getVirtualElements()
81
    {
82
        return ElementVirtual::get()->filter([
0 ignored issues
show
Bug Best Practice introduced by
The expression return DNADesign\Element...' => $this->owner->ID)) returns the type SilverStripe\ORM\DataList which is incompatible with the documented return type DNADesign\ElementalVirtual\Extensions\DataList.
Loading history...
83
            'LinkedElementID' => $this->owner->ID
84
        ]);
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getVirtualLinkedSummary()
91
    {
92
        return sprintf('%s (%s #%s)', $this->owner->Title, $this->owner->getType(), $this->owner->ID);
93
    }
94
95
    /**
96
     * @return DataList
97
     */
98
    public function getPublishedVirtualElements()
99
    {
100
        return ElementVirtual::get()->filter([
0 ignored issues
show
Bug Best Practice introduced by
The expression return DNADesign\Element...oned.stage' => 'Live')) returns the type SilverStripe\ORM\DataList which is incompatible with the documented return type DNADesign\ElementalVirtual\Extensions\DataList.
Loading history...
101
            'LinkedElementID' => $this->owner->ID
102
        ])->setDataQueryParam([
103
            'Versioned.mode' => 'stage',
104
            'Versioned.stage' => 'Live'
105
        ]);
106
    }
107
108
    /**
109
     * @param FieldList $fields
110
     *
111
     * @return FieldList
112
     */
113
    public function updateCMSFields(FieldList $fields)
114
    {
115
        $global = $fields->dataFieldByName('AvailableGlobally');
116
117
        if ($global) {
0 ignored issues
show
introduced by
$global is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
118
            $fields->removeByName('AvailableGlobally');
119
            $fields->addFieldToTab('Root.Settings', $global);
120
        }
121
122
        if ($virtual = $fields->dataFieldByName('VirtualClones')) {
123
            if ($this->owner->VirtualClones()->Count() > 0) {
124
                $tab = $fields->findOrMakeTab('Root.VirtualClones');
125
                $tab->setTitle(_t(__CLASS__ . '.LinkedTo', 'Linked To'));
126
127
                if ($ownerPage = $this->owner->getPage()) {
128
                    $fields->addFieldToTab(
129
                        'Root.VirtualClones',
130
                        LiteralField::create(
131
                            'DisplaysOnPage',
132
                            sprintf(
133
                                "<p>"
134
                                . _t(__CLASS__ . '.OriginalContentFrom', 'The original content element appears on')
135
                                . " <a href='%s'>%s</a></p>",
136
                                ($ownerPage->hasMethod('CMSEditLink') && $ownerPage->canEdit()) ? $ownerPage->CMSEditLink() : $ownerPage->Link(),
137
                                $ownerPage->MenuTitle
138
                            )
139
                        ),
140
                        'VirtualClones'
141
                    );
142
                }
143
144
                $virtual->setConfig(new GridFieldConfig_Base());
145
                $virtual
146
                    ->setTitle(_t(__CLASS__ . '.OtherPages', 'Other pages'))
147
                    ->getConfig()
148
                        ->removeComponentsByType(GridFieldAddExistingAutocompleter::class)
149
                        ->removeComponentsByType(GridFieldAddNewButton::class)
150
                        ->removeComponentsByType(GridFieldDeleteAction::class)
0 ignored issues
show
Bug introduced by
The type DNADesign\ElementalVirtu...s\GridFieldDeleteAction 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...
151
                        ->removeComponentsByType(GridFieldDetailForm::class)
152
                        ->addComponent(new ElementalGridFieldDeleteAction());
153
154
                $virtual->getConfig()
155
                    ->getComponentByType(GridFieldDataColumns::class)
156
                    ->setDisplayFields([
157
                        'getPage.Title' => _t(__CLASS__ . '.GridFieldTitle', 'Title'),
158
                        'ParentPageCMSEditLink' => _t(__CLASS__ . '.GridFieldUsedOn', 'Used on'),
159
                    ]);
160
            } else {
161
                $fields->removeByName('VirtualClones');
162
            }
163
        }
164
    }
165
166
    /**
167
     * Client specific requirement https://mbiessd.atlassian.net/browse/MWP-390
168
     * Delete all references of this Virtual element
169
     *
170
     */
171
    public function onBeforeDelete()
172
    {
173
        foreach ($this->getVirtualElements() as $virtualElement) {
174
            $virtualElement->delete();
175
        }
176
    }
177
178
    /**
179
     * @param array $classes
180
     */
181
    public function updateAllowedElementClasses(&$classes)
182
    {
183
        if (isset($classes[ElementVirtual::class])) {
184
            unset($classes[ElementVirtual::class]);
185
        }
186
    }
187
188
189
    /**
190
     * get all pages where this element is used
191
     *
192
     * @return ArrayList
193
     */
194
    public function getUsage()
195
    {
196
        $usage = new ArrayList();
197
198
        if ($page = $this->owner->getPage()) {
199
            $usage->push($page);
200
            if ($this->virtualOwner) {
201
                $page->setField('ElementType', 'Linked');
202
            } else {
203
                $page->setField('ElementType', 'Master');
204
            }
205
        }
206
207
        $linkedElements = ElementVirtual::get()->filter('LinkedElementID', $this->ID);
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist on DNADesign\ElementalVirtu...ns\BaseElementExtension. Did you maybe forget to declare it?
Loading history...
208
209
        foreach ($linkedElements as $element) {
210
            $area = $element->Parent();
211
212
            if ($area instanceof ElementalArea && $page = $area->getOwnerPage()) {
213
                $page->setField('ElementType', 'Linked');
214
                $usage->push($page);
215
            }
216
        }
217
218
        $usage->removeDuplicates();
219
        return $usage;
220
    }
221
222
    /**
223
     * @return DBHTMLText
224
     */
225
    public function UsageSummary()
226
    {
227
        $usage = $this->getUsage();
228
        $arr = [];
229
        foreach ($usage as $page) {
230
            $type = ($page->ElementType) ? sprintf("<em> - %s</em>", $page->ElementType) : null;
231
            $arr[] = sprintf("<a href=\"%s\" target=\"blank\">%s</a> %s", $page->CMSEditLink(), $page->Title, $type);
232
        }
233
        $html = DBHTMLText::create('UsageSummary');
234
        $html->setValue(implode('<br>', $arr));
235
236
        return $html;
237
    }
238
}
239