Completed
Push — master ( 86c16b...d765f6 )
by Robbie
7s
created

removeFieldsForCurrentTheme()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 9.4285
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
16
/**
17
 * Class CWPCleanupSiteConfigExtension
18
 */
19
class CWPSiteConfigExtension extends DataExtension
20
{
21
    use Configurable;
22
23
    private static $hide_fields = [
24
        'AddThisProfileID',
25
        'LogoRetina',
26
        'FooterLogoRetina'
27
    ];
28
29
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
30
        'AddThisProfileID' => 'Varchar(32)',
31
        'FooterLogoLink' => 'Varchar(255)',
32
        'FooterLogoDescription' => 'Varchar(255)',
33
        'FooterLogoSecondaryLink' => 'Varchar(255)',
34
        'FooterLogoSecondaryDescription' => 'Varchar(255)',
35
        'EmptySearch' => 'Varchar(255)',
36
        'NoSearchResults' => 'Varchar(255)'
37
    );
38
39
    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...
40
        'Logo' => Image::class,
41
        'LogoRetina' => Image::class,
42
        'FooterLogo' => Image::class,
43
        'FooterLogoRetina' => Image::class,
44
        'FooterLogoSecondary' => Image::class,
45
        'FavIcon' => File::class,
46
        'AppleTouchIcon144' => File::class,
47
        'AppleTouchIcon114' => File::class,
48
        'AppleTouchIcon72' => File::class,
49
        'AppleTouchIcon57' => File::class
50
    );
51
52
    /**
53
     * @param FieldList $fields
54
     */
55
    public function updateCMSFields(FieldList $fields)
56
    {
57
        $this
58
            ->addSocialMedia($fields)
59
            ->addLogosAndIcons($fields)
60
            ->addSearchOptions($fields)
61
            ->removeFieldsForCurrentTheme($fields);
62
    }
63
64
    /**
65
     * Remove fields from the given FieldList depending on the current theme and the configured fields to remove
66
     *
67
     * @param  FieldList $fields
68
     * @return $this
69
     */
70
    protected function removeFieldsForCurrentTheme(FieldList $fields)
71
    {
72
        foreach ((array)$this->config()->hide_fields as $fieldNames) {
73
            $fields->removeByName($fieldNames);
74
        }
75
        return $this;
76
    }
77
78
    /**
79
     * Add or extend social media fields
80
     *
81
     * @param  FieldList $fields
82
     * @return $this
83
     */
84
    protected function addSocialMedia(FieldList $fields)
85
    {
86
        $fields->addFieldToTab(
87
            'Root.SocialMedia',
88
            $addThisID = TextField::create(
89
                '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

89
                /** @scrutinizer ignore-type */ 'AddThisProfileID',
Loading history...
90
                _t(__CLASS__ . '.AddThisField', 'AddThis Profile ID')
91
            )
92
        );
93
        $addThisID->setRightTitle(
94
            _t(
95
                'CwpConfig.AddThisFieldDesc',
96
                'Profile ID to be used all across the site (in the format <strong>ra-XXXXXXXXXXXXXXXX</strong>)'
97
            )
98
        );
99
100
        return $this;
101
    }
102
103
    /**
104
     * Add fields for logo and icon uploads
105
     *
106
     * @param  FieldList $fields
107
     * @return $this
108
     */
109
    protected function addLogosAndIcons(FieldList $fields)
110
    {
111
        $logoTypes = array('jpg', 'jpeg', 'png', 'gif');
112
        $iconTypes = array('ico');
113
        $appleTouchTypes = array('png');
114
115
        $fields->findOrMakeTab(
116
            'Root.LogosIcons',
117
            _t(__CLASS__ . '.LogosIconsTab', 'Logos/Icons')
118
        );
119
120
        $fields->addFieldToTab(
121
            'Root.LogosIcons',
122
            $logoField = Injector::inst()->create(
123
                FileHandleField::class,
124
                'Logo',
125
                _t(__CLASS__ . '.LogoUploadField', 'Logo, to appear in the top left')
126
            )
127
        );
128
        $logoField->getValidator()->setAllowedExtensions($logoTypes);
129
130
        $fields->addFieldToTab(
131
            'Root.LogosIcons',
132
            $logoRetinaField = Injector::inst()->create(
133
                FileHandleField::class,
134
                'LogoRetina',
135
                _t(
136
                    'CwpConfig.LogoRetinaUploadField',
137
                    'High resolution logo, to appear in the top left ' .
138
                    '(recommended to be twice the height and width of the standard logo)'
139
                )
140
            )
141
        );
142
        $logoRetinaField->getValidator()->setAllowedExtensions($logoTypes);
143
144
        $fields->addFieldToTab(
145
            'Root.LogosIcons',
146
            $footerLogoField = Injector::inst()->create(
147
                FileHandleField::class,
148
                'FooterLogo',
149
                _t(__CLASS__ . '.FooterLogoField', 'Footer logo, to appear in the footer')
150
            )
151
        );
152
        $footerLogoField->getValidator()->setAllowedExtensions($logoTypes);
153
154
        $fields->addFieldToTab(
155
            'Root.LogosIcons',
156
            $footerLogoRetinaField = Injector::inst()->create(
157
                FileHandleField::class,
158
                'FooterLogoRetina',
159
                _t(
160
                    'CwpConfig.FooterLogoRetinaField',
161
                    'High resolution footer logo (recommended twice the height and width of the standard footer logo)'
162
                )
163
            )
164
        );
165
        $footerLogoRetinaField->getValidator()->setAllowedExtensions($logoTypes);
166
167
        $fields->addFieldToTab(
168
            'Root.LogosIcons',
169
            $footerLink = TextField::create(
170
                '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

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

281
                /** @scrutinizer ignore-type */ 'EmptySearch',
Loading history...
282
                _t(
283
                    'CWP.SITECONFIG.EmptySearch',
284
                    'Text to display when there is no search query'
285
                )
286
            )
287
        );
288
        $fields->addFieldToTab(
289
            'Root.SearchOptions',
290
            TextField::create(
291
                'NoSearchResults',
292
                _t(
293
                    'CWP.SITECONFIG.NoResult',
294
                    'Text to display when there are no results'
295
                )
296
            )
297
        );
298
299
        return $this;
300
    }
301
}
302