123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
-
- namespace SCF\Core;
-
- use SCF\Core\Database;
-
- class StatsGenerator {
-
- /**
- * @var \SCF\Core\Database
- */
- private $_db;
-
- /**
- * @var array
- */
- private $statValues = [];
-
- /**
- * @var array
- */
- private $dateLabels = [];
-
- /**
- * @var array
- */
- private $dateRows = [];
-
- /**
- * StatsGenerator constructor.
- * @param \SCF\Core\Database $db
- */
- public function __construct(Database $db)
- {
- $this->_db = $db;
-
- $this->readDatabaseToArray();
- $this->setDateLabels(25,12,2014,10);
- $this->setDateRows(25,12,2014,10);
-
- }
-
- /**
- * @return array
- */
- private function readDatabaseToArray()
- {
- $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->statValues[$day.'.'.$month.'.'.$year] = (int) $value['visit_count'];
- }
-
-
- return $this->statValues;
- }
-
- /**
- * @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 > 30 ? 30 : $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);
-
- foreach ($dateRange as $date) {
- $genDates[] = $date->format("d.m.Y");
- }
-
- return $genDates;
-
- }
-
- /**
- * @param string $day
- * @param string $month
- * @param string $year
- * @param string $range
- * @return array
- */
- private function generateRows($day = "", $month="", $year="", $range="")
- {
- $rowArray = $this->statValues;
- $genRows = [];
- $dateValue = $day.'.'.$month.'.'.$year;
-
- $genStart = new \DateTime($dateValue);
- $interval = new \DateInterval('P1D');
- if(is_numeric($range)) {
- $range = ($range > 30 ? 30 : $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="")
- {
- $this->dateLabels = $this->generateLabels($day,$month,$year,$range);
- }
-
- /**
- * @return array
- */
- public function getDateLabels()
- {
- return json_encode($this->dateLabels);
- }
-
- /**
- * @param string $day
- * @param string $month
- * @param string $year
- * @param string $range
- */
- public function setDateRows($day = "", $month="", $year="", $range="")
- {
- $this->dateRows = $this->generateRows($day,$month,$year,$range);
- }
-
- /**
- * @return array
- */
- public function getDateRows()
- {
- return json_encode($this->dateRows);
- }
- }
|