diff options
author | vipsoft <vipsoft@59fd770c-687e-43c8-a1e3-f5a4ff64c105> | 2010-04-08 04:51:36 +0400 |
---|---|---|
committer | vipsoft <vipsoft@59fd770c-687e-43c8-a1e3-f5a4ff64c105> | 2010-04-08 04:51:36 +0400 |
commit | 2e7a60f7f1aaa30d944a7c2cdea3a84c3ac5f56b (patch) | |
tree | 95704908e0bd81c155f56b2b4c99dc298ae7a551 /plugins/SitesManager | |
parent | 568b1aa7125a8ca1e0ebafeb011b6c08cf983de4 (diff) |
fixes #1261 - SitesManager: add getSitesIdFromSiteUrl( $url ) to API; add unit test
Diffstat (limited to 'plugins/SitesManager')
-rw-r--r-- | plugins/SitesManager/API.php | 31 | ||||
-rw-r--r-- | plugins/SitesManager/tests/SitesManager.test.php | 16 |
2 files changed, 45 insertions, 2 deletions
diff --git a/plugins/SitesManager/API.php b/plugins/SitesManager/API.php index 9ed3647ed6..ab8fb0fde3 100644 --- a/plugins/SitesManager/API.php +++ b/plugins/SitesManager/API.php @@ -213,7 +213,36 @@ class Piwik_SitesManager_API 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::prefixTable('site') . ' WHERE main_url = ? ' . + 'UNION SELECT idsite FROM ' . Piwik::prefixTable('site_url') . ' WHERE url = ?', array($url, $url)); + } + else + { + $login = Piwik::getCurrentUserLogin(); + $ids = Zend_Registry::get('db')->fetchAll( + 'SELECT idsite FROM ' . Piwik::prefixTable('site') . ' WHERE main_url = ? ' . + 'AND idsite IN (' . Piwik_Access::getSqlAccessSite('idsite') . ') ' . + 'UNION SELECT idsite FROM ' . Piwik::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. diff --git a/plugins/SitesManager/tests/SitesManager.test.php b/plugins/SitesManager/tests/SitesManager.test.php index 9eb4a8c5cd..86c0a53129 100644 --- a/plugins/SitesManager/tests/SitesManager.test.php +++ b/plugins/SitesManager/tests/SitesManager.test.php @@ -781,5 +781,19 @@ class Test_Piwik_SitesManager extends Test_Database $this->assertEqual($site->getExcludedQueryParameters(), ''); } -} + function test_getSitesIdFromSiteUrl() + { + $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); + } +} |