BackendInterface.php
5 years ago
BaseSettingsTable.php
6 years ago
Cache.php
5 years ago
Config.php
5 years ago
MeasurableSettingsTable.php
5 years ago
NullBackend.php
5 years ago
PluginSettingsTable.php
5 years ago
SitesTable.php
3 years ago
SitesTable.php
124 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik\Settings\Storage\Backend; |
| 11 | |
| 12 | use Piwik\Plugins\SitesManager\Model; |
| 13 | use Piwik\Site; |
| 14 | use Exception; |
| 15 | |
| 16 | /** |
| 17 | * Backend for an existing site. Stores all settings in the "site" database table. |
| 18 | */ |
| 19 | class SitesTable implements BackendInterface |
| 20 | { |
| 21 | /** |
| 22 | * @var int |
| 23 | */ |
| 24 | private $idSite; |
| 25 | |
| 26 | private $commaSeparatedArrayFields = array( |
| 27 | 'sitesearch_keyword_parameters', |
| 28 | 'sitesearch_category_parameters', |
| 29 | 'excluded_user_agents', |
| 30 | 'excluded_parameters', |
| 31 | 'excluded_ips', |
| 32 | 'excluded_referrers' |
| 33 | ); |
| 34 | |
| 35 | // these fields are standard fields of a site and cannot be adjusted via a setting |
| 36 | private $allowedNames = array( |
| 37 | 'ecommerce', 'sitesearch', 'sitesearch_keyword_parameters', |
| 38 | 'sitesearch_category_parameters', 'exclude_unknown_urls', |
| 39 | 'excluded_ips', 'excluded_parameters', 'excluded_referrers', |
| 40 | 'excluded_user_agents', 'keep_url_fragment', 'urls' |
| 41 | ); |
| 42 | |
| 43 | public function __construct($idSite) |
| 44 | { |
| 45 | if (empty($idSite)) { |
| 46 | throw new Exception('No idSite given for Measurable backend'); |
| 47 | } |
| 48 | |
| 49 | $this->idSite = (int) $idSite; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @inheritdoc |
| 54 | */ |
| 55 | public function getStorageId() |
| 56 | { |
| 57 | return 'SitesTable_' . $this->idSite; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Saves (persists) the current setting values in the database. |
| 62 | */ |
| 63 | public function save($values) |
| 64 | { |
| 65 | $model = $this->getModel(); |
| 66 | |
| 67 | foreach ($values as $key => $value) { |
| 68 | if (!in_array($key, $this->allowedNames)) { |
| 69 | unset($values[$key]); |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | if (is_array($value) && in_array($key, $this->commaSeparatedArrayFields)) { |
| 74 | $values[$key] = implode(',', $value); |
| 75 | } elseif (is_bool($value)) { |
| 76 | $values[$key] = (int) $value; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (!empty($values['urls'])) { |
| 81 | $urls = array_unique($values['urls']); |
| 82 | $values['main_url'] = array_shift($urls); |
| 83 | |
| 84 | $model->deleteSiteAliasUrls($this->idSite); |
| 85 | foreach ($urls as $url) { |
| 86 | $model->insertSiteUrl($this->idSite, $url); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | unset($values['urls']); |
| 91 | |
| 92 | $model->updateSite($values, $this->idSite); |
| 93 | Site::clearCacheForSite($this->idSite); |
| 94 | } |
| 95 | |
| 96 | public function load() |
| 97 | { |
| 98 | if (!empty($this->idSite)) { |
| 99 | $site = Site::getSite($this->idSite); |
| 100 | |
| 101 | $urls = $this->getModel(); |
| 102 | $site['urls'] = $urls->getSiteUrlsFromId($this->idSite); |
| 103 | |
| 104 | foreach ($this->commaSeparatedArrayFields as $field) { |
| 105 | if (!empty($site[$field]) && is_string($site[$field])) { |
| 106 | $site[$field] = explode(',', $site[$field]); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return $site; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private function getModel() |
| 115 | { |
| 116 | return new Model(); |
| 117 | } |
| 118 | |
| 119 | public function delete() |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | } |
| 124 |