Catalog_Page.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * Constructor.
  26. *
  27. * Retrieves the data and initializes the variables.
  28. *
  29. * @param resource $db Connection to the database.
  30. */
  31. public function __construct($db){
  32. parent::__construct($db);
  33. $this->view = PATH::VIEW . "catalog.php";
  34. $this->parse_filters();
  35. $s_mon = $this->build_query();
  36. $q_mon = $this->db->query($s_mon);
  37. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  38. array_push($this->units, new K_Unit($db, $r_mon["id"], false));
  39. }
  40. error_log($s_mon);
  41. $this->title = "Catalog - SWDB";
  42. $this->description = "Catalog of all monsters";
  43. $this->canonical = URL::BASE . "catalog/";
  44. }
  45. /**
  46. * Parses the request looking for the selected filters, validates them
  47. * and adds them to the $filters array.
  48. */
  49. private function parse_filters(){
  50. $this->filters = FILTER::CATALOG;
  51. if (isset($_GET["element"]) && property_exists(ELEMENT_ID, strtoupper($_GET["element"]))){
  52. $this->filters["ELEMENT"] = $_GET["element"];
  53. }
  54. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  55. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  56. }
  57. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 5){
  58. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  59. }
  60. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 5){
  61. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  62. }
  63. return;
  64. }
  65. /**
  66. * Builds the query to the rune table using the selected or default
  67. * filters.
  68. *
  69. * @return string The query to be executed.
  70. */
  71. private function build_query(){
  72. $s = "
  73. SELECT id
  74. FROM k_unit
  75. WHERE
  76. obtainable = 1 AND
  77. awakens_from IS NULL AND
  78. natural_stars >= " . $this->filters["MIN_STARS"] . " AND
  79. natural_stars <= " . $this->filters["MAX_STARS"];
  80. if(property_exists(ELEMENT_ID, $this->filters["ELEMENT"])){
  81. $s = $s . " AND element = " . $this->filters["ELEMENT"] . " ";
  82. }
  83. if ($this->filters["NAME"] != ""){
  84. $s = $s . " AND upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%' ";
  85. }
  86. $s = $s . "
  87. ORDER BY
  88. element = " . ELEMENT_ID::FIRE ." DESC,
  89. element = " . ELEMENT_ID::WATER ." DESC,
  90. element = " . ELEMENT_ID::WIND ." DESC,
  91. element = " . ELEMENT_ID::LIGHT ." DESC,
  92. element = " . ELEMENT_ID::DARK ." DESC,
  93. natural_stars;
  94. ";
  95. return $s;
  96. }
  97. }
  98. ?>