Catalog_Page.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. $f_element = strtoupper($_GET["element"]);
  42. if ($f_element == "FIRE" || $f_element == "WATER" || $f_element == "WIND" || $f_element == "LIGHT" || $f_element == "DARK"){
  43. $this->filters["element"] = $_GET["element"];
  44. }
  45. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  46. $this->filters["name"] = SQLite3::escapeString($_GET["name"]);
  47. }
  48. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) < 6){
  49. $this->filters["min_stars"] = $_GET["min_stars"];
  50. }
  51. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 6){
  52. $this->filters["max_stars"] = $_GET["max_stars"];
  53. }
  54. return;
  55. }
  56. private function build_query(){
  57. $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"];
  58. if (strlen($this->filters["element"]) > 0){
  59. $s = $s . " AND upper(element) = '" . strtoupper($this->filters["element"]) . "' ";
  60. }
  61. if ($this->filters["name"] != ""){
  62. $s = $s . " AND monster IN (SELECT id FROM k_monster WHERE upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%') ";
  63. }
  64. $s = $s . " ORDER BY element, natural_stars;";
  65. return $s;
  66. }
  67. }
  68. ?>