Monsters_Page.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 int[] IDs of the monsters marked in the catalog.
  25. */
  26. private $monsters_open = [];
  27. /**
  28. * @var mixed[] Default filter values.
  29. */
  30. private $filters = [
  31. "ELEMENT" => -1,
  32. "NAME" => "",
  33. "MIN_STARS" => 1,
  34. "MAX_STARS" => 6,
  35. ];
  36. /**
  37. * Constructor.
  38. *
  39. * Retrieves the data and initializes the variables.
  40. */
  41. public function __construct(){
  42. $this->view = PATH::VIEW . "monsters.php";
  43. $this->parse_filters();
  44. $result_set = $this->prepare_statement()->execute();
  45. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  46. array_push($this->monsters, new Monster($result["id"], false));
  47. }
  48. // Get open monsters
  49. $statement = get_context()->get_db()->prepare("
  50. SELECT monster FROM collection WHERE player = :player AND open = :open");
  51. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  52. $statement->bindValue(":open", 1, SQLITE3_INTEGER);
  53. $result_set = $statement->execute();
  54. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  55. array_push($this->monsters_open, $result["monster"]);
  56. }
  57. $this->title = "Catalog - SWDB";
  58. $this->description = "Catalog of all monsters";
  59. $this->canonical = URL::BASE . "catalog/";
  60. }
  61. /**
  62. * Parses the request looking for the selected filters, validates them
  63. * and adds them to the $filters array.
  64. */
  65. private function parse_filters(){
  66. if (isset($_GET["element"]) && APPLICATION::valid_id("ELEMENT_ID", strtoupper($_GET["element"]))){
  67. $this->filters["ELEMENT"] = $_GET["element"];
  68. }
  69. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  70. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  71. }
  72. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 5){
  73. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  74. }
  75. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 5){
  76. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  77. }
  78. return;
  79. }
  80. /**
  81. * Prepares the statement to execute against the database using the filter values.
  82. *
  83. * @return SQLite3Stmt prepared statement.
  84. */
  85. private function prepare_statement(){
  86. // Common items
  87. $query = "
  88. SELECT id
  89. FROM monster
  90. WHERE
  91. obtainable = 1 AND
  92. awakens_from IS NULL AND
  93. natural_stars BETWEEN :min_stars AND :max_stars
  94. ";
  95. if(APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
  96. $query .= " AND element = :element ";
  97. }
  98. if ($this->filters["NAME"] != ""){
  99. $query .= " AND upper(name) LIKE upper(:name) " ;
  100. }
  101. $query .= "
  102. ORDER BY
  103. element = :fire DESC,
  104. element = :water DESC,
  105. element = :wind DESC,
  106. element = :light DESC,
  107. element = :dark DESC,
  108. natural_stars;
  109. ";
  110. $statement = get_context()->get_db()->prepare($query);
  111. $statement->bindValue(":min_stars", $this->filters["MIN_STARS"], SQLITE3_INTEGER);
  112. $statement->bindValue(":max_stars", $this->filters["MAX_STARS"], SQLITE3_INTEGER);
  113. $statement->bindValue(":element", $this->filters["ELEMENT"], SQLITE3_INTEGER);
  114. $statement->bindValue(":name", "%" . $this->filters["NAME"] . "%", SQLITE3_TEXT);
  115. $statement->bindValue(":fire", ELEMENT_ID::FIRE, SQLITE3_INTEGER);
  116. $statement->bindValue(":water", ELEMENT_ID::WATER, SQLITE3_INTEGER);
  117. $statement->bindValue(":wind", ELEMENT_ID::WIND, SQLITE3_INTEGER);
  118. $statement->bindValue(":light", ELEMENT_ID::LIGHT, SQLITE3_INTEGER);
  119. $statement->bindValue(":dark", ELEMENT_ID::DARK, SQLITE3_INTEGER);
  120. return $statement;
  121. }
  122. /**
  123. * Retrieves the monsters to display
  124. *
  125. * @return Monster[] Selected monsters.
  126. */
  127. public function get_monsters(){
  128. return $this->monsters;
  129. }
  130. /**
  131. * Retrieves the page filters.
  132. *
  133. * @return mixed[] Page filtes
  134. */
  135. public function get_filters(){
  136. return $this->filters;
  137. }
  138. /**
  139. * Retrieves one filter.
  140. *
  141. * @param string $key
  142. * @return mixed Filter value, null if if doesn't exist.
  143. */
  144. public function get_filter($key){
  145. if (array_key_exists($key, $this->filters)){
  146. return $this->filters[$key];
  147. }
  148. else{
  149. return null;
  150. }
  151. }
  152. public function is_open($id){
  153. if (in_array($id, $this->monsters_open)){
  154. return true;
  155. }
  156. else{
  157. return false;
  158. }
  159. }
  160. }
  161. ?>