You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatsGenerator.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace SCF\Core;
  3. use SCF\Core\Database;
  4. class StatsGenerator {
  5. /**
  6. * @var \SCF\Core\Database
  7. */
  8. private $_db;
  9. /**
  10. * @var array
  11. */
  12. private $statValues = [];
  13. /**
  14. * @var array
  15. */
  16. private $dateLabels = [];
  17. /**
  18. * @var array
  19. */
  20. private $dateRows = [];
  21. /**
  22. * StatsGenerator constructor.
  23. * @param \SCF\Core\Database $db
  24. */
  25. public function __construct(Database $db)
  26. {
  27. $this->_db = $db;
  28. $this->readDatabaseToArray();
  29. $this->setDateLabels(25,12,2014,10);
  30. $this->setDateRows(25,12,2014,10);
  31. }
  32. /**
  33. * @return array
  34. */
  35. private function readDatabaseToArray()
  36. {
  37. $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");
  38. $visitorsPerDay = $this->_db->fetchArray();
  39. foreach ($visitorsPerDay as $key => $value) {
  40. $day = ($value['visit_day'] < 10 ? '0'.$value['visit_day'] : $value['visit_day']);
  41. $month = ($value['visit_month'] < 10 ? '0'.$value['visit_month'] : $value['visit_month']);
  42. $year = $value['visit_year'];
  43. $this->statValues[$day.'.'.$month.'.'.$year] = (int) $value['visit_count'];
  44. }
  45. return $this->statValues;
  46. }
  47. /**
  48. * @param string $day
  49. * @param string $month
  50. * @param string $year
  51. * @param string $range
  52. * @return array
  53. */
  54. private function generateLabels($day = "", $month="", $year="", $range="")
  55. {
  56. $genDates = [];
  57. $dateValue = $day.'.'.$month.'.'.$year;
  58. $genStart = new \DateTime($dateValue);
  59. $interval = new \DateInterval('P1D');
  60. if(is_numeric($range)) {
  61. $range = ($range > 30 ? 30 : $range);
  62. $genStart = $genStart->modify("-" . $range . " days");
  63. $genEnd = clone $genStart;
  64. $genEnd = $genEnd->modify("+" . ($range * 2) . " days");
  65. } else {
  66. $genEnd = clone $genStart;
  67. $genEnd->add(new \DateInterval("P1M"));
  68. }
  69. $dateRange = new \DatePeriod($genStart,$interval,$genEnd);
  70. foreach ($dateRange as $date) {
  71. $genDates[] = $date->format("d.m.Y");
  72. }
  73. return $genDates;
  74. }
  75. /**
  76. * @param string $day
  77. * @param string $month
  78. * @param string $year
  79. * @param string $range
  80. * @return array
  81. */
  82. private function generateRows($day = "", $month="", $year="", $range="")
  83. {
  84. $rowArray = $this->statValues;
  85. $genRows = [];
  86. $dateValue = $day.'.'.$month.'.'.$year;
  87. $genStart = new \DateTime($dateValue);
  88. $interval = new \DateInterval('P1D');
  89. if(is_numeric($range)) {
  90. $range = ($range > 30 ? 30 : $range);
  91. $genStart = $genStart->modify("-" . $range . " days");
  92. $genEnd = clone $genStart;
  93. $genEnd = $genEnd->modify("+" . ($range * 2) . " days");
  94. } else {
  95. $genEnd = clone $genStart;
  96. $genEnd->add(new \DateInterval("P1M"));
  97. }
  98. $dateRange = new \DatePeriod($genStart,$interval,$genEnd);
  99. $genEnd = clone $genStart;
  100. $genEnd->add(new \DateInterval("P1M"));
  101. foreach ($dateRange as $date) {
  102. $realDate = $date->format("d.m.Y");
  103. if(array_key_exists($realDate, $rowArray))
  104. {
  105. $genRows[] = $rowArray[$realDate];
  106. } else {
  107. $genRows[] = 0;
  108. }
  109. //var_dump($rowArray);
  110. }
  111. return $genRows;
  112. }
  113. /**
  114. * @param string $day
  115. * @param string $month
  116. * @param string $year
  117. * @param string $range
  118. */
  119. public function setDateLabels($day = "", $month="", $year="", $range="")
  120. {
  121. $this->dateLabels = $this->generateLabels($day,$month,$year,$range);
  122. }
  123. /**
  124. * @return array
  125. */
  126. public function getDateLabels()
  127. {
  128. return json_encode($this->dateLabels);
  129. }
  130. /**
  131. * @param string $day
  132. * @param string $month
  133. * @param string $year
  134. * @param string $range
  135. */
  136. public function setDateRows($day = "", $month="", $year="", $range="")
  137. {
  138. $this->dateRows = $this->generateRows($day,$month,$year,$range);
  139. }
  140. /**
  141. * @return array
  142. */
  143. public function getDateRows()
  144. {
  145. return json_encode($this->dateRows);
  146. }
  147. }