Catalog_Page.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. require_once($path["page"] . "Page.php");
  3. require_once($path["entity"] . "K_Monster.php");
  4. /**
  5. * Catalog page model.
  6. */
  7. class Catalog_Page extends Page{
  8. /**
  9. * Array of arrays of monsters, grouped by attribute id.
  10. */
  11. public $monsters = [];
  12. /**
  13. * The filters the page can handle.
  14. */
  15. public $filters = [
  16. "element" => "",
  17. "name" => "",
  18. "min_stars" => 1,
  19. "max_stars" => 6,
  20. ];
  21. /**
  22. * Constructor.
  23. *
  24. * Retrieves the data and initializes the variables.
  25. *
  26. * @param SQLite3 $db Connection to the database.
  27. */
  28. public function __construct($db){
  29. global $path;
  30. global $root;
  31. parent::__construct($db);
  32. $this->view = $path["view"] . "catalog.php";
  33. $this->parse_filters();
  34. $s_mon = $this->build_query();
  35. $q_mon = $this->db->query($s_mon);
  36. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  37. array_push($this->monsters, new K_Monster($db, $r_mon["id"]));
  38. }
  39. $this->title = "Catalog - SWDB";
  40. $this->description = "Catalog of all monsters";
  41. $this->canonical = $root . "catalog/";
  42. }
  43. /**
  44. * Parses the request looking for the selected filters, validates them
  45. * and adds them to the $filters array.
  46. */
  47. private function parse_filters(){
  48. if (isset($_GET["element"])){
  49. $f_element = strtoupper($_GET["element"]);
  50. if ($f_element == "FIRE" || $f_element == "WATER" || $f_element == "WIND" || $f_element == "LIGHT" || $f_element == "DARK"){
  51. $this->filters["element"] = $_GET["element"];
  52. }
  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"]) < 6){
  58. $this->filters["min_stars"] = $_GET["min_stars"];
  59. }
  60. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 6){
  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 The query to be executed.
  70. */
  71. private function build_query(){
  72. $s = "SELECT id FROM k_monster WHERE obtainable = 1 AND awakens_from IS NULL AND natural_stars >= " . $this->filters["min_stars"] . " AND natural_stars <= " . $this->filters["max_stars"];
  73. if (strlen($this->filters["element"]) > 0){
  74. $s = $s . " AND upper(element) = '" . strtoupper($this->filters["element"]) . "' ";
  75. }
  76. if ($this->filters["name"] != ""){
  77. $s = $s . " AND monster IN (SELECT id FROM k_monster WHERE upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%') ";
  78. }
  79. $s = $s . " ORDER BY element, natural_stars;";
  80. return $s;
  81. }
  82. }
  83. ?>