Catalog_Page.php 4.2 KB

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