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