Units_Page.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Unit 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. require_once(PATH::ENTITY . "Effect.php");
  15. /**
  16. * Unit list page model.
  17. *
  18. * @category Page
  19. */
  20. class Units_Page extends Page{
  21. /**
  22. * @var \Unit[] Units to show.
  23. */
  24. public $units = [];
  25. /**
  26. * @var mixed[] Default filter values.
  27. */
  28. public $filters = [
  29. "ELEMENT" => -1,
  30. "NAME" => "",
  31. "MIN_STARS" => 1,
  32. "MAX_STARS" => 6,
  33. "IN_STORAGE" => false,
  34. "WITHOUT_RUNES" => false,
  35. "EFFECT" => [],
  36. "EFFECT_OPTION" => "any"
  37. ];
  38. /**
  39. * @var Effect[] All availabe skill effects, for the tfilter selector.
  40. */
  41. public $effects = [];
  42. /**
  43. * Constructor.
  44. *
  45. * Retrieves the data and initializes the variables.
  46. */
  47. public function __construct(){
  48. $this->view = PATH::VIEW . "units.php";
  49. $this->parse_filters();
  50. $s = $this->build_query();
  51. $q = get_context()->get_db()->query($s);
  52. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  53. array_push($this->units, new Unit($r["id"]));
  54. }
  55. // Get skill effects
  56. $s_eff = "SELECT id FROM effect ORDER BY is_buff DESC";
  57. $q_eff = get_context()->get_db()->query($s_eff);
  58. while ($r_eff = $q_eff->fetchArray(SQLITE3_ASSOC)){
  59. array_push($this->effects, new Effect($r_eff["id"]));
  60. }
  61. $this->title = "Units - SWDB";
  62. $this->description = "Owned units";
  63. $this->canonical = URL::BASE . "units/";
  64. }
  65. /**
  66. * Parses the request looking for the selected filters, validates them
  67. * and adds them to the $filters array.
  68. */
  69. private function parse_filters(){
  70. if (isset($_GET["element"]) && APPLICATION::valid_id("ELEMENT_ID", strtoupper($_GET["element"]))){
  71. $this->filters["ELEMENT"] = $_GET["element"];
  72. }
  73. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  74. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  75. }
  76. if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 6){
  77. $this->filters["MIN_STARS"] = $_GET["min_stars"];
  78. }
  79. if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 6){
  80. $this->filters["MAX_STARS"] = $_GET["max_stars"];
  81. }
  82. if (isset($_GET["in_storage"]) && $_GET["in_storage"] == "on"){
  83. $this->filters["IN_STORAGE"] = true;
  84. }
  85. else{
  86. $this->filters["IN_STORAGE"] = false;
  87. }
  88. if (isset($_GET["without_runes"]) && $_GET["without_runes"] == "on"){
  89. $this->filters["WITHOUT_RUNES"] = true;
  90. }
  91. if (isset($_GET["effect"])){
  92. $effects = $_GET["effect"];
  93. foreach ($effects as $effect){
  94. if (intval($effect) > 0){
  95. array_push($this->filters["EFFECT"], intval($effect));
  96. }
  97. }
  98. }
  99. if (isset($_GET["effect_option"]) && ($_GET["effect_option"] == "and" || $_GET["effect_option"] == "or")){
  100. $this->filters["EFFECT_OPTION"] = $_GET["effect_option"];
  101. }
  102. return;
  103. }
  104. /**
  105. * Builds the query to the rune table using the selected or default
  106. * filters.
  107. *
  108. * @return string The query to be executed.
  109. */
  110. private function build_query(){
  111. $s = "
  112. SELECT unit.id AS id
  113. FROM
  114. unit,
  115. monster
  116. WHERE
  117. unit.player = '" . get_context()->get_player()->get_id() . "' AND
  118. unit.monster = monster.id AND
  119. stars >= " . $this->filters["MIN_STARS"] . " AND
  120. stars <= " . $this->filters["MAX_STARS"];
  121. if (APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
  122. $s = $s . " AND monster IN (SELECT id FROM monster WHERE element = " . $this->filters["ELEMENT"] . ") ";
  123. }
  124. if ($this->filters["NAME"] != ""){
  125. $s = $s . " AND monster IN (SELECT id FROM monster WHERE upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%') ";
  126. }
  127. if (!$this->filters["IN_STORAGE"]){
  128. $s = $s . " AND building NOT IN (SELECT id FROM building WHERE building = " . BUILDING_ID::MONSTER_STORAGE . ") ";
  129. }
  130. if (!$this->filters["WITHOUT_RUNES"]){
  131. $s = $s . " AND unit.id IN (SELECT DISTINCT unit FROM unit_rune) ";
  132. }
  133. if (sizeof($this->filters["EFFECT"]) > 0){
  134. if ($this->filters["EFFECT_OPTION"] == "and"){
  135. foreach ($this->filters["EFFECT"] as $effect){
  136. $s .= "
  137. AND monster.id IN (
  138. SELECT DISTINCT unit
  139. FROM
  140. monster_skill,
  141. skill_effect
  142. WHERE
  143. monster_skill.skill = skill_effect.skill AND
  144. skill_effect.effect = $effect
  145. )
  146. ";
  147. }
  148. }
  149. elseif ($this->filters["EFFECT_OPTION"] == "or"){
  150. $s .= " AND ( ";
  151. foreach ($this->filters["EFFECT"] as $effect){
  152. $s .= "
  153. monster.id IN (
  154. SELECT DISTINCT unit
  155. FROM
  156. monster_skill,
  157. skill_effect
  158. WHERE
  159. monster_skill.skill = skill_effect.skill AND
  160. skill_effect.effect = $effect
  161. ) OR
  162. ";
  163. }
  164. $s .= " 1 = 2 ) ";
  165. }
  166. }
  167. $s = $s . "
  168. ORDER BY
  169. stars DESC,
  170. level DESC,
  171. element = " . ELEMENT_ID::FIRE ." DESC,
  172. element = " . ELEMENT_ID::WATER ." DESC,
  173. element = " . ELEMENT_ID::WIND ." DESC,
  174. element = " . ELEMENT_ID::LIGHT ." DESC,
  175. element = " . ELEMENT_ID::DARK ." DESC
  176. ";
  177. return $s;
  178. }
  179. }
  180. ?>