Passed
Pull Request — master (#6)
by
unknown
06:16
created

CWPSiteConfigExtension::onAfterWrite()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace CWP\AgencyExtensions\Extensions;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\ORM\DataExtension;
7
use SilverStripe\Core\Environment;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Assets\File;
10
use SilverStripe\Forms\FileHandleField;
11
use SilverStripe\Assets\Image;
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\View\SSViewer;
14
use SilverStripe\Forms\TextField;
15
use SilverStripe\Versioned\Versioned;
16
17
/**
18
 * Class CWPCleanupSiteConfigExtension
19
 */
20
class CWPSiteConfigExtension extends DataExtension
21
{
22
    use Configurable;
23
24
    private static $hide_fields = [
25
        'AddThisProfileID',
26
        'LogoRetina',
27
        'FooterLogoRetina'
28
    ];
29
30
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
31
        'AddThisProfileID' => 'Varchar(32)',
32
        'FooterLogoLink' => 'Varchar(255)',
33
        'FooterLogoDescription' => 'Varchar(255)',
34
        'FooterLogoSecondaryLink' => 'Varchar(255)',
35
        'FooterLogoSecondaryDescription' => 'Varchar(255)',
36
        'EmptySearch' => 'Varchar(255)',
37
        'NoSearchResults' => 'Varchar(255)'
38
    );
39
40
    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...
41
        'Logo' => Image::class,
42
        'LogoRetina' => Image::class,
43
        'FooterLogo' => Image::class,
44
        'FooterLogoRetina' => Image::class,
45
        'FooterLogoSecondary' => Image::class,
46
        'FavIcon' => File::class,
47
        'AppleTouchIcon144' => File::class,
48
        'AppleTouchIcon114' => File::class,
49
        'AppleTouchIcon72' => File::class,
50
        'AppleTouchIcon57' => File::class
51
    );
52
53
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
54
        'Logo',
55
        'LogoRetina',
56
        'FooterLogo',
57
        'FooterLogoRetina',
58
        'FooterLogoSecondary',
59
        'FavIcon',
60
        'AppleTouchIcon144',
61
        'AppleTouchIcon114',
62
        'AppleTouchIcon72',
63
        'AppleTouchIcon57'
64
    ];
65
66
    /**
67
     * @param FieldList $fields
68
     */
69
    public function updateCMSFields(FieldList $fields)
70
    {
71
        $this
72
            ->addSocialMedia($fields)
73
            ->addLogosAndIcons($fields)
74
            ->addSearchOptions($fields)
75
            ->removeFieldsForCurrentTheme($fields);
76
    }
77
78
    /**
79
     * Remove fields from the given FieldList depending on the current theme and the configured fields to remove
80
     *
81
     * @param  FieldList $fields
82
     * @return $this
83
     */
84
    protected function removeFieldsForCurrentTheme(FieldList $fields)
85
    {
86
        foreach ((array)$this->config()->hide_fields as $fieldNames) {
87
            $fields->removeByName($fieldNames);
88
        }
89
        return $this;
90
    }
91
92
    /**
93
     * Add or extend social media fields
94
     *
95
     * @param  FieldList $fields
96
     * @return $this
97
     */
98
    protected function addSocialMedia(FieldList $fields)
99
    {
100
        $fields->addFieldToTab(
101
            'Root.SocialMedia',
102
            $addThisID = TextField::create(
103
                'AddThisProfileID',
0 ignored issues
show
Bug introduced by
'AddThisProfileID' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
                /** @scrutinizer ignore-type */ 'AddThisProfileID',
Loading history...
104
                _t(__CLASS__ . '.AddThisField', 'AddThis Profile ID')
105
            )
106
        );
107
        $addThisID->setRightTitle(
108
            _t(
109
                'CwpConfig.AddThisFieldDesc',
110
                'Profile ID to be used all across the site (in the format <strong>ra-XXXXXXXXXXXXXXXX</strong>)'
111
            )
112
        );
113
114
        return $this;
115
    }
116
117
    /**
118
     * Add fields for logo and icon uploads
119
     *
120
     * @param  FieldList $fields
121
     * @return $this
122
     */
123
    protected function addLogosAndIcons(FieldList $fields)
124
    {
125
        $logoTypes = array('jpg', 'jpeg', 'png', 'gif');
126
        $iconTypes = array('ico');
127
        $appleTouchTypes = array('png');
128
129
        $fields->findOrMakeTab(
130
            'Root.LogosIcons',
131
            _t(__CLASS__ . '.LogosIconsTab', 'Logos/Icons')
132
        );
133
134
        $fields->addFieldToTab(
135
            'Root.LogosIcons',
136
            $logoField = Injector::inst()->create(
137
                FileHandleField::class,
138
                'Logo',
139
                _t(__CLASS__ . '.LogoUploadField', 'Logo, to appear in the top left')
140
            )
141
        );
142
        $logoField->getValidator()->setAllowedExtensions($logoTypes);
143
144
        $fields->addFieldToTab(
145
            'Root.LogosIcons',
146
            $logoRetinaField = Injector::inst()->create(
147
                FileHandleField::class,
148
                'LogoRetina',
149
                _t(
150
                    'CwpConfig.LogoRetinaUploadField',
151
                    'High resolution logo, to appear in the top left ' .
152
                    '(recommended to be twice the height and width of the standard logo)'
153
                )
154
            )
155
        );
156
        $logoRetinaField->getValidator()->setAllowedExtensions($logoTypes);
157
158
        $fields->addFieldToTab(
159
            'Root.LogosIcons',
160
            $footerLogoField = Injector::inst()->create(
161
                FileHandleField::class,
162
                'FooterLogo',
163
                _t(__CLASS__ . '.FooterLogoField', 'Footer logo, to appear in the footer')
164
            )
165
        );
166
        $footerLogoField->getValidator()->setAllowedExtensions($logoTypes);
167
168
        $fields->addFieldToTab(
169
            'Root.LogosIcons',
170
            $footerLogoRetinaField = Injector::inst()->create(
171
                FileHandleField::class,
172
                'FooterLogoRetina',
173
                _t(
174
                    'CwpConfig.FooterLogoRetinaField',
175
                    'High resolution footer logo (recommended twice the height and width of the standard footer logo)'
176
                )
177
            )
178
        );
179
        $footerLogoRetinaField->getValidator()->setAllowedExtensions($logoTypes);
180
181
        $fields->addFieldToTab(
182
            'Root.LogosIcons',
183
            $footerLink = TextField::create(
184
                'FooterLogoLink',
0 ignored issues
show
Bug introduced by
'FooterLogoLink' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

184
                /** @scrutinizer ignore-type */ 'FooterLogoLink',
Loading history...
185
                _t(__CLASS__ . '.FooterLogoLinkField', 'Footer Logo link')
186
            )
187
        );
188
        $footerLink->setRightTitle(
189
            _t(
190
                'CwpConfig.FooterLogoLinkDesc',
191
                'Please include the protocol (ie, http:// or https://) unless it is an internal link.'
192
            )
193
        );
194
195
        $fields->addFieldToTab(
196
            'Root.LogosIcons',
197
            TextField::create(
198
                'FooterLogoDescription',
199
                _t(__CLASS__ . '.FooterLogoDescField', 'Footer Logo description')
200
            )
201
        );
202
203
        $fields->addFieldToTab(
204
            'Root.LogosIcons',
205
            $footerLogoSecondaryField = Injector::inst()->create(
206
                FileHandleField::class,
207
                'FooterLogoSecondary',
208
                _t(__CLASS__ . '.FooterLogoSecondaryField', 'Secondary Footer Logo, to appear in the footer.')
209
            )
210
        );
211
        $footerLogoSecondaryField->getValidator()->setAllowedExtensions($logoTypes);
212
213
        $fields->addFieldToTab('Root.LogosIcons', $footerSecondaryLink = TextField::create(
214
            'FooterLogoSecondaryLink',
215
            _t(__CLASS__ . '.FooterLogoSecondaryLinkField', 'Secondary Footer Logo link.')
216
        ));
217
        $footerSecondaryLink->setRightTitle(_t(
218
            'CwpConfig.FooterLogoSecondaryLinkDesc',
219
            'Please include the protocol (ie, http:// or https://) unless it is an internal link.'
220
        ));
221
        $fields->addFieldToTab('Root.LogosIcons', TextField::create(
222
            'FooterLogoSecondaryDescription',
223
            _t(__CLASS__ . '.FooterLogoSecondaryDescField', 'Secondary Footer Logo description')
224
        ));
225
226
        $fields->addFieldToTab(
227
            'Root.LogosIcons',
228
            $favIconField = Injector::inst()->create(
229
                FileHandleField::class,
230
                'FavIcon',
231
                _t(__CLASS__ . '.FavIconField', 'Favicon, in .ico format, dimensions of 16x16, 32x32, or 48x48')
232
            )
233
        );
234
        $favIconField->getValidator()->setAllowedExtensions($iconTypes);
235
236
        $fields->addFieldToTab(
237
            'Root.LogosIcons',
238
            $atIcon144 = Injector::inst()->create(
239
                FileHandleField::class,
240
                'AppleTouchIcon144',
241
                _t(
242
                    'CwpConfig.AppleIconField144',
243
                    'Apple Touch Web Clip and Windows 8 Tile Icon (dimensions of 144x144, PNG format)'
244
                )
245
            )
246
        );
247
        $atIcon144->getValidator()->setAllowedExtensions($appleTouchTypes);
248
249
        $fields->addFieldToTab(
250
            'Root.LogosIcons',
251
            $atIcon114 = Injector::inst()->create(
252
                FileHandleField::class,
253
                'AppleTouchIcon114',
254
                _t(__CLASS__ . '.AppleIconField114', 'Apple Touch Web Clip Icon (dimensions of 114x114, PNG format)')
255
            )
256
        );
257
        $atIcon114->getValidator()->setAllowedExtensions($appleTouchTypes);
258
259
        $fields->addFieldToTab(
260
            'Root.LogosIcons',
261
            $atIcon72 = Injector::inst()->create(
262
                FileHandleField::class,
263
                'AppleTouchIcon72',
264
                _t(__CLASS__ . '.AppleIconField72', 'Apple Touch Web Clip Icon (dimensions of 72x72, PNG format)')
265
            )
266
        );
267
        $atIcon72->getValidator()->setAllowedExtensions($appleTouchTypes);
268
269
        $fields->addFieldToTab(
270
            'Root.LogosIcons',
271
            $atIcon57 = Injector::inst()->create(
272
                FileHandleField::class,
273
                'AppleTouchIcon57',
274
                _t(__CLASS__ . '.AppleIconField57', 'Apple Touch Web Clip Icon (dimensions of 57x57, PNG format)')
275
            )
276
        );
277
        $atIcon57->getValidator()->setAllowedExtensions($appleTouchTypes);
278
279
        return $this;
280
    }
281
282
    /**
283
     * Add user configurable search field labels
284
     *
285
     * @param  FieldList $fields
286
     * @return $this
287
     */
288
    protected function addSearchOptions(FieldList $fields)
289
    {
290
        $fields->findOrMakeTab('Root.SearchOptions');
291
292
        $fields->addFieldToTab(
293
            'Root.SearchOptions',
294
            TextField::create(
295
                'EmptySearch',
0 ignored issues
show
Bug introduced by
'EmptySearch' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

295
                /** @scrutinizer ignore-type */ 'EmptySearch',
Loading history...
296
                _t(
297
                    'CWP.SITECONFIG.EmptySearch',
298
                    'Text to display when there is no search query'
299
                )
300
            )
301
        );
302
        $fields->addFieldToTab(
303
            'Root.SearchOptions',
304
            TextField::create(
305
                'NoSearchResults',
306
                _t(
307
                    'CWP.SITECONFIG.NoResult',
308
                    'Text to display when there are no results'
309
                )
310
            )
311
        );
312
313
        return $this;
314
    }
315
316
    public function onAfterWrite() {
317
        if (!$this->owner->hasExtension(Versioned::class)) {
318
            $this->owner->publishRecursive();
319
        }
320
    }
321
}
322