_db = $db; $this->userReadDatabaseToArray(); $this->downloadReadDatabaseToArray(); } /** * @return array */ private function userReadDatabaseToArray() { $this->_db->query("SELECT COUNT(guid) AS visit_count, DAY(FROM_UNIXTIME(`timestamp`)) as visit_day, MONTH(FROM_UNIXTIME(`timestamp`)) as visit_month, YEAR(FROM_UNIXTIME(`timestamp`)) AS visit_year FROM sks_unique_user GROUP BY visit_year DESC, visit_month DESC, visit_day DESC"); $visitorsPerDay = $this->_db->fetchArray(); foreach ($visitorsPerDay as $key => $value) { $day = ($value['visit_day'] < 10 ? '0'.$value['visit_day'] : $value['visit_day']); $month = ($value['visit_month'] < 10 ? '0'.$value['visit_month'] : $value['visit_month']); $year = $value['visit_year']; $this->userStatValues[$day.'.'.$month.'.'.$year] = (int) $value['visit_count']; } return $this->userStatValues; } /** * @return array */ private function downloadReadDatabaseToArray() { $this->_db->query("SELECT COUNT(guid) AS visit_count, DAY(FROM_UNIXTIME(`timestamp`)) as visit_day, MONTH(FROM_UNIXTIME(`timestamp`)) as visit_month, YEAR(FROM_UNIXTIME(`timestamp`)) AS visit_year FROM sks_unique_dls GROUP BY visit_year DESC, visit_month DESC, visit_day DESC"); $visitorsPerDay = $this->_db->fetchArray(); foreach ($visitorsPerDay as $key => $value) { $day = ($value['visit_day'] < 10 ? '0'.$value['visit_day'] : $value['visit_day']); $month = ($value['visit_month'] < 10 ? '0'.$value['visit_month'] : $value['visit_month']); $year = $value['visit_year']; $this->downloadStatValue[$day.'.'.$month.'.'.$year] = (int) $value['visit_count']; } return $this->downloadStatValue; } /** * @param string $day * @param string $month * @param string $year * @param string $range * @return array */ private function generateLabels($day = "", $month="", $year="", $range="") { $genDates = []; $dateValue = $day.'.'.$month.'.'.$year; $genStart = new \DateTime($dateValue); $interval = new \DateInterval('P1D'); if(is_numeric($range)) { $range = ($range > 100 ? 100 : $range); $genStart = $genStart->modify("-" . $range . " days"); $genEnd = clone $genStart; $genEnd = $genEnd->modify("+" .($range+1)." days"); } else { $genEnd = clone $genStart; $genEnd->add(new \DateInterval("P1M")); } $dateRange = new \DatePeriod($genStart,$interval,$genEnd); foreach ($dateRange as $date) { $genDates[] = $date->format("d.m."); } return $genDates; } /** * @param string $day * @param string $month * @param string $year * @param string $range * @return array */ private function generateRows($type="", $day = "", $month="", $year="", $range="") { if($type == "user") { $rowArray = $this->userStatValues; } elseif ($type="dls") { $rowArray = $this->downloadStatValue; } $genRows = []; $dateValue = $day.'.'.$month.'.'.$year; $genStart = new \DateTime($dateValue); $interval = new \DateInterval('P1D'); if(is_numeric($range)) { $range = ($range > 100 ? 100 : $range); $genStart = $genStart->modify("-" . $range . " days"); $genEnd = clone $genStart; $genEnd = $genEnd->modify("+" . ($range * 2) . " days"); } else { $genEnd = clone $genStart; $genEnd->add(new \DateInterval("P1M")); } $dateRange = new \DatePeriod($genStart,$interval,$genEnd); $genEnd = clone $genStart; $genEnd->add(new \DateInterval("P1M")); foreach ($dateRange as $date) { $realDate = $date->format("d.m.Y"); if(array_key_exists($realDate, $rowArray)) { $genRows[] = $rowArray[$realDate]; } else { $genRows[] = 0; } //var_dump($rowArray); } return $genRows; } /** * @param string $day * @param string $month * @param string $year * @param string $range */ public function setDateLabels($day = "", $month="", $year="", $range="") { $day = (!empty($day) ? $day : date("d", time())); $month = (!empty($month) ? $month : date("m", time())); $year = (!empty($year) ? $year : date("Y", time())); $range =(!empty($range) ? $range : 10); $this->dateLabels = $this->generateLabels($day,$month,$year,$range); } /** * @return array */ public function getDateLabels() { return json_encode($this->dateLabels); } /** * @param string $type * @param string $day * @param string $month * @param string $year * @param string $range */ public function setUserDateRows($type="", $day = "", $month="", $year="", $range="") { $day = (!empty($day) ? $day : date("d", time())); $month = (!empty($month) ? $month : date("m", time())); $year = (!empty($year) ? $year : date("Y", time())); $range =(!empty($range) ? $range : 10); $this->userDateRows = $this->generateRows($type, $day,$month,$year,$range); } /** * @param string $type * @param string $day * @param string $month * @param string $year * @param string $range */ public function setDownloadDateRows($type="", $day = "", $month="", $year="", $range="") { $day = (!empty($day) ? $day : date("d", time())); $month = (!empty($month) ? $month : date("m", time())); $year = (!empty($year) ? $year : date("Y", time())); $range =(!empty($range) ? $range : 10); $this->downloadDateRows = $this->generateRows($type, $day,$month,$year,$range); } /** * @return array */ public function getUserDateRows() { return $this->userDateRows; } /** * @return array */ public function getDownloadDateRows() { return $this->downloadDateRows; } }