Enchantments_Page.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Enchantment list page file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(PATH::PAGE . "Page.php");
  12. require_once(PATH::ENTITY . "Enchantment.php");
  13. /**
  14. * Enchantment list page model.
  15. *
  16. * @category Page
  17. */
  18. class Enchantments_Page extends Page{
  19. /**
  20. * @var Enchantment[] Selected enchantment gems.
  21. */
  22. private $gems = [];
  23. /**
  24. * @var Enchantment[] Selected enchantment grindstones.
  25. */
  26. private $grindstones = [];
  27. /**
  28. * @var Enchantment[] Selected enchantments.
  29. */
  30. private $enchantments = [];
  31. /**
  32. *
  33. * @var mixed[] Page filters.
  34. */
  35. private $filters = [
  36. "TYPE" => [],
  37. "RUNE" => [],
  38. "STAT" => [],
  39. "MIN_QUALITY" => QUALITY_ID::NORMAL,
  40. "MAX_QUALITY" => QUALITY_ID::LEGEND,
  41. ];
  42. /**
  43. * Constructor.
  44. *
  45. * Retrieves the data and initializes the variables.
  46. */
  47. public function __construct(){
  48. $this->view = PATH::VIEW . "enchantments.php";
  49. $this->parse_filters();
  50. $result_set = $this->prepare_statement()->execute();
  51. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  52. $enchantment = new Enchantment($result["id"]);
  53. array_push($this->enchantments, $enchantment);
  54. if ($enchantment->is_gem()){
  55. array_push($this->gems, $enchantment);
  56. }
  57. else{
  58. array_push($this->grindstones, $enchantment);
  59. }
  60. }
  61. $this->title = "Enchantments - SWDB";
  62. $this->description = "Enchantment inventory";
  63. $this->canonical = URL::BASE . "enchantments/";
  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["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
  71. $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
  72. }
  73. if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
  74. $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
  75. }
  76. if (isset($_GET["stat"])){
  77. $stats = $_GET["stat"];
  78. foreach ($stats as $stat){
  79. array_push($this->filters["STAT"], intval($stat));
  80. }
  81. }
  82. if (isset($_GET["rune"])){
  83. $runes = $_GET["rune"];
  84. foreach ($runes as $rune){
  85. array_push($this->filters["RUNE"], intval($rune));
  86. }
  87. }
  88. if (isset($_GET["type"])){
  89. $types = $_GET["type"];
  90. foreach ($types as $type){
  91. array_push($this->filters["TYPE"], $type);
  92. }
  93. }
  94. }
  95. /**
  96. * Prepares the statement to execute against the database using the filter values.
  97. *
  98. * @return SQLite3Stmt prepared statement.
  99. */
  100. private function prepare_statement(){
  101. // Common items
  102. $query = "
  103. SELECT id
  104. FROM enchantment
  105. WHERE
  106. player = :player AND
  107. quality BETWEEN :min_quality AND :max_quality
  108. ";
  109. // Some main stat selected.
  110. if (count($this->filters["STAT"]) > 0){
  111. $query .= " AND stat IN (";
  112. for ($i = 0; $i < sizeof($this->filters["STAT"]); $i ++){
  113. $query .= ":stat_$i, ";
  114. }
  115. $query = rtrim($query, ", ") . ") ";
  116. }
  117. // Some sets selected.
  118. if (count($this->filters["RUNE"]) > 0){
  119. $query .= " AND (rune IN (";
  120. for ($i = 0; $i < sizeof($this->filters["RUNE"]); $i ++){
  121. $query .= ":rune_$i, ";
  122. }
  123. $query = rtrim($query, ", ");
  124. $query .= ") OR type IN (:inmemorial_grindstone, :inmemorial_gem))"; // Inmemorials
  125. }
  126. // Only one type selected.
  127. if (count($this->filters["TYPE"]) == 1){
  128. // Only one selectd, it's either gems or grindstones
  129. if (in_array("GEM", $this->filters["TYPE"])){
  130. $query .= " AND type IN (:type_gem, :type_inmemorial_gem) "; // Gems.
  131. }
  132. else{
  133. $query .= " AND type IN (:type_grindstone, :type_inmemorial_grindstone) "; // Grinds.
  134. }
  135. }
  136. $query .= "
  137. ORDER BY
  138. type = :type_inmemorial_grindstone DESC, -- Inmemorial Grindstone
  139. type = :type_inmemorial_gem DESC, -- Inmemorial Gem
  140. rune,
  141. type,
  142. quality DESC,
  143. stat
  144. ";
  145. $statement = get_context()->get_db()->prepare($query);
  146. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  147. $statement->bindValue(":min_quality", $this->filters["MIN_QUALITY"], SQLITE3_INTEGER);
  148. $statement->bindValue(":max_quality", $this->filters["MAX_QUALITY"], SQLITE3_INTEGER);
  149. for ($i = 0; $i < sizeof($this->filters["STAT"]); $i ++){
  150. $statement->bindValue(":stat_$i", $this->filters["STAT"][$i], SQLITE3_INTEGER);
  151. }
  152. for ($i = 0; $i < sizeof($this->filters["RUNE"]); $i ++){
  153. $statement->bindValue(":rune_$i", $this->filters["RUNE"][$i], SQLITE3_INTEGER);
  154. }
  155. $statement->bindValue(":inmemorial_gem", ENCHANTMENT_TYPE_ID::INMEMORIAL_GEM, SQLITE3_INTEGER);
  156. $statement->bindValue(":inmemorial_grindstone", ENCHANTMENT_TYPE_ID::INMEMORIAL_GRINDSTONE, SQLITE3_INTEGER);
  157. $statement->bindValue(":type_gem", ENCHANTMENT_TYPE_ID::GEM, SQLITE3_INTEGER);
  158. $statement->bindValue(":type_grindstone", ENCHANTMENT_TYPE_ID::GRINDSTONE, SQLITE3_INTEGER);
  159. $statement->bindValue(":type_inmemorial_gem", ENCHANTMENT_TYPE_ID::INMEMORIAL_GEM, SQLITE3_INTEGER);
  160. $statement->bindValue(":type_inmemorial_grindstone", ENCHANTMENT_TYPE_ID::INMEMORIAL_GRINDSTONE, SQLITE3_INTEGER);
  161. return $statement;
  162. }
  163. /**
  164. * Retrieves the gems to show.
  165. *
  166. * @return Enchantment[] Selected gems.
  167. */
  168. public function get_gems(){
  169. return $this->gems;
  170. }
  171. /**
  172. * Retrieves the grindstones to show.
  173. *
  174. * @return Enchantment[] Selected grindstones.
  175. */
  176. public function get_grindstones(){
  177. return $this->grindstones;
  178. }
  179. /**
  180. * Retrieves the enchantments to show, both gems and grindstones.
  181. *
  182. * @return Enchantment[] Selected enchantments.
  183. */
  184. public function get_enchantments(){
  185. return $this->enchantments;
  186. }
  187. /**
  188. * Retrieves the page filters.
  189. *
  190. * @return mixed[] Page filtes
  191. */
  192. public function get_filters(){
  193. return $this->filters;
  194. }
  195. /**
  196. * Retrieves one filter.
  197. *
  198. * @param string $key
  199. * @return mixed Filter value, null if if doesn't exist.
  200. */
  201. public function get_filter($key){
  202. if (array_key_exists($key, $this->filters)){
  203. return $this->filters[$key];
  204. }
  205. else{
  206. return null;
  207. }
  208. }
  209. }