| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?php
- /**
- * Team 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 . "Team.php");
- /**
- * Team list page model.
- *
- * @category Page
- */
- class Teams_Page extends Page{
- /**
- * @var Team[] Teams to show.
- */
- private $teams = [];
- /**
- * @var Unit[] List of all units for team creation.
- */
- private $units = [];
-
- /**
- * @var mixed[] Page filters
- */
- private $filters = [
- "NAME" => "",
- "AREA" => null
- ];
- /**
- * Constructor.
- *
- * Retrieves the data and initializes the variables.
- */
- public function __construct(){
- $this->view = PATH::VIEW . "teams.php";
- $this->title = "Teams - SWDB";
- $this->description = "Teams";
- $this->canonical = URL::BASE . "teams/";
- $this->parse_filters();
- $result_set = $this->prepare_statement()->execute();
- while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
- array_push($this->teams, new Team($result["id"]));
- }
- $statement = get_context()->get_db()->prepare("
- SELECT id
- FROM unit
- WHERE player = :player;
- ");
- // Get all player units, for the team creation combos.
- $statement->bindValue(':player', get_context()->get_player()->get_id(), SQLITE3_TEXT);
- $result_set = $statement->execute();
- while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
- array_push($this->units, new Unit($result["id"], false));
- }
- }
- /**
- * 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"]) && APPLICATION::valid_id("AREA_TYPE_ID", $_GET["area"])){
- $this->filters["AREA"] = $_GET["area"];
- }
- if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
- $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
- }
- 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 team
- WHERE player = :player
- ";
-
- if (APPLICATION::valid_id("AREA_TYPE_ID", $this->filters["AREA"])){
- $query .= " AND area_type = :area ";
- }
- if ($this->filters["NAME"] != ""){
- $query .= " AND upper(name) LIKE :name ";
- }
- $query .= " ORDER BY area_type, area;";
- $statement = get_context()->get_db()->prepare($query);
- $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
- $statement->bindValue(":area", $this->filters["AREA"], SQLITE3_INTEGER);
- $statement->bindValue(":name", "%" . $this->filters["NAME"] . "%", SQLITE3_TEXT);
- return $statement;
- }
- /**
- * Retrieves the teams to show.
- *
- * @return Team[] Selected teams.
- */
- public function get_teams(){
- return $this->teams;
- }
- /**
- * Retrieves $all of the player units.
- *
- * @return Unit Player units.
- */
- public function get_units(){
- return $this->units;
- }
-
- /**
- * 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;
- }
- }
- }
|