Completed
Push — master ( 89f62d...6e0e08 )
by Nicola
02:25
created

code/CarouselPage.php (3 issues)

Severity

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 CarouselImageExtension extends DataExtension
4
{
5
6
    /**
7
     * If $width and $height are greater than 0, it is equivalent to
8
     * Image_Backend::croppedResize().
9
     *
10
     * If only $width is greater than 0, it is equivalent to SetWidth().
11
     *
12
     * If only $height is greater than 0, it is equivalent to
13
     * SetHeight().
14
     *
15
     * If neither $width or $height are greater than 0, return the
16
     * original image.
17
     *
18
     * @param  integer $width   The width to set or 0.
19
     * @param  integer $height  The height to set or 0.
20
     * @return Image_Backend
21
     */
22
    public function MaybeCroppedImage($width, $height)
23
    {
24
        return $this->owner->getFormattedImage('MaybeCroppedImage', $width, $height);
25
    }
26
27
    public function generateMaybeCroppedImage(Image_Backend $backend, $width, $height)
28
    {
29
        if ($width > 0 && $height > 0) {
30
            return $backend->croppedResize($width, $height);
31
        } elseif ($width > 0) {
32
            return $backend->resizeByWidth($width);
33
        } elseif ($height > 0) {
34
            return $backend->resizeByHeight($height);
35
        } else {
36
            return $backend;
37
        }
38
    }
39
}
40
41
/**
42
 * This class is needed in order to handle the 'Content' field of the
43
 * 'File' table with the WYSIWYG editor. That field is TEXT, hence just
44
 * using HtmlEditorField will result in an error when the 'saveInto'
45
 * method is called.
46
 */
47
class CarouselCaptionField extends HtmlEditorField
48
{
49
50
    public function __construct($name, $title = null, $value = '')
51
    {
52
        parent::__construct($name, $title, $value);
53
        $this->rows = 5;
54
        // The .htmleditor class enables TinyMCE
55
        $this->addExtraClass('htmleditor');
56
    }
57
58
    /**
59
     * Implementation directly borrowed from HtmlEditorField
60
     * without the blocking or useless code.
61
     */
62
    public function saveInto(DataObjectInterface $record)
63
    {
64
        $htmlValue = Injector::inst()->create('HTMLValue', $this->value);
65
66
        // Sanitise if requested
67
        if ($this->config()->sanitise_server_side) {
68
            $santiser = Injector::inst()->create('HtmlEditorSanitiser', HtmlEditorConfig::get_active());
69
            $santiser->sanitise($htmlValue);
70
        }
71
72
        $this->extend('processHTML', $htmlValue);
73
        $record->{$this->name} = $htmlValue->getContent();
74
    }
75
}
76
77
class CarouselPage extends Page
78
{
79
80
    private static $icon = 'carousel/img/carousel.png';
81
82
    private static $db = array(
83
        'Captions' => 'Boolean',
84
        'Width'    => 'Int',
85
        'Height'   => 'Int',
86
    );
87
88
    private static $many_many = array(
89
        'Images'   => 'Image',
90
    );
91
92
    private static $many_many_extraFields = array(
93
        'Images'   => array(
94
            'SortOrder' => 'Int',
95
        ),
96
    );
97
98
    /**
99
     * Search the first class name (that must have a 'Page' suffix) in
100
     * the object hierarchy that has a correspoding folder in
101
     * ASSETS_PATH, that is a folder with the same name with the 'Page'
102
     * suffix stripped out. This folder will be returned and used as
103
     * custom folder in the upload field.
104
     *
105
     * For example, if this class is `HomePage` and it is inherited from
106
     * `CarouselPage`, this function will check for `Home` first and
107
     * `Carousel` after.
108
     *
109
     * If no valid folders are found, `false` is returned.
110
     */
111
    protected function getClassFolder()
0 ignored issues
show
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...
112
    {
113
        for ($class = $this->class; $class; $class = get_parent_class($class)) {
114
            $folder = preg_replace('/Page$/', '', $class);
115
            if ($folder != $class && is_dir(ASSETS_PATH . '/' . $folder)) {
116
                return $folder;
117
            }
118
        }
119
120
        // false is the proper value to set in setFolderName()
121
        // to get the default folder (usually 'Uploads').
122
        return false;
123
    }
124
125
    public function getCMSFields()
0 ignored issues
show
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...
126
    {
127
        $fields = parent::getCMSFields();
128
129
        $field = SortableUploadField::create('Images', _t('CarouselPage.db_Images'));
130
        $field->setFolderName($this->getClassFolder());
131
132
        // Enable HTML caption handling if captions are enabled
133
        if ($this->Captions) {
134
            $caption = CarouselCaptionField::create('Content', _t('CarouselPage.Caption'));
135
            $field->setFileEditFields(FieldList::create($caption));
136
            unset($caption);
137
        }
138
139
        $root = $fields->fieldByName('Root');
140
        $tab = $root->fieldByName('Images');
141
        if (! $tab) {
142
            $tab = Tab::create('Images');
143
            $tab->setTitle(_t('CarouselPage.db_Images'));
144
            $root->insertAfter($tab, 'Main');
145
        }
146
        $tab->push($field);
147
148
        return $fields;
149
    }
150
151
    public function getSettingsFields()
0 ignored issues
show
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...
152
    {
153
        $fields = parent::getSettingsFields();
154
155
        $settings = FieldGroup::create(
156
            FieldGroup::create(
157
                NumericField::create('Width', _t('CarouselPage.db_Width')),
158
                NumericField::create('Height', _t('CarouselPage.db_Height')),
159
                CheckboxField::create('Captions', _t('CarouselPage.db_Captions'))
160
            )
161
        );
162
        $settings->setName('Carousel');
163
        $settings->setTitle(_t('CarouselPage.SINGULARNAME'));
164
        $fields->addFieldToTab('Root.Settings', $settings);
165
166
        return $fields;
167
    }
168
169
    public function getCMSValidator()
170
    {
171
        return RequiredFields::create(
172
            'ThumbnailWidth',
173
            'ThumbnailHeight'
174
        );
175
    }
176
177
    /**
178
     * Out of the box support for silverstripe/silverstripe-translatable.
179
     *
180
     * It the translatable module is not used, this will simply be a
181
     * dead method.
182
    */
183
    public function onTranslatableCreate($save)
184
    {
185
        // Chain up the parent method, if it exists
186
        if (method_exists('Page', 'onTranslatableCreate')) {
187
            parent::onTranslatableCreate($save);
188
        }
189
190
        $master = $this->getTranslation(Translatable::default_locale());
191
192
        foreach ($master->Images() as $master_image) {
193
            $image = $master_image->duplicate($save);
194
            $this->Images()->add($image);
195
        }
196
    }
197
}
198
199
class CarouselPage_Controller extends Page_Controller
200
{
201
202
    /**
203
     * From the controller the images are returned in proper order.
204
     * This means `<% loop $Images %>` returns the expected result.
205
     */
206
    public function Images()
207
    {
208
        return $this->dataRecord->Images()->Sort('SortOrder');
209
    }
210
}
211