/** * Based on the period, date and evolution_{$period}_last_n query parameters, * calculates the date range this evolution chart will display data for. */ private function calculateEvolutionDateRange() { $period = Piwik_Common::getRequestVar('period'); $defaultLastN = self::getDefaultLastN($period); $this->originalDate = Piwik_Common::getRequestVar('date', 'last' . $defaultLastN, 'string'); if ($period != 'range') { $this->alwaysShowLimitDropdown(); // set the evolution_{$period}_last_n query param if (Piwik_Period_Range::parseDateRange($this->originalDate)) { // overwrite last_n param using the date range $oPeriod = new Piwik_Period_Range($period, $this->originalDate); $lastN = count($oPeriod->getSubperiods()); } else { list($newDate, $lastN) = self::getDateRangeAndLastN($period, $this->originalDate, $defaultLastN); $this->setParametersToModify(array('date' => $newDate)); } $lastNParamName = self::getLastNParamName($period); $this->setParametersToModify(array($lastNParamName => $lastN)); } }
/** * Indicate if $dateString and $period correspond to multiple periods * * @static * @param $dateString * @param $period * @return boolean */ public static function isMultiplePeriod($dateString, $period) { return (preg_match('/^(last|previous){1}([0-9]*)$/D', $dateString, $regs) || Piwik_Period_Range::parseDateRange($dateString)) && $period != 'range'; }
protected function generate() { if ($this->subperiodsProcessed) { return; } parent::generate(); if (preg_match('/(last|previous)([0-9]*)/', $this->strDate, $regs)) { $lastN = $regs[2]; $lastOrPrevious = $regs[1]; if (!is_null($this->defaultEndDate)) { $defaultEndDate = $this->defaultEndDate; } else { $defaultEndDate = Piwik_Date::factory('now', $this->timezone); } $period = $this->strPeriod; if ($period == 'range') { $period = 'day'; } if ($lastOrPrevious == 'last') { $endDate = $defaultEndDate; } elseif ($lastOrPrevious == 'previous') { $endDate = self::removePeriod($period, $defaultEndDate, 1); } // last1 means only one result ; last2 means 2 results so we remove only 1 to the days/weeks/etc $lastN--; $lastN = abs($lastN); $lastN = $this->getMaxN($lastN); $startDate = self::removePeriod($period, $endDate, $lastN); } elseif ($dateRange = Piwik_Period_Range::parseDateRange($this->strDate)) { $strDateStart = $dateRange[1]; $strDateEnd = $dateRange[2]; $startDate = Piwik_Date::factory($strDateStart); if ($strDateEnd == 'today') { $strDateEnd = 'now'; } elseif ($strDateEnd == 'yesterday') { $strDateEnd = 'yesterdaySameTime'; } // we set the timezone in the Date object only if the date is relative eg. 'today', 'yesterday', 'now' $timezone = null; if (strpos($strDateEnd, '-') === false) { $timezone = $this->timezone; } $endDate = Piwik_Date::factory($strDateEnd, $timezone); } else { throw new Exception(Piwik_TranslateException('General_ExceptionInvalidDateRange', array($this->strDate, ' \'lastN\', \'previousN\', \'YYYY-MM-DD,YYYY-MM-DD\''))); } if ($this->strPeriod != 'range') { $this->fillArraySubPeriods($startDate, $endDate, $this->strPeriod); return; } $this->processOptimalSubperiods($startDate, $endDate); // When period=range, we want End Date to be the actual specified end date, // rather than the end of the month / week / whatever is used for processing this range $this->endDate = $endDate; }
private function loadLastVisitorDetailsFromDatabase($idSite, $period = false, $date = false, $segment = false, $filter_limit = false, $maxIdVisit = false, $visitorId = false, $minTimestamp = false) { // var_dump($period); var_dump($date); var_dump($filter_limit); var_dump($maxIdVisit); var_dump($visitorId); //var_dump($minTimestamp); if(empty($filter_limit)) { $filter_limit = 100; } $where = $whereBind = array(); $where[] = "log_visit.idsite = ? "; $whereBind[] = $idSite; $orderBy = "idsite, visit_last_action_time DESC"; $orderByParent = "sub.visit_last_action_time DESC"; if(!empty($visitorId)) { $where[] = "log_visit.idvisitor = ? "; $whereBind[] = Piwik_Common::hex2bin($visitorId); } if(!empty($maxIdVisit)) { $where[] = "log_visit.idvisit < ? "; $whereBind[] = $maxIdVisit; $orderBy = "idvisit DESC"; $orderByParent = "sub.idvisit DESC"; } if(!empty($minTimestamp)) { $where[] = "log_visit.visit_last_action_time > ? "; $whereBind[] = date("Y-m-d H:i:s", $minTimestamp); } // If no other filter, only look at the last 24 hours of stats if(empty($visitorId) && empty($maxIdVisit) && empty($period) && empty($date)) { $period = 'day'; $date = 'yesterdaySameTime'; } // SQL Filter with provided period if (!empty($period) && !empty($date)) { $currentSite = new Piwik_Site($idSite); $currentTimezone = $currentSite->getTimezone(); $dateString = $date; if($period == 'range') { $processedPeriod = new Piwik_Period_Range('range', $date); if($parsedDate = Piwik_Period_Range::parseDateRange($date)) { $dateString = $parsedDate[2]; } } else { $processedDate = Piwik_Date::factory($date); if($date == 'today' || $date == 'now' || $processedDate->toString() == Piwik_Date::factory('now', $currentTimezone)->toString()) { $processedDate = $processedDate->subDay(1); } $processedPeriod = Piwik_Period::factory($period, $processedDate); } $dateStart = $processedPeriod->getDateStart()->setTimezone($currentTimezone); $where[] = "log_visit.visit_last_action_time >= ?"; $whereBind[] = $dateStart->toString('Y-m-d H:i:s'); if(!in_array($date, array('now', 'today', 'yesterdaySameTime')) && strpos($date, 'last') === false && strpos($date, 'previous') === false && Piwik_Date::factory($dateString)->toString('Y-m-d') != Piwik_Date::factory('now', $currentTimezone)->toString()) { $dateEnd = $processedPeriod->getDateEnd()->setTimezone($currentTimezone); $where[] = " log_visit.visit_last_action_time <= ?"; $dateEndString = $dateEnd->addDay(1)->toString('Y-m-d H:i:s'); $whereBind[] = $dateEndString; } } $sqlWhere = ""; if(count($where) > 0) { $sqlWhere = " WHERE " . join(" AND ", $where); } $segment = new Piwik_Segment($segment, $idSite); $segmentSql = $segment->getSql(); $sqlSegment = $segmentSql['sql']; if(!empty($sqlSegment)) $sqlSegment = ' AND '.$sqlSegment; $whereBind = array_merge ( $whereBind, $segmentSql['bind'] ); // Subquery to use the indexes for ORDER BY // Group by idvisit so that a visitor converting 2 goals only appears twice $sql = " SELECT sub.* FROM ( SELECT * FROM " . Piwik_Common::prefixTable('log_visit') . " AS log_visit $sqlWhere $sqlSegment ORDER BY $orderBy LIMIT ".(int)$filter_limit." ) AS sub GROUP BY sub.idvisit ORDER BY $orderByParent "; try { $data = Piwik_FetchAll($sql, $whereBind); } catch(Exception $e) { echo $e->getMessage();exit; } //var_dump($whereBind); echo($sql); //var_dump($data); return $data; }
/** * Builds an Archive object or returns the same archive if previously built. * * @param string|int idSite integer, or comma separated list of integer * @param string|Piwik_Date $date 'YYYY-MM-DD' or magic keywords 'today' @see Piwik_Date::factory() * @param string $period 'week' 'day' etc. * @param string Segment definition - defaults to false for Backward Compatibility * * @return Piwik_Archive */ public static function build($idSite, $period, $strDate, $segment = false) { if ($idSite === 'all') { $sites = Piwik_SitesManager_API::getInstance()->getSitesIdWithAtLeastViewAccess(); } else { $sites = Piwik_Site::getIdSitesFromIdSitesString($idSite); } $segment = new Piwik_Segment($segment, $idSite); // idSite=1,3 or idSite=all if (count($sites) > 1 || $idSite === 'all') { $archive = new Piwik_Archive_Array_IndexedBySite($sites, $period, $strDate, $segment); } elseif (is_string($strDate) && (preg_match('/^(last|previous){1}([0-9]*)$/', $strDate, $regs) || Piwik_Period_Range::parseDateRange($strDate)) && $period != 'range') { $oSite = new Piwik_Site($idSite); $archive = new Piwik_Archive_Array_IndexedByDate($oSite, $period, $strDate, $segment); } else { $oSite = new Piwik_Site($idSite); if ($period == 'range') { $oPeriod = new Piwik_Period_Range('range', $strDate, $oSite->getTimezone(), Piwik_Date::factory('today', $oSite->getTimezone())); } else { if (is_string($strDate)) { if ($strDate == 'now' || $strDate == 'today') { $strDate = date('Y-m-d', Piwik_Date::factory('now', $oSite->getTimezone())->getTimestamp()); } elseif ($strDate == 'yesterday' || $strDate == 'yesterdaySameTime') { $strDate = date('Y-m-d', Piwik_Date::factory('now', $oSite->getTimezone())->subDay(1)->getTimestamp()); } $oDate = Piwik_Date::factory($strDate); } else { $oDate = $strDate; } $date = $oDate->toString(); $oPeriod = Piwik_Period::factory($period, $oDate); } $archive = new Piwik_Archive_Single(); $archive->setPeriod($oPeriod); $archive->setSite($oSite); $archive->setSegment($segment); } return $archive; }