Completed
Pull Request — master (#27)
by
unknown
05:23
created

CWPSiteConfigExtension::addThemeColorPicker()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 109
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 74
nc 3
nop 1
dl 0
loc 109
rs 8.5672
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace CWP\AgencyExtensions\Extensions;
4
5
use CWP\AgencyExtensions\Forms\FontPickerField;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\FileHandleField;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\ORM\DataExtension;
13
use SilverStripe\Versioned\Versioned;
14
15
/**
16
 * Class CWPCleanupSiteConfigExtension
17
 */
18
class CWPSiteConfigExtension extends DataExtension
19
{
20
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
21
        'FooterLogoLink' => 'Varchar(255)',
22
        'FooterLogoDescription' => 'Varchar(255)',
23
        'FooterLogoSecondaryLink' => 'Varchar(255)',
24
        'FooterLogoSecondaryDescription' => 'Varchar(255)',
25
        'EmptySearch' => 'Varchar(255)',
26
        'NoSearchResults' => 'Varchar(255)',
27
        'MainFontFamily' => 'Varchar(50)',
28
        'HeaderBackground' => 'Varchar(50)',
29
        'NavigationBarBackground' => 'Varchar(50)',
30
        'CarouselBackground' => 'Varchar(50)',
31
        'FooterBackground' => 'Varchar(50)',
32
    );
33
34
    private static $has_one = array(
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
35
        'Logo' => Image::class,
36
        'LogoRetina' => Image::class,
37
        'FooterLogo' => Image::class,
38
        'FooterLogoRetina' => Image::class,
39
        'FooterLogoSecondary' => Image::class,
40
        'FavIcon' => File::class,
41
        'AppleTouchIcon144' => File::class,
42
        'AppleTouchIcon114' => File::class,
43
        'AppleTouchIcon72' => File::class,
44
        'AppleTouchIcon57' => File::class
45
    );
46
47
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
48
        'Logo',
49
        'LogoRetina',
50
        'FooterLogo',
51
        'FooterLogoRetina',
52
        'FooterLogoSecondary',
53
        'FavIcon',
54
        'AppleTouchIcon144',
55
        'AppleTouchIcon114',
56
        'AppleTouchIcon72',
57
        'AppleTouchIcon57'
58
    ];
59
60
    /**
61
     * Defines the theme fonts that can be selected via the CMS
62
     *
63
     * @config
64
     * @var array
65
     */
66
    private static $theme_fonts = [
0 ignored issues
show
introduced by
The private property $theme_fonts is not used, and could be removed.
Loading history...
67
        'nunito-sans' => 'Nunito Sans',
68
        'fira-sans' => 'Fira Sans',
69
        'merriweather' => 'Merriweather',
70
    ];
71
72
    /**
73
     * @param FieldList $fields
74
     */
75
    public function updateCMSFields(FieldList $fields)
76
    {
77
        $this
78
            ->addLogosAndIcons($fields)
79
            ->addSearchOptions($fields)
80
            ->addFontPicker($fields);
81
    }
82
83
    /**
84
     * Add fields for selecting the font for different areas of the site.
85
     *
86
     * @param  FieldList $fields
87
     * @return $this
88
     */
89
    protected function addFontPicker(FieldList $fields)
90
    {
91
        $fonts = $this->owner->config()->get('theme_fonts');
92
93
        // Import each font via the google fonts api to render font preview
94
        foreach ($fonts as $fontTitle) {
95
            $fontFamilyName = str_replace(' ', '+', $fontTitle);
96
            Requirements::css("//fonts.googleapis.com/css?family=$fontFamilyName");
0 ignored issues
show
Bug introduced by
The type CWP\AgencyExtensions\Extensions\Requirements 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...
97
        }
98
99
        $fields->addFieldsToTab(
100
            'Root.ThemeOptions',
101
            [
102
                FontPickerField::create(
103
                    'MainFontFamily',
104
                    _t(
105
                        __CLASS__ . '.MainFontFamily',
106
                        'Main font family'
107
                    ),
108
                    $fonts
109
                )
110
            ]
111
        );
112
113
        return $this;
114
    }
115
116
    /**
117
     * Add fields for logo and icon uploads
118
     *
119
     * @param  FieldList $fields
120
     * @return $this
121
     */
122
    protected function addLogosAndIcons(FieldList $fields)
123
    {
124
        $logoTypes = array('jpg', 'jpeg', 'png', 'gif');
125
        $iconTypes = array('ico');
126
        $appleTouchTypes = array('png');
127
128
        $fields->findOrMakeTab(
129
            'Root.LogosIcons',
130
            _t(__CLASS__ . '.LogosIconsTab', 'Logos/Icons')
131
        );
132
133
        $fields->addFieldToTab(
134
            'Root.LogosIcons',
135
            $logoField = Injector::inst()->create(
136
                FileHandleField::class,
137
                'Logo',
138
                _t(__CLASS__ . '.LogoUploadField', 'Logo, to appear in the top left')
139
            )
140
        );
141
        $logoField->getValidator()->setAllowedExtensions($logoTypes);
142
143
        $fields->addFieldToTab(
144
            'Root.LogosIcons',
145
            $logoRetinaField = Injector::inst()->create(
146
                FileHandleField::class,
147
                'LogoRetina',
148
                _t(
149
                    'CwpConfig.LogoRetinaUploadField',
150
                    'High resolution logo, to appear in the top left ' .
151
                    '(recommended to be twice the height and width of the standard logo)'
152
                )
153
            )
154
        );
155
        $logoRetinaField->getValidator()->setAllowedExtensions($logoTypes);
156
157
        $fields->addFieldToTab(
158
            'Root.LogosIcons',
159
            $footerLogoField = Injector::inst()->create(
160
                FileHandleField::class,
161
                'FooterLogo',
162
                _t(__CLASS__ . '.FooterLogoField', 'Footer logo, to appear in the footer')
163
            )
164
        );
165
        $footerLogoField->getValidator()->setAllowedExtensions($logoTypes);
166
167
        $fields->addFieldToTab(
168
            'Root.LogosIcons',
169
            $footerLogoRetinaField = Injector::inst()->create(
170
                FileHandleField::class,
171
                'FooterLogoRetina',
172
                _t(
173
                    'CwpConfig.FooterLogoRetinaField',
174
                    'High resolution footer logo (recommended twice the height and width of the standard footer logo)'
175
                )
176
            )
177
        );
178
        $footerLogoRetinaField->getValidator()->setAllowedExtensions($logoTypes);
179
180
        $fields->addFieldToTab(
181
            'Root.LogosIcons',
182
            $footerLink = TextField::create(
183
                'FooterLogoLink',
184
                _t(__CLASS__ . '.FooterLogoLinkField', 'Footer Logo link')
185
            )
186
        );
187
        $footerLink->setRightTitle(
188
            _t(
189
                'CwpConfig.FooterLogoLinkDesc',
190
                'Please include the protocol (ie, http:// or https://) unless it is an internal link.'
191
            )
192
        );
193
194
        $fields->addFieldToTab(
195
            'Root.LogosIcons',
196
            TextField::create(
197
                'FooterLogoDescription',
198
                _t(__CLASS__ . '.FooterLogoDescField', 'Footer Logo description')
199
            )
200
        );
201
202
        $fields->addFieldToTab(
203
            'Root.LogosIcons',
204
            $footerLogoSecondaryField = Injector::inst()->create(
205
                FileHandleField::class,
206
                'FooterLogoSecondary',
207
                _t(__CLASS__ . '.FooterLogoSecondaryField', 'Secondary Footer Logo, to appear in the footer.')
208
            )
209
        );
210
        $footerLogoSecondaryField->getValidator()->setAllowedExtensions($logoTypes);
211
212
        $fields->addFieldToTab('Root.LogosIcons', $footerSecondaryLink = TextField::create(
213
            'FooterLogoSecondaryLink',
214
            _t(__CLASS__ . '.FooterLogoSecondaryLinkField', 'Secondary Footer Logo link.')
215
        ));
216
        $footerSecondaryLink->setRightTitle(_t(
217
            'CwpConfig.FooterLogoSecondaryLinkDesc',
218
            'Please include the protocol (ie, http:// or https://) unless it is an internal link.'
219
        ));
220
        $fields->addFieldToTab('Root.LogosIcons', TextField::create(
221
            'FooterLogoSecondaryDescription',
222
            _t(__CLASS__ . '.FooterLogoSecondaryDescField', 'Secondary Footer Logo description')
223
        ));
224
225
        $fields->addFieldToTab(
226
            'Root.LogosIcons',
227
            $favIconField = Injector::inst()->create(
228
                FileHandleField::class,
229
                'FavIcon',
230
                _t(__CLASS__ . '.FavIconField', 'Favicon, in .ico format, dimensions of 16x16, 32x32, or 48x48')
231
            )
232
        );
233
        $favIconField->getValidator()->setAllowedExtensions($iconTypes);
234
235
        $fields->addFieldToTab(
236
            'Root.LogosIcons',
237
            $atIcon144 = Injector::inst()->create(
238
                FileHandleField::class,
239
                'AppleTouchIcon144',
240
                _t(
241
                    'CwpConfig.AppleIconField144',
242
                    'Apple Touch Web Clip and Windows 8 Tile Icon (dimensions of 144x144, PNG format)'
243
                )
244
            )
245
        );
246
        $atIcon144->getValidator()->setAllowedExtensions($appleTouchTypes);
247
248
        $fields->addFieldToTab(
249
            'Root.LogosIcons',
250
            $atIcon114 = Injector::inst()->create(
251
                FileHandleField::class,
252
                'AppleTouchIcon114',
253
                _t(__CLASS__ . '.AppleIconField114', 'Apple Touch Web Clip Icon (dimensions of 114x114, PNG format)')
254
            )
255
        );
256
        $atIcon114->getValidator()->setAllowedExtensions($appleTouchTypes);
257
258
        $fields->addFieldToTab(
259
            'Root.LogosIcons',
260
            $atIcon72 = Injector::inst()->create(
261
                FileHandleField::class,
262
                'AppleTouchIcon72',
263
                _t(__CLASS__ . '.AppleIconField72', 'Apple Touch Web Clip Icon (dimensions of 72x72, PNG format)')
264
            )
265
        );
266
        $atIcon72->getValidator()->setAllowedExtensions($appleTouchTypes);
267
268
        $fields->addFieldToTab(
269
            'Root.LogosIcons',
270
            $atIcon57 = Injector::inst()->create(
271
                FileHandleField::class,
272
                'AppleTouchIcon57',
273
                _t(__CLASS__ . '.AppleIconField57', 'Apple Touch Web Clip Icon (dimensions of 57x57, PNG format)')
274
            )
275
        );
276
        $atIcon57->getValidator()->setAllowedExtensions($appleTouchTypes);
277
278
        return $this;
279
    }
280
281
    /**
282
     * Add user configurable search field labels
283
     *
284
     * @param  FieldList $fields
285
     * @return $this
286
     */
287
    protected function addSearchOptions(FieldList $fields)
288
    {
289
        $fields->findOrMakeTab('Root.SearchOptions');
290
291
        $fields->addFieldToTab(
292
            'Root.SearchOptions',
293
            TextField::create(
294
                'EmptySearch',
295
                _t(
296
                    'CWP.SITECONFIG.EmptySearch',
297
                    'Text to display when there is no search query'
298
                )
299
            )
300
        );
301
        $fields->addFieldToTab(
302
            'Root.SearchOptions',
303
            TextField::create(
304
                'NoSearchResults',
305
                _t(
306
                    'CWP.SITECONFIG.NoResult',
307
                    'Text to display when there are no results'
308
                )
309
            )
310
        );
311
312
        return $this;
313
    }
314
315
    /**
316
     * Auto-publish any images attached to the SiteConfig object if it's not versioned. Versioned objects will
317
     * handle their related objects via the "owns" API by default.
318
     */
319
    public function onAfterWrite()
320
    {
321
        if (!$this->owner->hasExtension(Versioned::class)) {
322
            $this->owner->publishRecursive();
323
        }
324
    }
325
326
    /**
327
     * If MainFontFamily is not set, populate the defaults.
328
     * We don't use populateDefaults() because we don't want SiteConfig to re-populate its own defaults.
329
     */
330
    public function onBeforeWrite()
331
    {
332
        if (!$this->owner->MainFontFamily) {
333
            $this->owner->update([
334
                'MainFontFamily' => FontPickerField::DEFAULT_VALUE,
335
            ]);
336
        }
337
    }
338
}
339