Runs_Page.php 4.6 KB

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