Completed
Push — master ( 0ebf95...33622c )
by Damian
12s
created

SubsiteDomain::fieldLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace SilverStripe\Subsites\Model;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Forms\CheckboxField;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\OptionsetField;
10
use SilverStripe\ORM\DataObject;
11
use SilverStripe\Subsites\Forms\WildcardDomainField;
12
13
/**
14
 * @property string $Domain domain name of this subsite. Can include wildcards. Do not include the URL scheme here
15
 * @property string $Protocol Required protocol (http or https) if only one is supported. 'automatic' implies
16
 * that any links to this subsite should use the current protocol, and that both are supported.
17
 * @property string $SubstitutedDomain Domain name with all wildcards filled in
18
 * @property string $FullProtocol Full protocol including ://
19
 * @property bool $IsPrimary Is this the primary subdomain?
20
 */
21
class SubsiteDomain extends DataObject
22
{
23
24
    private static $table_name = 'SubsiteDomain';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
25
26
    /**
27
     *
28
     * @var string
29
     */
30
    private static $default_sort = '"IsPrimary" DESC';
0 ignored issues
show
introduced by
The private property $default_sort is not used, and could be removed.
Loading history...
31
32
    /** *
33
     * @var array
34
     */
35
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
36
        'Domain' => 'Varchar(255)',
37
        'Protocol' => "Enum('http,https,automatic','automatic')",
38
        'IsPrimary' => 'Boolean',
39
    ];
40
41
    /**
42
     * Specifies that this subsite is http only
43
     */
44
    const PROTOCOL_HTTP = 'http';
45
46
    /**
47
     * Specifies that this subsite is https only
48
     */
49
    const PROTOCOL_HTTPS = 'https';
50
51
    /**
52
     * Specifies that this subsite supports both http and https
53
     */
54
    const PROTOCOL_AUTOMATIC = 'automatic';
55
56
    /**
57
     * Get the descriptive title for this domain
58
     *
59
     * @return string
60
     */
61
    public function getTitle()
62
    {
63
        return $this->Domain;
64
    }
65
66
    /**
67
     *
68
     * @var array
69
     */
70
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
71
        'Subsite' => Subsite::class,
72
    ];
73
74
    /**
75
     * @config
76
     * @var array
77
     */
78
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
79
        'Domain',
80
        'IsPrimary',
81
    ];
82
83
    /*** @config
84
     * @var array
85
     */
86
    private static $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
87
        'SubstitutedDomain' => 'Varchar',
88
        'FullProtocol' => 'Varchar',
89
        'AbsoluteLink' => 'Varchar',
90
    ];
91
92
    /**
93
     * Whenever a Subsite Domain is written, rewrite the hostmap
94
     *
95
     * @return void
96
     */
97
    public function onAfterWrite()
98
    {
99
        Subsite::writeHostMap();
100
    }
101
102
    /**
103
     *
104
     * @return FieldList
105
     */
106
    public function getCMSFields()
107
    {
108
        $protocols = [
109
            self::PROTOCOL_HTTP => _t(__CLASS__ . '.PROTOCOL_HTTP', 'http://'),
110
            self::PROTOCOL_HTTPS => _t(__CLASS__ . '.PROTOCOL_HTTPS', 'https://'),
111
            self::PROTOCOL_AUTOMATIC => _t(__CLASS__ . '.PROTOCOL_AUTOMATIC', 'Automatic')
112
        ];
113
        $fields = new FieldList(
114
            WildcardDomainField::create('Domain', $this->fieldLabel('Domain'), null, 255)
0 ignored issues
show
Bug introduced by
255 of type integer 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

114
            WildcardDomainField::create('Domain', $this->fieldLabel('Domain'), null, /** @scrutinizer ignore-type */ 255)
Loading history...
Bug introduced by
'Domain' 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

114
            WildcardDomainField::create(/** @scrutinizer ignore-type */ 'Domain', $this->fieldLabel('Domain'), null, 255)
Loading history...
115
                ->setDescription(_t(
116
                    __CLASS__ . '.DOMAIN_DESCRIPTION',
117
                    'Hostname of this subsite (exclude protocol). Allows wildcards (*).'
118
                )),
119
            OptionsetField::create('Protocol', $this->fieldLabel('Protocol'), $protocols)
120
                ->setDescription(_t(
121
                    __CLASS__ . '.PROTOCOL_DESCRIPTION',
122
                    'When generating links to this subsite, use the selected protocol. <br />' .
123
                    'Selecting \'Automatic\' means subsite links will default to the current protocol.'
124
                )),
125
            CheckboxField::create('IsPrimary', $this->fieldLabel('IsPrimary'))
126
                ->setDescription(_t(
127
                    __CLASS__ . '.PROTOCOL_DESCRIPTION',
128
                    'Mark this as the default domain for this subsite'
129
                ))
130
        );
131
132
        $this->extend('updateCMSFields', $fields);
133
        return $fields;
134
    }
135
136
    /**
137
     *
138
     * @param bool $includerelations
139
     * @return array
140
     */
141
    public function fieldLabels($includerelations = true)
142
    {
143
        $labels = parent::fieldLabels($includerelations);
144
        $labels['Domain'] = _t(__CLASS__ . '.DOMAIN', 'Domain');
145
        $labels['Protocol'] = _t(__CLASS__ . '.Protocol', 'Protocol');
146
        $labels['IsPrimary'] = _t(__CLASS__ . '.IS_PRIMARY', 'Is Primary Domain?');
147
148
        return $labels;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $labels also could return the type string which is incompatible with the documented return type array.
Loading history...
149
    }
150
151
    /**
152
     * Get the link to this subsite
153
     *
154
     * @return string
155
     */
156
    public function Link()
157
    {
158
        return $this->getFullProtocol() . $this->Domain;
159
    }
160
161
    /**
162
     * Gets the full protocol (including ://) for this domain
163
     *
164
     * @return string
165
     */
166
    public function getFullProtocol()
167
    {
168
        switch ($this->Protocol) {
169
            case self::PROTOCOL_HTTPS: {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
170
                return 'https://';
171
            }
172
            case self::PROTOCOL_HTTP: {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
173
                return 'http://';
174
            }
175
            default: {
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
176
                return Director::protocol();
177
            }
178
        }
179
    }
180
181
    /**
182
     * Retrieves domain name with wildcards substituted with actual values
183
     *
184
     * @todo Refactor domains into separate wildcards / primary domains
185
     *
186
     * @return string
187
     */
188
    public function getSubstitutedDomain()
189
    {
190
        $currentHost = $_SERVER['HTTP_HOST'];
191
192
        // If there are wildcards in the primary domain (not recommended), make some
193
        // educated guesses about what to replace them with:
194
        $domain = preg_replace('/\.\*$/', ".{$currentHost}", $this->Domain);
195
196
        // Default to "subsite." prefix for first wildcard
197
        // TODO Whats the significance of "subsite" in this context?!
198
        $domain = preg_replace('/^\*\./', "subsite.", $domain);
199
200
        // *Only* removes "intermediate" subdomains, so 'subdomain.www.domain.com' becomes 'subdomain.domain.com'
201
        $domain = str_replace('.www.', '.', $domain);
202
203
        return $domain;
204
    }
205
206
    /**
207
     * Get absolute link for this domain
208
     *
209
     * @return string
210
     */
211
    public function getAbsoluteLink()
212
    {
213
        return $this->getFullProtocol() . $this->getSubstitutedDomain();
214
    }
215
216
    /**
217
     * Get absolute baseURL for this domain
218
     *
219
     * @return string
220
     */
221
    public function absoluteBaseURL()
222
    {
223
        return Controller::join_links(
224
            $this->getAbsoluteLink(),
225
            Director::baseURL()
226
        );
227
    }
228
}
229