| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- /**
- * Monster 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 . "Monster.php");
- /**
- * Monster list page model.
- *
- * @category Page
- */
- class Monsters_Page extends Page{
- /**
- * @var Monster[] List of monsters to show.
- */
- private $monsters = [];
- /**
- * @var mixed[] Default filter values.
- */
- private $filters = [
- "ELEMENT" => -1,
- "NAME" => "",
- "MIN_STARS" => 1,
- "MAX_STARS" => 6,
- ];
- /**
- * Constructor.
- *
- * Retrieves the data and initializes the variables.
- */
- public function __construct(){
- parent::__construct();
- $this->set_public(true);
- $this->set_view("monsters.php");
- $this->set_title("Monsters");
- $this->set_description("Catalog of all monsters");
- $this->set_canonical("monsters/");
- $this->add_css("monsters.css");
-
- $this->parse_filters();
- $result_set = $this->prepare_statement()->execute();
- while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
- array_push($this->monsters, new Monster($result["id"], false));
- }
- $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["element"]) && APPLICATION::valid_id("ELEMENT_ID", strtoupper($_GET["element"]))){
- $this->filters["ELEMENT"] = $_GET["element"];
- }
- if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
- $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
- }
- if (isset($_GET["min_stars"]) && intval($_GET["min_stars"]) > 0 && intval($_GET["min_stars"]) <= 5){
- $this->filters["MIN_STARS"] = $_GET["min_stars"];
- }
- if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) <= 5){
- $this->filters["MAX_STARS"] = $_GET["max_stars"];
- }
- return;
- }
- /**
- * 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 monster
- WHERE
- obtainable = 1 AND
- awakens_from IS NULL AND
- natural_stars BETWEEN :min_stars AND :max_stars
- ";
- if(APPLICATION::valid_id("ELEMENT_ID", $this->filters["ELEMENT"])){
- $query .= " AND element = :element ";
- }
- if ($this->filters["NAME"] != ""){
- $query .= " AND upper(name) LIKE upper(:name) " ;
- }
- $query .= "
- ORDER BY
- element = :fire DESC,
- element = :water DESC,
- element = :wind DESC,
- element = :light DESC,
- element = :dark DESC,
- natural_stars;
- ";
- $statement = get_context()->get_db()->prepare($query);
- $statement->bindValue(":min_stars", $this->filters["MIN_STARS"], SQLITE3_INTEGER);
- $statement->bindValue(":max_stars", $this->filters["MAX_STARS"], SQLITE3_INTEGER);
- $statement->bindValue(":element", $this->filters["ELEMENT"], SQLITE3_INTEGER);
- $statement->bindValue(":name", "%" . $this->filters["NAME"] . "%", SQLITE3_TEXT);
- $statement->bindValue(":fire", ELEMENT_ID::FIRE, SQLITE3_INTEGER);
- $statement->bindValue(":water", ELEMENT_ID::WATER, SQLITE3_INTEGER);
- $statement->bindValue(":wind", ELEMENT_ID::WIND, SQLITE3_INTEGER);
- $statement->bindValue(":light", ELEMENT_ID::LIGHT, SQLITE3_INTEGER);
- $statement->bindValue(":dark", ELEMENT_ID::DARK, SQLITE3_INTEGER);
- return $statement;
- }
- /**
- * Retrieves the monsters to display
- *
- * @return Monster[] Selected monsters.
- */
- public function get_monsters(){
- return $this->monsters;
- }
-
- /**
- * 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;
- }
- }
- }
- ?>
|