Completed
Push — 1.1 ( a4c925...1876b7 )
by Daniel
07:54 queued 04:30
created

SubsiteDomain::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 30
rs 8.8571
cc 1
eloc 21
nc 1
nop 0
1
<?php
2
3
/**
4
 * @property string $Domain domain name of this subsite. Can include wildcards. Do not include the URL scheme here
5
 * @property string $Protocol Required protocol (http or https) if only one is supported. 'automatic' implies
6
 * that any links to this subsite should use the current protocol, and that both are supported.
7
 * @property string $SubstitutedDomain Domain name with all wildcards filled in
8
 * @property string $FullProtocol Full protocol including ://
9
 * @property bool $IsPrimary Is this the primary subdomain?
10
 */
11
class SubsiteDomain extends DataObject
12
{
13
    /**
14
     *
15
     * @var array
16
     */
17
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
18
        "Domain" => "Varchar(255)",
19
        "Protocol" => "Enum('http,https,automatic','automatic')",
20
        "IsPrimary" => "Boolean",
21
    );
22
23
    /**
24
     * Specifies that this subsite is http only
25
     */
26
    const PROTOCOL_HTTP = 'http';
27
28
    /**
29
     * Specifies that this subsite is https only
30
     */
31
    const PROTOCOL_HTTPS = 'https';
32
33
    /**
34
     * Specifies that this subsite supports both http and https
35
     */
36
    const PROTOCOL_AUTOMATIC = 'automatic';
37
38
    /**
39
     * Get the descriptive title for this domain
40
     *
41
     * @return string
42
     */
43
    public function getTitle()
44
    {
45
        return $this->Domain;
46
    }
47
48
    /**
49
     *
50
     * @var array
51
     */
52
    private static $has_one = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
53
        "Subsite" => "Subsite",
54
    );
55
56
    /**
57
     * @config
58
     * @var array
59
     */
60
    private static $summary_fields = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
61
        'Domain',
62
        'IsPrimary',
63
    );
64
65
    /**
66
     * @config
67
     * @var array
68
     */
69
    private static $casting = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
70
        'SubstitutedDomain' => 'Varchar',
71
        'FullProtocol' => 'Varchar',
72
        'AbsoluteLink' => 'Varchar',
73
    );
74
75
    /**
76
     * Whenever a Subsite Domain is written, rewrite the hostmap
77
     *
78
     * @return void
79
     */
80
    public function onAfterWrite()
81
    {
82
        Subsite::writeHostMap();
83
    }
84
    
85
    /**
86
     * 
87
     * @return \FieldList
88
     */
89
    public function getCMSFields()
90
    {
91
        $protocols = array(
92
            self::PROTOCOL_HTTP => _t('SubsiteDomain.PROTOCOL_HTTP', 'http://'),
93
            self::PROTOCOL_HTTPS => _t('SubsiteDomain.PROTOCOL_HTTPS', 'https://'),
94
            self::PROTOCOL_AUTOMATIC => _t('SubsiteDomain.PROTOCOL_AUTOMATIC', 'Automatic')
95
        );
96
97
        $fields = new FieldList(
98
            WildcardDomainField::create('Domain', $this->fieldLabel('Domain'), null, 255)
99
                ->setDescription(_t(
100
                    'SubsiteDomain.DOMAIN_DESCRIPTION',
101
                    'Hostname of this subsite (exclude protocol). Allows wildcards (*).'
102
                )),
103
            OptionsetField::create('Protocol', $this->fieldLabel('Protocol'), $protocols)
104
                ->setDescription(_t(
105
                    'SubsiteDomain.PROTOCOL_DESCRIPTION',
106
                    'When generating links to this subsite, use the selected protocol. <br />' .
107
                    'Selecting \'Automatic\' means subsite links will default to the current protocol.'
108
                )),
109
            CheckboxField::create('IsPrimary', $this->fieldLabel('IsPrimary'))
110
                ->setDescription(_t(
111
                    'SubsiteDomain.PROTOCOL_DESCRIPTION',
112
                    'Mark this as the default domain for this subsite'
113
                ))
114
        );
115
116
        $this->extend('updateCMSFields', $fields);
117
        return $fields;
118
    }
119
120
    /**
121
     * 
122
     * @param bool $includerelations
123
     * @return array
124
     */
125
    public function fieldLabels($includerelations = true)
126
    {
127
        $labels = parent::fieldLabels($includerelations);
128
        $labels['Domain'] = _t('SubsiteDomain.DOMAIN', 'Domain');
129
        $labels['Protocol'] = _t('SubsiteDomain.Protocol', 'Protocol');
130
        $labels['IsPrimary'] = _t('SubsiteDomain.IS_PRIMARY', 'Is Primary Domain?');
131
132
        return $labels;
133
    }
134
135
    /**
136
     * Get the link to this subsite
137
     *
138
     * @return string
139
     */
140
    public function Link()
141
    {
142
        return $this->getFullProtocol() . $this->Domain;
143
    }
144
145
    /**
146
     * Gets the full protocol (including ://) for this domain
147
     *
148
     * @return string
149
     */
150
    public function getFullProtocol()
151
    {
152
        switch ($this->Protocol) {
153
            case self::PROTOCOL_HTTPS:
154
            {
155
                return 'https://';
156
            }
157
            case self::PROTOCOL_HTTP:
158
            {
159
                return 'http://';
160
            }
161
            default:
162
            {
163
                return Director::protocol();
164
            }
165
        }
166
    }
167
168
    /**
169
     * Retrieves domain name with wildcards substituted with actual values
170
     *
171
     * @todo Refactor domains into separate wildcards / primary domains
172
     *
173
     * @return string
174
     */
175
    public function getSubstitutedDomain()
176
    {
177
        $currentHost = $_SERVER['HTTP_HOST'];
178
179
        // If there are wildcards in the primary domain (not recommended), make some
180
        // educated guesses about what to replace them with:
181
        $domain = preg_replace('/\.\*$/', ".{$currentHost}", $this->Domain);
182
183
        // Default to "subsite." prefix for first wildcard
184
        // TODO Whats the significance of "subsite" in this context?!
185
        $domain = preg_replace('/^\*\./', "subsite.", $domain);
186
        
187
        // *Only* removes "intermediate" subdomains, so 'subdomain.www.domain.com' becomes 'subdomain.domain.com'
188
        $domain = str_replace('.www.', '.', $domain);
189
190
        return $domain;
191
    }
192
193
    /**
194
     * Get absolute link for this domain
195
     *
196
     * @return string
197
     */
198
    public function getAbsoluteLink()
199
    {
200
        return $this->getFullProtocol() . $this->getSubstitutedDomain();
201
    }
202
203
    /**
204
     * Get absolute baseURL for this domain
205
     *
206
     * @return string
207
     */
208
    public function absoluteBaseURL()
209
    {
210
        return Controller::join_links(
211
            $this->getAbsoluteLink(),
212
            Director::baseURL()
213
        );
214
    }
215
}
216