Monsters_Page.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Catalog page 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 . "Monster.php");
  14. /**
  15. * Catalog page model.
  16. *
  17. * @category Page
  18. */
  19. class Monsters_Page extends Page{
  20. /**
  21. * @var Monster[] List of monsters to show.
  22. */
  23. public $monsters = [];
  24. /**
  25. * @var int[] IDs of the monsters marked in the catalog.
  26. */
  27. public $monsters_open = [];
  28. /**
  29. * @var mixed[] Default filter values.
  30. */
  31. public $filters = [
  32. "ELEMENT" => -1,
  33. "NAME" => "",
  34. "MIN_STARS" => 1,
  35. "MAX_STARS" => 6,
  36. ];
  37. /**
  38. * Constructor.
  39. *
  40. * Retrieves the data and initializes the variables.
  41. */
  42. public function __construct(){
  43. $this->view = PATH::VIEW . "monsters.php";
  44. $this->parse_filters();
  45. $s_mon = $this->build_query();
  46. $q_mon = get_context()->get_db()->query($s_mon);
  47. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  48. array_push($this->monsters, new Monster($r_mon["id"], false));
  49. }
  50. $s_open = "
  51. SELECT monster
  52. FROM collection
  53. WHERE
  54. player = " . get_context()->get_player()->get_id() . " AND
  55. open = 1
  56. ";
  57. $q_open = get_context()->get_db()->query($s_open);
  58. while ($r_open = $q_open->fetchArray(SQLITE3_ASSOC)){
  59. array_push($this->monsters_open, $r_open["monster"]);
  60. }
  61. $this->title = "Catalog - SWDB";
  62. $this->description = "Catalog of all monsters";
  63. $this->canonical = URL::BASE . "catalog/";
  64. }
  65. /**
  66. * Parses the request looking for the selected filters, validates them
  67. * and adds them to the $filters array.
  68. */
  69. private function parse_filters(){
  70. if (isset($_GET["element"]) && APPLICATION::valid_id("ELEMENT_ID", strtoupper($_GET["element"]))){
  71. $this->filters["ELEMENT"] = $_GET["element"];
  72. }
  73. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  74. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  75. }
  76. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 5){
  77. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  78. }
  79. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 5){
  80. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  81. }
  82. return;
  83. }
  84. /**
  85. * Builds the query to the rune table using the selected or default
  86. * filters.
  87. *
  88. * @return string The query to be executed.
  89. */
  90. private function build_query(){
  91. $s = "
  92. SELECT id
  93. FROM monster
  94. WHERE
  95. obtainable = 1 AND
  96. awakens_from IS NULL AND
  97. natural_stars >= " . $this->filters["MIN_STARS"] . " AND
  98. natural_stars <= " . $this->filters["MAX_STARS"];
  99. if(APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
  100. $s = $s . " AND element = " . $this->filters["ELEMENT"] . " ";
  101. }
  102. if ($this->filters["NAME"] != ""){
  103. $s = $s . " AND upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%' ";
  104. }
  105. $s = $s . "
  106. ORDER BY
  107. element = " . ELEMENT_ID::FIRE ." DESC,
  108. element = " . ELEMENT_ID::WATER ." DESC,
  109. element = " . ELEMENT_ID::WIND ." DESC,
  110. element = " . ELEMENT_ID::LIGHT ." DESC,
  111. element = " . ELEMENT_ID::DARK ." DESC,
  112. natural_stars;
  113. ";
  114. return $s;
  115. }
  116. }
  117. ?>