Monsters_Page.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Monster list page 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 . "Monster.php");
  13. /**
  14. * Monster list page model.
  15. *
  16. * @category Page
  17. */
  18. class Monsters_Page extends Page{
  19. /**
  20. * @var Monster[] List of monsters to show.
  21. */
  22. private $monsters = [];
  23. /**
  24. * @var mixed[] Default filter values.
  25. */
  26. private $filters = [
  27. "ELEMENT" => -1,
  28. "NAME" => "",
  29. "MIN_STARS" => 1,
  30. "MAX_STARS" => 6,
  31. ];
  32. /**
  33. * Constructor.
  34. *
  35. * Retrieves the data and initializes the variables.
  36. */
  37. public function __construct(){
  38. parent::__construct();
  39. $this->set_public(true);
  40. $this->set_view("monsters.php");
  41. $this->set_title("Monsters");
  42. $this->set_description("Catalog of all monsters");
  43. $this->set_canonical("monsters/");
  44. $this->add_css("monsters.css");
  45. $this->parse_filters();
  46. $result_set = $this->prepare_statement()->execute();
  47. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  48. array_push($this->monsters, new Monster($result["id"], false));
  49. }
  50. $this->set_code(200);
  51. $this->set_message("OK");
  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["element"]) && APPLICATION::valid_id("ELEMENT_ID", strtoupper($_GET["element"]))){
  59. $this->filters["ELEMENT"] = $_GET["element"];
  60. }
  61. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  62. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  63. }
  64. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 5){
  65. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  66. }
  67. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 5){
  68. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  69. }
  70. return;
  71. }
  72. /**
  73. * Prepares the statement to execute against the database using the filter values.
  74. *
  75. * @return SQLite3Stmt prepared statement.
  76. */
  77. private function prepare_statement(){
  78. // Common items
  79. $query = "
  80. SELECT id
  81. FROM monster
  82. WHERE
  83. obtainable = 1 AND
  84. awakens_from IS NULL AND
  85. natural_stars BETWEEN :min_stars AND :max_stars
  86. ";
  87. if(APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
  88. $query .= " AND element = :element ";
  89. }
  90. if ($this->filters["NAME"] != ""){
  91. $query .= " AND upper(name) LIKE upper(:name) " ;
  92. }
  93. $query .= "
  94. ORDER BY
  95. element = :fire DESC,
  96. element = :water DESC,
  97. element = :wind DESC,
  98. element = :light DESC,
  99. element = :dark DESC,
  100. natural_stars;
  101. ";
  102. $statement = get_context()->get_db()->prepare($query);
  103. $statement->bindValue(":min_stars", $this->filters["MIN_STARS"], SQLITE3_INTEGER);
  104. $statement->bindValue(":max_stars", $this->filters["MAX_STARS"], SQLITE3_INTEGER);
  105. $statement->bindValue(":element", $this->filters["ELEMENT"], SQLITE3_INTEGER);
  106. $statement->bindValue(":name", "%" . $this->filters["NAME"] . "%", SQLITE3_TEXT);
  107. $statement->bindValue(":fire", ELEMENT_ID::FIRE, SQLITE3_INTEGER);
  108. $statement->bindValue(":water", ELEMENT_ID::WATER, SQLITE3_INTEGER);
  109. $statement->bindValue(":wind", ELEMENT_ID::WIND, SQLITE3_INTEGER);
  110. $statement->bindValue(":light", ELEMENT_ID::LIGHT, SQLITE3_INTEGER);
  111. $statement->bindValue(":dark", ELEMENT_ID::DARK, SQLITE3_INTEGER);
  112. return $statement;
  113. }
  114. /**
  115. * Retrieves the monsters to display
  116. *
  117. * @return Monster[] Selected monsters.
  118. */
  119. public function get_monsters(){
  120. return $this->monsters;
  121. }
  122. /**
  123. * Retrieves the page filters.
  124. *
  125. * @return mixed[] Page filtes
  126. */
  127. public function get_filters(){
  128. return $this->filters;
  129. }
  130. /**
  131. * Retrieves one filter.
  132. *
  133. * @param string $key
  134. * @return mixed Filter value, null if if doesn't exist.
  135. */
  136. public function get_filter($key){
  137. if (array_key_exists($key, $this->filters)){
  138. return $this->filters[$key];
  139. }
  140. else{
  141. return null;
  142. }
  143. }
  144. }
  145. ?>