Completed
Push — master ( 177f79...a8e226 )
by Andrew
03:02 queued 01:25
created

updateCMSFields()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 24
nc 2
nop 1
1
<?php
2
/**
3
 * Adds enhanced HTML SEO metadata.
4
 *
5
 * @package SEO
6
 * @subpackage Metadata
7
 * @author Andrew Gerber <[email protected]>
8
 * @version 1.0.0
9
 */
10
11
/**
12
 * Class SEO_Metadata_SiteConfig_DataExtension
13
 *
14
 * Adds additional statuses and defaults to control metadata output.
15
 */
16
class SEO_Metadata_SiteConfig_DataExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
17
{
18
19
    /* Attributes
20
    ------------------------------------------------------------------------------*/
21
22
    //// statuses
23
24
    /**
25
     * Character set status.
26
     *
27
     * Boolean value governing whether the character set is output.
28
     *
29
     * @var bool $CharsetStatus
30
     */
31
    private static $CharsetStatus = false;
32
33
    /**
34
     * `rel="canonical"` status.
35
     *
36
     * Boolean value governing whether canonical links are output.
37
     *
38
     * @var bool $CanonicalStatus
39
     */
40
    private static $CanonicalStatus = false;
41
42
    /**
43
     * Title status.
44
     *
45
     * Boolean value governing whether the page title should be output.
46
     *
47
     * @var bool $TitleStatus
48
     */
49
    private static $TitleStatus = false;
50
51
    /**
52
     * Extra metadata status.
53
     *
54
     * Boolean value governing whether additional (arbitrary) metadata can be added to pages.
55
     *
56
     * @var bool $ExtraMetaStatus
57
     */
58
    private static $ExtraMetaStatus = false;
59
60
    //// defaults
61
62
    /**
63
     * Character set.
64
     *
65
     * The character set to be used. Should always be `UTF-8` except for fringe configurations.
66
     *
67
     * @var string
68
     */
69
    private static $Charset = 'UTF-8';
70
71
    /**
72
     * Default title separator.
73
     *
74
     * The default title (primary) separator.
75
     *
76
     * @var string
77
     */
78
    private static $TitleSeparatorDefault = '|';
79
80
    /**
81
     * Default tagline separator.
82
     *
83
     * The default tagline (secondary) separator.
84
     *
85
     * @var string
86
     */
87
    private static $TaglineSeparatorDefault = '-';
88
89
90
    /* Status Methods
91
    ------------------------------------------------------------------------------*/
92
93
    /**
94
     * Character set enabled.
95
     *
96
     * Gets whether the character set should be output.
97
     *
98
     * @return bool
99
     */
100
    public function CharsetEnabled()
101
    {
102
        return ($this->owner->config()->CharsetStatus === true) ? true : self::$CharsetStatus;
103
    }
104
105
    /**
106
     * Canonical links enabled.
107
     *
108
     * Gets whether the canonical link should be output.
109
     *
110
     * @return bool
111
     */
112
    public function CanonicalEnabled()
113
    {
114
        return ($this->owner->config()->CanonicalStatus === true) ? true : self::$CanonicalStatus;
115
    }
116
117
    /**
118
     * Title enabled.
119
     *
120
     * Gets whether the title should be output.
121
     *
122
     * @return bool
123
     */
124
    public function TitleEnabled()
125
    {
126
        return ($this->owner->config()->TitleStatus === true) ? true : self::$TitleStatus;
127
    }
128
129
    /**
130
     * Extra metadata enabled.
131
     *
132
     * Gets whether additional (arbitrary) metadata should be output.
133
     *
134
     * @return bool
135
     */
136
    public function ExtraMetaEnabled()
137
    {
138
        return ($this->owner->config()->ExtraMetaStatus === true) ? true : self::$ExtraMetaStatus;
139
    }
140
141
142
    /* Config Methods
143
    ------------------------------------------------------------------------------*/
144
145
    /**
146
     * Character set.
147
     *
148
     * Gets the character set from configuration, or uses the class-defined default.
149
     *
150
     * @return string
151
     */
152
    public function Charset()
153
    {
154
        return ($this->owner->config()->Charset) ? $this->owner->config()->Charset : self::$Charset;
155
    }
156
157
158
    /* Overload Model
159
    ------------------------------------------------------------------------------*/
160
161
    /**
162
     * Database fields.
163
     *
164
     * An associative array of database fields: `name` => `type`.
165
     *
166
     * @var array $db
167
     */
168
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
169
        'TitleOrder' => 'Enum(array("first", "last"), "first")',
170
        'Title' => 'Text', // redundant, but included for backwards-compatibility
171
        'TitleSeparator' => 'Varchar(1)',
172
        'Tagline' => 'Text', // redundant, but included for backwards-compatibility
173
        'TaglineSeparator' => 'Varchar(1)',
174
    );
175
176
177
    /* Overload Methods
178
    ------------------------------------------------------------------------------*/
179
180
    // @todo @inheritdoc ?? or does it happen automagically as promised?
181
    public function updateCMSFields(FieldList $fields)
182
    {
183
        // Tab Set
184
        $fields->addFieldToTab('Root', new TabSet('Metadata'), 'Access');
185
186
        //// Title
187
188
        if ($this->TitleEnabled()) {
189
190
            // Tab
191
            $tab = 'Root.Metadata.Title';
192
193
            // Title Order Options
194
            $titleOrderOptions = array(
195
                'first' => 'Page Title | Website Name - Tagline',
196
                'last' => 'Website Name - Tagline | Page Title'
197
            );
198
199
            // Fields
200
            $fields->addFieldsToTab($tab, array(
201
                // Information
202
                LabelField::create('FaviconDescription', 'A title tag is the main text that describes an online document. Title elements have long been considered one of the most important on-page SEO elements (the most important being overall content), and appear in three key places: browsers, search engine results pages, and external websites.<br />@ <a href="https://moz.com/learn/seo/title-tag" target="_blank">Title Tag - Learn SEO - Mozilla</a>')
203
                    ->addExtraClass('information'),
204
                // Title Order
205
                DropdownField::create('TitleOrder', 'Page Title Order', $titleOrderOptions),
206
                // Title Separator
207
                TextField::create('TitleSeparator', 'Page Title Separator')
208
                    ->setAttribute('placeholder', self::$TitleSeparatorDefault)
209
                    ->setAttribute('size', 1)
210
                    ->setMaxLength(1)
211
                    ->setDescription('max 1 character'),
212
                // Title
213
                TextField::create('Title', 'Website Name'),
214
                // Tagline Separator
215
                TextField::create('TaglineSeparator', 'Tagline Separator')
216
                    ->setAttribute('placeholder', self::$TaglineSeparatorDefault)
217
                    ->setAttribute('size', 1)
218
                    ->setMaxLength(1)
219
                    ->setDescription('max 1 character'),
220
                // Tagline
221
                TextField::create('Tagline', 'Tagline')
222
                    ->setDescription('optional')
223
            ));
224
        }
225
    }
226
227
228
    /* Custom Methods
229
    ------------------------------------------------------------------------------*/
230
231
    /**
232
     * Fetch title separator.
233
     *
234
     * Fetches the title (primary) separator, falls back to default.
235
     *
236
     * @return string
237
     */
238
    public function FetchTitleSeparator()
239
    {
240
        return ($this->owner->TitleSeparator) ? $this->owner->TitleSeparator : self::$TitleSeparatorDefault;
241
    }
242
243
    /**
244
     * Fetch tagline separator.
245
     *
246
     * Fetches the tagline (secondary) separator, falls back to default.
247
     *
248
     * @return string
249
     */
250
    public function FetchTaglineSeparator()
251
    {
252
        return ($this->owner->TaglineSeparator) ? $this->owner->TaglineSeparator : self::$TaglineSeparatorDefault;
253
    }
254
255
    /**
256
     * Generates HTML title based on configuration settings.
257
     *
258
     * @dev Override this function for any custom title functionality.
259
     *
260
     * @param string $pageTitle
261
     *
262
     * @return string
263
     */
264
    public function GenerateTitle($pageTitle = 'Title Error')
265
    {
266
        // variables
267
        $owner = $this->owner;
268
269
        // if the is a site name
270
        if ($owner->Title) {
271
272
            // title parts, begin with name/title
273
            $titles = array($owner->Title);
274
275
            // tagline
276
            if ($owner->Tagline) {
277
                array_push($titles, $owner->FetchTaglineSeparator());
278
                array_push($titles, $owner->Tagline);
279
            }
280
281
            // page title
282
            if ($owner->TitleOrder == 'first') {
283
                // add to the beginning
284
                array_unshift($titles, $owner->FetchTitleSeparator());
285
                array_unshift($titles, $pageTitle);
286
            } else {
287
                // add to the end
288
                array_push($titles, $owner->FetchTitleSeparator());
289
                array_push($titles, $pageTitle);
290
            }
291
292
            // implode to create title
293
            $title = implode(' ', $titles);
294
295
            // removes whitespace before punctuation marks: `,.;:!?`
296
            // @todo isn't this a little bit random ?
297
            $title = preg_replace('/\s*[,.;:!?]/', '', $title);
298
299
            // return
300
            return $title;
301
302
        } else {
303
            // just return the page title if there is no site name
304
            return $pageTitle;
305
        }
306
    }
307
308
}
309