Completed
Push — master ( 0ebf95...33622c )
by Damian
12s
created

SubsitesVirtualPage   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 220
rs 9.8
wmc 31

7 Methods

Rating   Name   Duplication   Size   Complexity  
A fieldLabels() 0 9 1
B validURLSegment() 0 38 6
B getVirtualFields() 0 16 5
C onBeforeWrite() 0 23 9
A getCopyContentFromID_SubsiteID() 0 6 2
A syncLinkTracking() 0 8 3
B getCMSFields() 0 89 5
1
<?php
2
3
namespace SilverStripe\Subsites\Pages;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\CMS\Model\VirtualPage;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\Forms\LabelField;
11
use SilverStripe\Forms\TextareaField;
12
use SilverStripe\Forms\TextField;
13
use SilverStripe\ORM\ArrayList;
14
use SilverStripe\ORM\DataObject;
15
use SilverStripe\Subsites\Forms\SubsitesTreeDropdownField;
16
use SilverStripe\Subsites\Model\Subsite;
17
use SilverStripe\Subsites\State\SubsiteState;
18
use SilverStripe\View\ArrayData;
19
20
class SubsitesVirtualPage extends VirtualPage
21
{
22
23
    private static $table_name = 'SubsitesVirtualPage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
24
25
    private static $description = 'Displays the content of a page on another subsite';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
26
27
    private static $db = [
28
        'CustomMetaTitle' => 'Varchar(255)',
29
        'CustomMetaKeywords' => 'Varchar(255)',
30
        'CustomMetaDescription' => 'Text',
31
        'CustomExtraMeta' => 'HTMLText'
32
    ];
33
34
    private static $non_virtual_fields = [
0 ignored issues
show
introduced by
The private property $non_virtual_fields is not used, and could be removed.
Loading history...
35
        'SubsiteID'
36
    ];
37
38
    public function getCMSFields()
39
    {
40
        $fields = parent::getCMSFields();
41
42
        $subsites = DataObject::get(Subsite::class);
43
        if (!$subsites) {
44
            $subsites = new ArrayList();
45
        } else {
46
            $subsites = ArrayList::create($subsites->toArray());
47
        }
48
49
        $subsites->push(new ArrayData(['Title' => 'Main site', 'ID' => 0]));
50
51
        $fields->addFieldToTab(
52
            'Root.Main',
53
            DropdownField::create(
54
                'CopyContentFromID_SubsiteID',
0 ignored issues
show
Bug introduced by
'CopyContentFromID_SubsiteID' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

54
                /** @scrutinizer ignore-type */ 'CopyContentFromID_SubsiteID',
Loading history...
55
                _t(__CLASS__ . '.SubsiteField', 'Subsite'),
56
                $subsites->map('ID', 'Title')
0 ignored issues
show
Bug introduced by
$subsites->map('ID', 'Title') of type SilverStripe\ORM\Map is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

56
                /** @scrutinizer ignore-type */ $subsites->map('ID', 'Title')
Loading history...
57
            )->addExtraClass('subsitestreedropdownfield-chooser no-change-track'),
58
            'CopyContentFromID'
59
        );
60
61
        // Setup the linking to the original page.
62
        $pageSelectionField = new SubsitesTreeDropdownField(
63
            'CopyContentFromID',
64
            _t('SilverStripe\\CMS\\Model\\VirtualPage.CHOOSE', 'Choose a page to link to'),
65
            "SilverStripe\\CMS\\Model\\SiteTree",
66
            'ID',
67
            'MenuTitle'
68
        );
69
70
        if (Controller::has_curr() && Controller::curr()->getRequest()) {
71
            $subsiteID = Controller::curr()->getRequest()->requestVar('CopyContentFromID_SubsiteID');
72
            $pageSelectionField->setSubsiteID($subsiteID);
73
        }
74
        $fields->replaceField('CopyContentFromID', $pageSelectionField);
75
76
        // Create links back to the original object in the CMS
77
        if ($this->CopyContentFromID) {
78
            $editLink = "admin/pages/edit/show/$this->CopyContentFromID/?SubsiteID=" . $this->CopyContentFrom()->SubsiteID;
79
            $linkToContent = "
80
				<a class=\"cmsEditlink\" href=\"$editLink\">" .
81
                _t('SilverStripe\\CMS\\Model\\VirtualPage.EDITCONTENT', 'Click here to edit the content') .
82
                '</a>';
83
            $fields->removeByName('VirtualPageContentLinkLabel');
84
            $fields->addFieldToTab(
85
                'Root.Main',
86
                $linkToContentLabelField = new LabelField('VirtualPageContentLinkLabel', $linkToContent),
87
                'Title'
88
            );
89
            $linkToContentLabelField->setAllowHTML(true);
90
        }
91
92
93
        $fields->addFieldToTab(
94
            'Root.Main',
95
            TextField::create(
96
                'CustomMetaTitle',
97
                $this->fieldLabel('CustomMetaTitle')
98
            )->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')),
99
            'MetaTitle'
100
        );
101
        $fields->addFieldToTab(
102
            'Root.Main',
103
            TextareaField::create(
104
                'CustomMetaKeywords',
105
                $this->fieldLabel('CustomMetaTitle')
106
            )->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')),
107
            'MetaKeywords'
108
        );
109
        $fields->addFieldToTab(
110
            'Root.Main',
111
            TextareaField::create(
112
                'CustomMetaDescription',
113
                $this->fieldLabel('CustomMetaTitle')
114
            )->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')),
115
            'MetaDescription'
116
        );
117
        $fields->addFieldToTab(
118
            'Root.Main',
119
            TextField::create(
120
                'CustomExtraMeta',
121
                $this->fieldLabel('CustomMetaTitle')
122
            )->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')),
123
            'ExtraMeta'
124
        );
125
126
        return $fields;
127
    }
128
129
    public function fieldLabels($includerelations = true)
130
    {
131
        $labels = parent::fieldLabels($includerelations);
132
        $labels['CustomMetaTitle'] = _t('SilverStripe\\Subsites\\Model\\Subsite.CustomMetaTitle', 'Title');
133
        $labels['CustomMetaKeywords'] = _t('SilverStripe\\Subsites\\Model\\Subsite.CustomMetaKeywords', 'Keywords');
134
        $labels['CustomMetaDescription'] = _t('SilverStripe\\Subsites\\Model\\Subsite.CustomMetaDescription', 'Description');
135
        $labels['CustomExtraMeta'] = _t('SilverStripe\\Subsites\\Model\\Subsite.CustomExtraMeta', 'Custom Meta Tags');
136
137
        return $labels;
138
    }
139
140
    public function getCopyContentFromID_SubsiteID()
141
    {
142
        if ($this->CopyContentFromID) {
143
            return (int) $this->CopyContentFrom()->SubsiteID;
144
        }
145
        return SubsiteState::singleton()->getSubsiteId();
146
    }
147
148
    public function getVirtualFields()
149
    {
150
        $fields = parent::getVirtualFields();
151
        foreach ($fields as $k => $v) {
152
            if ($v == 'SubsiteID') {
153
                unset($fields[$k]);
154
            }
155
        }
156
157
        foreach (self::$db as $field => $type) {
158
            if (in_array($field, $fields)) {
159
                unset($fields[array_search($field, $fields)]);
160
            }
161
        }
162
163
        return $fields;
164
    }
165
166
    public function syncLinkTracking()
167
    {
168
        $oldState = Subsite::$disable_subsite_filter;
169
        Subsite::$disable_subsite_filter = true;
170
        if ($this->CopyContentFromID) {
171
            $this->HasBrokenLink = DataObject::get_by_id(SiteTree::class, $this->CopyContentFromID) ? false : true;
0 ignored issues
show
Bug Best Practice introduced by
The property HasBrokenLink does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
172
        }
173
        Subsite::$disable_subsite_filter = $oldState;
174
    }
175
176
    public function onBeforeWrite()
177
    {
178
        parent::onBeforeWrite();
179
180
        if ($this->CustomMetaTitle) {
0 ignored issues
show
Bug Best Practice introduced by
The property CustomMetaTitle does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
181
            $this->MetaTitle = $this->CustomMetaTitle;
0 ignored issues
show
Bug Best Practice introduced by
The property MetaTitle does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
182
        } else {
183
            $this->MetaTitle = $this->ContentSource()->MetaTitle ?: $this->MetaTitle;
184
        }
185
        if ($this->CustomMetaKeywords) {
0 ignored issues
show
Bug Best Practice introduced by
The property CustomMetaKeywords does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
186
            $this->MetaKeywords = $this->CustomMetaKeywords;
0 ignored issues
show
Bug Best Practice introduced by
The property MetaKeywords does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
187
        } else {
188
            $this->MetaKeywords = $this->ContentSource()->MetaKeywords ?: $this->MetaKeywords;
189
        }
190
        if ($this->CustomMetaDescription) {
0 ignored issues
show
Bug Best Practice introduced by
The property CustomMetaDescription does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
191
            $this->MetaDescription = $this->CustomMetaDescription;
0 ignored issues
show
Bug Best Practice introduced by
The property MetaDescription does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
192
        } else {
193
            $this->MetaDescription = $this->ContentSource()->MetaDescription ?: $this->MetaDescription;
194
        }
195
        if ($this->CustomExtraMeta) {
0 ignored issues
show
Bug Best Practice introduced by
The property CustomExtraMeta does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
196
            $this->ExtraMeta = $this->CustomExtraMeta;
0 ignored issues
show
Bug Best Practice introduced by
The property ExtraMeta does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
197
        } else {
198
            $this->ExtraMeta = $this->ContentSource()->ExtraMeta ?: $this->ExtraMeta;
199
        }
200
    }
201
202
    public function validURLSegment()
203
    {
204
        $isValid = parent::validURLSegment();
205
206
        // Veto the validation rules if its false. In this case, some logic
207
        // needs to be duplicated from parent to find out the exact reason the validation failed.
208
        if (!$isValid) {
209
            $IDFilter = $this->ID ? "AND \"SiteTree\".\"ID\" <> $this->ID" : null;
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
210
            $parentFilter = null;
211
212
            if (Config::inst()->get(SiteTree::class, 'nested_urls')) {
213
                if ($this->ParentID) {
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
214
                    $parentFilter = " AND \"SiteTree\".\"ParentID\" = $this->ParentID";
215
                } else {
216
                    $parentFilter = ' AND "SiteTree"."ParentID" = 0';
217
                }
218
            }
219
220
            $origDisableSubsiteFilter = Subsite::$disable_subsite_filter;
221
            Subsite::$disable_subsite_filter = true;
222
            $existingPage = DataObject::get_one(
223
                SiteTree::class,
224
                "\"URLSegment\" = '$this->URLSegment' $IDFilter $parentFilter",
0 ignored issues
show
Bug Best Practice introduced by
The property URLSegment does not exist on SilverStripe\Subsites\Pages\SubsitesVirtualPage. Since you implemented __get, consider adding a @property annotation.
Loading history...
225
                false // disable cache, it doesn't include subsite status in the key
226
            );
227
            Subsite::$disable_subsite_filter = $origDisableSubsiteFilter;
228
            $existingPageInSubsite = DataObject::get_one(
229
                SiteTree::class,
230
                "\"URLSegment\" = '$this->URLSegment' $IDFilter $parentFilter",
231
                false // disable cache, it doesn't include subsite status in the key
232
            );
233
234
            // If URL has been vetoed because of an existing page,
235
            // be more specific and allow same URLSegments in different subsites
236
            $isValid = !($existingPage && $existingPageInSubsite);
237
        }
238
239
        return $isValid;
240
    }
241
}
242