Runs_Page.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Run list file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(PATH::PAGE . "Page.php");
  12. require_once(PATH::ENTITY . "Run.php");
  13. /**
  14. * Run list page model.
  15. *
  16. * @category Page
  17. */
  18. class Runs_Page extends Page{
  19. /**
  20. * @var Run[] Runs to show.
  21. */
  22. private $runs = [];
  23. /**
  24. * @var mixed[] Page filters
  25. */
  26. private $filters = [
  27. "AREA_TYPE" => 0,
  28. "AREA" => 0,
  29. "STAGE" => 0,
  30. "DIFFICULTY" => 0,
  31. "DATE_MIN" => null,
  32. "DATE_MAX" => null,
  33. "DEFEAT" => false,
  34. "HELPER" => false,
  35. "NUMBER" => 20,
  36. ];
  37. /**
  38. * Constructor.
  39. *
  40. * Retrieves the data and initializes the variables.
  41. */
  42. public function __construct(){
  43. $this->view = PATH::VIEW . "runs.php";
  44. $this->parse_filters();
  45. $result_set = $this->prepare_statement()->execute();
  46. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  47. array_push($this->runs, new Run($result["id"]));
  48. }
  49. $this->title = "Runs - SWDB";
  50. $this->description = "Run logs";
  51. $this->canonical = URL::BASE . "runs/";
  52. }
  53. /**
  54. * Parses the request looking for the selected filters, validates them
  55. * and adds them to the $filters array.
  56. */
  57. private function parse_filters(){
  58. if (isset($_GET["area_type"]) && intval($_GET["area_type"]) > 0){
  59. $this->filters["AREA_TYPE"] = $_GET["area_type"];
  60. }
  61. if (isset($_GET["area"]) && intval($_GET["area"]) > 0){
  62. $this->filters["AREA"] = $_GET["area"];
  63. }
  64. if (isset($_GET["stage"]) && intval($_GET["stage"]) > 0){
  65. $this->filters["STAGE"] = $_GET["stage"];
  66. }
  67. if (isset($_GET["difficulty"]) && intval($_GET["difficulty"]) >= DIFFICULTY_ID::NORMAL && intval($_GET["difficulty"]) <= DIFFICULTY_ID::HELL){
  68. $this->filters["DIFFICULTY"] = $_GET["difficulty"];
  69. }
  70. if (isset($_GET["date_min"]) && strlen($_GET["date_min"]) > 0){
  71. $this->filters["DATE_MIN"] = DateTime::createFromFormat('Y-m-d', $_GET["date_min"])->format('Y-m-d');
  72. }
  73. if (isset($_GET["date_max"]) && strlen($_GET["date_max"]) > 0){
  74. $this->filters["DATE_MAX"] = DateTime::createFromFormat('Y-m-d', $_GET["date_max"])->format('Y-m-d');
  75. }
  76. if (isset($_GET["defeat"]) && $_GET["defeat"] == "on"){
  77. $this->filters["DEFEAT"] = true;
  78. }
  79. else{
  80. $this->filters["HELPER"] = false;
  81. }
  82. if (isset($_GET["helper"]) && $_GET["helper"] == "on"){
  83. $this->filters["HELPER"] = true;
  84. }
  85. else{
  86. $this->filters["HELPER"] = false;
  87. }
  88. if (isset($_GET["number"]) && intval($_GET["number"]) > 0){
  89. $this->filters["NUMBER"] = $_GET["number"];
  90. }
  91. return;
  92. }
  93. /**
  94. * Prepares the statement to execute against the database using the filter values.
  95. *
  96. * @return SQLite3Stmt prepared statement.
  97. */
  98. private function prepare_statement(){
  99. // Common items
  100. $query = "
  101. SELECT id
  102. FROM run
  103. WHERE player = :player
  104. ";
  105. if ($this->filters["AREA_TYPE"] > 0){
  106. $query .= " AND area_type = :area_type ";
  107. }
  108. if ($this->filters["AREA"] > 0){
  109. $query .= " AND area = :area ";
  110. }
  111. if ($this->filters["STAGE"] > 0){
  112. $query .= " AND stage = :stage ";
  113. }
  114. if ($this->filters["DIFFICULTY"] > 0){
  115. $query .= " AND difficulty = :difficulty ";
  116. }
  117. if (! $this->filters["DEFEAT"]){
  118. $query .= " AND win = 1 ";
  119. }
  120. if (! $this->filters["HELPER"]){
  121. $query .= " AND helper = 0 ";
  122. }
  123. if ($this->filters["DATE_MIN"] != null){
  124. $query .= " AND date(dtime) >= date(:date_min) ";
  125. }
  126. if ($this->filters["DATE_MAX"] != null){
  127. $query .= " AND date(dtime) >= date(:date_max) ";
  128. }
  129. $query .= " ORDER BY dtime DESC LIMIT :limit;";
  130. $statement = get_context()->get_db()->prepare($query);
  131. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  132. $statement->bindValue(":area_type", $this->filters["AREA_TYPE"], SQLITE3_INTEGER);
  133. $statement->bindValue(":area", $this->filters["AREA"], SQLITE3_INTEGER);
  134. $statement->bindValue(":stage", $this->filters["STAGE"], SQLITE3_INTEGER);
  135. $statement->bindValue(":difficulty", $this->filters["DIFFICULTY"], SQLITE3_INTEGER);
  136. $statement->bindValue(":date_min", $this->filters["DATE_MIN"], SQLITE3_TEXT);
  137. $statement->bindValue(":date_max", $this->filters["DATE_MAX"], SQLITE3_TEXT);
  138. $statement->bindValue(":limit", $this->filters["NUMBER"], SQLITE3_INTEGER);
  139. return $statement;
  140. }
  141. /**
  142. * Retrieves the page filters.
  143. *
  144. * @return mixed[] Page filtes
  145. */
  146. public function get_filters(){
  147. return $this->filters;
  148. }
  149. /**
  150. * Retrieves one filter.
  151. *
  152. * @param string $key
  153. * @return mixed Filter value, null if if doesn't exist.
  154. */
  155. public function get_filter($key){
  156. if (array_key_exists($key, $this->filters)){
  157. return $this->filters[$key];
  158. }
  159. else{
  160. return null;
  161. }
  162. }
  163. /**
  164. * Retrieves the selection of runs.
  165. *
  166. * @return Run[] Runs to show on the page.
  167. */
  168. public function get_runs(){
  169. return $this->runs;
  170. }
  171. }
  172. ?>