* @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(){ parent::__construct(); $this->set_public(true); $this->set_view("enchantments.php"); $this->set_title("Enchantments"); $this->set_description(get_context()->get_player()->get_name() . "'s enchantments"); $this->set_canonical("player/" . get_context()->get_player()->get_id() . "/enchantments/"); $this->add_css("enchantments.css"); $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->set_code(200); $this->set_message("OK"); } /** * 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; } } }