File size: 3,589 Bytes
800aa60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Settings\Storage\Backend;

use Piwik\Plugins\SitesManager\Model;
use Piwik\Site;
use Exception;

/**
 * Backend for an existing site. Stores all settings in the "site" database table.
 */
class SitesTable implements BackendInterface
{
    /**
     * @var int
     */
    private $idSite;

    /**
     * @var string[]
     */
    private $commaSeparatedArrayFields = array(
        'sitesearch_keyword_parameters',
        'sitesearch_category_parameters',
        'excluded_user_agents',
        'excluded_parameters',
        'excluded_ips',
        'excluded_referrers',
    );

    /**
     * these fields are standard fields of a site and cannot be adjusted via a setting
     * @var string[]
     */
    private $allowedNames = array(
        'ecommerce', 'sitesearch', 'sitesearch_keyword_parameters',
        'sitesearch_category_parameters', 'exclude_unknown_urls',
        'excluded_ips', 'excluded_parameters', 'excluded_referrers',
        'excluded_user_agents', 'keep_url_fragment', 'urls',
    );

    /**
     * @param string|int|null $idSite
     */
    public function __construct($idSite)
    {
        if (empty($idSite)) {
            throw new Exception('No idSite given for Measurable backend');
        }

        $this->idSite = (int) $idSite;
    }

    /**
     * @inheritdoc
     */
    public function getStorageId()
    {
        return 'SitesTable_' . $this->idSite;
    }

    /**
     * Saves (persists) the current setting values in the database.
     * @param array $values
     */
    public function save($values)
    {
        $model = $this->getModel();

        foreach ($values as $key => $value) {
            if (!in_array($key, $this->allowedNames)) {
                unset($values[$key]);
                continue;
            }

            if (is_array($value) && in_array($key, $this->commaSeparatedArrayFields)) {
                $values[$key] = implode(',', $value);
            } elseif (is_bool($value)) {
                $values[$key] = (int) $value;
            }
        }

        if (!empty($values['urls']) && is_array($values['urls'])) {
            $values['main_url'] = $this->processAdditionalUrls($values['urls']);
        }

        unset($values['urls']);

        $model->updateSite($values, $this->idSite);
        Site::clearCacheForSite($this->idSite);
    }

    public function load()
    {
        if (!empty($this->idSite)) {
            $site = Site::getSite($this->idSite);

            $urls         = $this->getModel();
            $site['urls'] = $urls->getSiteUrlsFromId($this->idSite);

            foreach ($this->commaSeparatedArrayFields as $field) {
                if (!empty($site[$field]) && is_string($site[$field])) {
                    $site[$field] = explode(',', $site[$field]);
                }
            }

            return $site;
        }

        return null;
    }

    /**
     * @param string[] $urls
     */
    private function processAdditionalUrls(array $urls): string
    {
        $model   = $this->getModel();
        $urls    = array_unique($urls);
        $mainUrl = array_shift($urls);

        $model->deleteSiteAliasUrls($this->idSite);
        foreach ($urls as $url) {
            $model->insertSiteUrl($this->idSite, $url);
        }

        return $mainUrl;
    }

    private function getModel(): Model
    {
        return new Model();
    }

    public function delete()
    {
    }
}