private function buildDataTable($sites, $period, $date, $segment, $_restrictSitesToLogin, $enhanced) { $allWebsitesRequested = $sites == 'all'; if ($allWebsitesRequested) { if (Piwik::isUserIsSuperUser() && !Piwik_TaskScheduler::isTaskBeingExecuted()) { Piwik_Site::setSites(Piwik_SitesManager_API::getInstance()->getAllSites()); } else { Piwik_Site::setSitesFromArray(Piwik_SitesManager_API::getInstance()->getSitesWithAtLeastViewAccess($limit = false, $_restrictSitesToLogin)); } } // build the archive type used to query archive data $archive = Piwik_Archive::build($sites, $period, $date, $segment, $_restrictSitesToLogin); // determine what data will be displayed $fieldsToGet = array(); $columnNameRewrites = array(); $apiECommerceMetrics = array(); $apiMetrics = Piwik_MultiSites_API::getApiMetrics($enhanced); foreach ($apiMetrics as $metricName => $metricSettings) { $fieldsToGet[] = $metricSettings[self::METRIC_RECORD_NAME_KEY]; $columnNameRewrites[$metricSettings[self::METRIC_RECORD_NAME_KEY]] = $metricName; if ($metricSettings[self::METRIC_IS_ECOMMERCE_KEY]) { $apiECommerceMetrics[$metricName] = $metricSettings; } } // get the data // $dataTable instanceOf Piwik_DataTable_Array $dataTable = $archive->getDataTableFromNumeric($fieldsToGet); // get rid of the DataTable_Array that is created by the IndexedBySite archive type if ($dataTable instanceof Piwik_DataTable_Array && $allWebsitesRequested) { $dataTable = $dataTable->mergeChildren(); } else { if (!$dataTable instanceof Piwik_DataTable_Array) { $firstDataTableRow = $dataTable->getFirstRow(); $firstDataTableRow->setColumn('label', $sites); } } // if the period isn't a range & a lastN/previousN date isn't used, we get the same // data for the last period to show the evolution of visits/actions/revenue if ($period != 'range' && !preg_match('/(last|previous)([0-9]*)/', $date, $regs)) { if (strpos($date, ',')) { $rangePeriod = new Piwik_Period_Range($period, $date); $lastStartDate = Piwik_Period_Range::removePeriod($period, $rangePeriod->getDateStart(), $n = 1); $lastEndDate = Piwik_Period_Range::removePeriod($period, $rangePeriod->getDateEnd(), $n = 1); $strLastDate = "{$lastStartDate},{$lastEndDate}"; } else { $strLastDate = Piwik_Period_Range::removePeriod($period, Piwik_Date::factory($date), $n = 1)->toString(); } $pastArchive = Piwik_Archive::build('all', $period, $strLastDate, $segment, $_restrictSitesToLogin); $pastData = $pastArchive->getDataTableFromNumeric($fieldsToGet); $pastData = $pastData->mergeChildren(); // use past data to calculate evolution percentages $this->calculateEvolutionPercentages($dataTable, $pastData, $apiMetrics); } // remove eCommerce related metrics on non eCommerce Piwik sites // note: this is not optimal in terms of performance: those metrics should not be retrieved in the first place if ($enhanced) { // $dataTableRows instanceOf Piwik_DataTable_Row[] $dataTableRows = $dataTable->getRows(); foreach ($dataTableRows as $dataTableRow) { $siteId = $dataTableRow->getColumn('label'); if (!Piwik_Site::isEcommerceEnabledFor($siteId)) { foreach ($apiECommerceMetrics as $metricSettings) { $dataTableRow->deleteColumn($metricSettings[self::METRIC_RECORD_NAME_KEY]); $dataTableRow->deleteColumn($metricSettings[self::METRIC_EVOLUTION_COL_NAME_KEY]); } } } } // move the site id to a metadata column $dataTable->filter('ColumnCallbackAddMetadata', array('label', 'idsite')); // set the label of each row to the site name if ($allWebsitesRequested) { $getNameFor = array('Piwik_Site', 'getNameFor'); $dataTable->filter('ColumnCallbackReplace', array('label', $getNameFor)); } else { $dataTable->filter('ColumnDelete', array('label')); } // replace record names with user friendly metric names $dataTable->filter('ReplaceColumnNames', array($columnNameRewrites)); // Ensures data set sorted, for Metadata output $dataTable->filter('Sort', array(self::NB_VISITS_METRIC, 'desc', $naturalSort = false)); // filter rows without visits // note: if only one website is queried and there are no visits, we can not remove the row otherwise Piwik_API_ResponseBuilder throws 'Call to a member function getColumns() on a non-object' if ($allWebsitesRequested) { $dataTable->filter('ColumnCallbackDeleteRow', array(self::NB_VISITS_METRIC, create_function('$value', 'return $value != 0;'))); } return $dataTable; }
/** * Returns the list of websites ID with the 'view' or 'admin' access for the current user. * For the superUser it returns all the websites in the database. * * @return array list of websites ID */ public function getSitesIdWithAtLeastViewAccess($_restrictSitesToLogin = false) { if (!empty($_restrictSitesToLogin) && Piwik_TaskScheduler::isTaskBeingExecuted()) { $accessRaw = Piwik_Access::getRawSitesWithSomeViewAccess($_restrictSitesToLogin); $sitesId = array(); foreach ($accessRaw as $access) { $sitesId[] = $access['idsite']; } return $sitesId; } else { return Zend_Registry::get('access')->getSitesIdWithAtLeastViewAccess(); } }