Completed
Push — master ( 0d229b...c4ee3a )
by Werner
02:19
created

code/OnePageSlide.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class OnePageSlide extends DataExtension
4
{
5
6
    private static $db = array(
7
        'BackgroundColor' => 'Varchar',
8
        'HeadingColor' => 'Varchar',
9
        'TextColor' => 'Varchar',
10
        'AdditionalCSSClass' => 'Varchar'
11
    );
12
13
    private static $has_one = array(
14
        'BackgroundImage' => 'Image'
15
    );
16
17
    private static $background_color_palette = array(
18
        '#fff',
19
        '#444',
20
        '#000'
21
    );
22
    private static $heading_color_palette = array(
23
        '#000',
24
        '#fff'
25
    );
26
    private static $text_color_palette = array(
27
        '#000',
28
        '#fff'
29
    );
30
31
    /**
32
     * Should we modify the link to represent anchors?
33
     *
34
     * @var bool
35
     */
36
    private static $do_modify_link = true;
0 ignored issues
show
The property $do_modify_link 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...
37
38
    /**
39
     * limit the generated form fields to slides (direct children of a OnePageHolder)
40
     * @var bool
41
     */
42
    private static $use_only_on_onepage_slides = false;
43
44
    /**
45
     * do not require colors to be set
46
     * @var bool
47
     */
48
    private static $colors_can_be_empty = false;
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function updateFieldLabels(&$labels)
54
    {
55
        $labels = parent::updateFieldLabels($labels);
56
57
        $labels['Title'] = _t('OnePageSlide.db_Title', 'Title');
58
        $labels['BackgroundColor'] = _t('OnePageSlide.db_BackgroundColor', 'Background Color');
59
        $labels['HeadingColor'] = _t('OnePageSlide.db_HeadingColor', 'Heading Color');
60
        $labels['TextColor'] = _t('OnePageSlide.db_TextColor', 'Text Color');
61
        $labels['AdditionalCSSClass'] = _t('OnePageSlide.db_AdditionalCSSClass', 'Additional CSS class');
62
63
        $labels['BackgroundImage'] = _t('OnePageSlide.has_many_BackgroundImage', 'Background Image');
64
    }
65
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function updateCMSFields(FieldList $fields)
71
    {
72
        if (Config::inst()->get($this->class, 'use_only_on_onepage_slides')
73
            && !$this->owner->isOnePageSlide()) {
74
            return;
75
        }
76
77
        $image = UploadField::create('BackgroundImage', $this->owner->fieldLabel('BackgroundImage'))
78
            ->setAllowedFileCategories('image')
79
            ->setAllowedMaxFileNumber(1);
80
        if ($this->owner->hasMethod('getRootFolderName')) {
81
            $image->setFolderName($this->owner->getRootFolderName());
82
        }
83
84
        $colorFields = array(
85
            'BackgroundColor' => 'background_color_palette',
86
            'HeadingColor' => 'heading_color_palette',
87
            'TextColor' => 'text_color_palette'
88
        );
89
90
        $layout = $fields->findOrMakeTab('Root.Layout', _t('OnePageSlide.TABLAYOUT', 'Layout'));
91
        $layout->push($image);
92
93
        foreach ($colorFields as $fieldName => $palette) {
94
            $layout->push($this->generateColorPalette($fieldName, $palette));
95
        }
96
        $layout->push(TextField::create('AdditionalCSSClass', $this->owner->fieldLabel('AdditionalCSSClass')));
97
    }
98
99
    protected function generateColorPalette($fieldName, $paletteSetting)
100
    {
101
        $palette = $this->owner->config()->get($paletteSetting)
102
            ? $this->owner->config()->get($paletteSetting)
103
            : Config::inst()->get($this->class, $paletteSetting);
104
105
        $field = ColorPaletteField::create(
106
            $fieldName,
107
            $this->owner->fieldLabel($fieldName),
108
            ArrayLib::valuekey($palette)
109
        );
110
111
        if (Config::inst()->get($this->class, 'colors_can_be_empty')) {
112
            $field= $field->setEmptyString('none');
113
        }
114
115
        return $field;
116
    }
117
118
    //@todo: if Parent is a OnePageHolder modify $Link to show to $Parent->Link() / #$URLSegment
119
    //@todo: if Parent is a OnePageHolder disable ShowInMenus
120
    //@todo: don't show slide in google sitempap
121
122
    /**
123
     * @todo: use customCSS?
124
     * @return string
125
     */
126
    public function getOnePageSlideStyle()
127
    {
128
        $style = '';
129
130
        $style .= $this->owner->BackgroundColor
131
            ? 'background-color: ' . $this->owner->BackgroundColor . '; '
132
            : '';
133
134
        $style .= $this->owner->TextColor
135
            ? ' color: ' . $this->owner->TextColor. ' !important; '
136
            : '';
137
138
        $this->owner->extend('updateOnePageSlideStyle', $style);
139
140
        return $style;
141
    }
142
143
    /**
144
     * get's fired on ContentController::init()
145
     *
146
     * check if this is a OnePageSlide and redirect to parent if
147
     *  - controller has no action
148
     *  - request isn't an ajax request
149
     */
150
    public function contentcontrollerInit(&$controller)
151
    {
152
        if ($this->owner->isOnePageSlide() && $this->isCMSPreview()) {
153
            //redirect and pass current ID by param, as anchor tags re not sent to the server
154
            $url = Controller::join_links(
155
                $this->owner->RelativeLink(),
156
                '?EditPageID=' . $this->owner->ID,
157
                '?Stage=' . Versioned::current_stage(),
158
                '?CMSPreview=1'
159
            );
160
            $controller->redirect($url);
161
        }
162
163
        if ($this->owner->isOnePageSlide()
164
                && !$controller->urlParams['Action']
165
                && !Director::is_ajax()
166
                && !$this->isCMSPreview()
167
            ) {
168
            $controller->redirect($this->owner->RelativeLink(), 301);
169
        }
170
    }
171
172
173
    /**
174
     * Updates RelativeLink()
175
     *
176
     * If no $action is given it changes /path/to/URLSegment into /path/to#URLSegment
177
     *
178
     * @param $base
179
     * @param $action
180
     */
181
    public function updateRelativeLink(&$base, &$action)
182
    {
183
        //we need to call the redirection for cms preview
184
        if (Controller::curr() instanceof LeftAndMain) {
185
            return;
186
        }
187
188
        if (Config::inst()->get('OnePageSlide', 'do_modify_link') == false) {
189
            return;
190
        }
191
192
        if($this->owner->isNestedOnePageSlide()) {
193
            $base = $this->owner->Parent()->RelativeLink($action) . '-' . $this->owner->URLSegment;
194
            return;
195
        }
196
197
        if ($this->owner->isOnePageSlide()) {
198
            //			$base = $this->owner->Parent()->RelativeLink('#' . $this->owner->URLSegment); //e.g. /home/#urlsegment :(
199
            $base = Controller::join_links($this->owner->Parent()->RelativeLink($action), '#' . $this->owner->URLSegment); // just /#urlsegment
200
        }
201
    }
202
203
    /**
204
     * Helper to get a unmofified link if a slide should represent a classical page, not a "block" inside a OnePageHolder
205
     *
206
     * @param null $action
207
     * @return mixed
208
     */
209
    public function UnmodifiedRelativeLink($action = null)
210
    {
211
        Config::inst()->update('OnePageSlide', 'do_modify_link', false);
212
        $link = $this->owner->RelativeLink($action);
213
        Config::inst()->update('OnePageSlide', 'do_modify_link', true);
214
215
        return $link;
216
    }
217
218
    /**
219
     * Checks, if the current page is a slide of a one-page by checking if the parent page is a OnePageHolder
220
     *
221
     * @return bool
222
     */
223
    public function isOnePageSlide()
224
    {
225
        return ($this->owner->Parent() instanceof OnePageHolder);
226
    }
227
228
    /**
229
     * Checks if the current page is a nested one-page slide
230
     *
231
     * @return bool
232
     */
233
    public function isNestedOnePageSlide() {
234
        return $this->owner->ParentID
235
            ? $this->owner->Parent()->isOnePageSlide()
236
            : false;
237
    }
238
239
    /**
240
     * Helper to check if we're previewing the current page in CMS
241
     *
242
     * @return bool
243
     */
244
    public function isCMSPreview()
245
    {
246
        $isCMSPreview = Controller::curr()->getRequest()->getVar('CMSPreview');
247
248
        return (bool) $isCMSPreview;
249
    }
250
251
    /**
252
     * renders the current page using the ClassName_onepage template,
253
     * e.g. Page_onepage
254
     *
255
     * The suffix is generated by @link getOnePageTemplateSuffix
256
     *
257
     * @return HTMLText
258
     */
259
    public function getOnePageContent()
260
    {
261
        $templateName = SSViewer::get_templates_by_class($this->owner->Classname, $this->getOnePageTemplateSuffix(), 'SiteTree')
262
            ?: 'Page_onepage';
263
264
        $controller = ModelAsController::controller_for($this->owner);
265
266
        return $controller->renderWith($templateName);
267
    }
268
269
270
    /**
271
     * Helper function to generate the template suffix for the current page.
272
     * Calls page's "generateOnePageTemplateSuffix" method if it exists.
273
     * This way your page can define the template suffix to be e.g. '_layout1_onepage' instead of just '_onepage'
274
     *
275
     * @return string
276
     */
277
    public function getOnePageTemplateSuffix()
278
    {
279
        return $this->owner->hasMethod('generateOnePageTemplateSuffix')
280
            ? $this->owner->generateOnePageTemplateSuffix()
281
            : '_onepage';
282
    }
283
284
}
285