Runs_Page.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * The filters the page can handle.
  14. */
  15. public $filters = [
  16. "area" => [],
  17. "stage" => [],
  18. "difficulty" => [],
  19. "win_lose" => 0,
  20. "helper" => false
  21. ];
  22. /**
  23. * Constructor.
  24. *
  25. * Retrieves the data and initializes the variables.
  26. *
  27. * @param SQLite3 $db Connection to the database.
  28. */
  29. public function __construct($db){
  30. global $path;
  31. global $root;
  32. parent::__construct($db);
  33. $this->view = $path["view"] . "runs.php";
  34. $this->parse_filters();
  35. $s = $this->build_query();
  36. $q = $this->db->query($s);
  37. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  38. array_push($this->runs, new Run($db, $r["id"]));
  39. }
  40. $this->title = "Runs - SWDB";
  41. $this->description = "Run logs";
  42. $this->canonical = $root . "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. if (isset($_GET["area"])){
  50. $areas = $_GET["area"];
  51. foreach ($areas as $area){
  52. array_push($this->filters["area"], intval($ara));
  53. }
  54. }
  55. if (isset($_GET["stage"])){
  56. $stages = $_GET["stage"];
  57. foreach ($stages as $stage){
  58. array_push($this->filters["stage"], intval($ara));
  59. }
  60. }
  61. if (isset($_GET["difficulty"])){
  62. $difficultys = $_GET["difficulty"];
  63. foreach ($difficultys as $difficulty){
  64. array_push($this->filters["difficulty"], intval($ara));
  65. }
  66. }
  67. if (isset($_GET["win_lose"]) && intval($_GET["win_lose"]) >= 0 && intval($_GET["win_lose"]) <= 2){
  68. $this->filters["win_lose"] = $_GET["win_lose"];
  69. }
  70. if (isset($_GET["helper"]) && $_GET["elper"] == "on"){
  71. $this->filters["helper"] = true;
  72. }
  73. else{
  74. $this->filters["helper"] = false;
  75. }
  76. return;
  77. }
  78. /**
  79. * Builds the query to the run table using the selected or default
  80. * filters.
  81. *
  82. * @return The query to be executed.
  83. */
  84. private function build_query(){
  85. $s = "
  86. SELECT id
  87. FROM run
  88. WHERE 1 = 1
  89. ";
  90. if (count($this->filters["area"]) > 0){
  91. $s = $s . " AND area IN ( ";
  92. foreach($this->filters["area"] as $t){
  93. $s = $s . "'$t', ";
  94. }
  95. $s = $s . "'DUMMY0') ";
  96. }
  97. if (count($this->filters["stage"]) > 0){
  98. $s = $s . " AND stage IN ( ";
  99. foreach($this->filters["stage"] as $t){
  100. $s = $s . "'$t', ";
  101. }
  102. $s = $s . "'DUMMY0') ";
  103. }
  104. if (count($this->filters["difficulty"]) > 0){
  105. $s = $s . " AND difficulty IN ( ";
  106. foreach($this->filters["difficulty"] as $t){
  107. $s = $s . "'$t', ";
  108. }
  109. $s = $s . "'DUMMY0') ";
  110. }
  111. switch ($this->filters["win_lose"]){
  112. case 1:
  113. $s = $s . " AND win = 1 ";
  114. break;
  115. case 2:
  116. $s = $s . " AND win = 0 ";
  117. break;
  118. }
  119. return $s;
  120. }
  121. }
  122. ?>