Catalog_Page.php 3.7 KB

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