Completed
Pull Request — 1.2 (#241)
by
unknown
05:10
created

SubsitesVirtualPage_Controller   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 4
lcom 0
cbo 2
dl 0
loc 27
rs 10
c 1
b 1
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A reloadContent() 0 6 1
A init() 0 9 1
A getViewer() 0 7 2
1
<?php
2
class SubsitesVirtualPage extends VirtualPage
3
{
4
    private static $description = 'Displays the content of a page on another subsite';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
5
6
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
7
        'CustomMetaTitle' => 'Varchar(255)',
8
        'CustomMetaKeywords' => 'Varchar(255)',
9
        'CustomMetaDescription' => 'Text',
10
        'CustomExtraMeta' => 'HTMLText'
11
    );
12
    
13
    public function getCMSFields()
14
    {
15
        $fields = parent::getCMSFields();
16
        
17
        $subsites = DataObject::get('Subsite');
18
        if (!$subsites) {
19
            $subsites = new ArrayList();
20
        } else {
21
            $subsites=ArrayList::create($subsites->toArray());
22
        }
23
        
24
        $subsites->push(new ArrayData(array('Title' => 'Main site', 'ID' => 0)));
25
26
        $fields->addFieldToTab(
27
            'Root.Main',
28
            DropdownField::create(
29
                "CopyContentFromID_SubsiteID",
30
                _t('SubsitesVirtualPage.SubsiteField', "Subsite"),
31
                $subsites->map('ID', 'Title')
32
            )->addExtraClass('subsitestreedropdownfield-chooser no-change-track'),
33
            'CopyContentFromID'
34
        );
35
        
36
        // Setup the linking to the original page.
37
        $pageSelectionField = new SubsitesTreeDropdownField(
38
            "CopyContentFromID",
39
            _t('VirtualPage.CHOOSE', "Choose a page to link to"),
40
            "SiteTree",
41
            "ID",
42
            "MenuTitle"
43
        );
44
        
45
        if (Controller::has_curr() && Controller::curr()->getRequest()) {
46
            $subsiteID = Controller::curr()->getRequest()->requestVar('CopyContentFromID_SubsiteID');
47
            $pageSelectionField->setSubsiteID($subsiteID);
48
        }
49
        $fields->replaceField('CopyContentFromID', $pageSelectionField);
50
        
51
        // Create links back to the original object in the CMS
52
        if ($this->CopyContentFromID) {
53
            $editLink = "admin/pages/edit/show/$this->CopyContentFromID/?SubsiteID=" . $this->CopyContentFrom()->SubsiteID;
54
            $linkToContent = "
55
				<a class=\"cmsEditlink\" href=\"$editLink\">" .
56
                _t('VirtualPage.EDITCONTENT', 'Click here to edit the content') .
57
                "</a>";
58
            $fields->removeByName("VirtualPageContentLinkLabel");
59
            $fields->addFieldToTab(
60
                "Root.Main",
61
                $linkToContentLabelField = new LabelField('VirtualPageContentLinkLabel', $linkToContent),
62
                'Title'
63
            );
64
            $linkToContentLabelField->setAllowHTML(true);
65
        }
66
        
67
        
68
        $fields->addFieldToTab(
69
            'Root.Main',
70
            TextField::create(
71
                'CustomMetaTitle',
72
                $this->fieldLabel('CustomMetaTitle')
73
            )->setDescription(_t('SubsitesVirtualPage.OverrideNote', 'Overrides inherited value from the source')),
74
            'MetaTitle'
75
        );
76
        $fields->addFieldToTab(
77
            'Root.Main',
78
            TextareaField::create(
79
                'CustomMetaKeywords',
80
                $this->fieldLabel('CustomMetaTitle')
81
            )->setDescription(_t('SubsitesVirtualPage.OverrideNote')),
82
            'MetaKeywords'
83
        );
84
        $fields->addFieldToTab(
85
            'Root.Main',
86
            TextareaField::create(
87
                'CustomMetaDescription',
88
                $this->fieldLabel('CustomMetaTitle')
89
            )->setDescription(_t('SubsitesVirtualPage.OverrideNote')),
90
            'MetaDescription'
91
        );
92
        $fields->addFieldToTab(
93
            'Root.Main',
94
            TextField::create(
95
                'CustomExtraMeta',
96
                $this->fieldLabel('CustomMetaTitle')
97
            )->setDescription(_t('SubsitesVirtualPage.OverrideNote')),
98
            'ExtraMeta'
99
        );
100
        
101
        return $fields;
102
    }
103
104
    public function fieldLabels($includerelations = true)
105
    {
106
        $labels = parent::fieldLabels($includerelations);
107
        $labels['CustomMetaTitle'] = _t('Subsite.CustomMetaTitle', 'Title');
108
        $labels['CustomMetaKeywords'] = _t('Subsite.CustomMetaKeywords', 'Keywords');
109
        $labels['CustomMetaDescription'] = _t('Subsite.CustomMetaDescription', 'Description');
110
        $labels['CustomExtraMeta'] = _t('Subsite.CustomExtraMeta', 'Custom Meta Tags');
111
112
        return $labels;
113
    }
114
115
    public function getCopyContentFromID_SubsiteID()
116
    {
117
        return ($this->CopyContentFromID) ? (int)$this->CopyContentFrom()->SubsiteID : (int)Session::get('SubsiteID');
118
    }
119
    
120
    public function getVirtualFields()
121
    {
122
        $fields = parent::getVirtualFields();
123
        foreach ($fields as $k => $v) {
124
            if ($v == 'SubsiteID') {
125
                unset($fields[$k]);
126
            }
127
        }
128
        
129
        foreach (self::$db as $field => $type) {
130
            if (in_array($field, $fields)) {
131
                unset($fields[array_search($field, $fields)]);
132
            }
133
        }
134
135
        return $fields;
136
    }
137
    
138
    public function syncLinkTracking()
139
    {
140
        $oldState = Subsite::$disable_subsite_filter;
141
        Subsite::$disable_subsite_filter = true;
142
        if ($this->CopyContentFromID) {
143
            $this->HasBrokenLink = DataObject::get_by_id('SiteTree', $this->CopyContentFromID) ? false : true;
144
        }
145
        Subsite::$disable_subsite_filter = $oldState;
146
    }
147
148
    public function onBeforeWrite()
149
    {
150
        parent::onBeforeWrite();
151
    
152
        if ($this->CustomMetaTitle) {
153
            $this->MetaTitle = $this->CustomMetaTitle;
154
        } else {
155
            $this->MetaTitle = $this->ContentSource()->MetaTitle ? $this->ContentSource()->MetaTitle : $this->MetaTitle;
156
        }
157
        if ($this->CustomMetaKeywords) {
158
            $this->MetaKeywords = $this->CustomMetaKeywords;
159
        } else {
160
            $this->MetaKeywords = $this->ContentSource()->MetaKeywords ? $this->ContentSource()->MetaKeywords : $this->MetaKeywords;
161
        }
162
        if ($this->CustomMetaDescription) {
163
            $this->MetaDescription = $this->CustomMetaDescription;
164
        } else {
165
            $this->MetaDescription = $this->ContentSource()->MetaDescription ? $this->ContentSource()->MetaDescription : $this->MetaDescription;
166
        }
167
        if ($this->CustomExtraMeta) {
168
            $this->ExtraMeta = $this->CustomExtraMeta;
169
        } else {
170
            $this->ExtraMeta = $this->ContentSource()->ExtraMeta ? $this->ContentSource()->ExtraMeta : $this->ExtraMeta;
171
        }
172
    }
173
    
174
    public function validURLSegment()
175
    {
176
        $isValid = parent::validURLSegment();
177
        
178
        // Veto the validation rules if its false. In this case, some logic
179
        // needs to be duplicated from parent to find out the exact reason the validation failed.
180
        if (!$isValid) {
181
            $IDFilter = ($this->ID) ? "AND \"SiteTree\".\"ID\" <> $this->ID" :  null;
182
            $parentFilter = null;
183
184
            if (Config::inst()->get('SiteTree', 'nested_urls')) {
185
                if ($this->ParentID) {
186
                    $parentFilter = " AND \"SiteTree\".\"ParentID\" = $this->ParentID";
187
                } else {
188
                    $parentFilter = ' AND "SiteTree"."ParentID" = 0';
189
                }
190
            }
191
            
192
            $origDisableSubsiteFilter = Subsite::$disable_subsite_filter;
193
            Subsite::$disable_subsite_filter = true;
194
            $existingPage = DataObject::get_one(
195
                'SiteTree',
196
                "\"URLSegment\" = '$this->URLSegment' $IDFilter $parentFilter",
197
                false // disable cache, it doesn't include subsite status in the key
198
            );
199
            Subsite::$disable_subsite_filter = $origDisableSubsiteFilter;
200
            $existingPageInSubsite = DataObject::get_one(
201
                'SiteTree',
202
                "\"URLSegment\" = '$this->URLSegment' $IDFilter $parentFilter",
203
                false // disable cache, it doesn't include subsite status in the key
204
            );
205
206
            // If URL has been vetoed because of an existing page,
207
            // be more specific and allow same URLSegments in different subsites
208
            $isValid = !($existingPage && $existingPageInSubsite);
209
        }
210
        
211
        return $isValid;
212
    }
213
}
214
215
class SubsitesVirtualPage_Controller extends VirtualPage_Controller
216
{
217
    public function reloadContent()
218
    {
219
        $this->failover->copyFrom($this->failover->CopyContentFrom());
220
        $this->failover->write();
221
        return;
222
    }
223
    
224
    public function init()
225
    {
226
        $origDisableSubsiteFilter = Subsite::$disable_subsite_filter;
227
        Subsite::$disable_subsite_filter = true;
228
        
229
        parent::init();
230
        
231
        Subsite::$disable_subsite_filter = $origDisableSubsiteFilter;
232
    }
233
    
234
    public function getViewer($action) {
235
        $originalClass = get_class($this->CopyContentFrom());
236
        if ($originalClass == 'SiteTree') $name = 'Page_Controller';
237
        else $name = $originalClass."_Controller";
238
        $controller = new $name($this->CopyContentFrom());
239
        return $controller->getViewer($action);
240
    }
241
}
242