BackendInterface.php
6 years ago
BaseSettingsTable.php
6 years ago
Cache.php
6 years ago
Config.php
6 years ago
MeasurableSettingsTable.php
6 years ago
NullBackend.php
6 years ago
PluginSettingsTable.php
6 years ago
SitesTable.php
6 years ago
SitesTable.php
123 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - 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 | ); |
| 33 | |
| 34 | // these fields are standard fields of a site and cannot be adjusted via a setting |
| 35 | private $allowedNames = array( |
| 36 | 'ecommerce', 'sitesearch', 'sitesearch_keyword_parameters', |
| 37 | 'sitesearch_category_parameters', 'exclude_unknown_urls', |
| 38 | 'excluded_ips', 'excluded_parameters', |
| 39 | 'excluded_user_agents', 'keep_url_fragment', 'urls' |
| 40 | ); |
| 41 | |
| 42 | public function __construct($idSite) |
| 43 | { |
| 44 | if (empty($idSite)) { |
| 45 | throw new Exception('No idSite given for Measurable backend'); |
| 46 | } |
| 47 | |
| 48 | $this->idSite = (int) $idSite; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @inheritdoc |
| 53 | */ |
| 54 | public function getStorageId() |
| 55 | { |
| 56 | return 'SitesTable_' . $this->idSite; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Saves (persists) the current setting values in the database. |
| 61 | */ |
| 62 | public function save($values) |
| 63 | { |
| 64 | $model = $this->getModel(); |
| 65 | |
| 66 | foreach ($values as $key => $value) { |
| 67 | if (!in_array($key, $this->allowedNames)) { |
| 68 | unset($values[$key]); |
| 69 | continue; |
| 70 | } |
| 71 | |
| 72 | if (is_array($value) && in_array($key, $this->commaSeparatedArrayFields)) { |
| 73 | $values[$key] = implode(',', $value); |
| 74 | } elseif (is_bool($value)) { |
| 75 | $values[$key] = (int) $value; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (!empty($values['urls'])) { |
| 80 | $urls = array_unique($values['urls']); |
| 81 | $values['main_url'] = array_shift($urls); |
| 82 | |
| 83 | $model->deleteSiteAliasUrls($this->idSite); |
| 84 | foreach ($urls as $url) { |
| 85 | $model->insertSiteUrl($this->idSite, $url); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | unset($values['urls']); |
| 90 | |
| 91 | $model->updateSite($values, $this->idSite); |
| 92 | Site::clearCacheForSite($this->idSite); |
| 93 | } |
| 94 | |
| 95 | public function load() |
| 96 | { |
| 97 | if (!empty($this->idSite)) { |
| 98 | $site = Site::getSite($this->idSite); |
| 99 | |
| 100 | $urls = $this->getModel(); |
| 101 | $site['urls'] = $urls->getSiteUrlsFromId($this->idSite); |
| 102 | |
| 103 | foreach ($this->commaSeparatedArrayFields as $field) { |
| 104 | if (!empty($site[$field]) && is_string($site[$field])) { |
| 105 | $site[$field] = explode(',', $site[$field]); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return $site; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private function getModel() |
| 114 | { |
| 115 | return new Model(); |
| 116 | } |
| 117 | |
| 118 | public function delete() |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | } |
| 123 |