* @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 . "Run.php"); /** * Run list page model. * * @category Page */ class Runs_Page extends Page{ /** * @var Run[] Runs to show. */ private $runs = []; /** * @var mixed[] Page filters */ private $filters = [ "AREA_TYPE" => 0, "AREA" => 0, "STAGE" => 0, "DIFFICULTY" => 0, "DATE_MIN" => null, "DATE_MAX" => null, "DEFEAT" => false, "HELPER" => false, "NUMBER" => 20, ]; /** * Constructor. * * Retrieves the data and initializes the variables. */ public function __construct(){ parent::__construct(); $this->set_public(false); $this->set_view("runs.php"); $this->set_title("Runs"); $this->set_description(get_context()->get_player()->get_name() . "'s run logs"); $this->set_canonical("player/" . get_context()->get_player()->get_id() . "/runs/"); $this->add_css("runs.css"); $this->parse_filters(); $result_set = $this->prepare_statement()->execute(); while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){ array_push($this->runs, new Run($result["id"])); } $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["area_type"]) && intval($_GET["area_type"]) > 0){ $this->filters["AREA_TYPE"] = $_GET["area_type"]; } if (isset($_GET["area"]) && intval($_GET["area"]) > 0){ $this->filters["AREA"] = $_GET["area"]; } if (isset($_GET["stage"]) && intval($_GET["stage"]) > 0){ $this->filters["STAGE"] = $_GET["stage"]; } if (isset($_GET["difficulty"]) && intval($_GET["difficulty"]) >= DIFFICULTY_ID::NORMAL && intval($_GET["difficulty"]) <= DIFFICULTY_ID::HELL){ $this->filters["DIFFICULTY"] = $_GET["difficulty"]; } if (isset($_GET["date_min"]) && strlen($_GET["date_min"]) > 0){ $this->filters["DATE_MIN"] = DateTime::createFromFormat('Y-m-d', $_GET["date_min"])->format('Y-m-d'); } if (isset($_GET["date_max"]) && strlen($_GET["date_max"]) > 0){ $this->filters["DATE_MAX"] = DateTime::createFromFormat('Y-m-d', $_GET["date_max"])->format('Y-m-d'); } if (isset($_GET["defeat"]) && $_GET["defeat"] == "on"){ $this->filters["DEFEAT"] = true; } else{ $this->filters["HELPER"] = false; } if (isset($_GET["helper"]) && $_GET["helper"] == "on"){ $this->filters["HELPER"] = true; } else{ $this->filters["HELPER"] = false; } if (isset($_GET["number"]) && intval($_GET["number"]) > 0){ $this->filters["NUMBER"] = $_GET["number"]; } 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 run WHERE player = :player "; if ($this->filters["AREA_TYPE"] > 0){ $query .= " AND area_type = :area_type "; } if ($this->filters["AREA"] > 0){ $query .= " AND area = :area "; } if ($this->filters["STAGE"] > 0){ $query .= " AND stage = :stage "; } if ($this->filters["DIFFICULTY"] > 0){ $query .= " AND difficulty = :difficulty "; } if (! $this->filters["DEFEAT"]){ $query .= " AND win = 1 "; } if (! $this->filters["HELPER"]){ $query .= " AND helper = 0 "; } if ($this->filters["DATE_MIN"] != null){ $query .= " AND date(dtime) >= date(:date_min) "; } if ($this->filters["DATE_MAX"] != null){ $query .= " AND date(dtime) >= date(:date_max) "; } $query .= " ORDER BY dtime DESC LIMIT :limit;"; $statement = get_context()->get_db()->prepare($query); $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT); $statement->bindValue(":area_type", $this->filters["AREA_TYPE"], SQLITE3_INTEGER); $statement->bindValue(":area", $this->filters["AREA"], SQLITE3_INTEGER); $statement->bindValue(":stage", $this->filters["STAGE"], SQLITE3_INTEGER); $statement->bindValue(":difficulty", $this->filters["DIFFICULTY"], SQLITE3_INTEGER); $statement->bindValue(":date_min", $this->filters["DATE_MIN"], SQLITE3_TEXT); $statement->bindValue(":date_max", $this->filters["DATE_MAX"], SQLITE3_TEXT); $statement->bindValue(":limit", $this->filters["NUMBER"], SQLITE3_INTEGER); return $statement; } /** * 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; } } /** * Retrieves the selection of runs. * * @return Run[] Runs to show on the page. */ public function get_runs(){ return $this->runs; } } ?>