public function getReports($idSite = false, $period = false, $idReport = false, $ifSuperUserReturnOnlySuperUserReports = false, $idSegment = false)
{
Piwik::checkUserHasSomeViewAccess();
$cacheKey = (int) $idSite . '.' . (string) $period . '.' . (int) $idReport . '.' . (int) $ifSuperUserReturnOnlySuperUserReports;
if (isset(self::$cache[$cacheKey])) {
return self::$cache[$cacheKey];
}
$sqlWhere = '';
$bind = array();
// Super user gets all reports back, other users only their own
if (!Piwik::hasUserSuperUserAccess() || $ifSuperUserReturnOnlySuperUserReports) {
$sqlWhere .= "AND login = ?";
$bind[] = Piwik::getCurrentUserLogin();
}
if (!empty($period)) {
$this->validateReportPeriod($period);
$sqlWhere .= " AND period = ? ";
$bind[] = $period;
}
if (!empty($idSite)) {
Piwik::checkUserHasViewAccess($idSite);
$sqlWhere .= " AND " . Common::prefixTable('site') . ".idsite = ?";
$bind[] = $idSite;
}
if (!empty($idReport)) {
$sqlWhere .= " AND idreport = ?";
$bind[] = $idReport;
}
if (!empty($idSegment)) {
$sqlWhere .= " AND idsegment = ?";
$bind[] = $idSegment;
}
// Joining with the site table to work around pre-1.3 where reports could still be linked to a deleted site
$reports = Db::fetchAll("SELECT report.*\n\t\t\t\t\t\t\t\tFROM " . Common::prefixTable('report') . " AS `report`\n\t\t\t\t\t\t\t\t\tJOIN " . Common::prefixTable('site') . "\n\t\t\t\t\t\t\t\t\tUSING (idsite)\n\t\t\t\t\t\t\t\tWHERE deleted = 0\n\t\t\t\t\t\t\t\t\t{$sqlWhere}", $bind);
// When a specific report was requested and not found, throw an error
if ($idReport !== false && empty($reports)) {
throw new Exception("Requested report couldn't be found.");
}
foreach ($reports as &$report) {
// decode report parameters
$report['parameters'] = json_decode($report['parameters'], true);
// decode report list
$report['reports'] = json_decode($report['reports'], true);
if (!empty($report['parameters']['additionalEmails']) && is_array($report['parameters']['additionalEmails'])) {
$report['parameters']['additionalEmails'] = array_values($report['parameters']['additionalEmails']);
}
}
// static cache
self::$cache[$cacheKey] = $reports;
return $reports;
}