diff --git a/core/Plugin/Controller.php b/core/Plugin/Controller.php index 4b1e4e5efcd..43e385f2639 100644 --- a/core/Plugin/Controller.php +++ b/core/Plugin/Controller.php @@ -29,8 +29,7 @@ use Piwik\Plugins\CoreAdminHome\CustomLogo; use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph\Evolution; use Piwik\Plugins\LanguagesManager\LanguagesManager; -use Piwik\Plugins\SitesManager\API as APISitesManager; -use Piwik\Plugins\UsersManager\API as APIUsersManager; +use Piwik\Plugins\UsersManager\UserPreferences; use Piwik\Registry; use Piwik\SettingsPiwik; use Piwik\Site; @@ -769,14 +768,17 @@ public static function setPeriodVariablesView($view) public function redirectToIndex($moduleToRedirect, $actionToRedirect, $websiteId = null, $defaultPeriod = null, $defaultDate = null, $parameters = array()) { + + $userPreferences = new UserPreferences(); + if (empty($websiteId)) { - $websiteId = $this->getDefaultWebsiteId(); + $websiteId = $userPreferences->getDefaultWebsiteId(); } if (empty($defaultDate)) { - $defaultDate = $this->getDefaultDate(); + $defaultDate = $userPreferences->getDefaultDate(); } if (empty($defaultPeriod)) { - $defaultPeriod = $this->getDefaultPeriod(); + $defaultPeriod = $userPreferences->getDefaultPeriod(); } $parametersString = ''; if (!empty($parameters)) { @@ -813,82 +815,6 @@ public function redirectToIndex($moduleToRedirect, $actionToRedirect, $websiteId exit; } - /** - * Returns default site ID that Piwik should load. - * - * _Note: This value is a Piwik setting set by each user._ - * - * @return bool|int - * @api - */ - protected function getDefaultWebsiteId() - { - $defaultWebsiteId = false; - - // User preference: default website ID to load - $defaultReport = APIUsersManager::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), APIUsersManager::PREFERENCE_DEFAULT_REPORT); - if (is_numeric($defaultReport)) { - $defaultWebsiteId = $defaultReport; - } - - if ($defaultWebsiteId && Piwik::isUserHasViewAccess($defaultWebsiteId)) { - return $defaultWebsiteId; - } - - $sitesId = APISitesManager::getInstance()->getSitesIdWithAtLeastViewAccess(); - if (!empty($sitesId)) { - return $sitesId[0]; - } - return false; - } - - /** - * Returns default date for Piwik reports. - * - * _Note: This value is a Piwik setting set by each user._ - * - * @return string `'today'`, `'2010-01-01'`, etc. - * @api - */ - protected function getDefaultDate() - { - // NOTE: a change in this function might mean a change in plugins/UsersManager/javascripts/usersSettings.js as well - $userSettingsDate = APIUsersManager::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), APIUsersManager::PREFERENCE_DEFAULT_REPORT_DATE); - if ($userSettingsDate == 'yesterday') { - return $userSettingsDate; - } - // if last7, last30, etc. - if (strpos($userSettingsDate, 'last') === 0 - || strpos($userSettingsDate, 'previous') === 0 - ) { - return $userSettingsDate; - } - return 'today'; - } - - /** - * Returns default period type for Piwik reports. - * - * @return string `'day'`, `'week'`, `'month'`, `'year'` or `'range'` - * @api - */ - protected function getDefaultPeriod() - { - $userSettingsDate = APIUsersManager::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), APIUsersManager::PREFERENCE_DEFAULT_REPORT_DATE); - if ($userSettingsDate === false) { - return PiwikConfig::getInstance()->General['default_period']; - } - if (in_array($userSettingsDate, array('today', 'yesterday'))) { - return 'day'; - } - if (strpos($userSettingsDate, 'last') === 0 - || strpos($userSettingsDate, 'previous') === 0 - ) { - return 'range'; - } - return $userSettingsDate; - } - /** * Checks that the token_auth in the URL matches the currently logged-in user's token_auth. * diff --git a/core/Plugin/ControllerAdmin.php b/core/Plugin/ControllerAdmin.php index 2e39b7f8527..4254cdbb2c3 100644 --- a/core/Plugin/ControllerAdmin.php +++ b/core/Plugin/ControllerAdmin.php @@ -206,13 +206,4 @@ private static function checkPhpVersion($view) $view->phpVersion = PHP_VERSION; $view->phpIsNewEnough = version_compare($view->phpVersion, '5.3.0', '>='); } - - protected function getDefaultWebsiteId() - { - $sitesId = \Piwik\Plugins\SitesManager\API::getInstance()->getSitesIdWithAdminAccess(); - if (!empty($sitesId)) { - return $sitesId[0]; - } - return parent::getDefaultWebsiteId(); - } } diff --git a/plugins/UsersManager/Controller.php b/plugins/UsersManager/Controller.php index 3fd75e0f701..12691a5ce01 100644 --- a/plugins/UsersManager/Controller.php +++ b/plugins/UsersManager/Controller.php @@ -209,14 +209,17 @@ public function userSettings() $view->userAlias = $user['alias']; $view->userEmail = $user['email']; - $defaultReport = APIUsersManager::getInstance()->getUserPreference($userLogin, APIUsersManager::PREFERENCE_DEFAULT_REPORT); + $userPreferences = new UserPreferences(); + $defaultReport = $userPreferences->getDefaultWebsiteId(); if ($defaultReport === false) { $defaultReport = $this->getDefaultWebsiteId(); } $view->defaultReport = $defaultReport; if ($defaultReport == 'MultiSites') { - $view->defaultReportSiteName = Site::getNameFor($this->getDefaultWebsiteId()); + + $userPreferences = new UserPreferences(); + $view->defaultReportSiteName = Site::getNameFor($userPreferences->getDefaultWebsiteId()); } else { $view->defaultReportSiteName = Site::getNameFor($defaultReport); } @@ -234,6 +237,15 @@ public function userSettings() return $view->render(); } + protected function getDefaultWebsiteId() + { + $sitesId = \Piwik\Plugins\SitesManager\API::getInstance()->getSitesIdWithAdminAccess(); + if (!empty($sitesId)) { + return $sitesId[0]; + } + return false; + } + public function setIgnoreCookie() { Piwik::checkUserHasSomeViewAccess(); diff --git a/plugins/UsersManager/UserPreferences.php b/plugins/UsersManager/UserPreferences.php new file mode 100644 index 00000000000..649271144d6 --- /dev/null +++ b/plugins/UsersManager/UserPreferences.php @@ -0,0 +1,93 @@ +getUserPreference(Piwik::getCurrentUserLogin(), APIUsersManager::PREFERENCE_DEFAULT_REPORT); + if (is_numeric($defaultReport)) { + $defaultWebsiteId = $defaultReport; + } + + if ($defaultWebsiteId && Piwik::isUserHasViewAccess($defaultWebsiteId)) { + return $defaultWebsiteId; + } + + $sitesId = APISitesManager::getInstance()->getSitesIdWithAtLeastViewAccess(); + if (!empty($sitesId)) { + return $sitesId[0]; + } + return false; + } + + /** + * Returns default date for Piwik reports. + * + * _Note: This value is a Piwik setting set by each user._ + * + * @return string `'today'`, `'2010-01-01'`, etc. + * @api + */ + public function getDefaultDate() + { + // NOTE: a change in this function might mean a change in plugins/UsersManager/javascripts/usersSettings.js as well + $userSettingsDate = APIUsersManager::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), APIUsersManager::PREFERENCE_DEFAULT_REPORT_DATE); + if ($userSettingsDate == 'yesterday') { + return $userSettingsDate; + } + // if last7, last30, etc. + if (strpos($userSettingsDate, 'last') === 0 + || strpos($userSettingsDate, 'previous') === 0 + ) { + return $userSettingsDate; + } + return 'today'; + } + + /** + * Returns default period type for Piwik reports. + * + * @return string `'day'`, `'week'`, `'month'`, `'year'` or `'range'` + * @api + */ + public function getDefaultPeriod() + { + $userSettingsDate = APIUsersManager::getInstance()->getUserPreference(Piwik::getCurrentUserLogin(), APIUsersManager::PREFERENCE_DEFAULT_REPORT_DATE); + if ($userSettingsDate === false) { + return Config::getInstance()->General['default_period']; + } + if (in_array($userSettingsDate, array('today', 'yesterday'))) { + return 'day'; + } + if (strpos($userSettingsDate, 'last') === 0 + || strpos($userSettingsDate, 'previous') === 0 + ) { + return 'range'; + } + return $userSettingsDate; + } +} \ No newline at end of file diff --git a/tests/PHPUnit/UI b/tests/PHPUnit/UI index bf3580ee9fd..a29aec44747 160000 --- a/tests/PHPUnit/UI +++ b/tests/PHPUnit/UI @@ -1 +1 @@ -Subproject commit bf3580ee9fd2cbd904cd581409127581de5c87f2 +Subproject commit a29aec44747e85fd2e95aeefb5236db6561f01bf