diff options
-rw-r--r-- | core/Plugin/Controller.php | 88 | ||||
-rw-r--r-- | core/Plugin/ControllerAdmin.php | 9 | ||||
-rw-r--r-- | plugins/UsersManager/Controller.php | 16 | ||||
-rw-r--r-- | plugins/UsersManager/UserPreferences.php | 93 | ||||
m--------- | tests/PHPUnit/UI | 0 |
5 files changed, 114 insertions, 92 deletions
diff --git a/core/Plugin/Controller.php b/core/Plugin/Controller.php index 4b1e4e5efc..43e385f263 100644 --- a/core/Plugin/Controller.php +++ b/core/Plugin/Controller.php @@ -29,8 +29,7 @@ use Piwik\Piwik; 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 @@ abstract class Controller 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)) { @@ -814,82 +816,6 @@ abstract class Controller } /** - * 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. * * This is a protection against CSRF and should be used in all controller diff --git a/core/Plugin/ControllerAdmin.php b/core/Plugin/ControllerAdmin.php index 2e39b7f852..4254cdbb2c 100644 --- a/core/Plugin/ControllerAdmin.php +++ b/core/Plugin/ControllerAdmin.php @@ -206,13 +206,4 @@ abstract class ControllerAdmin extends Controller $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 3fd75e0f70..12691a5ce0 100644 --- a/plugins/UsersManager/Controller.php +++ b/plugins/UsersManager/Controller.php @@ -209,14 +209,17 @@ class Controller extends \Piwik\Plugin\ControllerAdmin $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 @@ class Controller extends \Piwik\Plugin\ControllerAdmin 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 0000000000..649271144d --- /dev/null +++ b/plugins/UsersManager/UserPreferences.php @@ -0,0 +1,93 @@ +<?php +/** + * Piwik - free/libre analytics platform + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * + */ +namespace Piwik\Plugins\UsersManager; + +use Piwik\Config; +use Piwik\Piwik; +use Piwik\Plugins\SitesManager\API as APISitesManager; +use Piwik\Plugins\UsersManager\API as APIUsersManager; + +class UserPreferences +{ + /** + * Returns default site ID that Piwik should load. + * + * _Note: This value is a Piwik setting set by each user._ + * + * @return bool|int + * @api + */ + public 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 + */ + 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 -Subproject bf3580ee9fd2cbd904cd581409127581de5c87f +Subproject a29aec44747e85fd2e95aeefb5236db6561f01b |