Runs_Page.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. require_once($path["page"] . "Page.php");
  3. require_once($path["entity"] . "Run.php");
  4. /**
  5. * Run list page.
  6. */
  7. class Runs_Page extends Page{
  8. /**
  9. * Array of @{see Run}s.
  10. */
  11. public $runs = [];
  12. /**
  13. * Filters the page can handle.
  14. */
  15. public $filters;
  16. /**
  17. * Constructor.
  18. *
  19. * Retrieves the data and initializes the variables.
  20. *
  21. * @param SQLite3 $db Connection to the database.
  22. */
  23. public function __construct($db){
  24. global $path;
  25. global $root;
  26. parent::__construct($db);
  27. $this->view = $path["view"] . "runs.php";
  28. $this->parse_filters();
  29. $s = $this->build_query();
  30. $q = $this->db->query($s);
  31. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  32. array_push($this->runs, new Run($db, $r["id"]));
  33. }
  34. $this->title = "Runs - SWDB";
  35. $this->description = "Run logs";
  36. $this->canonical = $root . "runs/";
  37. }
  38. /**
  39. * Parses the request looking for the selected filters, validates them
  40. * and adds them to the $filters array.
  41. */
  42. private function parse_filters(){
  43. global $FILTER_RUN;
  44. global $DIFFICULTY;
  45. $this->filters = $FILTER_RUN;
  46. if (isset($_GET["area_type"]) && intval($_GET["area_type"]) > 0){
  47. $this->filters["area_type"] = $_GET["area_type"];
  48. }
  49. if (isset($_GET["area"]) && intval($_GET["area"]) > 0){
  50. $this->filters["area"] = $_GET["area"];
  51. }
  52. if (isset($_GET["stage"]) && intval($_GET["stage"]) > 0){
  53. $this->filters["stage"] = $_GET["stage"];
  54. }
  55. if (isset($_GET["difficulty"]) && intval($_GET["difficulty"]) >= $DIFFICULTY["NORMAL"] && intval($_GET["difficulty"]) <= $DIFFICULTY["HELL"]){
  56. $this->filters["difficulty"] = $_GET["difficulty"];
  57. }
  58. if (isset($_GET["date_min"]) && strlen($_GET["date_min"]) > 0){
  59. $this->filters["date_min"] = DateTime::createFromFormat('Y-m-d', $_GET["date_min"])->format('Y-m-d');
  60. }
  61. if (isset($_GET["date_max"]) && strlen($_GET["date_max"]) > 0){
  62. $this->filters["date_max"] = DateTime::createFromFormat('Y-m-d', $_GET["date_max"])->format('Y-m-d');
  63. }
  64. if (isset($_GET["defeat"]) && $_GET["defeat"] == "on"){
  65. $this->filters["defeat"] = true;
  66. }
  67. else{
  68. $this->filters["helper"] = false;
  69. }
  70. if (isset($_GET["helper"]) && $_GET["helper"] == "on"){
  71. $this->filters["helper"] = true;
  72. }
  73. else{
  74. $this->filters["helper"] = false;
  75. }
  76. if (isset($_GET["number"]) && intval($_GET["number"]) > 0){
  77. $this->filters["number"] = $_GET["number"];
  78. }
  79. return;
  80. }
  81. /**
  82. * Builds the query to the run table using the selected or default
  83. * filters.
  84. *
  85. * @return The query to be executed.
  86. */
  87. private function build_query(){
  88. $s = "
  89. SELECT id
  90. FROM run
  91. WHERE 1 = 1
  92. ";
  93. if ($this->filters["area"] > 0){
  94. $s = $s . " AND area = " . $this->filters["area"] . " ";
  95. }
  96. if ($this->filters["stage"] > 0){
  97. $s = $s . " AND stage = " . $this->filters["stage"] . " ";
  98. }
  99. if ($this->filters["difficulty"] > 0){
  100. $s = $s . " AND difficulty = " . $this->filters["difficulty"] . " ";
  101. }
  102. if (!$this->filters["defeat"]){
  103. $s = $s . " AND win = 1 ";
  104. }
  105. if (!$this->filters["helper"]){
  106. $s = $s . " AND helper = 0 ";
  107. }
  108. if ($this->filters["date_min"] != null){
  109. $s = $s . " AND date(dtime) >= date('" . $this->filters["date_min"] . "') ";
  110. }
  111. if ($this->filters["date_max"] != null){
  112. $s = $s . " AND date(dtime) >= date('" . $this->filters["date_max"] . "') ";
  113. }
  114. $s = $s . " ORDER BY dtime DESC ";
  115. $s = $s . " LIMIT " . $this->filters["number"] . ";";
  116. return $s;
  117. }
  118. }
  119. ?>