Runs_Page.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. $q = $db->query($s);
  37. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  38. array_push($this->runs, new Run($r["id"]));
  39. }
  40. $this->title = "Runs - SWDB";
  41. $this->description = "Run logs";
  42. $this->canonical = URL::BASE . "runs/";
  43. }
  44. /**
  45. * Parses the request looking for the selected filters, validates them
  46. * and adds them to the $filters array.
  47. */
  48. private function parse_filters(){
  49. $this->filters = FILTER::RUN;
  50. if (isset($_GET["area_type"]) && intval($_GET["area_type"]) > 0){
  51. $this->filters["AREA_TYPE"] = $_GET["area_type"];
  52. }
  53. if (isset($_GET["area"]) && intval($_GET["area"]) > 0){
  54. $this->filters["AREA"] = $_GET["area"];
  55. }
  56. if (isset($_GET["stage"]) && intval($_GET["stage"]) > 0){
  57. $this->filters["STAGE"] = $_GET["stage"];
  58. }
  59. if (isset($_GET["difficulty"]) && intval($_GET["difficulty"]) >= DIFFICULTY_ID::NORMAL && intval($_GET["difficulty"]) <= DIFFICULTY_ID::HELL){
  60. $this->filters["DIFFICULTY"] = $_GET["difficulty"];
  61. }
  62. if (isset($_GET["date_min"]) && strlen($_GET["date_min"]) > 0){
  63. $this->filters["DATE_MIN"] = DateTime::createFromFormat('Y-m-d', $_GET["date_min"])->format('Y-m-d');
  64. }
  65. if (isset($_GET["date_max"]) && strlen($_GET["date_max"]) > 0){
  66. $this->filters["DATE_MAX"] = DateTime::createFromFormat('Y-m-d', $_GET["date_max"])->format('Y-m-d');
  67. }
  68. if (isset($_GET["defeat"]) && $_GET["defeat"] == "on"){
  69. $this->filters["DEFEAT"] = true;
  70. }
  71. else{
  72. $this->filters["HELPER"] = false;
  73. }
  74. if (isset($_GET["helper"]) && $_GET["helper"] == "on"){
  75. $this->filters["HELPER"] = true;
  76. }
  77. else{
  78. $this->filters["HELPER"] = false;
  79. }
  80. if (isset($_GET["number"]) && intval($_GET["number"]) > 0){
  81. $this->filters["NUMBER"] = $_GET["number"];
  82. }
  83. return;
  84. }
  85. /**
  86. * Builds the query to the run table using the selected or default
  87. * filters.
  88. *
  89. * @return string The query to be executed.
  90. * @global int Player ID.
  91. */
  92. private function build_query(){
  93. global $UID;
  94. $s = "
  95. SELECT id
  96. FROM run
  97. WHERE uid = '$UID'
  98. ";
  99. if ($this->filters["AREA_TYPE"] > 0){
  100. $s = $s . " AND area_type = " . $this->filters["AREA_TYPE"] . " ";
  101. }
  102. if ($this->filters["AREA"] > 0){
  103. $s = $s . " AND area = " . $this->filters["AREA"] . " ";
  104. }
  105. if ($this->filters["STAGE"] > 0){
  106. $s = $s . " AND stage = " . $this->filters["STAGE"] . " ";
  107. }
  108. if ($this->filters["DIFFICULTY"] > 0){
  109. $s = $s . " AND difficulty = " . $this->filters["DIFFICULTY"] . " ";
  110. }
  111. if (!$this->filters["DEFEAT"]){
  112. $s = $s . " AND win = 1 ";
  113. }
  114. if (!$this->filters["HELPER"]){
  115. $s = $s . " AND helper = 0 ";
  116. }
  117. if ($this->filters["DATE_MIN"] != null){
  118. $s = $s . " AND date(dtime) >= date('" . $this->filters["DATE_MIN"] . "') ";
  119. }
  120. if ($this->filters["DATE_MAX"] != null){
  121. $s = $s . " AND date(dtime) >= date('" . $this->filters["DATE_MAX"] . "') ";
  122. }
  123. $s = $s . " ORDER BY dtime DESC ";
  124. $s = $s . " LIMIT " . $this->filters["NUMBER"] . ";";
  125. return $s;
  126. }
  127. }
  128. ?>