Catalog_Page.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. public $filters = [
  13. "element" => "",
  14. "name" => "",
  15. "min_stars" => 1,
  16. "max_stars" => 6,
  17. ];
  18. /**
  19. * Constructor.
  20. *
  21. * Retrieves the data and initializes the variables.
  22. *
  23. * @param SQLite3 $db Connection to the database.
  24. */
  25. public function __construct($db){
  26. global $path;
  27. global $root;
  28. parent::__construct($db);
  29. $this->view = $path["view"] . "catalog.php";
  30. $this->parse_filters();
  31. $s_mon = $this->build_query();
  32. $q_mon = $this->db->query($s_mon);
  33. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  34. array_push($this->monsters, new K_Monster($db, $r_mon["id"]));
  35. }
  36. $this->title = "Catalog - SWDB";
  37. $this->description = "Catalog of all monsters";
  38. $this->canonical = $root . "catalog/";
  39. }
  40. private function parse_filters(){
  41. if (isset($_GET["element"])){
  42. $f_element = strtoupper($_GET["element"]);
  43. if ($f_element == "FIRE" || $f_element == "WATER" || $f_element == "WIND" || $f_element == "LIGHT" || $f_element == "DARK"){
  44. $this->filters["element"] = $_GET["element"];
  45. }
  46. }
  47. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  48. $this->filters["name"] = SQLite3::escapeString($_GET["name"]);
  49. }
  50. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 6){
  51. $this->filters["min_stars"] = $_GET["min_stars"];
  52. }
  53. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 6){
  54. $this->filters["max_stars"] = $_GET["max_stars"];
  55. }
  56. return;
  57. }
  58. private function build_query(){
  59. $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"];
  60. if (strlen($this->filters["element"]) > 0){
  61. $s = $s . " AND upper(element) = '" . strtoupper($this->filters["element"]) . "' ";
  62. }
  63. if ($this->filters["name"] != ""){
  64. $s = $s . " AND monster IN (SELECT id FROM k_monster WHERE upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%') ";
  65. }
  66. $s = $s . " ORDER BY element, natural_stars;";
  67. return $s;
  68. }
  69. }
  70. ?>