Catalog_Page.php 4.3 KB

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