Completed
Push — master ( dcf8ce...9fc3b6 )
by
unknown
01:53 queued 01:50
created

rentTheme()   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\Assets\File;
6
use SilverStripe\Assets\Image;
7
use SilverStripe\Core\Injector\Injector;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\FileHandleField;
10
use SilverStripe\Forms\TextField;
11
use SilverStripe\ORM\DataExtension;
12
use SilverStripe\Versioned\Versioned;
13
14
/**
15
 * Class CWPCleanupSiteConfigExtension
16
 */
17
class CWPSiteConfigExtension extends DataExtension
18
{
19
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
20
        'FooterLogoLink' => 'Varchar(255)',
21
        'FooterLogoDescription' => 'Varchar(255)',
22
        'FooterLogoSecondaryLink' => 'Varchar(255)',
23
        'FooterLogoSecondaryDescription' => 'Varchar(255)',
24
        'EmptySearch' => 'Varchar(255)',
25
        'NoSearchResults' => 'Varchar(255)'
26
    );
27
28
    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...
29
        'Logo' => Image::class,
30
        'LogoRetina' => Image::class,
31
        'FooterLogo' => Image::class,
32
        'FooterLogoRetina' => Image::class,
33
        'FooterLogoSecondary' => Image::class,
34
        'FavIcon' => File::class,
35
        'AppleTouchIcon144' => File::class,
36
        'AppleTouchIcon114' => File::class,
37
        'AppleTouchIcon72' => File::class,
38
        'AppleTouchIcon57' => File::class
39
    );
40
41
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
42
        'Logo',
43
        'LogoRetina',
44
        'FooterLogo',
45
        'FooterLogoRetina',
46
        'FooterLogoSecondary',
47
        'FavIcon',
48
        'AppleTouchIcon144',
49
        'AppleTouchIcon114',
50
        'AppleTouchIcon72',
51
        'AppleTouchIcon57'
52
    ];
53
54
    /**
55
     * @param FieldList $fields
56
     */
57
    public function updateCMSFields(FieldList $fields)
58
    {
59
        $this
60
            ->addLogosAndIcons($fields)
61
            ->addSearchOptions($fields);
62
    }
63
64
    /**
65
     * Add fields for logo and icon uploads
66
     *
67
     * @param  FieldList $fields
68
     * @return $this
69
     */
70
    protected function addLogosAndIcons(FieldList $fields)
71
    {
72
        $logoTypes = array('jpg', 'jpeg', 'png', 'gif');
73
        $iconTypes = array('ico');
74
        $appleTouchTypes = array('png');
75
76
        $fields->findOrMakeTab(
77
            'Root.LogosIcons',
78
            _t(__CLASS__ . '.LogosIconsTab', 'Logos/Icons')
79
        );
80
81
        $fields->addFieldToTab(
82
            'Root.LogosIcons',
83
            $logoField = Injector::inst()->create(
84
                FileHandleField::class,
85
                'Logo',
86
                _t(__CLASS__ . '.LogoUploadField', 'Logo, to appear in the top left')
87
            )
88
        );
89
        $logoField->getValidator()->setAllowedExtensions($logoTypes);
90
91
        $fields->addFieldToTab(
92
            'Root.LogosIcons',
93
            $logoRetinaField = Injector::inst()->create(
94
                FileHandleField::class,
95
                'LogoRetina',
96
                _t(
97
                    'CwpConfig.LogoRetinaUploadField',
98
                    'High resolution logo, to appear in the top left ' .
99
                    '(recommended to be twice the height and width of the standard logo)'
100
                )
101
            )
102
        );
103
        $logoRetinaField->getValidator()->setAllowedExtensions($logoTypes);
104
105
        $fields->addFieldToTab(
106
            'Root.LogosIcons',
107
            $footerLogoField = Injector::inst()->create(
108
                FileHandleField::class,
109
                'FooterLogo',
110
                _t(__CLASS__ . '.FooterLogoField', 'Footer logo, to appear in the footer')
111
            )
112
        );
113
        $footerLogoField->getValidator()->setAllowedExtensions($logoTypes);
114
115
        $fields->addFieldToTab(
116
            'Root.LogosIcons',
117
            $footerLogoRetinaField = Injector::inst()->create(
118
                FileHandleField::class,
119
                'FooterLogoRetina',
120
                _t(
121
                    'CwpConfig.FooterLogoRetinaField',
122
                    'High resolution footer logo (recommended twice the height and width of the standard footer logo)'
123
                )
124
            )
125
        );
126
        $footerLogoRetinaField->getValidator()->setAllowedExtensions($logoTypes);
127
128
        $fields->addFieldToTab(
129
            'Root.LogosIcons',
130
            $footerLink = TextField::create(
131
                '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

131
                /** @scrutinizer ignore-type */ 'FooterLogoLink',
Loading history...
132
                _t(__CLASS__ . '.FooterLogoLinkField', 'Footer Logo link')
133
            )
134
        );
135
        $footerLink->setRightTitle(
136
            _t(
137
                'CwpConfig.FooterLogoLinkDesc',
138
                'Please include the protocol (ie, http:// or https://) unless it is an internal link.'
139
            )
140
        );
141
142
        $fields->addFieldToTab(
143
            'Root.LogosIcons',
144
            TextField::create(
145
                'FooterLogoDescription',
146
                _t(__CLASS__ . '.FooterLogoDescField', 'Footer Logo description')
147
            )
148
        );
149
150
        $fields->addFieldToTab(
151
            'Root.LogosIcons',
152
            $footerLogoSecondaryField = Injector::inst()->create(
153
                FileHandleField::class,
154
                'FooterLogoSecondary',
155
                _t(__CLASS__ . '.FooterLogoSecondaryField', 'Secondary Footer Logo, to appear in the footer.')
156
            )
157
        );
158
        $footerLogoSecondaryField->getValidator()->setAllowedExtensions($logoTypes);
159
160
        $fields->addFieldToTab('Root.LogosIcons', $footerSecondaryLink = TextField::create(
161
            'FooterLogoSecondaryLink',
162
            _t(__CLASS__ . '.FooterLogoSecondaryLinkField', 'Secondary Footer Logo link.')
163
        ));
164
        $footerSecondaryLink->setRightTitle(_t(
165
            'CwpConfig.FooterLogoSecondaryLinkDesc',
166
            'Please include the protocol (ie, http:// or https://) unless it is an internal link.'
167
        ));
168
        $fields->addFieldToTab('Root.LogosIcons', TextField::create(
169
            'FooterLogoSecondaryDescription',
170
            _t(__CLASS__ . '.FooterLogoSecondaryDescField', 'Secondary Footer Logo description')
171
        ));
172
173
        $fields->addFieldToTab(
174
            'Root.LogosIcons',
175
            $favIconField = Injector::inst()->create(
176
                FileHandleField::class,
177
                'FavIcon',
178
                _t(__CLASS__ . '.FavIconField', 'Favicon, in .ico format, dimensions of 16x16, 32x32, or 48x48')
179
            )
180
        );
181
        $favIconField->getValidator()->setAllowedExtensions($iconTypes);
182
183
        $fields->addFieldToTab(
184
            'Root.LogosIcons',
185
            $atIcon144 = Injector::inst()->create(
186
                FileHandleField::class,
187
                'AppleTouchIcon144',
188
                _t(
189
                    'CwpConfig.AppleIconField144',
190
                    'Apple Touch Web Clip and Windows 8 Tile Icon (dimensions of 144x144, PNG format)'
191
                )
192
            )
193
        );
194
        $atIcon144->getValidator()->setAllowedExtensions($appleTouchTypes);
195
196
        $fields->addFieldToTab(
197
            'Root.LogosIcons',
198
            $atIcon114 = Injector::inst()->create(
199
                FileHandleField::class,
200
                'AppleTouchIcon114',
201
                _t(__CLASS__ . '.AppleIconField114', 'Apple Touch Web Clip Icon (dimensions of 114x114, PNG format)')
202
            )
203
        );
204
        $atIcon114->getValidator()->setAllowedExtensions($appleTouchTypes);
205
206
        $fields->addFieldToTab(
207
            'Root.LogosIcons',
208
            $atIcon72 = Injector::inst()->create(
209
                FileHandleField::class,
210
                'AppleTouchIcon72',
211
                _t(__CLASS__ . '.AppleIconField72', 'Apple Touch Web Clip Icon (dimensions of 72x72, PNG format)')
212
            )
213
        );
214
        $atIcon72->getValidator()->setAllowedExtensions($appleTouchTypes);
215
216
        $fields->addFieldToTab(
217
            'Root.LogosIcons',
218
            $atIcon57 = Injector::inst()->create(
219
                FileHandleField::class,
220
                'AppleTouchIcon57',
221
                _t(__CLASS__ . '.AppleIconField57', 'Apple Touch Web Clip Icon (dimensions of 57x57, PNG format)')
222
            )
223
        );
224
        $atIcon57->getValidator()->setAllowedExtensions($appleTouchTypes);
225
226
        return $this;
227
    }
228
229
    /**
230
     * Add user configurable search field labels
231
     *
232
     * @param  FieldList $fields
233
     * @return $this
234
     */
235
    protected function addSearchOptions(FieldList $fields)
236
    {
237
        $fields->findOrMakeTab('Root.SearchOptions');
238
239
        $fields->addFieldToTab(
240
            'Root.SearchOptions',
241
            TextField::create(
242
                '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

242
                /** @scrutinizer ignore-type */ 'EmptySearch',
Loading history...
243
                _t(
244
                    'CWP.SITECONFIG.EmptySearch',
245
                    'Text to display when there is no search query'
246
                )
247
            )
248
        );
249
        $fields->addFieldToTab(
250
            'Root.SearchOptions',
251
            TextField::create(
252
                'NoSearchResults',
253
                _t(
254
                    'CWP.SITECONFIG.NoResult',
255
                    'Text to display when there are no results'
256
                )
257
            )
258
        );
259
260
        return $this;
261
    }
262
263
    /**
264
     * Auto-publish any images attached to the SiteConfig object if it's not versioned. Versioned objects will
265
     * handle their related objects via the "owns" API by default.
266
     */
267
    public function onAfterWrite()
268
    {
269
        if (!$this->owner->hasExtension(Versioned::class)) {
270
            $this->owner->publishRecursive();
271
        }
272
    }
273
}
274