diff options
author | matt <matt@59fd770c-687e-43c8-a1e3-f5a4ff64c105> | 2010-06-23 07:02:26 +0400 |
---|---|---|
committer | matt <matt@59fd770c-687e-43c8-a1e3-f5a4ff64c105> | 2010-06-23 07:02:26 +0400 |
commit | 999f46479294713104c962bfe7469e9b6e7a4bbf (patch) | |
tree | a22e349d850bc2f2946a85a3cab373c4b6dc1809 /plugins/SitesManager | |
parent | 462d78c290f19dd691d9634bf2b17d2adc4446af (diff) | |
parent | c0b42ff664a0846064df23db232ea774931ab7a3 (diff) |
0.6.3?
Diffstat (limited to 'plugins/SitesManager')
-rw-r--r-- | plugins/SitesManager/API.php | 567 | ||||
-rw-r--r-- | plugins/SitesManager/Controller.php | 45 | ||||
-rw-r--r-- | plugins/SitesManager/SitesManager.php | 106 | ||||
-rw-r--r-- | plugins/SitesManager/templates/DisplayJavascriptCode.tpl | 5 | ||||
-rw-r--r-- | plugins/SitesManager/templates/SitesManager.js | 168 | ||||
-rw-r--r-- | plugins/SitesManager/templates/SitesManager.tpl | 132 | ||||
-rw-r--r-- | plugins/SitesManager/tests/SitesManager.test.php | 369 |
7 files changed, 212 insertions, 1180 deletions
diff --git a/plugins/SitesManager/API.php b/plugins/SitesManager/API.php index e00c159a8b..81c660875f 100644 --- a/plugins/SitesManager/API.php +++ b/plugins/SitesManager/API.php @@ -17,10 +17,6 @@ class Piwik_SitesManager_API { static private $instance = null; - - /** - * @return Piwik_SitesManager_API - */ static public function getInstance() { if (self::$instance == null) @@ -31,11 +27,6 @@ class Piwik_SitesManager_API return self::$instance; } - const OPTION_EXCLUDED_IPS_GLOBAL = 'SitesManager_ExcludedIpsGlobal'; - const OPTION_DEFAULT_TIMEZONE = 'SitesManager_DefaultTimezone'; - const OPTION_DEFAULT_CURRENCY = 'SitesManager_DefaultCurrency'; - const OPTION_EXCLUDED_QUERY_PARAMETERS_GLOBAL = 'SitesManager_ExcludedQueryParameters'; - /** * Returns the javascript tag for the given idSite. * This tag must be included on every page to be tracked by Piwik @@ -43,7 +34,7 @@ class Piwik_SitesManager_API * @param int $idSite * @return string The Javascript tag ready to be included on the HTML pages */ - public function getJavascriptTag( $idSite, $piwikUrl = '', $actionName = '') + static public function getJavascriptTag( $idSite, $piwikUrl = '', $actionName = '') { Piwik::checkUserHasViewAccess($idSite); @@ -65,10 +56,10 @@ class Piwik_SitesManager_API * @exception if the site ID doesn't exist or the user doesn't have access to it * @return array */ - public function getSiteFromId( $idSite ) + static public function getSiteFromId( $idSite ) { Piwik::checkUserHasViewAccess( $idSite ); - $site = Zend_Registry::get('db')->fetchRow("SELECT * FROM ".Piwik_Common::prefixTable("site")." WHERE idsite = ?", $idSite); + $site = Zend_Registry::get('db')->fetchRow("SELECT * FROM ".Piwik::prefixTable("site")." WHERE idsite = ?", $idSite); return $site; } @@ -78,11 +69,11 @@ class Piwik_SitesManager_API * * @return array list of alias URLs */ - private function getAliasSiteUrlsFromId( $idsite ) + static private function getAliasSiteUrlsFromId( $idsite ) { $db = Zend_Registry::get('db'); $result = $db->fetchAll("SELECT url - FROM ".Piwik_Common::prefixTable("site_url"). " + FROM ".Piwik::prefixTable("site_url"). " WHERE idsite = ?", $idsite); $urls = array(); foreach($result as $url) @@ -98,11 +89,11 @@ class Piwik_SitesManager_API * @exception if the website ID doesn't exist or the user doesn't have access to it * @return array list of URLs */ - public function getSiteUrlsFromId( $idSite ) + static public function getSiteUrlsFromId( $idSite ) { Piwik::checkUserHasViewAccess($idSite); - $site = $this->getSiteFromId($idSite); - $urls = $this->getAliasSiteUrlsFromId($idSite); + $site = self::getSiteFromId($idSite); + $urls = self::getAliasSiteUrlsFromId($idSite); return array_merge(array($site['main_url']), $urls); } @@ -111,10 +102,10 @@ class Piwik_SitesManager_API * * @return array the list of websites ID */ - public function getAllSitesId() + static public function getAllSitesId() { Piwik::checkUserIsSuperUser(); - $result = Piwik_FetchAll("SELECT idsite FROM ".Piwik_Common::prefixTable('site')); + $result = Piwik_FetchAll("SELECT idsite FROM ".Piwik::prefixTable('site')); $idSites = array(); foreach($result as $idSite) { @@ -130,10 +121,10 @@ class Piwik_SitesManager_API * * @return array for each site, an array of information (idsite, name, main_url, etc.) */ - public function getSitesWithAdminAccess() + static public function getSitesWithAdminAccess() { - $sitesId = $this->getSitesIdWithAdminAccess(); - return $this->getSitesFromIds($sitesId); + $sitesId = self::getSitesIdWithAdminAccess(); + return self::getSitesFromIds($sitesId); } /** @@ -142,10 +133,10 @@ class Piwik_SitesManager_API * * @return array for each site, an array of information (idsite, name, main_url, etc.) */ - public function getSitesWithViewAccess() + static public function getSitesWithViewAccess() { - $sitesId = $this->getSitesIdWithViewAccess(); - return $this->getSitesFromIds($sitesId); + $sitesId = self::getSitesIdWithViewAccess(); + return self::getSitesFromIds($sitesId); } /** @@ -154,10 +145,10 @@ class Piwik_SitesManager_API * * @return array array for each site, an array of information (idsite, name, main_url, etc.) */ - public function getSitesWithAtLeastViewAccess() + static public function getSitesWithAtLeastViewAccess() { - $sitesId = $this->getSitesIdWithAtLeastViewAccess(); - return $this->getSitesFromIds($sitesId); + $sitesId = self::getSitesIdWithAtLeastViewAccess(); + return self::getSitesFromIds($sitesId); } /** @@ -166,7 +157,7 @@ class Piwik_SitesManager_API * * @return array list of websites ID */ - public function getSitesIdWithAdminAccess() + static public function getSitesIdWithAdminAccess() { $sitesId = Zend_Registry::get('access')->getSitesIdWithAdminAccess(); return $sitesId; @@ -178,7 +169,7 @@ class Piwik_SitesManager_API * * @return array list of websites ID */ - public function getSitesIdWithViewAccess() + static public function getSitesIdWithViewAccess() { return Zend_Registry::get('access')->getSitesIdWithViewAccess(); } @@ -189,7 +180,7 @@ class Piwik_SitesManager_API * * @return array list of websites ID */ - public function getSitesIdWithAtLeastViewAccess() + static public function getSitesIdWithAtLeastViewAccess() { return Zend_Registry::get('access')->getSitesIdWithAtLeastViewAccess(); } @@ -200,7 +191,7 @@ class Piwik_SitesManager_API * * @param array list of website ID */ - private function getSitesFromIds( $idSites ) + static private function getSitesFromIds( $idSites ) { if(count($idSites) === 0) { @@ -208,105 +199,57 @@ class Piwik_SitesManager_API } $db = Zend_Registry::get('db'); $sites = $db->fetchAll("SELECT * - FROM ".Piwik_Common::prefixTable("site")." + FROM ".Piwik::prefixTable("site")." WHERE idsite IN (".implode(", ", $idSites).") ORDER BY idsite ASC"); return $sites; } - - /** - * Returns the list of websites ID associated with a URL. - * - * @param string $url - * @return array list of websites ID - */ - public function getSitesIdFromSiteUrl( $url ) - { - $url = $this->removeTrailingSlash($url); - - if(Piwik::isUserIsSuperUser()) - { - $ids = Zend_Registry::get('db')->fetchAll( - 'SELECT idsite FROM ' . Piwik_Common::prefixTable('site') . ' WHERE main_url = ? ' . - 'UNION SELECT idsite FROM ' . Piwik_Common::prefixTable('site_url') . ' WHERE url = ?', array($url, $url)); - } - else - { - $login = Piwik::getCurrentUserLogin(); - $ids = Zend_Registry::get('db')->fetchAll( - 'SELECT idsite FROM ' . Piwik_Common::prefixTable('site') . ' WHERE main_url = ? ' . - 'AND idsite IN (' . Piwik_Access::getSqlAccessSite('idsite') . ') ' . - 'UNION SELECT idsite FROM ' . Piwik_Common::prefixTable('site_url') . ' WHERE url = ? ' . - 'AND idsite IN (' . Piwik_Access::getSqlAccessSite('idsite') . ')', array($url, $login, $url, $login)); - } - - return $ids; - } - + /** - * Add a website. - * Requires Super User access. + * Add a website in the database. * * The website is defined by a name and an array of URLs. - * @param string Site name - * @param array|string The URLs array must contain at least one URL called the 'main_url' ; - * if several URLs are provided in the array, they will be recorded - * as Alias URLs for this website. - * @param string Comma separated list of IPs to exclude from the reports (allows wildcards) - * @param string Timezone string, eg. 'Europe/London' + * The name must not be empty. + * The URLs array must contain at least one URL called the 'main_url' ; + * if several URLs are provided in the array, they will be recorded as Alias URLs for + * this website. + * + * Requires Super User access. * * @return int the website ID created */ - public function addSite( $siteName, $urls, $excludedIps = null, $excludedQueryParameters = null, $timezone = null, $currency = null ) + static public function addSite( $siteName, $urls ) { Piwik::checkUserIsSuperUser(); - $this->checkName($siteName); - $urls = $this->cleanParameterUrls($urls); - $this->checkUrls($urls); - $this->checkAtLeastOneUrl($urls); - $timezone = trim($timezone); - - if(empty($timezone)) - { - $timezone = $this->getDefaultTimezone(); - } - $this->checkValidTimezone($timezone); - - if(empty($currency)) - { - $currency = $this->getDefaultCurrency(); - } - $this->checkValidCurrency($currency); + self::checkName($siteName); + $urls = self::cleanParameterUrls($urls); + self::checkUrls($urls); + self::checkAtLeastOneUrl($urls); $db = Zend_Registry::get('db'); $url = $urls[0]; $urls = array_slice($urls, 1); - $bind = array( 'name' => $siteName, - 'main_url' => $url, - 'ts_created' => Piwik_Date::now()->getDatetime() - ); - - $bind['excluded_ips'] = $this->checkAndReturnExcludedIps($excludedIps); - $bind['excluded_parameters'] = $this->checkAndReturnExcludedQueryParameters($excludedQueryParameters); - $bind['timezone'] = $timezone; - $bind['currency'] = $currency; - $db->insert(Piwik_Common::prefixTable("site"), $bind); + $db->insert(Piwik::prefixTable("site"), array( + 'name' => $siteName, + 'main_url' => $url, + ) + ); $idSite = $db->lastInsertId(); - $this->insertSiteUrls($idSite, $urls); + self::insertSiteUrls($idSite, $urls); // we reload the access list which doesn't yet take in consideration this new website Zend_Registry::get('access')->reloadAccess(); - $this->postUpdateWebsite($idSite); + self::postUpdateWebsite($idSite); return (int)$idSite; } - private function postUpdateWebsite($idSite) + private static function postUpdateWebsite($idSite) { Piwik_Common::regenerateCacheWebsiteAttributes($idSite); } @@ -318,11 +261,11 @@ class Piwik_SitesManager_API * * @param int $idSite */ - public function deleteSite( $idSite ) + static public function deleteSite( $idSite ) { Piwik::checkUserIsSuperUser(); - $idSites = Piwik_SitesManager_API::getInstance()->getAllSitesId(); + $idSites = Piwik_SitesManager_API::getAllSitesId(); if(!in_array($idSite, $idSites)) { throw new Exception("website id = $idSite not found"); @@ -335,13 +278,13 @@ class Piwik_SitesManager_API $db = Zend_Registry::get('db'); - $db->query("DELETE FROM ".Piwik_Common::prefixTable("site")." + $db->query("DELETE FROM ".Piwik::prefixTable("site")." WHERE idsite = ?", $idSite); - $db->query("DELETE FROM ".Piwik_Common::prefixTable("site_url")." + $db->query("DELETE FROM ".Piwik::prefixTable("site_url")." WHERE idsite = ?", $idSite); - $db->query("DELETE FROM ".Piwik_Common::prefixTable("access")." + $db->query("DELETE FROM ".Piwik::prefixTable("access")." WHERE idsite = ?", $idSite); Piwik_Common::deleteCacheWebsiteAttributes($idSite); @@ -353,7 +296,7 @@ class Piwik_SitesManager_API * * @exception if the parameter is not an array or if array empty */ - private function checkAtLeastOneUrl( $urls ) + static private function checkAtLeastOneUrl( $urls ) { if(!is_array($urls) || count($urls) == 0) @@ -362,52 +305,6 @@ class Piwik_SitesManager_API } } - private function checkValidTimezone($timezone) - { - $timezones = $this->getTimezonesList(); - foreach($timezones as $continent => $cities) - { - foreach($cities as $timezoneId => $city) - { - if($timezoneId == $timezone) - { - return true; - } - } - } - throw new Exception(Piwik_TranslateException('SitesManager_ExceptionInvalidTimezone', array($timezone))); - } - - private function checkValidCurrency($currency) - { - if(!in_array($currency, array_keys($this->getCurrencyList()))) - { - throw new Exception(Piwik_TranslateException('SitesManager_ExceptionInvalidCurrency', array($currency, "USD, EUR, etc."))); - } - } - - /** - * Checks that the submitted IPs (comma separated list) are valid - * Returns the cleaned up IPs - * @param $excludedIps - * - * @return array of IPs - */ - private function checkAndReturnExcludedIps($excludedIps) - { - $ips = explode(',', $excludedIps); - $ips = array_map('trim', $ips); - $ips = array_filter($ips, 'strlen'); - foreach($ips as $ip) - { - if(!$this->isValidIp($ip)) - { - throw new Exception(Piwik_TranslateException('SitesManager_ExceptionInvalidIPFormat', array($ip, "1.2.3.4 or 1.2.3.*"))); - } - } - $ips = implode(',', $ips); - return $ips; - } /** * Add a list of alias Urls to the given idSite * @@ -416,136 +313,22 @@ class Piwik_SitesManager_API * * @return int the number of inserted URLs */ - public function addSiteAliasUrls( $idSite, $urls) + static public function addSiteAliasUrls( $idSite, $urls) { Piwik::checkUserHasAdminAccess( $idSite ); - $urls = $this->cleanParameterUrls($urls); - $this->checkUrls($urls); + $urls = self::cleanParameterUrls($urls); + self::checkUrls($urls); - $urlsInit = $this->getSiteUrlsFromId($idSite); + $urlsInit = self::getSiteUrlsFromId($idSite); $toInsert = array_diff($urls, $urlsInit); - $this->insertSiteUrls($idSite, $toInsert); - $this->postUpdateWebsite($idSite); + self::insertSiteUrls($idSite, $toInsert); + self::postUpdateWebsite($idSite); return count($toInsert); } /** - * Sets IPs to be excluded from all websites. IPs can contain wildcards. - * Will also apply to websites created in the future. - * - * @param string Comma separated list of IPs to exclude from being tracked (allows wildcards) - * @return bool - */ - public function setGlobalExcludedIps($excludedIps) - { - Piwik::checkUserIsSuperUser(); - $excludedIps = $this->checkAndReturnExcludedIps($excludedIps); - Piwik_SetOption(self::OPTION_EXCLUDED_IPS_GLOBAL, $excludedIps); - Piwik_Common::deleteAllCache(); - return true; - } - - /** - * Returns the list of URL query parameters that are excluded from all websites - * - * @return string Comma separated list of URL parameters - */ - public function getExcludedQueryParametersGlobal() - { - Piwik::checkUserHasSomeAdminAccess(); - return Piwik_GetOption(self::OPTION_EXCLUDED_QUERY_PARAMETERS_GLOBAL); - } - - /** - * Sets list of URL query parameters to be excluded on all websites. - * Will also apply to websites created in the future. - * - * @param string Comma separated list of URL query parameters to exclude from URLs - * @return bool - */ - public function setGlobalExcludedQueryParameters($excludedQueryParameters) - { - Piwik::checkUserIsSuperUser(); - $excludedQueryParameters = $this->checkAndReturnExcludedQueryParameters($excludedQueryParameters); - Piwik_SetOption(self::OPTION_EXCLUDED_QUERY_PARAMETERS_GLOBAL, $excludedQueryParameters); - Piwik_Common::deleteAllCache(); - return true; - } - - /** - * Returns the list of IPs that are excluded from all websites - * - * @return string Comma separated list of IPs - */ - public function getExcludedIpsGlobal() - { - Piwik::checkUserHasSomeAdminAccess(); - return Piwik_GetOption(self::OPTION_EXCLUDED_IPS_GLOBAL); - } - - /** - * Returns the default currency that will be set when creating a website through the API. - * - * @return string Currency ID eg. 'USD' - */ - public function getDefaultCurrency() - { - Piwik::checkUserHasSomeAdminAccess(); - $defaultCurrency = Piwik_GetOption(self::OPTION_DEFAULT_CURRENCY); - if($defaultCurrency) - { - return $defaultCurrency; - } - return 'USD'; - } - - /** - * Sets the default currency that will be used when creating websites - * - * @param $defaultCurrency string eg. 'USD' - * @return bool - */ - public function setDefaultCurrency($defaultCurrency) - { - Piwik::checkUserIsSuperUser(); - $this->checkValidCurrency($defaultCurrency); - Piwik_SetOption(self::OPTION_DEFAULT_CURRENCY, $defaultCurrency); - return true; - } - - /** - * Returns the default timezone that will be set when creating a website through the API. - * Via the UI, if the default timezone is not UTC, it will be pre-selected in the drop down - * - * @return string Timezone eg. UTC+7 or Europe/Paris - */ - public function getDefaultTimezone() - { - $defaultTimezone = Piwik_GetOption(self::OPTION_DEFAULT_TIMEZONE); - if($defaultTimezone) - { - return $defaultTimezone; - } - return 'UTC'; - } - - /** - * Sets the default timezone that will be used when creating websites - * - * @param $defaultTimezone string eg. Europe/Paris or UTC+8 - * @return bool - */ - public function setDefaultTimezone($defaultTimezone) - { - Piwik::checkUserIsSuperUser(); - $this->checkValidTimezone($defaultTimezone); - Piwik_SetOption(self::OPTION_DEFAULT_TIMEZONE, $defaultTimezone); - return true; - } - - /** * Update an existing website. * If only one URL is specified then only the main url will be updated. * If several URLs are specified, both the main URL and the alias URLs will be updated. @@ -553,234 +336,59 @@ class Piwik_SitesManager_API * @param int website ID defining the website to edit * @param string website name * @param string|array the website URLs - * @param string Comma separated list of IPs to exclude from being tracked (allows wildcards) - * @param string Timezone * * @exception if any of the parameter is not correct * * @return bool true on success */ - public function updateSite( $idSite, $siteName, $urls = null, $excludedIps = null, $excludedQueryParameters = null, $timezone = null, $currency = null) + static public function updateSite( $idSite, $siteName, $urls = null) { Piwik::checkUserHasAdminAccess($idSite); - $this->checkName($siteName); + self::checkName($siteName); // SQL fields to update $bind = array(); if(!is_null($urls)) { - $urls = $this->cleanParameterUrls($urls); - $this->checkUrls($urls); - $this->checkAtLeastOneUrl($urls); + $urls = self::cleanParameterUrls($urls); + self::checkUrls($urls); + self::checkAtLeastOneUrl($urls); $url = $urls[0]; $bind['main_url'] = $url; } - - if(!is_null($currency)) - { - $currency = trim($currency); - $this->checkValidCurrency($currency); - $bind['currency'] = $currency; - } - if(!is_null($timezone)) - { - $timezone = trim($timezone); - $this->checkValidTimezone($timezone); - $bind['timezone'] = $timezone; - } - $bind['excluded_ips'] = $this->checkAndReturnExcludedIps($excludedIps); - $bind['excluded_parameters'] = $this->checkAndReturnExcludedQueryParameters($excludedQueryParameters); $bind['name'] = $siteName; + $db = Zend_Registry::get('db'); - $db->update(Piwik_Common::prefixTable("site"), + $db->update(Piwik::prefixTable("site"), $bind, "idsite = $idSite" ); // we now update the main + alias URLs - $this->deleteSiteAliasUrls($idSite); + self::deleteSiteAliasUrls($idSite); if(count($urls) > 1) { - $insertedUrls = $this->addSiteAliasUrls($idSite, array_slice($urls,1)); - } - $this->postUpdateWebsite($idSite); - } - - private function checkAndReturnExcludedQueryParameters($parameters) - { - $parameters = trim($parameters); - if(empty($parameters)) - { - return ''; + $insertedUrls = self::addSiteAliasUrls($idSite, array_slice($urls,1)); } - - $parameters = explode(',', $parameters); - $parameters = array_map('trim', $parameters); - $parameters = array_filter($parameters, 'strlen'); - $parameters = array_unique($parameters); - return implode(',', $parameters); - } - - /** - * Returns the list of supported currencies - * @see getCurrencySymbols() - * @return array ( currencyId => currencyName) - */ - public function getCurrencyList() - { - return array( - 'USD' => 'US Dollar ($)', - 'EUR' => 'Euro (€)', - 'JPY' => 'Japanese Yen (¥)', - 'GBP' => 'British Pound Sterling (£)', - 'AUD' => 'Australian Dollar (A$)', - 'KRW' => 'South Korean Won (₩)', - 'BRL' => 'Brazilian Real (R$)', - 'CNY' => 'Chinese Yuan Renminbi (CN¥)', - 'DKK' => 'Danish Krone (Dkr)', - 'RUB' => 'Russian Ruble (RUB)', - 'SEK' => 'Swedish Krona (Skr)', - 'NOK' => 'Norwegian Krone (Nkr)', - 'PLN' => 'Polish Zloty (zł)', - 'TRY' => 'Turkish Lira (TL)', - 'TWD' => 'New Taiwan Dollar (NT$)', - 'HKD' => 'Hong Kong Dollar (HK$)', - 'THB' => 'Thai Baht (฿)', - 'IDR' => 'Indonesian Rupiah (Rp)', - 'ARS' => 'Argentine Peso (AR$)', - 'MXN' => 'Mexican Peso (MXN)', - 'VND' => 'Vietnamese Dong (₫)', - 'PHP' => 'Philippine Peso (Php)', - 'INR' => 'Indian Rupee (Rs.)', - 'VEF' => 'Venezuelan bolívar (Bs. F)', - 'CHF' => 'Swiss Franc (Fr.)', - ); - } - - /** - * Returns the list of currency symbols - * @see getCurrencyList() - * @return array( currencyId => currencySymbol ) - */ - public function getCurrencySymbols() - { - return array( - 'USD' => '$', - 'EUR' => '€', - 'JPY' => '¥', - 'GBP' => '£', - 'AUD' => 'A$', - 'KRW' => '₩', - 'BRL' => 'R$', - 'CNY' => 'CN¥', - 'DKK' => 'Dkr', - 'RUB' => 'RUB', - 'SEK' => 'Skr', - 'NOK' => 'Nkr', - 'PLN' => 'zł', - 'TRY' => 'TL', - 'TWD' => 'NT$', - 'HKD' => 'HK$', - 'THB' => '฿', - 'IDR' => 'Rp', - 'ARS' => 'AR$', - 'MXN' => 'MXN', - 'VND' => '₫', - 'PHP' => 'Php', - 'INR' => 'Rs.', - 'VEF' => 'Bs. F', - 'CHF' => 'Fr.', - ); - } - - - /** - * Returns the list of timezones supported. - * Used for addSite and updateSite - * - * @TODO NOT COMPATIBLE WITH API RESPONSE AUTO BUILDER - * - * @return array of timezone strings - */ - public function getTimezonesList() - { - if(!Piwik::isTimezoneSupportEnabled()) - { - return array('UTC' => $this->getTimezonesListUTCOffsets()); - } - - $continents = array( 'Africa', 'America', 'Antarctica', 'Arctic', 'Asia', 'Atlantic', 'Australia', 'Europe', 'Indian', 'Pacific'); - $timezones = timezone_identifiers_list(); - - $return = array(); - foreach($timezones as $timezone) - { - $timezoneExploded = explode('/', $timezone); - $continent = $timezoneExploded[0]; - - // only display timezones that are grouped by continent - if(!in_array($continent, $continents)) - { - continue; - } - $city = $timezoneExploded[1]; - if(!empty($timezoneExploded[2])) - { - $city .= ' - '.$timezoneExploded[2]; - } - $city = str_replace('_', ' ', $city); - $return[$continent][$timezone] = $city; - } - - foreach($continents as $continent) - { - ksort($return[$continent]); - } - - $return['UTC'] = $this->getTimezonesListUTCOffsets(); - return $return; - } - - private function getTimezonesListUTCOffsets() - { - // manually add the UTC offsets - $GmtOffsets = array (-12, -11.5, -11, -10.5, -10, -9.5, -9, -8.5, -8, -7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, - 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 5.75, 6, 6.5, 7, 7.5, 8, 8.5, 8.75, 9, 9.5, 10, 10.5, 11, 11.5, 12, 12.75, 13, 13.75, 14); - - $return = array(); - foreach($GmtOffsets as $offset) - { - if($offset > 0) - { - $offset = '+'.$offset; - } - elseif($offset == 0) - { - $offset = ''; - } - $offset = 'UTC' . $offset; - $offsetName = str_replace(array('.25','.5','.75'), array(':15',':30',':45'), $offset); - $return[$offset] = $offsetName; - } - return $return; + self::postUpdateWebsite($idSite); } /** * Insert the list of alias URLs for the website. * The URLs must not exist already for this website! */ - private function insertSiteUrls($idSite, $urls) + static private function insertSiteUrls($idSite, $urls) { if(count($urls) != 0) { $db = Zend_Registry::get('db'); foreach($urls as $url) { - $db->insert(Piwik_Common::prefixTable("site_url"), array( + $db->insert(Piwik::prefixTable("site_url"), array( 'idsite' => $idSite, 'url' => $url ) @@ -792,10 +400,10 @@ class Piwik_SitesManager_API /** * Delete all the alias URLs for the given idSite. */ - private function deleteSiteAliasUrls($idsite) + static private function deleteSiteAliasUrls($idsite) { $db = Zend_Registry::get('db'); - $db->query("DELETE FROM ".Piwik_Common::prefixTable("site_url") ." + $db->query("DELETE FROM ".Piwik::prefixTable("site_url") ." WHERE idsite = ?", $idsite); } @@ -804,7 +412,7 @@ class Piwik_SitesManager_API * * @return string the URL without the trailing slash */ - private function removeTrailingSlash($url) + static private function removeTrailingSlash($url) { // if there is a final slash, we take the URL without this slash (expected URL format) if(strlen($url) > 5 @@ -820,33 +428,17 @@ class Piwik_SitesManager_API * * @return bool */ - private function isValidUrl( $url ) + static private function isValidUrl( $url ) { return Piwik_Common::isLookLikeUrl($url); } /** - * Tests if the IP is a valid IP, allowing wildcards, except in the first octet. - * Wildcards can only be used from right to left, ie. 1.1.*.* is allowed, but 1.1.*.1 is not. - * - * @param $ip - * @return bool - */ - private function isValidIp( $ip ) - { - return preg_match('~^(\d+)\.(\d+)\.(\d+)\.(\d+)$~', $ip, $matches) !== 0 - || preg_match('~^(\d+)\.(\d+)\.(\d+)\.\*$~', $ip, $matches) !== 0 - || preg_match('~^(\d+)\.(\d+)\.\*.\*$~', $ip, $matches) !== 0 - || preg_match('~^(\d+)\.\*\.\*\.\*$~', $ip, $matches) !== 0 - ; - } - - /** * Check that the website name has a correct format. * * @exception if the website name is empty */ - private function checkName($siteName) + static private function checkName($siteName) { if(empty($siteName)) { @@ -860,11 +452,11 @@ class Piwik_SitesManager_API * @exception if any of the urls is not valid * @param array */ - private function checkUrls($urls) + static private function checkUrls($urls) { foreach($urls as $url) { - if(!$this->isValidUrl($url)) + if(!self::isValidUrl($url)) { throw new Exception(sprintf(Piwik_TranslateException("SitesManager_ExceptionInvalidUrl"),$url)); } @@ -879,20 +471,19 @@ class Piwik_SitesManager_API * @param string|array urls * @return array the array of cleaned URLs */ - private function cleanParameterUrls( $urls ) + static private function cleanParameterUrls( $urls ) { if(!is_array($urls)) { $urls = array($urls); } - - $urls = array_map('urldecode', $urls); foreach($urls as &$url) { - $url = $this->removeTrailingSlash($url); + $url = self::removeTrailingSlash($url); } $urls = array_unique($urls); return $urls; } } + diff --git a/plugins/SitesManager/Controller.php b/plugins/SitesManager/Controller.php index 84dc3d5a03..ddf93d109b 100644 --- a/plugins/SitesManager/Controller.php +++ b/plugins/SitesManager/Controller.php @@ -19,56 +19,17 @@ class Piwik_SitesManager_Controller extends Piwik_Controller function index() { $view = Piwik_View::factory('SitesManager'); - $sites = Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess(); + $sites = Piwik_SitesManager_API::getSitesWithAdminAccess(); foreach($sites as &$site) { - $site['alias_urls'] = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($site['idsite']); - $site['excluded_ips'] = str_replace(',','<br/>', $site['excluded_ips']); - $site['excluded_parameters'] = str_replace(',','<br/>', $site['excluded_parameters']); + $site['alias_urls'] = Piwik_SitesManager_API::getSiteUrlsFromId($site['idsite']); } $view->adminSites = $sites; - - $timezones = Piwik_SitesManager_API::getInstance()->getTimezonesList(); - $view->timezoneSupported = Piwik::isTimezoneSupportEnabled(); - $view->timezones = json_encode($timezones); - $view->defaultTimezone = Piwik_SitesManager_API::getInstance()->getDefaultTimezone(); - - $view->currencies = json_encode(Piwik_SitesManager_API::getInstance()->getCurrencyList()); - $view->defaultCurrency = Piwik_SitesManager_API::getInstance()->getDefaultCurrency(); - - $view->utcTime = Piwik_Date::now()->getDatetime(); - $excludedIpsGlobal = Piwik_SitesManager_API::getInstance()->getExcludedIpsGlobal(); - $view->globalExcludedIps = str_replace(',',"\n", $excludedIpsGlobal); - $excludedQueryParametersGlobal = Piwik_SitesManager_API::getInstance()->getExcludedQueryParametersGlobal(); - $view->globalExcludedQueryParameters = str_replace(',',"\n", $excludedQueryParametersGlobal); - $view->currentIpAddress = Piwik_Common::getIpString(); - $this->setGeneralVariablesView($view); $view->menu = Piwik_GetAdminMenu(); echo $view->render(); } - function setGlobalSettings() - { - $response = new Piwik_API_ResponseBuilder(Piwik_Common::getRequestVar('format')); - - try { - $this->checkTokenInUrl(); - $timezone = Piwik_Common::getRequestVar('timezone', false); - $excludedIps = Piwik_Common::getRequestVar('excludedIps', false); - $excludedQueryParameters = Piwik_Common::getRequestVar('excludedQueryParameters', false); - $currency = Piwik_Common::getRequestVar('currency', false); - Piwik_SitesManager_API::getInstance()->setDefaultTimezone($timezone); - Piwik_SitesManager_API::getInstance()->setDefaultCurrency($currency); - Piwik_SitesManager_API::getInstance()->setGlobalExcludedQueryParameters($excludedQueryParameters); - Piwik_SitesManager_API::getInstance()->setGlobalExcludedIps($excludedIps); - $toReturn = $response->getResponse(); - } catch(Exception $e ) { - $toReturn = $response->getResponseException( $e ); - } - echo $toReturn; - } - function displayJavascriptCode() { $idSite = Piwik_Common::getRequestVar('idsite', 1); @@ -77,8 +38,6 @@ class Piwik_SitesManager_Controller extends Piwik_Controller $view = Piwik_View::factory('DisplayJavascriptCode'); $this->setGeneralVariablesView($view); $view->menu = Piwik_GetAdminMenu(); - $site = new Piwik_Site($idSite); - $view->displaySiteName = $site->getName(); $view->jsTag = $jsTag; echo $view->render(); } diff --git a/plugins/SitesManager/SitesManager.php b/plugins/SitesManager/SitesManager.php index dfca3e2c6f..b2542e966c 100644 --- a/plugins/SitesManager/SitesManager.php +++ b/plugins/SitesManager/SitesManager.php @@ -19,10 +19,11 @@ class Piwik_SitesManager extends Piwik_Plugin public function getInformation() { $info = array( - 'description' => Piwik_Translate('SitesManager_PluginDescription'), + 'name' => 'Sites Management', + 'description' => 'Websites Management in Piwik: Add a new Website, Edit an existing one, Show the Javascript code to include on your pages. All the actions are also available through the API.', 'author' => 'Piwik', - 'author_homepage' => 'http://piwik.org/', - 'version' => Piwik_Version::VERSION, + 'homepage' => 'http://piwik.org/', + 'version' => '0.1', ); return $info; } @@ -32,105 +33,21 @@ class Piwik_SitesManager extends Piwik_Plugin return array( 'template_css_import' => 'css', 'AdminMenu.add' => 'addMenu', - 'Common.fetchWebsiteAttributes' => 'recordWebsiteDataInCache', + 'Common.fetchWebsiteAttributes' => 'recordWebsiteHostsInCache', ); } - function addMenu() - { - Piwik_AddAdminMenu('SitesManager_MenuSites', - array('module' => 'SitesManager', 'action' => 'index'), - Piwik::isUserHasSomeAdminAccess(), - $order = 5); - } - function css() { echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"themes/default/styles.css\" />\n"; } - /** - * Hooks when a website tracker cache is flushed (website updated, cache deleted, or empty cache) - * Will record in the tracker config file all data needed for this website in Tracker. - * - * @param $notification - * @return void - */ - function recordWebsiteDataInCache($notification) + function recordWebsiteHostsInCache($notification) { - $idSite = $notification->getNotificationInfo(); + $idsite = $notification->getNotificationInfo(); // add the 'hosts' entry in the website array $array =& $notification->getNotificationObject(); - $array['hosts'] = $this->getTrackerHosts($idSite); - $array['excluded_ips'] = $this->getTrackerExcludedIps($idSite); - $array['excluded_parameters'] = $this->getTrackerExcludedQueryParameters($idSite); - } - - /** - * Returns the array of excluded IPs to save in the config file - * @param $idSite - * @return array - */ - private function getTrackerExcludedIps($idSite) - { - $website = Piwik_SitesManager_API::getInstance()->getSiteFromId($idSite); - $excludedIps = $website['excluded_ips']; - $globalExcludedIps = Piwik_SitesManager_API::getInstance()->getExcludedIpsGlobal(); - - $excludedIps .= ',' . $globalExcludedIps; - - $ipRanges = array(); - foreach(explode(',', $excludedIps) as $ip) - { - $ipMin = $ipMax = $ip; - if(substr_count($ip, '*') > 0) - { - $ipMin = str_replace('*', '0', $ip); - $ipMax = str_replace('*', '255', $ip); - } - $ipRange = array( ip2long($ipMin), ip2long($ipMax)); - - // we can still get invalid IPs at this stage (eg. ip2long(555.1.1.1) would return false) - if($ipRange[0] === false || $ipRange[1] === false) - { - continue; - } - - // long data type is signed; convert to stringified unsigned number - $ipRange[0] = sprintf("%u", $ipRange[0]); - $ipRange[1] = sprintf("%u", $ipRange[1]); - - $ipRanges[] = $ipRange; - } - return $ipRanges; - } - - /** - * Returns the array of URL query parameters to exclude from URLs - * @param $idSite - * @return array - */ - private function getTrackerExcludedQueryParameters($idSite) - { - $website = Piwik_SitesManager_API::getInstance()->getSiteFromId($idSite); - $excludedQueryParameters = $website['excluded_parameters']; - $globalExcludedQueryParameters = Piwik_SitesManager_API::getInstance()->getExcludedQueryParametersGlobal(); - - $excludedQueryParameters .= ',' . $globalExcludedQueryParameters; - $parameters = explode(',', $excludedQueryParameters); - $parameters = array_filter($parameters, 'strlen'); - $parameters = array_unique($parameters); - return $parameters; - } - - /** - * Returns the hosts alias URLs - * @param $idSite - * @return array - */ - private function getTrackerHosts($idSite) - { - $urls = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idSite); + $urls = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $hosts = array(); foreach($urls as $url) { @@ -140,7 +57,12 @@ class Piwik_SitesManager extends Piwik_Plugin $hosts[] = $url['host']; } } - return $hosts; + $array['hosts'] = $hosts; } + function addMenu() + { + Piwik_AddAdminMenu('SitesManager_MenuSites', array('module' => 'SitesManager', 'action' => 'index')); + } } + diff --git a/plugins/SitesManager/templates/DisplayJavascriptCode.tpl b/plugins/SitesManager/templates/DisplayJavascriptCode.tpl index 29a1e53170..9fa7c7f617 100644 --- a/plugins/SitesManager/templates/DisplayJavascriptCode.tpl +++ b/plugins/SitesManager/templates/DisplayJavascriptCode.tpl @@ -2,6 +2,7 @@ {assign var=showPeriodSelection value=false} {include file="CoreAdminHome/templates/header.tpl"} {loadJavascriptTranslations plugins='SitesManager'} +{include file="CoreAdminHome/templates/menu.tpl"} {literal} <style> @@ -12,7 +13,7 @@ code { border-width:1px 1px 1px 5px; direction:ltr; display:block; - font-size:90%; + font-size:80%; margin:2px 2px 20px; padding:4px; text-align:left; @@ -21,7 +22,7 @@ code { </style> {/literal} -<h2>{$displaySiteName}</h2> +<h2>{$siteName}</h2> <p>{'SitesManager_JsTrackingTagHelp'|translate}:</p> <code>{$jsTag}</code> diff --git a/plugins/SitesManager/templates/SitesManager.js b/plugins/SitesManager/templates/SitesManager.js index cf14893c8c..7b523b98a4 100644 --- a/plugins/SitesManager/templates/SitesManager.js +++ b/plugins/SitesManager/templates/SitesManager.js @@ -1,7 +1,8 @@ function getDeleteSiteAJAX( idSite ) { var ajaxRequest = piwikHelper.getStandardAjaxConf(); - + piwikHelper.toggleAjaxLoading(); + var parameters = {}; parameters.module = 'API'; parameters.format = 'json'; @@ -17,28 +18,19 @@ function getDeleteSiteAJAX( idSite ) function getAddSiteAJAX( row ) { var ajaxRequest = piwikHelper.getStandardAjaxConf(); + piwikHelper.toggleAjaxLoading(); var parameters = {}; - var siteName = $(row).find('input#name').val(); - var urls = $(row).find('textarea#urls').val(); - urls = getApiFormatUrls(urls); - var excludedIps = $(row).find('textarea#excludedIps').val(); - excludedIps = getApiFormatTextarea(excludedIps); - var timezone = encodeURIComponent($(row).find('#timezones option:selected').val()); - var currency = encodeURIComponent($(row).find('#currencies option:selected').val()); - var excludedQueryParameters = $(row).find('textarea#excludedQueryParameters').val(); - excludedQueryParameters = getApiFormatTextarea(excludedQueryParameters); - + var siteName = $(row).find('input#siteadd_name').val(); + var urls = $(row).find('textarea#siteadd_urls').val(); + var urls = urls.trim().split("\n"); + var request = ''; request += '&module=API'; request += '&format=json'; request += '&method=SitesManager.addSite'; siteName = encodeURIComponent(siteName); request += '&siteName='+siteName; - request += '&timezone='+timezone; - request += '¤cy='+currency; - request += '&excludedIps='+excludedIps; - request += '&excludedQueryParameters='+excludedQueryParameters; $.each(urls, function (key,value){ request+= '&urls[]='+escape(value);} ); request += '&token_auth=' + piwik.token_auth; @@ -47,33 +39,15 @@ function getAddSiteAJAX( row ) return ajaxRequest; } -function getApiFormatUrls(urls) -{ - var aUrls = urls.trim().split("\n"); - for(var i=0; i < aUrls.length; i++) { - aUrls[i] = encodeURIComponent(aUrls[i]); - } - return aUrls; -} -function getApiFormatTextarea(textareaContent) -{ - return textareaContent.trim().split("\n").join(','); -} - function getUpdateSiteAJAX( row ) { var ajaxRequest = piwikHelper.getStandardAjaxConf(); + piwikHelper.toggleAjaxLoading(); var siteName = $(row).find('input#siteName').val(); var idSite = $(row).children('#idSite').html(); - var urls = $(row).find('textarea#urls').val(); - urls = getApiFormatUrls(urls); - var excludedIps = $(row).find('textarea#excludedIps').val(); - excludedIps = getApiFormatTextarea(excludedIps); - var excludedQueryParameters = $(row).find('textarea#excludedQueryParameters').val(); - excludedQueryParameters = getApiFormatTextarea(excludedQueryParameters); - var timezone = encodeURIComponent($(row).find('#timezones option:selected').val()); - var currency = encodeURIComponent($(row).find('#currencies option:selected').val()); + var urls = $(row).find('textarea#urls').val().trim().split("\n"); + var request = ''; request += '&module=API'; request += '&format=json'; @@ -81,10 +55,6 @@ function getUpdateSiteAJAX( row ) siteName = encodeURIComponent(siteName); request += '&siteName='+siteName; request += '&idSite='+idSite; - request += '&timezone='+timezone; - request += '¤cy='+currency; - request += '&excludedIps='+excludedIps; - request += '&excludedQueryParameters='+excludedQueryParameters; $.each(urls, function (key,value){ if(value.length>1) request+= '&urls[]='+value;} ); request += '&token_auth=' + piwik.token_auth; @@ -93,31 +63,9 @@ function getUpdateSiteAJAX( row ) return ajaxRequest; } -function getGlobalSettingsAJAX() -{ - var ajaxRequest = piwikHelper.getStandardAjaxConf('ajaxLoadingGlobalSettings', 'ajaxErrorGlobalSettings'); - var timezone = encodeURIComponent($('#defaultTimezone option:selected').val()); - var currency = encodeURIComponent($('#defaultCurrency option:selected').val()); - var excludedIps = $('textarea#globalExcludedIps').val(); - excludedIps = getApiFormatTextarea(excludedIps); - var excludedQueryParameters = $('textarea#globalExcludedQueryParameters').val(); - excludedQueryParameters = getApiFormatTextarea(excludedQueryParameters); - var request = ''; - request += 'module=SitesManager'; - request += '&action=setGlobalSettings'; - request += '&format=json'; - request += '&timezone='+timezone; - request += '¤cy='+currency; - request += '&excludedIps='+excludedIps; - request += '&excludedQueryParameters='+excludedQueryParameters; - request += '&token_auth=' + piwik.token_auth; - ajaxRequest.data = request; - return ajaxRequest; -} - $(document).ready( function() { $('.addRowSite').click( function() { - piwikHelper.hideAjaxError(); + piwikHelper.ajaxHideError(); $(this).toggle(); var numberOfRows = $('table#editSites')[0].rows.length; @@ -125,25 +73,22 @@ $(document).ready( function() { $(' <tr id="'+newRowId+'">\ <td> </td>\ - <td><input id="name" value="Name" size="15" /></td>\ - <td><textarea cols="25" rows="3" id="urls">http://siteUrl.com/\nhttp://siteUrl2.com/</textarea><br />'+aliasUrlsHelp+'</td>\ - <td><textarea cols="20" rows="4" id="excludedIps"></textarea><br />'+excludedIpHelp+'</td>\ - <td><textarea cols="20" rows="4" id="excludedQueryParameters"></textarea><br />'+excludedQueryParametersHelp+'</td>\ - <td>'+getTimezoneSelector(defaultTimezone)+'<br />' + timezoneHelp + '</td>\ - <td>'+getCurrencySelector(defaultCurrency)+'<br />' + currencyHelp + '</td>\ - <td><img src="plugins/UsersManager/images/ok.png" class="addsite" href="#" title="' + _pk_translate('SitesManager_Save_js') + '" /></td>\ - <td><img src="plugins/UsersManager/images/remove.png" class="cancel" title="' + _pk_translate('SitesManager_Cancel_js') +'" /></td>\ + <td><input id="siteadd_name" value="Name" size=25></td>\ + <td><textarea cols=30 rows=3 id="siteadd_urls">http://siteUrl.com/\nhttp://siteUrl2.com/</textarea></td>\ + <td><img src="plugins/UsersManager/images/ok.png" class="addsite" href="#"></td>\ + <td><img src="plugins/UsersManager/images/remove.png" class="cancel"></td>\ </tr>') .appendTo('#editSites') ; + $('#'+newRowId).keypress( submitSiteOnEnter ); $('.addsite').click( function(){ $.ajax( getAddSiteAJAX($('tr#'+newRowId)) ); } ); - $('.cancel').click(function() { piwikHelper.hideAjaxError(); $(this).parents('tr').remove(); $('.addRowSite').toggle(); }); - return false; + $('.cancel').click(function() { piwikHelper.ajaxHideError(); $(this).parents('tr').remove(); $('.addRowSite').toggle(); }); + } ); // when click on deleteuser, the we ask for confirmation and then delete the user $('.deleteSite').click( function() { - piwikHelper.hideAjaxError(); + piwikHelper.ajaxHideError(); var idRow = $(this).attr('id'); var nameToDelete = $(this).parent().parent().find('input#siteName').val() || $(this).parent().parent().find('td#siteName').html(); var idsiteToDelete = $(this).parent().parent().find('#idSite').html(); @@ -156,7 +101,7 @@ $(document).ready( function() { var alreadyEdited = new Array; $('.editSite') .click( function() { - piwikHelper.hideAjaxError(); + piwikHelper.ajaxHideError(); var idRow = $(this).attr('id'); if(alreadyEdited[idRow]==1) return; alreadyEdited[idRow] = 1; @@ -168,39 +113,14 @@ $(document).ready( function() { var idName = $(n).attr('id'); if(idName == 'siteName') { - var contentAfter = '<input id="'+idName+'" value="'+contentBefore+'" size="15" />'; + var contentAfter = '<input id="'+idName+'" value="'+contentBefore+'" size="25">'; $(n) .html(contentAfter) .keypress( submitSiteOnEnter ); } if(idName == 'urls') { - var contentAfter = '<textarea cols="25" rows="3" id="urls">'+contentBefore.replace(/<br *\/? *>/gi,"\n")+'</textarea>'; - contentAfter += '<br />'+aliasUrlsHelp; - $(n).html(contentAfter); - } - if(idName == 'excludedIps') - { - var contentAfter = '<textarea cols="20" rows="4" id="excludedIps">'+contentBefore.replace(/<br *\/? *>/gi,"\n")+'</textarea>'; - contentAfter += '<br />'+excludedIpHelp; - $(n).html(contentAfter); - } - if(idName == 'excludedQueryParameters') - { - var contentAfter = '<textarea cols="20" rows="4" id="excludedQueryParameters">'+contentBefore.replace(/<br *\/? *>/gi,"\n")+'</textarea>'; - contentAfter += '<br />'+excludedQueryParametersHelp; - $(n).html(contentAfter); - } - if(idName == 'timezone') - { - var contentAfter = getTimezoneSelector(contentBefore); - contentAfter += '<br />' + timezoneHelp; - $(n).html(contentAfter); - } - if(idName == 'currency') - { - var contentAfter = getCurrencySelector(contentBefore); - contentAfter += '<br />' + currencyHelp; + var contentAfter = '<textarea cols=30 rows=3 id="urls">'+contentBefore.replace(/<br *\/? *>/gi,"\n")+'</textarea>'; $(n).html(contentAfter); } } @@ -208,55 +128,15 @@ $(document).ready( function() { $(this) .toggle() .parent() - .prepend( $('<img src="plugins/UsersManager/images/ok.png" class="updateSite" title="' + _pk_translate('SitesManager_Save_js') + '" />') + .prepend( $('<img src="plugins/UsersManager/images/ok.png" class="updateSite">') .click( function(){ $.ajax( getUpdateSiteAJAX( $('tr#'+idRow) ) ); } ) ); } ); - $('#globalSettingsSubmit').click( function() { - $.ajax( getGlobalSettingsAJAX() ); - }); - - $('#defaultTimezone').html( getTimezoneSelector(defaultTimezone)); - $('#defaultCurrency').html( getCurrencySelector(defaultCurrency)); - $('td.editableSite').click( function(){ $(this).parent().find('.editSite').click(); } ); }); - -function getTimezoneSelector(selectedTimezone) -{ - var html = '<select id="timezones">'; - for(var continent in timezones) { - html += '<optgroup label="' + continent + '">'; - for(var timezoneId in timezones[continent]) { - var selected = ''; - if(timezoneId == selectedTimezone) { - selected = ' selected="selected" '; - } - html += '<option ' + selected + ' value="'+ timezoneId + '">' + timezones[continent][timezoneId] + '</option>'; - } - html += "</optgroup>\n"; - } - html += '</select>'; - return html; -} - - -function getCurrencySelector(selectedCurrency) -{ - var html = '<select id="currencies">'; - for(var currency in currencies) { - var selected = ''; - if(currency == selectedCurrency) { - selected = ' selected="selected" '; - } - html += '<option ' + selected + ' value="'+ currency + '">' + currencies[currency] + '</option>'; - } - html += '</select>'; - return html; -} - + function submitSiteOnEnter(e) { var key=e.keyCode || e.which; diff --git a/plugins/SitesManager/templates/SitesManager.tpl b/plugins/SitesManager/templates/SitesManager.tpl index b61a3eaea2..39d7f9261a 100644 --- a/plugins/SitesManager/templates/SitesManager.tpl +++ b/plugins/SitesManager/templates/SitesManager.tpl @@ -2,47 +2,9 @@ {assign var=showPeriodSelection value=false} {include file="CoreAdminHome/templates/header.tpl"} {loadJavascriptTranslations plugins='SitesManager'} - -<script type="text/javascript"> -{capture assign=excludedIpHelpPlain}{'SitesManager_HelpExcludedIps'|translate:"1.2.3.*":"1.2.*.*"}<br /><br /> {'SitesManager_YourCurrentIpAddressIs'|translate:"<i>$currentIpAddress</i>"}{/capture} -{assign var=excludedIpHelp value=$excludedIpHelpPlain|inlineHelp} -var excludedIpHelp = '{$excludedIpHelp|escape:javascript}'; -var aliasUrlsHelp = '{'SitesManager_AliasUrlHelp'|translate|inlineHelp|escape:javascript}'; -{capture assign=defaultTimezoneHelpPlain} - {if $timezoneSupported} - {'SitesManager_ChooseCityInSameTimezoneAsYou'|translate} - {else} - {'SitesManager_AdvancedTimezoneSupportNotFound'|translate} - {/if} <br /><br />{'SitesManager_UTCTimeIs'|translate:$utcTime} -{/capture} - -{capture assign=timezoneHelpPlain} - {$defaultTimezoneHelpPlain} - <br /><br />{'SitesManager_ChangingYourTimezoneWillOnlyAffectDataForward'|translate} -{/capture} - -{capture assign=currencyHelpPlain} - {'SitesManager_CurrencySymbolWillBeUsedForGoals'|translate|inlineHelp} -{/capture} - -{capture assign=excludedQueryParametersHelp} - {'SitesManager_ListOfQueryParametersToExclude'|translate} - <br /><br /> - {'SitesManager_PiwikWillAutomaticallyExcludeCommonSessionParameters'|translate:"phpsessid, sessionid, etc."} -{/capture} -{assign var=excludedQueryParametersHelp value=$excludedQueryParametersHelp|inlineHelp} -var excludedQueryParametersHelp = '{$excludedQueryParametersHelp|escape:javascript}'; -var timezoneHelp = '{$timezoneHelpPlain|inlineHelp|escape:javascript}'; -var currencyHelp = '{$currencyHelpPlain|escape:javascript}'; -{assign var=defaultTimezoneHelp value=$defaultTimezoneHelpPlain|inlineHelp}; -var timezones = {$timezones}; -var currencies = {$currencies}; -var defaultTimezone = '{$defaultTimezone}'; -var defaultCurrency = '{$defaultCurrency}'; -</script> +{include file="CoreAdminHome/templates/menu.tpl"} <script type="text/javascript" src="plugins/SitesManager/templates/SitesManager.js"></script> - {literal} <style> .addRowSite:hover, .editableSite:hover, .addsite:hover, .cancel:hover, .deleteSite:hover, .editSite:hover, .updateSite:hover{ @@ -60,27 +22,13 @@ var defaultCurrency = '{$defaultCurrency}'; #editSites { valign: top; } -option, select { - font-size:11px; -} -textarea { -font-size:9pt; -} -.admin thead th { -vertical-align:middle; -} </style> {/literal} - <h2>{'SitesManager_WebsitesManagement'|translate}</h2> -<p>{'SitesManager_MainDescription'|translate} -{if $isSuperUser} -<br />{'SitesManager_SuperUserCan'|translate:"<a href='#globalSettings'>":"</a>"} -{/if} -</p> -{ajaxErrorDiv} -{ajaxLoadingDiv} +<p>{'SitesManager_MainDescription'|translate}</p> +<div id="ajaxError" style="display:none"></div> +<div id="ajaxLoading" style="display:none"><div id="loadingPiwik"><img src="themes/default/images/loading-blue.gif" alt="" /> {'General_LoadingData'|translate} </div></div> {if $adminSites|@count == 0} {'SitesManager_NoWebsites'|translate} @@ -91,10 +39,6 @@ vertical-align:middle; <th>{'SitesManager_Id'|translate}</th> <th>{'SitesManager_Name'|translate}</th> <th>{'SitesManager_Urls'|translate}</th> - <th>{'SitesManager_ExcludedIps'|translate}</th> - <th>{'SitesManager_ExcludedParameters'|translate|replace:" ":"<br />"}</th> - <th>{'SitesManager_Timezone'|translate}</th> - <th>{'SitesManager_Currency'|translate}</th> <th> </th> <th> </th> <th> {'SitesManager_JsTrackingTag'|translate} </th> @@ -106,75 +50,15 @@ vertical-align:middle; <td id="idSite">{$site.idsite}</td> <td id="siteName" class="editableSite">{$site.name}</td> <td id="urls" class="editableSite">{foreach from=$site.alias_urls item=url}{$url}<br />{/foreach}</td> - <td id="excludedIps" class="editableSite">{foreach from=$site.excluded_ips item=ip}{$ip}<br />{/foreach}</td> - <td id="excludedQueryParameters" class="editableSite">{foreach from=$site.excluded_parameters item=parameter}{$parameter}<br />{/foreach}</td> - <td id="timezone" class="editableSite">{$site.timezone}</td> - <td id="currency" class="editableSite">{$site.currency}</td> - <td><img src='plugins/UsersManager/images/edit.png' class="editSite" id="row{$i}" href='#' title="{'General_Edit'|translate}" /></td> - <td><img src='plugins/UsersManager/images/remove.png' class="deleteSite" id="row{$i}" title="{'General_Delete'|translate}" value="{'General_Delete'|translate}" /></td> + <td><img src='plugins/UsersManager/images/edit.png' class="editSite" id="row{$i}" href='#' alt="" /></td> + <td><img src='plugins/UsersManager/images/remove.png' class="deleteSite" id="row{$i}" value="{'General_Delete'|translate}" alt="" /></td> <td><a href='{url action=displayJavascriptCode idsite=$site.idsite}'>{'SitesManager_ShowTrackingTag'|translate}</a></td> </tr> {/foreach} + </tbody> </table> - {if $isSuperUser} - <div class="addRowSite"><a href=""><img src='plugins/UsersManager/images/add.png' alt="" /> {'SitesManager_AddSite'|translate}</a></div> - {/if} -{/if} - -{if $isSuperUser} -<br /> - <a name='globalSettings'></a> - <h2>{'SitesManager_GlobalWebsitesSettings'|translate}</h2> - <br /> - <table style='width:600px' class="adminTable adminTableNoBorder" > - - <tr><td colspan="2"> - <b>{'SitesManager_GlobalListExcludedIps'|translate}</b> - <p>{'SitesManager_ListOfIpsToBeExcludedOnAllWebsites'|translate} </p> - </td></tr> - <tr><td> - <textarea cols="30" rows="3" id="globalExcludedIps">{$globalExcludedIps} -</textarea> - </td><td> - {$excludedIpHelp} - </td></tr> - - <tr><td colspan="2"> - <b>{'SitesManager_GlobalListExcludedQueryParameters'|translate}</b> - <p>{'SitesManager_ListOfQueryParametersToBeExcludedOnAllWebsites'|translate} </p> - </td></tr> - <tr><td> - <textarea cols="30" rows="3" id="globalExcludedQueryParameters">{$globalExcludedQueryParameters} -</textarea> - </td><td> - {$excludedQueryParametersHelp} - </td></tr> - - <tr><td colspan="2"> - <b>{'SitesManager_DefaultTimezoneForNewWebsites'|translate}</b> - <p>{'SitesManager_SelectDefaultTimezone'|translate} </p> - </td></tr> - <tr><td> - <div id='defaultTimezone'></div> - </td><td> - {$defaultTimezoneHelp} - </td></tr> - - <tr><td colspan="2"> - <b>{'SitesManager_DefaultCurrencyForNewWebsites'|translate}</b> - <p>{'SitesManager_SelectDefaultCurrency'|translate} </p> - </td></tr> - <tr><td> - <div id='defaultCurrency'></div> - </td><td> - {$currencyHelpPlain} - </td></tr> - </table> - <span style='margin-left:20px'><input type="submit" class="submit" id='globalSettingsSubmit' value="{'General_Save'|translate}" /></span> - {ajaxErrorDiv id=ajaxErrorGlobalSettings} - {ajaxLoadingDiv id=ajaxLoadingGlobalSettings} + <div class="addRowSite"><a href="#"><img src='plugins/UsersManager/images/add.png' alt="" /> {'SitesManager_AddSite'|translate}</a></div> {/if} -<br /><br /><br /><br /> {include file="CoreAdminHome/templates/footer.tpl"} diff --git a/plugins/SitesManager/tests/SitesManager.test.php b/plugins/SitesManager/tests/SitesManager.test.php index fbaf8c7adf..db4c765812 100644 --- a/plugins/SitesManager/tests/SitesManager.test.php +++ b/plugins/SitesManager/tests/SitesManager.test.php @@ -7,15 +7,10 @@ if(!defined('PIWIK_CONFIG_TEST_INCLUDED')) require_once PIWIK_PATH_TEST_TO_ROOT . "/tests/config_test.php"; } -require_once PIWIK_PATH_TEST_TO_ROOT . '/tests/core/Database.test.php'; +require_once "Database.test.php"; class Test_Piwik_SitesManager extends Test_Database { - public function __construct() - { - Piwik_PluginsManager::getInstance()->unloadPlugin('ExampleFeedburner'); - parent::__construct(); - } public function setUp() { parent::setUp(); @@ -24,9 +19,6 @@ class Test_Piwik_SitesManager extends Test_Database $pseudoMockAccess = new FakeAccess; FakeAccess::$superUser = true; Zend_Registry::set('access', $pseudoMockAccess); - - // clear static Site cache - Piwik_Site::clearCache(); } /** @@ -35,7 +27,7 @@ class Test_Piwik_SitesManager extends Test_Database public function test_addSite_emptyName() { try { - Piwik_SitesManager_API::getInstance()->addSite("",array("http://piwik.net")); + Piwik_SitesManager_API::addSite("",array("http://piwik.net")); } catch (Exception $expected) { return; @@ -49,7 +41,7 @@ class Test_Piwik_SitesManager extends Test_Database public function test_addSite_noUrls() { try { - Piwik_SitesManager_API::getInstance()->addSite("name",array()); + Piwik_SitesManager_API::addSite("name",array()); } catch (Exception $expected) { return; @@ -63,7 +55,7 @@ class Test_Piwik_SitesManager extends Test_Database public function test_addSite_wrongUrls1() { try { - Piwik_SitesManager_API::getInstance()->addSite("name",array("")); + Piwik_SitesManager_API::addSite("name",array("")); } catch (Exception $expected) { return; @@ -76,7 +68,7 @@ class Test_Piwik_SitesManager extends Test_Database public function test_addSite_wrongUrls2() { try { - Piwik_SitesManager_API::getInstance()->addSite("name",""); + Piwik_SitesManager_API::addSite("name",""); } catch (Exception $expected) { return; @@ -90,7 +82,7 @@ class Test_Piwik_SitesManager extends Test_Database public function test_addSite_wrongUrls3() { try { - Piwik_SitesManager_API::getInstance()->addSite("name","httpww://piwik.net"); + Piwik_SitesManager_API::addSite("name","httpww://piwik.net"); } catch (Exception $expected) { return; @@ -104,7 +96,7 @@ class Test_Piwik_SitesManager extends Test_Database public function test_addSite_wrongUrls4() { try { - Piwik_SitesManager_API::getInstance()->addSite("name","httpww://piwik.net/gqg~#"); + Piwik_SitesManager_API::addSite("name","httpww://piwik.net/gqg~#"); } catch (Exception $expected) { return; @@ -113,68 +105,19 @@ class Test_Piwik_SitesManager extends Test_Database } /** - * Test with valid IPs - */ - public function test_addSite_excludedIpsAndtimezoneAndCurrencyAndExcludedQueryParameters_valid() - { - $ips = '1.2.3.4,1.1.1.*,1.2.*.*,1.*.*.*'; - $timezone = 'Europe/Paris'; - $currency = 'EUR'; - $excludedQueryParameters = 'p1,P2, P33333'; - $expectedExcludedQueryParameters = 'p1,P2,P33333'; - $idsite = Piwik_SitesManager_API::getInstance()->addSite("name","http://piwik.net/", $ips, $excludedQueryParameters,$timezone, $currency); - $siteInfo = Piwik_SitesManager_API::getInstance()->getSiteFromId($idsite); - $this->assertEqual($siteInfo['excluded_ips'], $ips); - $this->assertEqual($siteInfo['timezone'], $timezone); - $this->assertEqual($siteInfo['currency'], $currency); - $this->assertEqual($siteInfo['excluded_parameters'], $expectedExcludedQueryParameters); - } - - /** - * Test with invalid IPs - */ - public function test_addSite_excludedIps_notValid() - { - $invalidIps = array( - '35817587341', - 'ieagieha', - '1.2.3', - '*.1.1.1', - '*.*.1.1', - '*.*.*.1', - '*.*.*.*', - '1.1.1.1.1', - ); - foreach($invalidIps as $ip) - { - $raised = false; - try { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("name","http://piwik.net/", $ip); - } catch(Exception $e) { - $raised = true; - } - if(!$raised) - { - $this->fail('was expecting invalid IP exception to raise'); - } - } - $this->pass(); - } - - /** * one url -> one main_url and nothing inserted as alias urls */ public function test_addSite_oneUrl() { $url = "http://piwik.net/"; $urlOK = "http://piwik.net"; - $idsite = Piwik_SitesManager_API::getInstance()->addSite("name",$url); + $idsite = Piwik_SitesManager_API::addSite("name",$url); $this->assertIsA( $idsite,'int'); - $siteInfo = Piwik_SitesManager_API::getInstance()->getSiteFromId($idsite); + $siteInfo = Piwik_SitesManager_API::getSiteFromId($idsite); $this->assertEqual($siteInfo['main_url'], $urlOK); - $siteUrls = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $siteUrls = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $this->assertTrue(count($siteUrls)===1); } @@ -185,13 +128,13 @@ class Test_Piwik_SitesManager extends Test_Database { $urls = array("http://piwik.net/","http://piwik.com","https://piwik.net/test/"); $urlsOK = array("http://piwik.net","http://piwik.com","https://piwik.net/test"); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("super website",$urls); + $idsite = Piwik_SitesManager_API::addSite("super website",$urls); $this->assertIsA( $idsite,'int'); - $siteInfo = Piwik_SitesManager_API::getInstance()->getSiteFromId($idsite); + $siteInfo = Piwik_SitesManager_API::getSiteFromId($idsite); $this->assertEqual($siteInfo['main_url'], $urlsOK[0]); - $siteUrls = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $siteUrls = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $this->assertEqual($siteUrls, $urlsOK); } @@ -201,10 +144,10 @@ class Test_Piwik_SitesManager extends Test_Database public function test_addSite_strangeName() { $name = "supertest(); ~@@()''!£\$'%%^'!£"; - $idsite = Piwik_SitesManager_API::getInstance()->addSite($name,"http://piwik.net"); + $idsite = Piwik_SitesManager_API::addSite($name,"http://piwik.net"); $this->assertIsA( $idsite,'int'); - $siteInfo = Piwik_SitesManager_API::getInstance()->getSiteFromId($idsite); + $siteInfo = Piwik_SitesManager_API::getSiteFromId($idsite); $this->assertEqual($siteInfo['name'], $name); } @@ -214,14 +157,14 @@ class Test_Piwik_SitesManager extends Test_Database public function test_addSite() { $name = "website "; - $idsite = Piwik_SitesManager_API::getInstance()->addSite($name,array("http://piwik.net","http://piwik.com/test/")); + $idsite = Piwik_SitesManager_API::addSite($name,array("http://piwik.net","http://piwik.com/test/")); $this->assertIsA( $idsite,'int'); - $siteInfo = Piwik_SitesManager_API::getInstance()->getSiteFromId($idsite); + $siteInfo = Piwik_SitesManager_API::getSiteFromId($idsite); $this->assertEqual($siteInfo['name'], $name); $this->assertEqual($siteInfo['main_url'], "http://piwik.net"); - $siteUrls = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $siteUrls = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $this->assertEqual($siteUrls, array("http://piwik.net","http://piwik.com/test")); return $idsite; @@ -234,7 +177,7 @@ class Test_Piwik_SitesManager extends Test_Database { $idsite = $this->test_addSite(); - $siteUrlsBefore = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $siteUrlsBefore = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $toAdd = array( "http://piwik1.net", "http://piwik2.net", @@ -252,10 +195,10 @@ class Test_Piwik_SitesManager extends Test_Database "http://l42578gqege.f4", "http://super.com/test/test/atqata675675/te"); - $insertedUrls = Piwik_SitesManager_API::getInstance()->addSiteAliasUrls($idsite, $toAdd); + $insertedUrls = Piwik_SitesManager_API::addSiteAliasUrls($idsite, $toAdd); $this->assertEqual($insertedUrls, count($toAdd)); - $siteUrlsAfter = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $siteUrlsAfter = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $shouldHave = array_merge($siteUrlsBefore, $toAddValid); sort($shouldHave); @@ -272,14 +215,14 @@ class Test_Piwik_SitesManager extends Test_Database { $idsite = $this->test_addSite(); - $siteUrlsBefore = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $siteUrlsBefore = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $toAdd = array_merge($siteUrlsBefore, array("http://piwik1.net","http://piwik2.net")); - $insertedUrls = Piwik_SitesManager_API::getInstance()->addSiteAliasUrls($idsite, $toAdd); + $insertedUrls = Piwik_SitesManager_API::addSiteAliasUrls($idsite, $toAdd); $this->assertEqual($insertedUrls, count($toAdd) - count($siteUrlsBefore)); - $siteUrlsAfter = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $siteUrlsAfter = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $shouldHave = $toAdd; sort($shouldHave); @@ -296,14 +239,14 @@ class Test_Piwik_SitesManager extends Test_Database { $idsite = $this->test_addSite(); - $siteUrlsBefore = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $siteUrlsBefore = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $toAdd = array(); - $insertedUrls = Piwik_SitesManager_API::getInstance()->addSiteAliasUrls($idsite, $toAdd); + $insertedUrls = Piwik_SitesManager_API::addSiteAliasUrls($idsite, $toAdd); $this->assertEqual($insertedUrls, count($toAdd)); - $siteUrlsAfter = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $siteUrlsAfter = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $shouldHave = $siteUrlsBefore; sort($shouldHave); @@ -320,14 +263,14 @@ class Test_Piwik_SitesManager extends Test_Database { $idsite = $this->test_addSite(); - $siteUrlsBefore = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $siteUrlsBefore = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $toAdd = $siteUrlsBefore; - $insertedUrls = Piwik_SitesManager_API::getInstance()->addSiteAliasUrls($idsite, $toAdd); + $insertedUrls = Piwik_SitesManager_API::addSiteAliasUrls($idsite, $toAdd); $this->assertEqual($insertedUrls, 0); - $siteUrlsAfter = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $siteUrlsAfter = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $shouldHave = $siteUrlsBefore; sort($shouldHave); @@ -344,7 +287,7 @@ class Test_Piwik_SitesManager extends Test_Database $idsite = $this->test_addSite(); $toAdd = array("htt{}p://pigeq.com/test"); try { - $insertedUrls = Piwik_SitesManager_API::getInstance()->addSiteAliasUrls($idsite, $toAdd); + $insertedUrls = Piwik_SitesManager_API::addSiteAliasUrls($idsite, $toAdd); } catch (Exception $expected) { return; @@ -359,7 +302,7 @@ class Test_Piwik_SitesManager extends Test_Database { $toAdd = array("http://pigeq.com/test"); try { - $insertedUrls = Piwik_SitesManager_API::getInstance()->addSiteAliasUrls(-1, $toAdd); + $insertedUrls = Piwik_SitesManager_API::addSiteAliasUrls(-1, $toAdd); } catch (Exception $expected) { return; @@ -375,7 +318,7 @@ class Test_Piwik_SitesManager extends Test_Database $toAdd = array("http://pigeq.com/test"); try { - $insertedUrls = Piwik_SitesManager_API::getInstance()->addSiteAliasUrls(155, $toAdd); + $insertedUrls = Piwik_SitesManager_API::addSiteAliasUrls(155, $toAdd); } catch (Exception $expected) { return; @@ -388,7 +331,7 @@ class Test_Piwik_SitesManager extends Test_Database */ function test_getAllSitesId_noId() { - $ids = Piwik_SitesManager_API::getInstance()->getAllSitesId(); + $ids = Piwik_SitesManager_API::getAllSitesId(); $this->assertEqual(array(),$ids); } @@ -399,14 +342,14 @@ class Test_Piwik_SitesManager extends Test_Database { $name="tetq"; $idsites = array( - Piwik_SitesManager_API::getInstance()->addSite($name,array("http://piwik.net","http://piwik.com/test/")), - Piwik_SitesManager_API::getInstance()->addSite($name,array("http://piwik.net","http://piwik.com/test/")), - Piwik_SitesManager_API::getInstance()->addSite($name,array("http://piwik.net","http://piwik.com/test/")), - Piwik_SitesManager_API::getInstance()->addSite($name,array("http://piwik.net","http://piwik.com/test/")), - Piwik_SitesManager_API::getInstance()->addSite($name,array("http://piwik.net","http://piwik.com/test/")), + Piwik_SitesManager_API::addSite($name,array("http://piwik.net","http://piwik.com/test/")), + Piwik_SitesManager_API::addSite($name,array("http://piwik.net","http://piwik.com/test/")), + Piwik_SitesManager_API::addSite($name,array("http://piwik.net","http://piwik.com/test/")), + Piwik_SitesManager_API::addSite($name,array("http://piwik.net","http://piwik.com/test/")), + Piwik_SitesManager_API::addSite($name,array("http://piwik.net","http://piwik.com/test/")), ); - $ids = Piwik_SitesManager_API::getInstance()->getAllSitesId(); + $ids = Piwik_SitesManager_API::getAllSitesId(); $this->assertEqual($idsites,$ids); } @@ -417,7 +360,7 @@ class Test_Piwik_SitesManager extends Test_Database { try { - $siteInfo = Piwik_SitesManager_API::getInstance()->getSiteFromId(0); + $siteInfo = Piwik_SitesManager_API::getSiteFromId(0); } catch (Exception $expected) { return; @@ -432,7 +375,7 @@ class Test_Piwik_SitesManager extends Test_Database { try { - $siteInfo = Piwik_SitesManager_API::getInstance()->getSiteFromId("x1"); + $siteInfo = Piwik_SitesManager_API::getSiteFromId("x1"); } catch (Exception $expected) { return; @@ -445,7 +388,7 @@ class Test_Piwik_SitesManager extends Test_Database */ function test_getSiteFromId_wrongId3() { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site",array("http://piwik.net","http://piwik.com/test/")); + $idsite = Piwik_SitesManager_API::addSite("site",array("http://piwik.net","http://piwik.com/test/")); $this->assertEqual($idsite,1); // set noaccess to site 1 @@ -453,7 +396,7 @@ class Test_Piwik_SitesManager extends Test_Database FakeAccess::setIdSitesAdmin (array()); try { - $siteInfo = Piwik_SitesManager_API::getInstance()->getSiteFromId(1); + $siteInfo = Piwik_SitesManager_API::getSiteFromId(1); } catch (Exception $expected) { return; @@ -467,10 +410,10 @@ class Test_Piwik_SitesManager extends Test_Database function test_getSiteFromId_normalId() { $name = "website ''"; - $idsite = Piwik_SitesManager_API::getInstance()->addSite($name,array("http://piwik.net","http://piwik.com/test/")); + $idsite = Piwik_SitesManager_API::addSite($name,array("http://piwik.net","http://piwik.com/test/")); $this->assertIsA( $idsite,'int'); - $siteInfo = Piwik_SitesManager_API::getInstance()->getSiteFromId($idsite); + $siteInfo = Piwik_SitesManager_API::getSiteFromId($idsite); $this->assertEqual($siteInfo['name'], $name); $this->assertEqual($siteInfo['main_url'], "http://piwik.net"); } @@ -483,7 +426,7 @@ class Test_Piwik_SitesManager extends Test_Database { FakeAccess::setIdSitesAdmin (array()); - $sites = Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess(); + $sites = Piwik_SitesManager_API::getSitesWithAdminAccess(); $this->assertEqual($sites, array()); } @@ -492,18 +435,18 @@ class Test_Piwik_SitesManager extends Test_Database */ function test_getSitesWithAdminAccess() { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",array("http://piwik.net","http://piwik.com/test/")); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site2",array("http://piwik.com/test/")); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site3",array("http://piwik.org")); + $idsite = Piwik_SitesManager_API::addSite("site1",array("http://piwik.net","http://piwik.com/test/")); + $idsite = Piwik_SitesManager_API::addSite("site2",array("http://piwik.com/test/")); + $idsite = Piwik_SitesManager_API::addSite("site3",array("http://piwik.org")); $resultWanted = array( - 0 => array("idsite" => 1, "name" => "site1", "main_url" =>"http://piwik.net", "excluded_ips" => "", 'excluded_parameters' => '', 'timezone' => 'UTC', 'currency' => 'USD'), - 1 => array("idsite" => 3, "name" => "site3", "main_url" =>"http://piwik.org", "excluded_ips" => "", 'excluded_parameters' => '', 'timezone' => 'UTC', 'currency' => 'USD'), + 0 => array("idsite" => 1, "name" => "site1", "main_url" =>"http://piwik.net"), + 1 => array("idsite" => 3, "name" => "site3", "main_url" =>"http://piwik.org"), ); FakeAccess::setIdSitesAdmin (array(1,3)); - $sites = Piwik_SitesManager_API::getInstance()->getSitesWithAdminAccess(); + $sites = Piwik_SitesManager_API::getSitesWithAdminAccess(); // we dont test the ts_created unset($sites[0]['ts_created']); @@ -519,7 +462,7 @@ class Test_Piwik_SitesManager extends Test_Database FakeAccess::setIdSitesView (array()); FakeAccess::setIdSitesAdmin (array()); - $sites = Piwik_SitesManager_API::getInstance()->getSitesWithViewAccess(); + $sites = Piwik_SitesManager_API::getSitesWithViewAccess(); $this->assertEqual($sites, array()); } @@ -528,19 +471,19 @@ class Test_Piwik_SitesManager extends Test_Database */ function test_getSitesWithViewAccess() { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",array("http://piwik.net","http://piwik.com/test/")); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site2",array("http://piwik.com/test/")); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site3",array("http://piwik.org")); + $idsite = Piwik_SitesManager_API::addSite("site1",array("http://piwik.net","http://piwik.com/test/")); + $idsite = Piwik_SitesManager_API::addSite("site2",array("http://piwik.com/test/")); + $idsite = Piwik_SitesManager_API::addSite("site3",array("http://piwik.org")); $resultWanted = array( - 0 => array("idsite" => 1, "name" => "site1", "main_url" =>"http://piwik.net", "excluded_ips" => "", 'excluded_parameters' => '', 'timezone' => 'UTC', 'currency' => 'USD'), - 1 => array("idsite" => 3, "name" => "site3", "main_url" =>"http://piwik.org", "excluded_ips" => "", 'excluded_parameters' => '', 'timezone' => 'UTC', 'currency' => 'USD'), + 0 => array("idsite" => 1, "name" => "site1", "main_url" =>"http://piwik.net"), + 1 => array("idsite" => 3, "name" => "site3", "main_url" =>"http://piwik.org"), ); FakeAccess::setIdSitesView (array(1,3)); FakeAccess::setIdSitesAdmin (array()); - $sites = Piwik_SitesManager_API::getInstance()->getSitesWithViewAccess(); + $sites = Piwik_SitesManager_API::getSitesWithViewAccess(); // we dont test the ts_created unset($sites[0]['ts_created']); unset($sites[1]['ts_created']); @@ -555,7 +498,7 @@ class Test_Piwik_SitesManager extends Test_Database FakeAccess::setIdSitesView (array()); FakeAccess::setIdSitesAdmin (array()); - $sites = Piwik_SitesManager_API::getInstance()->getSitesWithAtLeastViewAccess(); + $sites = Piwik_SitesManager_API::getSitesWithAtLeastViewAccess(); $this->assertEqual($sites, array()); } @@ -564,19 +507,19 @@ class Test_Piwik_SitesManager extends Test_Database */ function test_getSitesWithAtLeastViewAccess() { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",array("http://piwik.net","http://piwik.com/test/")); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site2",array("http://piwik.com/test/")); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site3",array("http://piwik.org")); + $idsite = Piwik_SitesManager_API::addSite("site1",array("http://piwik.net","http://piwik.com/test/")); + $idsite = Piwik_SitesManager_API::addSite("site2",array("http://piwik.com/test/")); + $idsite = Piwik_SitesManager_API::addSite("site3",array("http://piwik.org")); $resultWanted = array( - 0 => array("idsite" => 1, "name" => "site1", "main_url" =>"http://piwik.net", "excluded_ips" => "", 'excluded_parameters' => '', 'timezone' => 'UTC', 'currency' => 'USD'), - 1 => array("idsite" => 3, "name" => "site3", "main_url" =>"http://piwik.org", "excluded_ips" => "", 'excluded_parameters' => '', 'timezone' => 'UTC', 'currency' => 'USD'), + 0 => array("idsite" => 1, "name" => "site1", "main_url" =>"http://piwik.net"), + 1 => array("idsite" => 3, "name" => "site3", "main_url" =>"http://piwik.org"), ); FakeAccess::setIdSitesView (array(1,3)); FakeAccess::setIdSitesAdmin (array()); - $sites = Piwik_SitesManager_API::getInstance()->getSitesWithAtLeastViewAccess(); + $sites = Piwik_SitesManager_API::getSitesWithAtLeastViewAccess(); // we dont test the ts_created unset($sites[0]['ts_created']); unset($sites[1]['ts_created']); @@ -589,9 +532,9 @@ class Test_Piwik_SitesManager extends Test_Database */ function test_getSiteUrlsFromId_noUrls() { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",array("http://piwik.net")); + $idsite = Piwik_SitesManager_API::addSite("site1",array("http://piwik.net")); - $urls = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $urls = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $this->assertEqual(array("http://piwik.net"),$urls); } @@ -606,14 +549,14 @@ class Test_Piwik_SitesManager extends Test_Database "http://piwik.com"); sort($site); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",$site); + $idsite = Piwik_SitesManager_API::addSite("site1",$site); $siteWanted = array("http://piwik.net", "http://piwik.org", "http://piwik.com"); sort($siteWanted); - $urls = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $urls = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $this->assertEqual($siteWanted, $urls); @@ -628,7 +571,7 @@ class Test_Piwik_SitesManager extends Test_Database FakeAccess::setIdSitesAdmin (array()); try { - Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId(1); + Piwik_SitesManager_API::getSiteUrlsFromId(1); } catch (Exception $expected) { return; @@ -645,12 +588,12 @@ class Test_Piwik_SitesManager extends Test_Database "http://piwiknew.net", "http://piwiknew.org", "http://piwiknew.fr"); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",$urls); + $idsite = Piwik_SitesManager_API::addSite("site1",$urls); $newMainUrl = "http://main.url"; - Piwik_SitesManager_API::getInstance()->updateSite($idsite, "test toto@{}", $newMainUrl ); + Piwik_SitesManager_API::updateSite($idsite, "test toto@{}", $newMainUrl ); - $allUrls = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $allUrls = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); $this->assertEqual($allUrls[0], $newMainUrl); $aliasUrls = array_slice($allUrls,1); @@ -662,12 +605,12 @@ class Test_Piwik_SitesManager extends Test_Database */ function test_updateSite_strangeNameNoUrl() { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1","http://main.url"); + $idsite = Piwik_SitesManager_API::addSite("site1","http://main.url"); $newName ="test toto@{'786'}"; - Piwik_SitesManager_API::getInstance()->updateSite($idsite, $newName ); + Piwik_SitesManager_API::updateSite($idsite, $newName ); - $site = Piwik_SitesManager_API::getInstance()->getSiteFromId($idsite); + $site = Piwik_SitesManager_API::getSiteFromId($idsite); $this->assertEqual($site['name'],$newName); // url didn't change because parameter url NULL in updateSite @@ -686,168 +629,20 @@ class Test_Piwik_SitesManager extends Test_Database "http://piwiknew.net", "http://piwiknew.org", "http://piwiknew.fr"); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",$urls); + $idsite = Piwik_SitesManager_API::addSite("site1",$urls); $newurls = array("http://piwiknew2.com", "http://piwiknew2.net", "http://piwiknew2.org", "http://piwiknew2.fr"); - Piwik_SitesManager_API::getInstance()->updateSite($idsite, "test toto@{}",$newurls ); + Piwik_SitesManager_API::updateSite($idsite, "test toto@{}",$newurls ); - $allUrls = Piwik_SitesManager_API::getInstance()->getSiteUrlsFromId($idsite); + $allUrls = Piwik_SitesManager_API::getSiteUrlsFromId($idsite); sort($allUrls); sort($newurls); $this->assertEqual($allUrls,$newurls); } - - function test_addSites_invalidTimezone() - { - // trying invalid timezones - try { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",array('http://example.org'), '', '', 'UTC+15'); - $this->fail('invalid timezone should raise an exception'); - } catch(Exception $e) { - } - try { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",array('http://example.org'), '', '', 'Paris'); - $this->fail('invalid timezone should raise an exception'); - } catch(Exception $e) { - } - $this->pass(); - } - - function test_addSites_invalidCurrency() - { - $invalidCurrency = '€'; - try { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",array('http://example.org'), '', 'UTC', $invalidCurrency); - $this->fail('invalid currency should raise an exception'); - } catch(Exception $e) { - } - $this->pass(); - } - - function test_setDefaultTimezoneAndCurrencyAndExcludedQueryParametersAndExcludedIps() - { - // test that they return default values - $defaultTimezone = Piwik_SitesManager_API::getInstance()->getDefaultTimezone(); - $this->assertEqual($defaultTimezone, 'UTC'); - $defaultCurrency = Piwik_SitesManager_API::getInstance()->getDefaultCurrency(); - $this->assertEqual($defaultCurrency, 'USD'); - $excludedIps = Piwik_SitesManager_API::getInstance()->getExcludedIpsGlobal(); - $this->assertEqual($excludedIps, ''); - $excludedQueryParameters = Piwik_SitesManager_API::getInstance()->getExcludedQueryParametersGlobal(); - $this->assertEqual($excludedQueryParameters, ''); - - // test that when not specified, defaults are set as expected - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",array('http://example.org')); - $site = new Piwik_Site($idsite); - $this->assertEqual($site->getTimezone(), 'UTC'); - $this->assertEqual($site->getCurrency(), 'USD'); - $this->assertEqual($site->getExcludedQueryParameters(), ''); - $this->assertEqual($site->getExcludedIps(), ''); - - // set the global timezone and get it - $newDefaultTimezone = 'UTC+5.5'; - Piwik_SitesManager_API::getInstance()->setDefaultTimezone($newDefaultTimezone); - $defaultTimezone = Piwik_SitesManager_API::getInstance()->getDefaultTimezone(); - $this->assertEqual($defaultTimezone, $newDefaultTimezone); - - // set the default currency and get it - $newDefaultCurrency = 'EUR'; - Piwik_SitesManager_API::getInstance()->setDefaultCurrency($newDefaultCurrency); - $defaultCurrency = Piwik_SitesManager_API::getInstance()->getDefaultCurrency(); - $this->assertEqual($defaultCurrency, $newDefaultCurrency); - - // set the global IPs to exclude and get it - $newGlobalExcludedIps = '1.1.1.*,1.1.*.*,150.1.1.1'; - Piwik_SitesManager_API::getInstance()->setGlobalExcludedIps($newGlobalExcludedIps); - $globalExcludedIps = Piwik_SitesManager_API::getInstance()->getExcludedIpsGlobal(); - $this->assertEqual($globalExcludedIps, $newGlobalExcludedIps); - - // set the global URL query params to exclude and get it - $newGlobalExcludedQueryParameters = 'PHPSESSID,blabla, TesT'; - // removed the space - $expectedGlobalExcludedQueryParameters = 'PHPSESSID,blabla,TesT'; - Piwik_SitesManager_API::getInstance()->setGlobalExcludedQueryParameters($newGlobalExcludedQueryParameters); - $globalExcludedQueryParameters = Piwik_SitesManager_API::getInstance()->getExcludedQueryParametersGlobal(); - $this->assertEqual($globalExcludedQueryParameters, $expectedGlobalExcludedQueryParameters); - - // create a website and check that default currency and default timezone are set - // however, excluded IPs and excluded query Params are not returned - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",array('http://example.org'), '', '', $newDefaultTimezone); - $site = new Piwik_Site($idsite); - $this->assertEqual($site->getTimezone(), $newDefaultTimezone); - $this->assertEqual($site->getCreationDate()->toString(), date('Y-m-d')); - $this->assertEqual($site->getCurrency(), $newDefaultCurrency); - $this->assertEqual($site->getExcludedIps(), ''); - $this->assertEqual($site->getExcludedQueryParameters(), ''); - } - - function test_getSitesIdFromSiteUrl_SuperUser() - { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",array("http://piwik.net","http://piwik.com")); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site2",array("http://piwik.com","http://piwik.net")); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site3",array("http://piwik.com","http://piwik.org")); - - $idsites = Piwik_SitesManager_API::getInstance()->getSitesIdFromSiteUrl('http://piwik.org'); - $this->assertTrue(count($idsites) == 1); - - $idsites = Piwik_SitesManager_API::getInstance()->getSitesIdFromSiteUrl('http://piwik.net'); - $this->assertTrue(count($idsites) == 2); - - $idsites = Piwik_SitesManager_API::getInstance()->getSitesIdFromSiteUrl('http://piwik.com'); - $this->assertTrue(count($idsites) == 3); - } - - function test_getSitesIdFromSiteUrl_User() - { - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site1",array("http://piwik.net","http://piwik.com")); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site2",array("http://piwik.com","http://piwik.net")); - $idsite = Piwik_SitesManager_API::getInstance()->addSite("site3",array("http://piwik.com","http://piwik.org")); - - $saveAccess = Zend_Registry::get('access'); - - Piwik_UsersManager_API::getInstance()->addUser("user1", "geqgegagae", "tegst@tesgt.com", "alias"); - Piwik_UsersManager_API::getInstance()->setUserAccess("user1", "view", array(1)); - - Piwik_UsersManager_API::getInstance()->addUser("user2", "geqgegagae", "tegst2@tesgt.com", "alias"); - Piwik_UsersManager_API::getInstance()->setUserAccess("user2", "view", array(1)); - Piwik_UsersManager_API::getInstance()->setUserAccess("user2", "admin", array(3)); - - Piwik_UsersManager_API::getInstance()->addUser("user3", "geqgegagae", "tegst3@tesgt.com", "alias"); - Piwik_UsersManager_API::getInstance()->setUserAccess("user3", "view", array(1,2)); - Piwik_UsersManager_API::getInstance()->setUserAccess("user3", "admin", array(3)); - - $pseudoMockAccess = new FakeAccess; - FakeAccess::$superUser = false; - FakeAccess::$identity = 'user1'; - FakeAccess::setIdSitesView (array(1)); - FakeAccess::setIdSitesAdmin (array()); - Zend_Registry::set('access', $pseudoMockAccess); - $idsites = Piwik_SitesManager_API::getInstance()->getSitesIdFromSiteUrl('http://piwik.com'); - $this->assertTrue(count($idsites) == 1); - - $pseudoMockAccess = new FakeAccess; - FakeAccess::$superUser = false; - FakeAccess::$identity = 'user2'; - FakeAccess::setIdSitesView (array(1)); - FakeAccess::setIdSitesAdmin (array(3)); - Zend_Registry::set('access', $pseudoMockAccess); - $idsites = Piwik_SitesManager_API::getInstance()->getSitesIdFromSiteUrl('http://piwik.com'); - $this->assertTrue(count($idsites) == 2); - - $pseudoMockAccess = new FakeAccess; - FakeAccess::$superUser = false; - FakeAccess::$identity = 'user3'; - FakeAccess::setIdSitesView (array(1,2)); - FakeAccess::setIdSitesAdmin (array(3)); - Zend_Registry::set('access', $pseudoMockAccess); - $idsites = Piwik_SitesManager_API::getInstance()->getSitesIdFromSiteUrl('http://piwik.com'); - $this->assertTrue(count($idsites) == 3); - - Zend_Registry::set('access', $saveAccess); - } } + |