Runs_Page.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Run list file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @category Page
  8. */
  9. /**
  10. * Require dependent files if not present.
  11. */
  12. require_once(PATH::PAGE . "Page.php");
  13. require_once(PATH::ENTITY . "Run.php");
  14. /**
  15. * Run list page model.
  16. *
  17. * @category Page
  18. */
  19. class Runs_Page extends Page{
  20. /**
  21. * @var \Run[] Runs to show.
  22. */
  23. public $runs = [];
  24. /**
  25. * Constructor.
  26. *
  27. * Retrieves the data and initializes the variables.
  28. *
  29. * @global resource Database connection.
  30. */
  31. public function __construct(){
  32. global $db;
  33. $this->view = PATH::VIEW . "runs.php";
  34. $this->parse_filters();
  35. $s = $this->build_query();
  36. error_log($s);
  37. $q = $db->query($s);
  38. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  39. array_push($this->runs, new Run($r["id"]));
  40. }
  41. $this->title = "Runs - SWDB";
  42. $this->description = "Run logs";
  43. $this->canonical = URL::BASE . "runs/";
  44. }
  45. /**
  46. * Parses the request looking for the selected filters, validates them
  47. * and adds them to the $filters array.
  48. */
  49. private function parse_filters(){
  50. $this->filters = FILTER::RUN;
  51. if (isset($_GET["area_type"]) && intval($_GET["area_type"]) > 0){
  52. $this->filters["AREA_TYPE"] = $_GET["area_type"];
  53. }
  54. if (isset($_GET["area"]) && intval($_GET["area"]) > 0){
  55. $this->filters["AREA"] = $_GET["area"];
  56. }
  57. if (isset($_GET["stage"]) && intval($_GET["stage"]) > 0){
  58. $this->filters["STAGE"] = $_GET["stage"];
  59. }
  60. if (isset($_GET["difficulty"]) && intval($_GET["difficulty"]) >= DIFFICULTY_ID::NORMAL && intval($_GET["difficulty"]) <= DIFFICULTY_ID::HELL){
  61. $this->filters["DIFFICULTY"] = $_GET["difficulty"];
  62. }
  63. if (isset($_GET["date_min"]) && strlen($_GET["date_min"]) > 0){
  64. $this->filters["DATE_MIN"] = DateTime::createFromFormat('Y-m-d', $_GET["date_min"])->format('Y-m-d');
  65. }
  66. if (isset($_GET["date_max"]) && strlen($_GET["date_max"]) > 0){
  67. $this->filters["DATE_MAX"] = DateTime::createFromFormat('Y-m-d', $_GET["date_max"])->format('Y-m-d');
  68. }
  69. if (isset($_GET["defeat"]) && $_GET["defeat"] == "on"){
  70. $this->filters["DEFEAT"] = true;
  71. }
  72. else{
  73. $this->filters["HELPER"] = false;
  74. }
  75. if (isset($_GET["helper"]) && $_GET["helper"] == "on"){
  76. $this->filters["HELPER"] = true;
  77. }
  78. else{
  79. $this->filters["HELPER"] = false;
  80. }
  81. if (isset($_GET["number"]) && intval($_GET["number"]) > 0){
  82. $this->filters["NUMBER"] = $_GET["number"];
  83. }
  84. return;
  85. }
  86. /**
  87. * Builds the query to the run table using the selected or default
  88. * filters.
  89. *
  90. * @return string The query to be executed.
  91. * @global int Player ID.
  92. */
  93. private function build_query(){
  94. global $UID;
  95. $s = "
  96. SELECT id
  97. FROM run
  98. WHERE uid = '$UID'
  99. ";
  100. if ($this->filters["AREA_TYPE"] > 0){
  101. $s = $s . " AND area_type = " . $this->filters["AREA_TYPE"] . " ";
  102. }
  103. if ($this->filters["AREA"] > 0){
  104. $s = $s . " AND area = " . $this->filters["AREA"] . " ";
  105. }
  106. if ($this->filters["STAGE"] > 0){
  107. $s = $s . " AND stage = " . $this->filters["STAGE"] . " ";
  108. }
  109. if ($this->filters["DIFFICULTY"] > 0){
  110. $s = $s . " AND difficulty = " . $this->filters["DIFFICULTY"] . " ";
  111. }
  112. if (!$this->filters["DEFEAT"]){
  113. $s = $s . " AND win = 1 ";
  114. }
  115. if (!$this->filters["HELPER"]){
  116. $s = $s . " AND helper = 0 ";
  117. }
  118. if ($this->filters["DATE_MIN"] != null){
  119. $s = $s . " AND date(dtime) >= date('" . $this->filters["DATE_MIN"] . "') ";
  120. }
  121. if ($this->filters["DATE_MAX"] != null){
  122. $s = $s . " AND date(dtime) >= date('" . $this->filters["DATE_MAX"] . "') ";
  123. }
  124. $s = $s . " ORDER BY dtime DESC ";
  125. $s = $s . " LIMIT " . $this->filters["NUMBER"] . ";";
  126. return $s;
  127. }
  128. }
  129. ?>