* @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(){ parent::__construct(); $this->set_public(true); $this->set_view("teams.php"); $this->set_title("Teams"); $this->set_description(get_context()->get_player()->get_name() . "'s teams"); $this->set_canonical("player/" . get_context()->get_player()->get_id() . "/teams/"); $this->add_css("teams.css"); $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)); } $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"]) && 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; } } }