Runs_Page.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. parent::__construct();
  44. $this->set_public(false);
  45. $this->set_view("runs.php");
  46. $this->set_title("Runs");
  47. $this->set_description(get_context()->get_player()->get_name() . "'s run logs");
  48. $this->set_canonical("player/" . get_context()->get_player()->get_id() . "/runs/");
  49. $this->add_css("runs.css");
  50. $this->parse_filters();
  51. $result_set = $this->prepare_statement()->execute();
  52. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  53. array_push($this->runs, new Run($result["id"]));
  54. }
  55. $this->set_code(200);
  56. $this->set_message("OK");
  57. }
  58. /**
  59. * Parses the request looking for the selected filters, validates them
  60. * and adds them to the $filters array.
  61. */
  62. private function parse_filters(){
  63. if (isset($_GET["area_type"]) && intval($_GET["area_type"]) > 0){
  64. $this->filters["AREA_TYPE"] = $_GET["area_type"];
  65. }
  66. if (isset($_GET["area"]) && intval($_GET["area"]) > 0){
  67. $this->filters["AREA"] = $_GET["area"];
  68. }
  69. if (isset($_GET["stage"]) && intval($_GET["stage"]) > 0){
  70. $this->filters["STAGE"] = $_GET["stage"];
  71. }
  72. if (isset($_GET["difficulty"]) && intval($_GET["difficulty"]) >= DIFFICULTY_ID::NORMAL && intval($_GET["difficulty"]) <= DIFFICULTY_ID::HELL){
  73. $this->filters["DIFFICULTY"] = $_GET["difficulty"];
  74. }
  75. if (isset($_GET["date_min"]) && strlen($_GET["date_min"]) > 0){
  76. $this->filters["DATE_MIN"] = DateTime::createFromFormat('Y-m-d', $_GET["date_min"])->format('Y-m-d');
  77. }
  78. if (isset($_GET["date_max"]) && strlen($_GET["date_max"]) > 0){
  79. $this->filters["DATE_MAX"] = DateTime::createFromFormat('Y-m-d', $_GET["date_max"])->format('Y-m-d');
  80. }
  81. if (isset($_GET["defeat"]) && $_GET["defeat"] == "on"){
  82. $this->filters["DEFEAT"] = true;
  83. }
  84. else{
  85. $this->filters["HELPER"] = false;
  86. }
  87. if (isset($_GET["helper"]) && $_GET["helper"] == "on"){
  88. $this->filters["HELPER"] = true;
  89. }
  90. else{
  91. $this->filters["HELPER"] = false;
  92. }
  93. if (isset($_GET["number"]) && intval($_GET["number"]) > 0){
  94. $this->filters["NUMBER"] = $_GET["number"];
  95. }
  96. return;
  97. }
  98. /**
  99. * Prepares the statement to execute against the database using the filter values.
  100. *
  101. * @return SQLite3Stmt prepared statement.
  102. */
  103. private function prepare_statement(){
  104. // Common items
  105. $query = "
  106. SELECT id
  107. FROM run
  108. WHERE player = :player
  109. ";
  110. if ($this->filters["AREA_TYPE"] > 0){
  111. $query .= " AND area_type = :area_type ";
  112. }
  113. if ($this->filters["AREA"] > 0){
  114. $query .= " AND area = :area ";
  115. }
  116. if ($this->filters["STAGE"] > 0){
  117. $query .= " AND stage = :stage ";
  118. }
  119. if ($this->filters["DIFFICULTY"] > 0){
  120. $query .= " AND difficulty = :difficulty ";
  121. }
  122. if (! $this->filters["DEFEAT"]){
  123. $query .= " AND win = 1 ";
  124. }
  125. if (! $this->filters["HELPER"]){
  126. $query .= " AND helper = 0 ";
  127. }
  128. if ($this->filters["DATE_MIN"] != null){
  129. $query .= " AND date(dtime) >= date(:date_min) ";
  130. }
  131. if ($this->filters["DATE_MAX"] != null){
  132. $query .= " AND date(dtime) >= date(:date_max) ";
  133. }
  134. $query .= " ORDER BY dtime DESC LIMIT :limit;";
  135. $statement = get_context()->get_db()->prepare($query);
  136. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  137. $statement->bindValue(":area_type", $this->filters["AREA_TYPE"], SQLITE3_INTEGER);
  138. $statement->bindValue(":area", $this->filters["AREA"], SQLITE3_INTEGER);
  139. $statement->bindValue(":stage", $this->filters["STAGE"], SQLITE3_INTEGER);
  140. $statement->bindValue(":difficulty", $this->filters["DIFFICULTY"], SQLITE3_INTEGER);
  141. $statement->bindValue(":date_min", $this->filters["DATE_MIN"], SQLITE3_TEXT);
  142. $statement->bindValue(":date_max", $this->filters["DATE_MAX"], SQLITE3_TEXT);
  143. $statement->bindValue(":limit", $this->filters["NUMBER"], SQLITE3_INTEGER);
  144. return $statement;
  145. }
  146. /**
  147. * Retrieves the page filters.
  148. *
  149. * @return mixed[] Page filtes
  150. */
  151. public function get_filters(){
  152. return $this->filters;
  153. }
  154. /**
  155. * Retrieves one filter.
  156. *
  157. * @param string $key
  158. * @return mixed Filter value, null if if doesn't exist.
  159. */
  160. public function get_filter($key){
  161. if (array_key_exists($key, $this->filters)){
  162. return $this->filters[$key];
  163. }
  164. else{
  165. return null;
  166. }
  167. }
  168. /**
  169. * Retrieves the selection of runs.
  170. *
  171. * @return Run[] Runs to show on the page.
  172. */
  173. public function get_runs(){
  174. return $this->runs;
  175. }
  176. }
  177. ?>