Catalog_Page.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. require_once($path["page"] . "Page.php");
  3. require_once($path["entity"] . "K_Unit.php");
  4. /**
  5. * Catalog page model.
  6. */
  7. class Catalog_Page extends Page{
  8. /**
  9. * Array of {@see K_Unit}.
  10. */
  11. public $monsters = [];
  12. /**
  13. * The filters the page can handle.
  14. */
  15. public $filters;
  16. /**
  17. * Constructor.
  18. *
  19. * Retrieves the data and initializes the variables.
  20. *
  21. * @param SQLite3 $db Connection to the database.
  22. */
  23. public function __construct($db){
  24. global $path;
  25. global $root;
  26. parent::__construct($db);
  27. $this->view = $path["view"] . "catalog.php";
  28. $this->parse_filters();
  29. $s_mon = $this->build_query();
  30. $q_mon = $this->db->query($s_mon);
  31. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  32. array_push($this->monsters, new K_Unit($db, $r_mon["id"], false));
  33. }
  34. $this->title = "Catalog - SWDB";
  35. $this->description = "Catalog of all monsters";
  36. $this->canonical = $root . "catalog/";
  37. }
  38. /**
  39. * Parses the request looking for the selected filters, validates them
  40. * and adds them to the $filters array.
  41. */
  42. private function parse_filters(){
  43. global $ELEMENT;
  44. global $FILTER_CATALOG;
  45. $this->filters = $FILTER_CATALOG;
  46. if (isset($_GET["element"]) && in_array($_GET["element"], $ELEMENT)){
  47. $this->filters["element"] = $_GET["element"];
  48. }
  49. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  50. $this->filters["name"] = SQLite3::escapeString($_GET["name"]);
  51. }
  52. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 5){
  53. $this->filters["min_stars"] = $_GET["min_stars"];
  54. }
  55. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 5){
  56. $this->filters["max_stars"] = $_GET["max_stars"];
  57. }
  58. return;
  59. }
  60. /**
  61. * Builds the query to the rune table using the selected or default
  62. * filters.
  63. *
  64. * @return The query to be executed.
  65. */
  66. private function build_query(){
  67. global $ELEMENT;
  68. $s = "
  69. SELECT id
  70. FROM k_unit
  71. WHERE
  72. obtainable = 1 AND
  73. awakens_from IS NULL AND
  74. natural_stars >= " . $this->filters["min_stars"] . " AND
  75. natural_stars <= " . $this->filters["max_stars"];
  76. if (in_array($this->filters["element"], $ELEMENT)){
  77. $s = $s . " AND element = " . strtoupper($this->filters["element"]) . " ";
  78. }
  79. if ($this->filters["name"] != ""){
  80. $s = $s . " AND upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%' ";
  81. }
  82. $s = $s . "
  83. ORDER BY
  84. element = " . $ELEMENT["FIRE"] ." DESC,
  85. element = " . $ELEMENT["WATER"] ." DESC,
  86. element = " . $ELEMENT["WIND"] ." DESC,
  87. element = " . $ELEMENT["LIGHT"] ." DESC,
  88. element = " . $ELEMENT["DARK"] ." DESC,
  89. natural_stars;
  90. ";
  91. return $s;
  92. }
  93. }
  94. ?>