Completed
Push — master ( 8bcb21...3bc627 )
by Nicolaas
01:15
created

UseDefaultSideBarContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
/**
4
 *@author nicolaas [at] sunnysideup.co.nz
5
 *
6
 *
7
 **/
8
9
class SilverstripeColumnsPageExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
    private static $db = [
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
        'Summary' => 'HTMLVarchar(255)',
13
        'DefaultSidebarContent' => 'HTMLText'
14
    ];
15
16
    private static $has_one = [
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
        'SummaryImage' => 'Image',
18
        'SidebarImage' => 'Image'
19
    ];
20
21
    private static $casting = [
0 ignored issues
show
Unused Code introduced by
The property $casting is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
22
        'MyDefaultSidebarContent' => 'HTMLText',
23
        'FullWidthContent' => 'HTMLText',
24
        'SummaryContent' => 'HTMLText'
25
    ];
26
27
    private static $field_labels = [
0 ignored issues
show
Unused Code introduced by
The property $field_labels is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
        'Summary' => 'Page Summary',
29
        'DefaultSidebarContent' => 'Sidebar content',
30
        'SummaryImage' => 'Image for Summaries',
31
        'SidebarImage' => 'Sidebar Image'
32
    ];
33
34
    private static $field_labels_right = [
0 ignored issues
show
Unused Code introduced by
The property $field_labels_right is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
        'Summary' => 'A summary of the page for use on other pages.',
36
        'DefaultSidebarContent' => 'The sidebar show up to the right of the main content. It is usually for something like DID YOU KNOW? or CONTACT DETAILS.',
37
        'SummaryImage' => 'Image used to show a link to this page together with the summary of the page provided.',
38
        'SidebarImage' => 'Image to show up in the sidebar instead of content.'
39
    ];
40
41
    public function updateCMSFields(FieldList $fields)
42
    {
43
        $fieldLabels = $this->owner->FieldLabels();
44
        $fieldLabelsRight = Config::inst()->get('SilverstripeColumnsPageExtension', 'field_labels_right');
45
        $tabTitleSummary = _t('SilverstripeColumnsPageExtension.SUMMARY_TAB', 'Summary');
46
        $tabTitleContent = _t('SilverstripeColumnsPageExtension.ADDITIONAL_CONTENT_TAB', 'MoreContent');
47 View Code Duplication
        if($this->owner->UseSummaries())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
        $fields->addFieldsToTab(
49
            'Root.' . $tabTitleSummary,
50
            [
51
                HTMLEditorField::create(
52
                    'Summary',
53
                    $fieldLabels['Summary']
54
                )->setRows(3)
55
                ->setRightTitle($fieldLabelsRight['Summary']),
56
                UploadField::create(
57
                    'SummaryImage',
58
                    $fieldLabels['SummaryImage']
59
                )->setRightTitle($fieldLabelsRight['SummaryImage'])
60
            ]
61
        );
62 View Code Duplication
        if($this->owner->UseDefaultSidebarContent()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            $fields->addFieldsToTab(
64
                'Root.' . $tabTitleContent,
65
                [
66
                    UploadField::create(
67
                        'SidebarImage',
68
                        $fieldLabels['SidebarImage']
69
                    )->setRightTitle($fieldLabelsRight['SidebarImage']),
70
                    HTMLEditorField::create(
71
                        'DefaultSidebarContent',
72
                        $fieldLabels['DefaultSidebarContent']
73
                    )->setRightTitle($fieldLabelsRight['DefaultSidebarContent'])
74
                ]
75
            );
76
        }
77
78
        return $fields;
79
    }
80
81
82
83
    /**
84
     * @return boolean
85
     */
86
    function UseDefaultSideBarContent()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
87
    {
88
        if($this->owner->hasMethod('UseDefaultSideBarContentOverloaded')) {
89
            $v = $this->owner->UseDefaultSideBarContentOverloaded();
90
            if($v !== null) {
91
                return $v;
92
            }
93
        }
94
95
        return false;
96
    }
97
98
99
    /**
100
     * @return boolean
101
     */
102
    function UseSummaries()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
103
    {
104
        if($this->owner->hasMethod('UseSummariesOverloaded')) {
105
            $v = $this->owner->UseSummariesOverloaded();
106
            if($v !== null) {
107
                return $v;
108
            }
109
        }
110
111
        return false;
112
    }
113
114
    /**
115
     * @return Image | null
116
     */
117
    function MySidebarImage()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
118
    {
119
        if($this->owner->hasMethod('MySidebarImageOverloaded')) {
120
            $v = $this->owner->MySidebarImageOverloaded();
121
            if($v !== null) {
122
                return $v;
123
            }
124
        }
125
126
        if($this->owner->SidebarImageID) {
127
            $image = $this->owner->SidebarImage();
128
            if($image && $image->exists()) {
129
                return $image;
130
            }
131
        }
132
        $parent = $this->owner->Parent();
133
        if($parent && $parent->exists() && $parent instanceof SiteTree) {
134
            return $parent->MySidebarImage();
135
        }
136
137
        return null;
138
    }
139
140
    /**
141
     *
142
     * @return string (HTML)
143
     */
144 View Code Duplication
    function getMyDefaultSidebarContent()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        if($this->owner->hasMethod('MyDefaultSidebarContentOverloaded')) {
147
            $v = $this->owner->MyDefaultSidebarContentOverloaded();
148
            if($v !== null) {
149
                return $v;
150
            }
151
        }
152
        return $this->owner->DefaultSidebarContent;
153
    }
154
155
    /**
156
     *
157
     * @return string (HTML)
158
     */
159 View Code Duplication
    function getFullWidthContent()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
    {
161
        if($this->owner->hasMethod('FullWidthContentOverloaded')) {
162
            $v = $this->owner->FullWidthContentOverloaded();
163
            if($v !== null) {
164
                return $v;
165
            }
166
        }
167
        return $this->owner->renderWith('FullWidthContent');
168
    }
169
170
    /**
171
     *
172
     * @return string (HTML)
173
     */
174 View Code Duplication
    function getSummaryContent()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        if($this->owner->hasMethod('SummaryContentOverloaded')) {
177
            $v = $this->owner->SummaryContentOverloaded();
178
            if($v !== null) {
179
                return $v;
180
            }
181
        }
182
        return $this->owner->renderWith('SummaryContent');
183
    }
184
185
    private static $_children_show_in_menu = [];
186
187
    private $showMenuItemsFor = null;
188
189
    public function setShowMenuItemsFor($showMenuItemsFor) {
190
        $showMenuItemsFor = intval($showMenuItemsFor);
191
        $this->showMenuItemsFor = $showMenuItemsFor;
192
    }
193
194
    function ChildrenShowInMenu($root = false)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
195
    {
196
        $key = $this->owner->ID. '_'.($root ? 'true' : 'false');
197
        if(!isset(self::$_children_show_in_menu[$key])) {
198
            if($this->owner->hasMethod('ChildrenShowInMenuOverloaded')) {
199
                $v = $this->owner->ChildrenShowInMenuOverloaded();
200
                if($v instanceof ArrayList) {
201
                    self::$_children_show_in_menu[$key] = $v;
202
                }
203
            } else {
204
                if($root) {
205
                    $list = Page::get()->filter(['ShowInMenus' => 1, 'ParentID' => 0]);
206
                    foreach($list as $page) {
207
                        if(! $page->canView()) {
208
                            $list->remove($page);
209
                        }
210
                    }
211
                } else {
212
                    $list = $this->owner->Children();
213
                    foreach($list as $page) {
214
                        if(! $page->ShowInMenus) {
215
                            $list->remove($page);
216
                        }
217
                    }
218
                }
219
                self::$_children_show_in_menu[$key] = $list;
220
            }
221
        }
222
        return self::$_children_show_in_menu[$key];
223
    }
224
225
    function MyMenuItems()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
226
    {
227
        if($this->owner->hasMethod('MyMenuItemsOverloaded')) {
228
            $v = $this->owner->MyMenuItemsOverloaded();
229
            if($v !== null) {
230
                return $v;
231
            }
232
        }
233
        //first stop: children ...
234
        $parent = $this->owner;
235
        $dataSet = false;
236
        if($this->showMenuItemsFor !== null) {
237
            if($this->showMenuItemsFor) {
238
                $page = Page::get()->byID($this->showMenuItemsFor);
239
                $dataSet = $page->ChildrenShowInMenu();
240
            } else {
241
                $dataSet = $this->ChildrenShowInMenu(true);
242
            }
243
244
        } else {
245
            while($parent && $dataSet === false) {
246
                $dataSet = $parent->ChildrenShowInMenu();
247
                if($dataSet->count() === 0) {
248
                    $dataSet = false;
249
                }
250
                if($dataSet === false) {
251
                    $parent = Page::get()->byID($parent->ParentID);
252
                }
253
            }
254
            if($dataSet === false) {
255
                $dataSet = $this->ChildrenShowInMenu(true);
256
            }
257
        }
258
        return $dataSet;
259
    }
260
261
    function MyMenuItemsParentPage()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
262
    {
263
        $children = $this->MyMenuItems();
264
        if($children) {
265
            if($child = $children->first()) {
266
                $page = Page::get()->byID($child->ParentID);
267
                if($page && $page->ShowInMenus && $page->canView()) {
268
                    return $page;
269
                }
270
            }
271
        }
272
    }
273
274
    function MyMenuItemsParentLink()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
275
    {
276
        $parent = $this->MyMenuItemsParentPage();
277
        if($parent) {
278
            return $parent->MyMenuItemsMenuLink($parent->ParentID);
279
        }
280
        return $this->MyMenuItemsMenuLink(0);
281
    }
282
283
    function MyMenuItemsMenuLink($id = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
284
    {
285
        if($id === null) {
286
            $id = $this->owner->ID;
287
        }
288
        return Controller::curr()->Link().'myspecificpagemenuitems/'.$id.'/';
289
    }
290
291
}
292