Completed
Push — master ( 99b8ba...b2990f )
by Nicolaas
01:15
created

SilverstripeColumnsPageExtension::MyMenuItems()   C

Complexity

Conditions 8
Paths 21

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 16
nc 21
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
    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
    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...
185
    {
186
        if($this->owner->hasMethod('ChildrenShowInMenuOverloaded')) {
187
            $v = $this->owner->ChildrenShowInMenuOverloaded();
188
            if($v !== null) {
189
                return $v;
190
            }
191
        }
192
        $key = $this->owner->ID. '_'.($root ? 'true' : 'false');
193
        if(!isset(self::$_children_show_in_menu[$key])) {
194
            if($root) {
195
                $list = Page::get()->filter(['ShowInMenus' => 1, 'ParentID' => 0]);
196
                foreach($list as $page) {
197
                    if(! $page->canView()) {
198
                        $list->remove($page);
199
                    }
200
                }
201
            } else {
202
                $list = $this->owner->Children();
203
                foreach($list as $page) {
204
                    if(! $page->ShowInMenus) {
205
                        $list->remove($page);
206
                    }
207
                }
208
            }
209
            self::$_children_show_in_menu[$key] = $list;
210
        }
211
        return self::$_children_show_in_menu[$key];
212
    }
213
214
215
    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...
216
    {
217
        if($this->owner->hasMethod('MyMenuItemsOverloaded')) {
218
            $v = $this->owner->MyMenuItemsOverloaded();
219
            if($v !== null) {
220
                return $v;
221
            }
222
        }
223
        //first stop: children ...
224
        $parent = $this->owner;
225
        $dataSet = false;
226
        while($parent && $dataSet === false) {
227
            $dataSet = $parent->ChildrenShowInMenu();
228
            if($dataSet->count() === 0) {
229
                $dataSet = false;
230
            }
231
            if($dataSet === false) {
232
                $parent = Page::get()->byID($parent->ParentID);
233
            }
234
        }
235
        if($dataSet === false) {
236
            $dataSet = $this->ChildrenShowInMenu(true);
237
        }
238
        return $dataSet;
239
    }
240
241
    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...
242
    {
243
        $children = $this->MyMenuItems();
244
        if($children) {
245
            if($child = $children->first()) {
246
                $page = Page::get()->byID($child->ParentID);
247
                if($page && $page->ShowInMenus && $page->canView()) {
248
                    return $page;
249
                }
250
            }
251
        }
252
    }
253
254
    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...
255
    {
256
        $parent = $this->MyMenuItemsParentPage();
257
        if($parent) {
258
            return $parent->MyMenuItemsMenuLink($parent->ParentID);
259
        }
260
        return $this->MyMenuItemsMenuLink(0);
261
    }
262
263
    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...
264
    {
265
        if($id === null) {
266
            $id = $this->owner->ID;
267
        }
268
        return $this->owner->Link().'myspecificpagemenuitems/'.$id.'/';
269
    }
270
271
}
272