Catalog_Page.php 3.5 KB

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