Completed
Push — master ( 385c28...0d229b )
by Werner
02:31
created

OnePageSlide::isNestedOnePageSlide()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
class OnePageSlide 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...
4
{
5
6
    private static $db = array(
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...
7
        'BackgroundColor' => 'Varchar',
8
        'HeadingColor' => 'Varchar',
9
        'TextColor' => 'Varchar',
10
        'AdditionalCSSClass' => 'Varchar'
11
    );
12
13
    private static $has_one = array(
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...
14
        'BackgroundImage' => 'Image'
15
    );
16
17
    private static $background_color_palette = array(
0 ignored issues
show
Unused Code introduced by
The property $background_color_palette 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...
18
        '#fff',
19
        '#444',
20
        '#000'
21
    );
22
    private static $heading_color_palette = array(
0 ignored issues
show
Unused Code introduced by
The property $heading_color_palette 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...
23
        '#000',
24
        '#fff'
25
    );
26
    private static $text_color_palette = array(
0 ignored issues
show
Unused Code introduced by
The property $text_color_palette 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...
27
        '#000',
28
        '#fff'
29
    );
30
31
    /**
32
     * limit the generated form fields to slides (direct children of a OnePageHolder)
33
     * @var bool
34
     */
35
    private static $use_only_on_onepage_slides = false;
0 ignored issues
show
Unused Code introduced by
The property $use_only_on_onepage_slides 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...
36
37
    /**
38
     * do not require colors to be set
39
     * @var bool
40
     */
41
    private static $colors_can_be_empty = false;
0 ignored issues
show
Unused Code introduced by
The property $colors_can_be_empty 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
    /**
44
     * @inheritdoc
45
     */
46
    public function updateFieldLabels(&$labels)
47
    {
48
        $labels = parent::updateFieldLabels($labels);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $labels is correct as parent::updateFieldLabels($labels) (which targets DataExtension::updateFieldLabels()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
49
50
        $labels['Title'] = _t('OnePageSlide.db_Title', 'Title');
51
        $labels['BackgroundColor'] = _t('OnePageSlide.db_BackgroundColor', 'Background Color');
52
        $labels['HeadingColor'] = _t('OnePageSlide.db_HeadingColor', 'Heading Color');
53
        $labels['TextColor'] = _t('OnePageSlide.db_TextColor', 'Text Color');
54
        $labels['AdditionalCSSClass'] = _t('OnePageSlide.db_AdditionalCSSClass', 'Additional CSS class');
55
56
        $labels['BackgroundImage'] = _t('OnePageSlide.has_many_BackgroundImage', 'Background Image');
57
    }
58
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function updateCMSFields(FieldList $fields)
64
    {
65
        if (Config::inst()->get($this->class, 'use_only_on_onepage_slides')
66
            && !$this->owner->isOnePageSlide()) {
67
            return;
68
        }
69
70
        $image = UploadField::create('BackgroundImage', $this->owner->fieldLabel('BackgroundImage'))
71
            ->setAllowedFileCategories('image')
72
            ->setAllowedMaxFileNumber(1);
73
        if ($this->owner->hasMethod('getRootFolderName')) {
74
            $image->setFolderName($this->owner->getRootFolderName());
75
        }
76
77
        $colorFields = array(
78
            'BackgroundColor' => 'background_color_palette',
79
            'HeadingColor' => 'heading_color_palette',
80
            'TextColor' => 'text_color_palette'
81
        );
82
83
        $layout = $fields->findOrMakeTab('Root.Layout', _t('OnePageSlide.TABLAYOUT', 'Layout'));
84
        $layout->push($image);
85
86
        foreach ($colorFields as $fieldName => $palette) {
87
            $layout->push($this->generateColorPalette($fieldName, $palette));
88
        }
89
        $layout->push(TextField::create('AdditionalCSSClass', $this->owner->fieldLabel('AdditionalCSSClass')));
90
    }
91
92
    protected function generateColorPalette($fieldName, $paletteSetting)
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...
93
    {
94
        $palette = $this->owner->config()->get($paletteSetting)
95
            ? $this->owner->config()->get($paletteSetting)
96
            : Config::inst()->get($this->class, $paletteSetting);
97
98
        $field = ColorPaletteField::create(
99
            $fieldName,
100
            $this->owner->fieldLabel($fieldName),
101
            ArrayLib::valuekey($palette)
102
        );
103
104
        if (Config::inst()->get($this->class, 'colors_can_be_empty')) {
105
            $field= $field->setEmptyString('none');
106
        }
107
108
        return $field;
109
    }
110
111
    //@todo: if Parent is a OnePageHolder modify $Link to show to $Parent->Link() / #$URLSegment
112
    //@todo: if Parent is a OnePageHolder disable ShowInMenus
113
    //@todo: don't show slide in google sitempap
114
115
    /**
116
     * @todo: use customCSS?
117
     * @return string
118
     */
119
    public function getOnePageSlideStyle()
120
    {
121
        $style = '';
122
123
        $style .= $this->owner->BackgroundColor
124
            ? 'background-color: ' . $this->owner->BackgroundColor . '; '
125
            : '';
126
127
        $style .= $this->owner->TextColor
128
            ? ' color: ' . $this->owner->TextColor. ' !important; '
129
            : '';
130
131
        $this->owner->extend('updateOnePageSlideStyle', $style);
132
133
        return $style;
134
    }
135
136
    /**
137
     * get's fired on ContentController::init()
138
     *
139
     * check if this is a OnePageSlide and redirect to parent if
140
     *  - controller has no action
141
     *  - request isn't an ajax request
142
     */
143
    public function contentcontrollerInit(&$controller)
144
    {
145
        if ($this->owner->isOnePageSlide() && $this->isCMSPreview()) {
146
            //redirect and pass current ID by param, as anchor tags re not sent to the server
147
            $url = Controller::join_links(
148
                $this->owner->RelativeLink(),
149
                '?EditPageID=' . $this->owner->ID,
150
                '?Stage=' . Versioned::current_stage(),
151
                '?CMSPreview=1'
152
            );
153
            $controller->redirect($url);
154
        }
155
156
        if ($this->owner->isOnePageSlide()
157
                && !$controller->urlParams['Action']
158
                && !Director::is_ajax()
159
                && !$this->isCMSPreview()
160
            ) {
161
            $controller->redirect($this->owner->RelativeLink(), 301);
162
        }
163
    }
164
165
    /**
166
     * Udates RelativeLink()
167
     *
168
     * If no $action is given it changes /path/to/URLSegment into /path/to#URLSegment
169
     *
170
     * @param $base
171
     * @param $action
172
     */
173
    public function updateRelativeLink(&$base, &$action)
174
    {
175
        //we need to call the redirection for cms preview
176
        if (Controller::curr() instanceof LeftAndMain) {
177
            return;
178
        }
179
180
181
        if($this->owner->isNestedOnePageSlide()) {
182
            $base = $this->owner->Parent()->RelativeLink($action) . '-' . $this->owner->URLSegment;
183
            return;
184
        }
185
186
        if ($this->owner->isOnePageSlide()) {
187
            //			$base = $this->owner->Parent()->RelativeLink('#' . $this->owner->URLSegment); //e.g. /home/#urlsegment :(
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
188
            $base = Controller::join_links($this->owner->Parent()->RelativeLink($action), '#' . $this->owner->URLSegment); // just /#urlsegment
189
        }
190
    }
191
192
193
    /**
194
     * Checks, if the current page is a slide of a one-page by checking if the parent page is a OnePageHolder
195
     *
196
     * @return bool
197
     */
198
    public function isOnePageSlide()
199
    {
200
        return ($this->owner->Parent() instanceof OnePageHolder);
201
    }
202
203
    /**
204
     * Checks if the current page is a nested one-page slide
205
     *
206
     * @return bool
207
     */
208
    public function isNestedOnePageSlide() {
209
        return $this->owner->ParentID
210
            ? $this->owner->Parent()->isOnePageSlide()
211
            : false;
212
    }
213
214
    /**
215
     * Helper to check if we're previewing the current page in CMS
216
     *
217
     * @return bool
218
     */
219
    public function isCMSPreview()
220
    {
221
        $isCMSPreview = Controller::curr()->getRequest()->getVar('CMSPreview');
222
223
        return (bool) $isCMSPreview;
224
    }
225
226
    /**
227
     * renders the current page using the ClassName_onepage template,
228
     * e.g. Page_onepage
229
     *
230
     * The suffix is generated by @link getOnePageTemplateSuffix
231
     *
232
     * @return HTMLText
233
     */
234
    public function getOnePageContent()
235
    {
236
        $templateName = SSViewer::get_templates_by_class($this->owner->Classname, $this->getOnePageTemplateSuffix(), 'SiteTree')
237
            ?: 'Page_onepage';
238
239
        $controller = ModelAsController::controller_for($this->owner);
240
241
        return $controller->renderWith($templateName);
242
    }
243
244
245
    /**
246
     * Helper function to generate the template suffix for the current page.
247
     * Calls page's "generateOnePageTemplateSuffix" method if it exists.
248
     * This way your page can define the template suffix to be e.g. '_layout1_onepage' instead of just '_onepage'
249
     *
250
     * @return string
251
     */
252
    public function getOnePageTemplateSuffix()
253
    {
254
        return $this->owner->hasMethod('generateOnePageTemplateSuffix')
255
            ? $this->owner->generateOnePageTemplateSuffix()
256
            : '_onepage';
257
    }
258
259
}
260