Completed
Push — master ( b2990f...fc46cf )
by Nicolaas
01:39
created

SilverstripeColumnsPageExtension::MyMenuItems()   D

Complexity

Conditions 10
Paths 9

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 23
nc 9
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    private static $page_types_that_use_the_default_sidebar = [];
0 ignored issues
show
Unused Code introduced by
The property $page_types_that_use_the_default_sidebar 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...
42
43
    private static $page_types_that_use_the_second_column = [];
0 ignored issues
show
Unused Code introduced by
The property $page_types_that_use_the_second_column 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...
44
45
    public function updateCMSFields(FieldList $fields)
46
    {
47
        $fieldLabels = $this->owner->FieldLabels();
48
        $fieldLabelsRight = Config::inst()->get('SilverstripeColumnsPageExtension', 'field_labels_right');
49
        $tabTitleSummary = _t('SilverstripeColumnsPageExtension.SUMMARY_TAB', 'Summary');
50
        $tabTitleContent = _t('SilverstripeColumnsPageExtension.ADDITIONAL_CONTENT_TAB', 'MoreContent');
51
        $fields->addFieldsToTab(
52
            'Root.' . $tabTitleSummary,
53
            [
54
                HTMLEditorField::create(
55
                    'Summary',
56
                    $fieldLabels['Summary']
57
                )->setRows(3)
58
                ->setRightTitle($fieldLabelsRight['Summary']),
59
                UploadField::create(
60
                    'SummaryImage',
61
                    $fieldLabels['SummaryImage']
62
                )->setRightTitle($fieldLabelsRight['SummaryImage'])
63
            ]
64
        );
65
        if($this->owner->UseDefaultSidebarContent()) {
66
            $fields->addFieldsToTab(
67
                'Root.' . $tabTitleContent,
68
                [
69
                    HTMLEditorField::create(
70
                        'DefaultSidebarContent',
71
                        $fieldLabels['DefaultSidebarContent']
72
                    )->setRightTitle($fieldLabelsRight['DefaultSidebarContent']),
73
                    UploadField::create(
74
                        'SidebarImage',
75
                        $fieldLabels['SidebarImage']
76
                    )->setRightTitle($fieldLabelsRight['SidebarImage'])
77
                ]
78
            );
79
        }
80
81
        return $fields;
82
    }
83
84
85
86
    /**
87
     * @return boolean
88
     */
89
    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...
90
    {
91
        if($this->owner->hasMethod('UseDefaultSideBarContentOverloaded')) {
92
            $v = $this->owner->UseDefaultSideBarContentOverloaded();
93
            if($v !== null) {
94
                return $v;
95
            }
96
        }
97
98
        $testArray = Config::inst()->get('SilverstripeColumnsPageExtension', 'page_types_that_use_the_default_sidebar');
99
        if(count($testArray) === 0) {
100
101
            return true;
102
        } else {
103
            if(in_array($this->owner->ClassName, $testArray)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return in_array($this->o...ClassName, $testArray);.
Loading history...
104
                return true;
105
            }
106
            return false;
107
        }
108
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
109
    }
110
111
    /**
112
     * @return Image | null
113
     */
114
    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...
115
    {
116
        if($this->owner->hasMethod('MySidebarImageOverloaded')) {
117
            $v = $this->owner->MySidebarImageOverloaded();
118
            if($v !== null) {
119
                return $v;
120
            }
121
        }
122
123
        if($this->owner->SidebarImageID) {
124
            $image = $this->owner->SidebarImage();
125
            if($image && $image->exists()) {
126
                return $image;
127
            }
128
        }
129
        $parent = $this->owner->Parent();
130
        if($parent && $parent->exists() && $parent instanceof SiteTree) {
131
            return $parent->MySidebarImage();
132
        }
133
134
        return null;
135
    }
136
137
    /**
138
     *
139
     * @return string (HTML)
140
     */
141 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...
142
    {
143
        if($this->owner->hasMethod('MyDefaultSidebarContentOverloaded')) {
144
            $v = $this->owner->MyDefaultSidebarContentOverloaded();
145
            if($v !== null) {
146
                return $v;
147
            }
148
        }
149
        return $this->owner->DefaultSidebarContent;
150
    }
151
152
    /**
153
     *
154
     * @return string (HTML)
155
     */
156 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...
157
    {
158
        if($this->owner->hasMethod('FullWidthContentOverloaded')) {
159
            $v = $this->owner->FullWidthContentOverloaded();
160
            if($v !== null) {
161
                return $v;
162
            }
163
        }
164
        return $this->owner->renderWith('FullWidthContent');
165
    }
166
167
    /**
168
     *
169
     * @return string (HTML)
170
     */
171 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...
172
    {
173
        if($this->owner->hasMethod('SummaryContentOverloaded')) {
174
            $v = $this->owner->SummaryContentOverloaded();
175
            if($v !== null) {
176
                return $v;
177
            }
178
        }
179
        return $this->owner->renderWith('SummaryContent');
180
    }
181
182
    private static $_children_show_in_menu = [];
183
184
    private $showMenuItemsFor = null;
185
186
    public function setShowMenuItemsFor($showMenuItemsFor) {
187
        $showMenuItemsFor = intval($showMenuItemsFor);
188
        $this->showMenuItemsFor = $showMenuItemsFor;
189
    }
190
191
    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...
192
    {
193
        $key = $this->owner->ID. '_'.($root ? 'true' : 'false');
194
        if(!isset(self::$_children_show_in_menu[$key])) {
195
            if($this->owner->hasMethod('ChildrenShowInMenuOverloaded')) {
196
                $v = $this->owner->ChildrenShowInMenuOverloaded();
197
                if($v instanceof ArrayList) {
198
                    self::$_children_show_in_menu[$key] = $v;
199
                }
200
            } else {
201
                if($root) {
202
                    $list = Page::get()->filter(['ShowInMenus' => 1, 'ParentID' => 0]);
203
                    foreach($list as $page) {
204
                        if(! $page->canView()) {
205
                            $list->remove($page);
206
                        }
207
                    }
208
                } else {
209
                    $list = $this->owner->Children();
210
                    foreach($list as $page) {
211
                        if(! $page->ShowInMenus) {
212
                            $list->remove($page);
213
                        }
214
                    }
215
                }
216
                self::$_children_show_in_menu[$key] = $list;
217
            }
218
        }
219
        return self::$_children_show_in_menu[$key];
220
    }
221
222
    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...
223
    {
224
        if($this->owner->hasMethod('MyMenuItemsOverloaded')) {
225
            $v = $this->owner->MyMenuItemsOverloaded();
226
            if($v !== null) {
227
                return $v;
228
            }
229
        }
230
        //first stop: children ...
231
        $parent = $this->owner;
232
        $dataSet = false;
233
        if($this->showMenuItemsFor !== null) {
234
            if($this->showMenuItemsFor) {
235
                $page = Page::get()->byID($this->showMenuItemsFor);
236
                $dataSet = $page->ChildrenShowInMenu();
237
            } else {
238
                $dataSet = $this->ChildrenShowInMenu(true);
239
            }
240
241
        } else {
242
            while($parent && $dataSet === false) {
243
                $dataSet = $parent->ChildrenShowInMenu();
244
                if($dataSet->count() === 0) {
245
                    $dataSet = false;
246
                }
247
                if($dataSet === false) {
248
                    $parent = Page::get()->byID($parent->ParentID);
249
                }
250
            }
251
            if($dataSet === false) {
252
                $dataSet = $this->ChildrenShowInMenu(true);
253
            }
254
        }
255
        return $dataSet;
256
    }
257
258
    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...
259
    {
260
        $children = $this->MyMenuItems();
261
        if($children) {
262
            if($child = $children->first()) {
263
                $page = Page::get()->byID($child->ParentID);
264
                if($page && $page->ShowInMenus && $page->canView()) {
265
                    return $page;
266
                }
267
            }
268
        }
269
    }
270
271
    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...
272
    {
273
        $parent = $this->MyMenuItemsParentPage();
274
        if($parent) {
275
            return $parent->MyMenuItemsMenuLink($parent->ParentID);
276
        }
277
        return $this->MyMenuItemsMenuLink(0);
278
    }
279
280
    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...
281
    {
282
        if($id === null) {
283
            $id = $this->owner->ID;
284
        }
285
        return Controller::curr()->Link().'myspecificpagemenuitems/'.$id.'/';
286
    }
287
288
}
289