Teams_Page.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Team list page file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(PATH::PAGE . "Page.php");
  12. require_once(PATH::ENTITY . "Team.php");
  13. /**
  14. * Team list page model.
  15. *
  16. * @category Page
  17. */
  18. class Teams_Page extends Page{
  19. /**
  20. * @var Team[] Teams to show.
  21. */
  22. private $teams = [];
  23. /**
  24. * @var Unit[] List of all units for team creation.
  25. */
  26. private $units = [];
  27. /**
  28. * @var mixed[] Page filters
  29. */
  30. private $filters = [
  31. "NAME" => "",
  32. "AREA" => null
  33. ];
  34. /**
  35. * Constructor.
  36. *
  37. * Retrieves the data and initializes the variables.
  38. */
  39. public function __construct(){
  40. parent::__construct();
  41. $this->set_public(true);
  42. $this->set_view("teams.php");
  43. $this->set_title("Teams");
  44. $this->set_description(get_context()->get_player()->get_name() . "'s teams");
  45. $this->set_canonical("player/" . get_context()->get_player()->get_id() . "/teams/");
  46. $this->add_css("teams.css");
  47. $this->parse_filters();
  48. $result_set = $this->prepare_statement()->execute();
  49. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  50. array_push($this->teams, new Team($result["id"]));
  51. }
  52. $statement = get_context()->get_db()->prepare("
  53. SELECT id
  54. FROM unit
  55. WHERE player = :player;
  56. ");
  57. // Get all player units, for the team creation combos.
  58. $statement->bindValue(':player', get_context()->get_player()->get_id(), SQLITE3_TEXT);
  59. $result_set = $statement->execute();
  60. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  61. array_push($this->units, new Unit($result["id"], false));
  62. }
  63. $this->set_code(200);
  64. $this->set_message("OK");
  65. }
  66. /**
  67. * Parses the request looking for the selected filters, validates them
  68. * and adds them to the $filters array.
  69. */
  70. private function parse_filters(){
  71. if (isset($_GET["area"]) && APPLICATION::valid_id("AREA_TYPE_ID", $_GET["area"])){
  72. $this->filters["AREA"] = $_GET["area"];
  73. }
  74. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  75. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  76. }
  77. return;
  78. }
  79. /**
  80. * Prepares the statement to execute against the database using the filter values.
  81. *
  82. * @return SQLite3Stmt prepared statement.
  83. */
  84. private function prepare_statement(){
  85. // Common items
  86. $query = "
  87. SELECT id
  88. FROM team
  89. WHERE player = :player
  90. ";
  91. if (APPLICATION::valid_id("AREA_TYPE_ID", $this->filters["AREA"])){
  92. $query .= " AND area_type = :area ";
  93. }
  94. if ($this->filters["NAME"] != ""){
  95. $query .= " AND upper(name) LIKE :name ";
  96. }
  97. $query .= " ORDER BY area_type, area;";
  98. $statement = get_context()->get_db()->prepare($query);
  99. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  100. $statement->bindValue(":area", $this->filters["AREA"], SQLITE3_INTEGER);
  101. $statement->bindValue(":name", "%" . $this->filters["NAME"] . "%", SQLITE3_TEXT);
  102. return $statement;
  103. }
  104. /**
  105. * Retrieves the teams to show.
  106. *
  107. * @return Team[] Selected teams.
  108. */
  109. public function get_teams(){
  110. return $this->teams;
  111. }
  112. /**
  113. * Retrieves $all of the player units.
  114. *
  115. * @return Unit Player units.
  116. */
  117. public function get_units(){
  118. return $this->units;
  119. }
  120. /**
  121. * Retrieves the page filters.
  122. *
  123. * @return mixed[] Page filtes
  124. */
  125. public function get_filters(){
  126. return $this->filters;
  127. }
  128. /**
  129. * Retrieves one filter.
  130. *
  131. * @param string $key
  132. * @return mixed Filter value, null if if doesn't exist.
  133. */
  134. public function get_filter($key){
  135. if (array_key_exists($key, $this->filters)){
  136. return $this->filters[$key];
  137. }
  138. else{
  139. return null;
  140. }
  141. }
  142. }