Issues (4)

src/Extension/MatomoSiteConfigExtension.php (1 issue)

Severity
1
<?php
2
namespace ElliotSawyer\Matomo;
3
4
use SilverStripe\Forms\TextField;
5
use SilverStripe\Forms\ToggleCompositeField;
6
use SilverStripe\ORM\DataExtension;
7
8
class MatomoSiteConfigExtension extends DataExtension {
9
10
    private static $db = [
0 ignored issues
show
The private property $db is not used, and could be removed.
Loading history...
11
        'MatomoTrackingURL' => 'Varchar(255)',
12
        'MatomoSiteId' => 'Int'
13
    ];
14
15
    public function updateCMSFields(\SilverStripe\Forms\FieldList $fields)
16
    {
17
        $fields->addFieldToTab('Root.Analytics',
18
            ToggleCompositeField::create(
19
                'MatomoToggle',
20
                'Matomo',
21
                [
22
                    TextField::create('MatomoTrackingURL', 'Tracking URL'),
23
                    TextField::create('MatomoSiteId', 'Site ID'),
24
                ]
25
            )
26
        );
27
    }
28
29
    private function getProtocolAgnosticHostname()
30
    {
31
        $hostname = rtrim($this->owner->MatomoTrackingURL);
32
        $hostname = rtrim($hostname, '/');
33
        $hostname .= '/';
34
35
        $hostname = ltrim($hostname);
36
        $hostname = str_replace(['http://', 'https://', '//'], '', $hostname);
37
38
        $hostname = '//' . $hostname;
39
40
        return $hostname;
41
42
    }
43
44
    public function onBeforeWrite()
45
    {
46
        $this->owner->MatomoTrackingURL = $this->getProtocolAgnosticHostname();
47
    }
48
}
49