| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Plugin; |
|
|
| use Piwik\Cache; |
| use Piwik\CacheId; |
| use Piwik\Category\CategoryList; |
| use Piwik\Common; |
| use Piwik\Piwik; |
| use Piwik\Plugin; |
| use Piwik\Cache as PiwikCache; |
| use Piwik\Site; |
|
|
| |
| |
| |
| class ReportsProvider |
| { |
| private $categoryList; |
|
|
| |
| |
| |
| |
| |
| |
| |
| public static function factory($module, $action) |
| { |
| $listApiToReport = self::getMapOfModuleActionsToReport(); |
| $api = $module . '.' . ucfirst($action); |
|
|
| if (!array_key_exists($api, $listApiToReport)) { |
| return null; |
| } |
|
|
| $klassName = $listApiToReport[$api]; |
|
|
| return new $klassName(); |
| } |
|
|
| private static function getMapOfModuleActionsToReport() |
| { |
| $cacheKey = 'ReportFactoryMap'; |
| $idSite = Common::getRequestVar('idSite', 0, 'int'); |
|
|
| if (!empty($idSite)) { |
| |
| $cacheKey .= '_' . (int) $idSite; |
| } |
|
|
| |
| $idSites = Common::getRequestVar('idSites', '', $type = null); |
| if (!empty($idSites)) { |
| $transientCache = Cache::getTransientCache(); |
| $transientCacheKey = 'ReportIdSitesParam'; |
| if ($transientCache->contains($transientCacheKey)) { |
| $idSites = $transientCache->fetch($transientCacheKey); |
| } else { |
| |
| $idSites = Site::getIdSitesFromIdSitesString($idSites); |
| sort($idSites); |
| $transientCache->save($transientCacheKey, $idSites); |
| } |
|
|
| |
| |
| |
| if (count($idSites) <= 5) { |
| $cacheKey .= '_' . implode('_', $idSites); |
| } else { |
| $cacheKey .= '_' . md5(implode('_', $idSites)); |
| } |
| } |
|
|
| $lazyCacheId = CacheId::pluginAware($cacheKey); |
|
|
| $cache = PiwikCache::getLazyCache(); |
| $mapApiToReport = $cache->fetch($lazyCacheId); |
|
|
| if (empty($mapApiToReport)) { |
| $reports = new static(); |
| $reports = $reports->getAllReports(); |
|
|
| $mapApiToReport = array(); |
| foreach ($reports as $report) { |
| $key = $report->getModule() . '.' . ucfirst($report->getAction()); |
|
|
| if (isset($mapApiToReport[$key]) && $report->getParameters()) { |
| |
| |
| |
| continue; |
| } |
| $mapApiToReport[$key] = get_class($report); |
| } |
|
|
| $cache->save($lazyCacheId, $mapApiToReport, $lifeTime = 3600); |
| } |
|
|
| return $mapApiToReport; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function getAllReports() |
| { |
| $reports = $this->getAllReportClasses(); |
| $cacheId = CacheId::siteAware(CacheId::languageAware('Reports' . md5(implode('', $reports)))); |
| $cache = PiwikCache::getTransientCache(); |
|
|
| if (!$cache->contains($cacheId)) { |
| $instances = array(); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Piwik::postEvent('Report.addReports', array(&$instances)); |
|
|
| foreach ($reports as $report) { |
| $instances[] = new $report(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Piwik::postEvent('Report.filterReports', array(&$instances)); |
|
|
| @usort($instances, array($this, 'sort')); |
|
|
| $cache->save($cacheId, $instances); |
| } |
|
|
| return $cache->fetch($cacheId); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| private function sort($a, $b) |
| { |
| $result = $this->compareCategories($a->getCategoryId(), $a->getSubcategoryId(), $a->getOrder(), $b->getCategoryId(), $b->getSubcategoryId(), $b->getOrder()); |
|
|
| |
| if (!$result) { |
| $aId = $a->getId(); |
| $bId = $b->getId(); |
|
|
| if ($aId == $bId) { |
| return 0; |
| } |
|
|
| return $aId < $bId ? -1 : 1; |
| } |
|
|
| return $result; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function compareCategories($catIdA, $subcatIdA, $orderA, $catIdB, $subcatIdB, $orderB) |
| { |
| if (!isset($this->categoryList)) { |
| $this->categoryList = CategoryList::get(); |
| } |
|
|
| $catA = $this->categoryList->getCategory($catIdA); |
| $catB = $this->categoryList->getCategory($catIdB); |
|
|
| |
| if (isset($catA) && isset($catB)) { |
| if ($catA->getOrder() == $catB->getOrder()) { |
| |
| $subcatA = $catA->getSubcategory($subcatIdA); |
| $subcatB = $catB->getSubcategory($subcatIdB); |
|
|
| |
| if ($subcatA && $subcatB) { |
| if ($subcatA->getOrder() == $subcatB->getOrder()) { |
| |
|
|
| if ($orderA == $orderB) { |
| return 0; |
| } |
|
|
| return $orderA < $orderB ? -1 : 1; |
| } |
|
|
| return $subcatA->getOrder() < $subcatB->getOrder() ? -1 : 1; |
| } elseif ($subcatA) { |
| return 1; |
| } elseif ($subcatB) { |
| return -1; |
| } |
|
|
| if ($orderA == $orderB) { |
| return 0; |
| } |
|
|
| return $orderA < $orderB ? -1 : 1; |
| } |
|
|
| return $catA->getOrder() < $catB->getOrder() ? -1 : 1; |
| } elseif (isset($catA)) { |
| return -1; |
| } elseif (isset($catB)) { |
| return 1; |
| } |
|
|
| if ($catIdA === $catIdB) { |
| |
| if ($orderA == $orderB) { |
| return 0; |
| } |
|
|
| return $orderA < $orderB ? -1 : 1; |
| } |
|
|
| return strnatcasecmp($catIdA ?? '', $catIdB ?? ''); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public function getAllReportClasses() |
| { |
| return Plugin\Manager::getInstance()->findMultipleComponents('Reports', '\\Piwik\\Plugin\\Report'); |
| } |
|
|
| |
| public function unsetCategoryList() |
| { |
| unset($this->categoryList); |
| } |
| } |
|
|