Completed
Push — master ( a6e78f...99b8ba )
by Nicolaas
01:11
created

ChildrenShowInMenu()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 6.1403
c 0
b 0
f 0
cc 8
eloc 15
nc 6
nop 1
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
        $key = $this->owner->ID. '_'.($root ? 'true' : 'false');
187
        if(!isset(self::$_children_show_in_menu[$key])) {
188
            if($root) {
189
                $list = Page::get()->filter(['ShowInMenus' => 1, 'ParentID' => 0]);
190
                foreach($list as $page) {
191
                    if(! $page->canView()) {
192
                        $list->remove($page);
193
                    }
194
                }
195
            } else {
196
                $list = $this->owner->Children();
197
                foreach($list as $page) {
198
                    if(! $page->ShowInMenus) {
199
                        $list->remove($page);
200
                    }
201
                }
202
            }
203
            self::$_children_show_in_menu[$key] = $list;
204
        }
205
        return self::$_children_show_in_menu[$key];
206
    }
207
208
209
    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...
210
    {
211
        //first stop: children ...
212
        $parent = $this->owner;
213
        $dataSet = false;
214
        while($parent && $dataSet === false) {
215
            $dataSet = $parent->ChildrenShowInMenu();
216
            if($dataSet->count() === 0) {
217
                $dataSet = false;
218
            }
219
            if($dataSet === false) {
220
                $parent = Page::get()->byID($parent->ParentID);
221
            }
222
        }
223
        if($dataSet === false) {
224
            $dataSet = $this->ChildrenShowInMenu(true);
225
        }
226
        return $dataSet;
227
228
    }
229
230
231
}
232