Monsters_Page.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. require_once($path["page"] . "Page.php");
  3. require_once($path["entity"] . "Monster.php");
  4. /**
  5. * Monster list page.
  6. */
  7. class Monsters_Page extends Page{
  8. /**
  9. * Array of @{see Monsters}s.
  10. */
  11. public $monsters = [];
  12. public $filters = [
  13. "element" => "",
  14. "name" => "",
  15. "min_stars" => 1,
  16. "max_stars" => 6,
  17. "in_storage" => true,
  18. "without_runes" => true
  19. ];
  20. /**
  21. * Constructor.
  22. *
  23. * Retrieves the data and initializes the variables.
  24. *
  25. * @param SQLite3 $db Connection to the database.
  26. */
  27. public function __construct($db){
  28. global $path;
  29. global $root;
  30. parent::__construct($db);
  31. $this->view = $path["view"] . "monsters.php";
  32. $this->parse_filters();
  33. $s_mon = $this->build_query();
  34. $q_mon = $this->db->query($s_mon);
  35. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  36. array_push($this->monsters, new Monster($db, $r_mon["id"]));
  37. }
  38. $this->title = "Monsters - SWDB";
  39. $this->description = "Owned monsters";
  40. $this->canonical = $root . "monsters/";
  41. }
  42. private function parse_filters(){
  43. $f_element = strtoupper($_GET["element"]);
  44. if ($f_element == "FIRE" || $f_element == "WATER" || $f_element == "WIND" || $f_element == "LIGHT" || $f_element == "DARK"){
  45. $this->filters["element"] = $_GET["element"];
  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"]) < 7){
  51. $this->filters["min_stars"] = $_GET["min_stars"];
  52. }
  53. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 7){
  54. $this->filters["max_stars"] = $_GET["max_stars"];
  55. }
  56. if (isset($_GET["in_storage"]) && $_GET["in_storage"] != "on"){
  57. $this->filters["in_storage"] = false;
  58. }
  59. if (isset($_GET["without_runes"]) && $_GET["without_runes"] != "on"){
  60. $this->filters["without_runes"] = false;
  61. }
  62. return;
  63. }
  64. private function build_query(){
  65. $s = "SELECT monster.id AS id FROM monster, k_monster WHERE monster.monster = k_monster.id AND stars >= " . $this->filters["min_stars"] . " AND stars <= " . $this->filters["max_stars"];
  66. if (strlen($this->filters["element"]) > 0){
  67. $s = $s . " AND monster IN (SELECT id FROM k_monster WHERE upper(element) = '" . strtoupper($this->filters["element"]) . "') ";
  68. }
  69. if ($this->filters["name"] != ""){
  70. $s = $s . " AND monster IN (SELECT id FROM k_monster WHERE upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%') ";
  71. }
  72. if (!$this->filters["in_storage"]){
  73. $s = $s . " AND in_storage = 0 ";
  74. }
  75. if (!$this->filters["without_runes"]){
  76. $s = $s . " AND id IN (SELECT DISTINCT monster FROM monster_rune) ";
  77. }
  78. $s = $s . " ORDER BY stars DESC, level DESC, element;";
  79. return $s;
  80. }
  81. }
  82. ?>