| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- require_once($path["page"] . "Page.php");
- require_once($path["entity"] . "K_Monster.php");
- /**
- * Catalog page model.
- */
- class Catalog_Page extends Page{
- /**
- * Array of arrays of monsters, grouped by attribute id.
- */
- public $monsters = [];
-
- public $filters = [
- "element" => "",
- "name" => "",
- "min_stars" => 1,
- "max_stars" => 6,
- ];
- /**
- * Constructor.
- *
- * Retrieves the data and initializes the variables.
- *
- * @param SQLite3 $db Connection to the database.
- */
- public function __construct($db){
- global $path;
- global $root;
- parent::__construct($db);
- $this->view = $path["view"] . "catalog.php";
- $this->parse_filters();
- $s_mon = $this->build_query();
- $q_mon = $this->db->query($s_mon);
- while ($r_mon = $q_mon->fetchArray(SQLITE3_ASSOC)){
- array_push($this->monsters, new K_Monster($db, $r_mon["id"]));
- }
- $this->title = "Catalog - SWDB";
- $this->description = "Catalog of all monsters";
- $this->canonical = $root . "catalog/";
- }
-
- private function parse_filters(){
- if (isset($_GET["element"])){
- $f_element = strtoupper($_GET["element"]);
- if ($f_element == "FIRE" || $f_element == "WATER" || $f_element == "WIND" || $f_element == "LIGHT" || $f_element == "DARK"){
- $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"]) < 6){
- $this->filters["min_stars"] = $_GET["min_stars"];
- }
- if (isset($_GET["max_stars"]) && intval($_GET["max_stars"]) > 0 && intval($_GET["max_stars"]) < 6){
- $this->filters["max_stars"] = $_GET["max_stars"];
- }
- return;
- }
-
- private function build_query(){
- $s = "SELECT id FROM k_monster WHERE obtainable = 1 AND awakens_from IS NULL AND natural_stars >= " . $this->filters["min_stars"] . " AND natural_stars <= " . $this->filters["max_stars"];
- if (strlen($this->filters["element"]) > 0){
- $s = $s . " AND upper(element) = '" . strtoupper($this->filters["element"]) . "' ";
- }
- if ($this->filters["name"] != ""){
- $s = $s . " AND monster IN (SELECT id FROM k_monster WHERE upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%') ";
- }
- $s = $s . " ORDER BY element, natural_stars;";
- return $s;
- }
- }
- ?>
|