ColorpickerSiteConfigExtension   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 277
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 171
c 2
b 1
f 0
dl 0
loc 277
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 3 1
A getThemeOptionsExcluding() 0 14 3
A onBeforeWrite() 0 13 3
B addThemeColorPicker() 0 111 2
1
<?php
2
3
namespace SilverStripe\Colorpicker\Extensions;
4
5
use SilverStripe\Colorpicker\Forms\ColorPickerField;
6
use SilverStripe\Colorpicker\Forms\FontPickerField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Colorpicker\Forms\FontPickerField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\ORM\DataExtension;
9
10
/**
11
 * Class ColorpickerSiteConfigExtension
12
 * @package SilverStripe\Colorpicker\Extensions
13
 *
14
 * Adds color picker fields to the site config.
15
 */
16
class ColorpickerSiteConfigExtension extends DataExtension
17
{
18
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
        'HeaderBackground' => 'Varchar(50)',
20
        'NavigationBarBackground' => 'Varchar(50)',
21
        'CarouselBackground' => 'Varchar(50)',
22
        'FooterBackground' => 'Varchar(50)',
23
        'AccentColor' => 'Varchar(50)',
24
        'BannerBlockBackground' => 'Varchar(50)',
25
        'TextLinkColor' => 'Varchar(50)',
26
    );
27
28
    /**
29
     * Defines if the theme color picker is enabled in the CMS
30
     *
31
     * @config
32
     * @var boolean
33
     */
34
    private static $enable_theme_color_picker = false;
0 ignored issues
show
introduced by
The private property $enable_theme_color_picker is not used, and could be removed.
Loading history...
35
36
    /**
37
     * Defines the theme colors that can be selected via the CMS
38
     *
39
     * @config
40
     * @var array
41
     */
42
    private static $theme_colors = [
0 ignored issues
show
introduced by
The private property $theme_colors is not used, and could be removed.
Loading history...
43
        'default-accent' => [
44
            'Title' => 'Default',
45
            'CSSClass' => 'default-accent',
46
            'Color' => '#0F7EB2',
47
        ],
48
        'default-background' => [
49
            'Title' => 'Default',
50
            'CSSClass' => 'default-background',
51
            'Color' => '#001F2C',
52
        ],
53
        'red' => [
54
            'Title' => 'Red',
55
            'CSSClass' => 'red',
56
            'Color' => '#E51016',
57
        ],
58
        'dark-red' => [
59
            'Title' => 'Dark red',
60
            'CSSClass' => 'dark-red',
61
            'Color' => '#AD161E',
62
        ],
63
        'pink' => [
64
            'Title' => 'Pink',
65
            'CSSClass' => 'pink',
66
            'Color' => '#B32A95',
67
        ],
68
        'purple' => [
69
            'Title' => 'Purple',
70
            'CSSClass' => 'purple',
71
            'Color' => '#6239C8',
72
        ],
73
        'blue' => [
74
            'Title' => 'Blue',
75
            'CSSClass' => 'blue',
76
            'Color' => '#1F6BFE',
77
        ],
78
        'dark-blue' => [
79
            'Title' => 'Dark blue',
80
            'CSSClass' => 'dark-blue',
81
            'Color' => '#123581',
82
        ],
83
        'teal' => [
84
            'Title' => 'Teal',
85
            'CSSClass' => 'teal',
86
            'Color' => '#00837A',
87
        ],
88
        'green' => [
89
            'Title' => 'Green',
90
            'CSSClass' => 'green',
91
            'Color' => '#298436',
92
        ],
93
        'dark-orange' => [
94
            'Title' => 'Dark orange',
95
            'CSSClass' => 'dark-orange',
96
            'Color' => '#D34300',
97
        ],
98
        'dark-ochre' => [
99
            'Title' => 'Dark ochre',
100
            'CSSClass' => 'dark-ochre',
101
            'Color' => '#947200',
102
        ],
103
        'black' => [
104
            'Title' => 'Black',
105
            'CSSClass' => 'black',
106
            'Color' => '#111111',
107
        ],
108
        'dark-grey' => [
109
            'Title' => 'Dark grey',
110
            'CSSClass' => 'dark-grey',
111
            'Color' => '#555555',
112
        ],
113
        'light-grey' => [
114
            'Title' => 'Light grey',
115
            'CSSClass' => 'light-grey',
116
            'Color' => '#EAEAEA',
117
        ],
118
        'white' => [
119
            'Title' => 'White',
120
            'CSSClass' => 'white',
121
            'Color' => '#FFFFFF',
122
        ],
123
    ];
124
125
    /**
126
     * @param FieldList $fields
127
     */
128
    public function updateCMSFields(FieldList $fields)
129
    {
130
        $this->addThemeColorPicker($fields);
131
    }
132
133
    /**
134
     * Add fields for selecting the theme colors for different areas of the site.
135
     *
136
     * @param  FieldList $fields
137
     * @return $this
138
     */
139
    protected function addThemeColorPicker(FieldList $fields)
140
    {
141
        // Only show theme color selector if enabled
142
        if (!$this->owner->config()->get('enable_theme_color_picker')) {
143
            return $this;
144
        }
145
146
        $fields->addFieldsToTab(
147
            'Root.ThemeOptions',
148
            [
149
                ColorPickerField::create(
150
                    'HeaderBackground',
151
                    _t(
152
                        __CLASS__ . '.HeaderBackground',
153
                        'Header background'
154
                    ),
155
                    $this->getThemeOptionsExcluding([
156
                        'default-accent',
157
                    ])
158
                ),
159
                ColorPickerField::create(
160
                    'NavigationBarBackground',
161
                    _t(
162
                        __CLASS__ . '.NavigationBarBackground',
163
                        'Navigation bar background'
164
                    ),
165
                    $this->getThemeOptionsExcluding([
166
                        'default-accent',
167
                    ])
168
                ),
169
                ColorPickerField::create(
170
                    'CarouselBackground',
171
                    _t(
172
                        __CLASS__ . '.CarouselBackground',
173
                        'Carousel background'
174
                    ),
175
                    $this->getThemeOptionsExcluding([
176
                        'default-accent',
177
                    ])
178
                )->setDescription(
179
                    _t(
180
                        __CLASS__ . '.CarouselBackgroundDescription',
181
                        'The background color of the carousel when there is no image set.'
182
                    )
183
                ),
184
                ColorPickerField::create(
185
                    'FooterBackground',
186
                    _t(
187
                        __CLASS__ . '.FooterBackground',
188
                        'Footer background'
189
                    ),
190
                    $this->getThemeOptionsExcluding([
191
                        'light-grey',
192
                        'white',
193
                        'default-accent',
194
                    ])
195
                ),
196
                ColorPickerField::create(
197
                    'AccentColor',
198
                    _t(
199
                        __CLASS__ . '.AccentColor',
200
                        'Accent color'
201
                    ),
202
                    $this->getThemeOptionsExcluding([
203
                        'light-grey',
204
                        'white',
205
                        'default-background',
206
                    ])
207
                )->setDescription(
208
                    _t(
209
                        __CLASS__ . '.AccentColorDescription',
210
                        'Affects color of buttons, current navigation items, etc. '.
211
                        'Please ensure sufficient contrast with background colors.'
212
                    )
213
                ),
214
                ColorPickerField::create(
215
                    'BannerBlockBackground',
216
                    _t(
217
                        __CLASS__ . '.BannerBlockBackground',
218
                        'Banner block background'
219
                    ),
220
                    $this->getOwner()->getThemeOptionsExcluding([
221
                        'light-grey',
222
                        'white',
223
                        'default-background',
224
                        'default-accent'
225
                    ])
226
                )->setDescription(
227
                    _t(
228
                        __CLASS__ . '.BannerBlockBackgroundDescription',
229
                        'Background color of banner blocks.'
230
                    )
231
                ),
232
                ColorPickerField::create(
233
                    'TextLinkColor',
234
                    _t(
235
                        __CLASS__ . '.TextLinkColor',
236
                        'Text link color'
237
                    ),
238
                    $this->getThemeOptionsExcluding([
239
                        'black',
240
                        'light-grey',
241
                        'dark-grey',
242
                        'white',
243
                        'default-background',
244
                    ])
245
                ),
246
            ]
247
        );
248
249
        return $this;
250
    }
251
252
    /**
253
     * Returns theme_colors used for ColorPickerField.
254
     *
255
     * @param  array  $excludedColors list of colors to exclude from the returned options
256
     *                                based on the theme color's 'CSSClass' value
257
     * @return array
258
     */
259
    public function getThemeOptionsExcluding($excludedColors = [])
260
    {
261
        $themeColors = $this->owner->config()->get('theme_colors');
262
        $options = [];
263
264
        foreach ($themeColors as $themeColor) {
265
            if (in_array($themeColor['CSSClass'], $excludedColors)) {
266
                continue;
267
            }
268
269
            $options[] = $themeColor;
270
        }
271
272
        return $options;
273
    }
274
275
    /**
276
     * If HeaderBackground is not set, assume no theme colors exist and populate some defaults if the color
277
     * picker is enabled. We don't use populateDefaults() because we don't want SiteConfig to re-populate its own
278
     * defaults.
279
     */
280
    public function onBeforeWrite()
281
    {
282
        $colorPickerEnabled = $this->owner->config()->get('enable_theme_color_picker');
283
284
        if ($colorPickerEnabled && !$this->owner->HeaderBackground) {
285
            $this->owner->update([
286
                'HeaderBackground' => 'default-background',
287
                'NavigationBarBackground' => 'default-background',
288
                'CarouselBackground' => 'default-background',
289
                'FooterBackground' => 'default-background',
290
                'AccentColor' => 'default-accent',
291
                'TextLinkColor' => 'default-accent',
292
                'BannerBlockBackground' => 'dark-orange'
293
            ]);
294
        }
295
    }
296
}
297