| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- /**
- * Enchantment list page file.
- *
- * Provides a class with all the properties and methods to display the page.
- *
- * @author Iñigo Valentin <i@inigovalentin.com>
- * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
- * @package SWDB
- */
- require_once(PATH::PAGE . "Page.php");
- require_once(PATH::ENTITY . "Enchantment.php");
- /**
- * Enchantment list page model.
- *
- * @category Page
- */
- class Enchantments_Page extends Page{
- /**
- * @var Enchantment[] Selected enchantment gems.
- */
- private $gems = [];
- /**
- * @var Enchantment[] Selected enchantment grindstones.
- */
- private $grindstones = [];
- /**
- * @var Enchantment[] Selected enchantments.
- */
- private $enchantments = [];
- /**
- *
- * @var mixed[] Page filters.
- */
- private $filters = [
- "TYPE" => [],
- "RUNE" => [],
- "STAT" => [],
- "MIN_QUALITY" => QUALITY_ID::NORMAL,
- "MAX_QUALITY" => QUALITY_ID::LEGEND,
- ];
- /**
- * Constructor.
- *
- * Retrieves the data and initializes the variables.
- */
- public function __construct(){
- $this->view = PATH::VIEW . "enchantments.php";
- $this->parse_filters();
- $result_set = $this->prepare_statement()->execute();
- while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
- $enchantment = new Enchantment($result["id"]);
- array_push($this->enchantments, $enchantment);
- if ($enchantment->is_gem()){
- array_push($this->gems, $enchantment);
- }
- else{
- array_push($this->grindstones, $enchantment);
- }
- }
- $this->title = "Enchantments - SWDB";
- $this->description = "Enchantment inventory";
- $this->canonical = URL::BASE . "enchantments/";
- }
- /**
- * Parses the request looking for the selected filters, validates them
- * and adds them to the $filters array.
- */
- private function parse_filters(){
- if (isset($_GET["min_quality"]) && intval($_GET["min_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["min_quality"]) <= QUALITY_ID::LEGEND){
- $this->filters["MIN_QUALITY"] = $_GET["min_quality"];
- }
- if (isset($_GET["max_quality"]) && intval($_GET["max_quality"]) >= QUALITY_ID::NORMAL && intval($_GET["max_quality"]) <= QUALITY_ID::LEGEND){
- $this->filters["MAX_QUALITY"] = $_GET["max_quality"];
- }
- if (isset($_GET["stat"])){
- $stats = $_GET["stat"];
- foreach ($stats as $stat){
- array_push($this->filters["STAT"], intval($stat));
- }
- }
- if (isset($_GET["rune"])){
- $runes = $_GET["rune"];
- foreach ($runes as $rune){
- array_push($this->filters["RUNE"], intval($rune));
- }
- }
- if (isset($_GET["type"])){
- $types = $_GET["type"];
- foreach ($types as $type){
- array_push($this->filters["TYPE"], $type);
- }
- }
- }
- /**
- * Prepares the statement to execute against the database using the filter values.
- *
- * @return SQLite3Stmt prepared statement.
- */
- private function prepare_statement(){
- // Common items
- $query = "
- SELECT id
- FROM enchantment
- WHERE
- player = :player AND
- quality BETWEEN :min_quality AND :max_quality
- ";
-
- // Some main stat selected.
- if (count($this->filters["STAT"]) > 0){
- $query .= " AND stat IN (";
- for ($i = 0; $i < sizeof($this->filters["STAT"]); $i ++){
- $query .= ":stat_$i, ";
- }
- $query = rtrim($query, ", ") . ") ";
- }
-
- // Some sets selected.
- if (count($this->filters["RUNE"]) > 0){
- $query .= " AND (rune IN (";
- for ($i = 0; $i < sizeof($this->filters["RUNE"]); $i ++){
- $query .= ":rune_$i, ";
- }
- $query = rtrim($query, ", ");
- $query .= ") OR type IN (:inmemorial_grindstone, :inmemorial_gem))"; // Inmemorials
- }
-
- // Only one type selected.
- if (count($this->filters["TYPE"]) == 1){
- // Only one selectd, it's either gems or grindstones
- if (in_array("GEM", $this->filters["TYPE"])){
- $query .= " AND type IN (:type_gem, :type_inmemorial_gem) "; // Gems.
- }
- else{
- $query .= " AND type IN (:type_grindstone, :type_inmemorial_grindstone) "; // Grinds.
- }
- }
- $query .= "
- ORDER BY
- type = :type_inmemorial_grindstone DESC, -- Inmemorial Grindstone
- type = :type_inmemorial_gem DESC, -- Inmemorial Gem
- rune,
- type,
- quality DESC,
- stat
- ";
- $statement = get_context()->get_db()->prepare($query);
- $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
- $statement->bindValue(":min_quality", $this->filters["MIN_QUALITY"], SQLITE3_INTEGER);
- $statement->bindValue(":max_quality", $this->filters["MAX_QUALITY"], SQLITE3_INTEGER);
- for ($i = 0; $i < sizeof($this->filters["STAT"]); $i ++){
- $statement->bindValue(":stat_$i", $this->filters["STAT"][$i], SQLITE3_INTEGER);
- }
- for ($i = 0; $i < sizeof($this->filters["RUNE"]); $i ++){
- $statement->bindValue(":rune_$i", $this->filters["RUNE"][$i], SQLITE3_INTEGER);
- }
- $statement->bindValue(":inmemorial_gem", ENCHANTMENT_TYPE_ID::INMEMORIAL_GEM, SQLITE3_INTEGER);
- $statement->bindValue(":inmemorial_grindstone", ENCHANTMENT_TYPE_ID::INMEMORIAL_GRINDSTONE, SQLITE3_INTEGER);
- $statement->bindValue(":type_gem", ENCHANTMENT_TYPE_ID::GEM, SQLITE3_INTEGER);
- $statement->bindValue(":type_grindstone", ENCHANTMENT_TYPE_ID::GRINDSTONE, SQLITE3_INTEGER);
- $statement->bindValue(":type_inmemorial_gem", ENCHANTMENT_TYPE_ID::INMEMORIAL_GEM, SQLITE3_INTEGER);
- $statement->bindValue(":type_inmemorial_grindstone", ENCHANTMENT_TYPE_ID::INMEMORIAL_GRINDSTONE, SQLITE3_INTEGER);
- return $statement;
- }
- /**
- * Retrieves the gems to show.
- *
- * @return Enchantment[] Selected gems.
- */
- public function get_gems(){
- return $this->gems;
- }
- /**
- * Retrieves the grindstones to show.
- *
- * @return Enchantment[] Selected grindstones.
- */
- public function get_grindstones(){
- return $this->grindstones;
- }
-
- /**
- * Retrieves the enchantments to show, both gems and grindstones.
- *
- * @return Enchantment[] Selected enchantments.
- */
- public function get_enchantments(){
- return $this->enchantments;
- }
- /**
- * Retrieves the page filters.
- *
- * @return mixed[] Page filtes
- */
- public function get_filters(){
- return $this->filters;
- }
- /**
- * Retrieves one filter.
- *
- * @param string $key
- * @return mixed Filter value, null if if doesn't exist.
- */
- public function get_filter($key){
- if (array_key_exists($key, $this->filters)){
- return $this->filters[$key];
- }
- else{
- return null;
- }
- }
- }
|