Monsters_Page.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Monster list page file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @category Page
  8. */
  9. /**
  10. * Require dependent files if not present.
  11. */
  12. require_once(PATH::PAGE . "Page.php");
  13. require_once(PATH::ENTITY . "Unit.php");
  14. /**
  15. * Monster list page model.
  16. *
  17. * @category Page
  18. */
  19. class Monsters_Page extends Page{
  20. /**
  21. * @var \Unit[] Units to show.
  22. */
  23. public $monsters = [];
  24. /**
  25. * Constructor.
  26. *
  27. * Retrieves the data and initializes the variables.
  28. *
  29. * @global resource Database connection.
  30. */
  31. public function __construct(){
  32. global $db;
  33. $this->view = PATH::VIEW . "monsters.php";
  34. $this->parse_filters();
  35. $s_mon = $this->build_query();
  36. $q_mon = $db->query($s_mon);
  37. while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
  38. array_push($this->monsters, new Unit($r_mon["id"]));
  39. }
  40. $this->title = "Monsters - SWDB";
  41. $this->description = "Owned monsters";
  42. $this->canonical = URL::BASE . "monsters/";
  43. }
  44. /**
  45. * Parses the request looking for the selected filters, validates them
  46. * and adds them to the $filters array.
  47. */
  48. private function parse_filters(){
  49. $this->filters = FILTER::MONSTER;
  50. if (isset($_GET["element"]) && APPLICATION::valid_id("ELEMENT_ID", strtoupper($_GET["element"]))){
  51. $this->filters["ELEMENT"] = $_GET["element"];
  52. }
  53. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  54. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  55. }
  56. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 6){
  57. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  58. }
  59. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 6){
  60. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  61. }
  62. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  63. $this->filters["IN_STORAGE"] = true;
  64. }
  65. else{
  66. $this->filters["IN_STORAGE"] = false;
  67. }
  68. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  69. $this->filters["WITHOUT_RUNES"] = true;
  70. }
  71. return;
  72. }
  73. /**
  74. * Builds the query to the rune table using the selected or default
  75. * filters.
  76. *
  77. * @return string The query to be executed.
  78. * @global int Player ID.
  79. * @global resource Database connection.
  80. */
  81. private function build_query(){
  82. global $UID;
  83. global $db;
  84. $s = "
  85. SELECT unit.id AS id
  86. FROM
  87. unit,
  88. k_unit
  89. WHERE
  90. unit.uid = '$UID' AND
  91. unit.unit = k_unit.id AND
  92. stars >= " . $this->filters["MIN_STARS"] . " AND
  93. stars <= " . $this->filters["MAX_STARS"];
  94. if (APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
  95. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE element = " . $this->filters["ELEMENT"] . ") ";
  96. }
  97. if ($this->filters["NAME"] != ""){
  98. $s = $s . " AND unit IN (SELECT id FROM k_unit WHERE upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%') ";
  99. }
  100. if (!$this->filters["IN_STORAGE"]){
  101. $s = $s . " AND building NOT IN (SELECT id FROM building WHERE building = " . BUILDING_ID::MONSTER_STORAGE . ") ";
  102. }
  103. if (!$this->filters["WITHOUT_RUNES"]){
  104. $s = $s . " AND unit.id IN (SELECT DISTINCT assigned_to FROM rune) ";
  105. }
  106. $s = $s . "
  107. ORDER BY
  108. stars DESC,
  109. level DESC,
  110. element = " . ELEMENT_ID::FIRE ." DESC,
  111. element = " . ELEMENT_ID::WATER ." DESC,
  112. element = " . ELEMENT_ID::WIND ." DESC,
  113. element = " . ELEMENT_ID::LIGHT ." DESC,
  114. element = " . ELEMENT_ID::DARK ." DESC
  115. ";
  116. return $s;
  117. }
  118. }
  119. ?>