SilverstripeColumnsPageExtension::MySidebarImage()   C
last analyzed

Complexity

Conditions 9
Paths 11

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.412
c 0
b 0
f 0
cc 9
eloc 13
nc 11
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
        }
63 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...
64
            $fields->addFieldsToTab(
65
                'Root.' . $tabTitleContent,
66
                [
67
                    UploadField::create(
68
                        'SidebarImage',
69
                        $fieldLabels['SidebarImage']
70
                    )->setRightTitle($fieldLabelsRight['SidebarImage']),
71
                    HTMLEditorField::create(
72
                        'DefaultSidebarContent',
73
                        $fieldLabels['DefaultSidebarContent']
74
                    )->setRightTitle($fieldLabelsRight['DefaultSidebarContent'])
75
                ]
76
            );
77
        }
78
79
        return $fields;
80
    }
81
82
83
84
    /**
85
     * @return boolean
86
     */
87
    public function UseDefaultSideBarContent()
88
    {
89
        if ($this->owner->hasMethod('UseDefaultSideBarContentOverloaded')) {
90
            $v = $this->owner->UseDefaultSideBarContentOverloaded();
91
            if ($v !== null) {
92
                return $v;
93
            }
94
        }
95
96
        return false;
97
    }
98
99
100
    /**
101
     * @return boolean
102
     */
103
    public function UseSummaries()
104
    {
105
        if ($this->owner->hasMethod('UseSummariesOverloaded')) {
106
            $v = $this->owner->UseSummariesOverloaded();
107
            if ($v !== null) {
108
                return $v;
109
            }
110
        }
111
112
        return false;
113
    }
114
115
    /**
116
     * @return Image | null
117
     */
118
    public function MySidebarImage()
119
    {
120
        if ($this->owner->hasMethod('MySidebarImageOverloaded')) {
121
            $v = $this->owner->MySidebarImageOverloaded();
122
            if ($v !== null) {
123
                return $v;
124
            }
125
        }
126
127
        if ($this->owner->SidebarImageID) {
128
            $image = $this->owner->SidebarImage();
129
            if ($image && $image->exists()) {
130
                return $image;
131
            }
132
        }
133
        $parent = $this->owner->Parent();
134
        if ($parent && $parent->exists() && $parent instanceof SiteTree) {
135
            return $parent->MySidebarImage();
136
        }
137
138
        return null;
139
    }
140
141
    /**
142
     *
143
     * @return string (HTML)
144
     */
145 View Code Duplication
    public function getMyDefaultSidebarContent()
0 ignored issues
show
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...
146
    {
147
        if ($this->owner->hasMethod('MyDefaultSidebarContentOverloaded')) {
148
            $v = $this->owner->MyDefaultSidebarContentOverloaded();
149
            if ($v !== null) {
150
                return $v;
151
            }
152
        }
153
        return $this->owner->DefaultSidebarContent;
154
    }
155
156
    /**
157
     *
158
     * @return string (HTML)
159
     */
160 View Code Duplication
    public function getFullWidthContent()
0 ignored issues
show
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...
161
    {
162
        if ($this->owner->hasMethod('FullWidthContentOverloaded')) {
163
            $v = $this->owner->FullWidthContentOverloaded();
164
            if ($v !== null) {
165
                return $v;
166
            }
167
        }
168
        return $this->owner->renderWith('FullWidthContent');
169
    }
170
171
    /**
172
     *
173
     * @return string (HTML)
174
     */
175 View Code Duplication
    public function getSummaryContent()
0 ignored issues
show
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...
176
    {
177
        if ($this->owner->hasMethod('SummaryContentOverloaded')) {
178
            $v = $this->owner->SummaryContentOverloaded();
179
            if ($v !== null) {
180
                return $v;
181
            }
182
        }
183
        return $this->owner->renderWith('SummaryContent');
184
    }
185
186
    private static $_children_show_in_menu = [];
187
188
    private $showMenuItemsFor = null;
189
190
    public function setShowMenuItemsFor($showMenuItemsFor)
191
    {
192
        $showMenuItemsFor = intval($showMenuItemsFor);
193
        $this->showMenuItemsFor = $showMenuItemsFor;
194
    }
195
196
    public 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...
197
    {
198
        $key = $this->owner->ID. '_'.($root ? 'true' : 'false');
199
        if (!isset(self::$_children_show_in_menu[$key])) {
200
            if ($this->owner->hasMethod('ChildrenShowInMenuOverloaded')) {
201
                $v = $this->owner->ChildrenShowInMenuOverloaded();
202
                if ($v instanceof ArrayList) {
203
                    self::$_children_show_in_menu[$key] = $v;
204
                }
205
            } else {
206
                if ($root) {
207
                    $list = Page::get()->filter(['ShowInMenus' => 1, 'ParentID' => 0]);
208
                    foreach ($list as $page) {
209
                        if (! $page->canView()) {
210
                            $list = $list->exclude(['ID' => $page->ID]);
211
                        }
212
                    }
213
                } else {
214
                    $list = $this->owner->Children();
215
                    foreach ($list as $page) {
216
                        if (! $page->ShowInMenus ) {
217
                            $list = $list->exclude(['ID' => $page->ID]);
218
                        }
219
                    }
220
                }
221
                self::$_children_show_in_menu[$key] = $list;
222
            }
223
        }
224
        return self::$_children_show_in_menu[$key];
225
    }
226
227
    public 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...
228
    {
229
        if ($this->owner->hasMethod('MyMenuItemsOverloaded')) {
230
            $v = $this->owner->MyMenuItemsOverloaded();
231
            if ($v !== null) {
232
                return $v;
233
            }
234
        }
235
        //first stop: children ...
236
        $parent = $this->owner;
237
        $dataSet = false;
238
        if ($this->showMenuItemsFor !== null) {
239
            if ($this->showMenuItemsFor) {
240
                $page = Page::get()->byID($this->showMenuItemsFor);
241
                $dataSet = $page->ChildrenShowInMenu();
242
            } else {
243
                $dataSet = $this->ChildrenShowInMenu(true);
244
            }
245
        } else {
246
            $isHomePage = $this->owner->URLSegment === Config::inst()->get('RootURLController', 'default_homepage_link');
247
            while ($parent && $dataSet === false) {
248
                $dataSet = $parent->ChildrenShowInMenu($isHomePage);
249
                if ($dataSet->count() === 0) {
250
                    $dataSet = false;
251
                }
252
                if ($dataSet === false) {
253
                    $parent = Page::get()->byID($parent->ParentID);
254
                }
255
            }
256
            if ($dataSet === false) {
257
                $dataSet = $this->ChildrenShowInMenu(true);
258
            }
259
        }
260
261
        return $dataSet;
262
    }
263
264
    public 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...
265
    {
266
        $children = $this->MyMenuItems();
267
        if ($children) {
268
            if ($child = $children->first()) {
269
                $page = Page::get()->byID($child->ParentID);
270
                if ($page && $page->ShowInMenus && $page->canView()) {
271
                    return $page;
272
                }
273
            }
274
        }
275
    }
276
277
    public 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...
278
    {
279
        $parent = $this->MyMenuItemsParentPage();
280
        if ($parent) {
281
            return $parent->MyMenuItemsMenuLink($parent->ParentID);
282
        }
283
        return $this->MyMenuItemsMenuLink(0);
284
    }
285
286
    public function MyMenuItemsMenuLink($id = null)
287
    {
288
        if ($id === null) {
289
            $id = $this->owner->ID;
290
        }
291
        return Controller::curr()->Link().'myspecificpagemenuitems/'.$id.'/';
292
    }
293
}
294