Teams_Page.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. $this->view = PATH::VIEW . "teams.php";
  41. $this->title = "Teams - SWDB";
  42. $this->description = "Teams";
  43. $this->canonical = URL::BASE . "teams/";
  44. $this->parse_filters();
  45. $result_set = $this->prepare_statement()->execute();
  46. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  47. array_push($this->teams, new Team($result["id"]));
  48. }
  49. $statement = get_context()->get_db()->prepare("
  50. SELECT id
  51. FROM unit
  52. WHERE player = :player;
  53. ");
  54. // Get all player units, for the team creation combos.
  55. $statement->bindValue(':player', get_context()->get_player()->get_id(), SQLITE3_TEXT);
  56. $result_set = $statement->execute();
  57. while ($result = $result_set->fetchArray(SQLITE3_ASSOC)){
  58. array_push($this->units, new Unit($result["id"], false));
  59. }
  60. }
  61. /**
  62. * Parses the request looking for the selected filters, validates them
  63. * and adds them to the $filters array.
  64. */
  65. private function parse_filters(){
  66. if (isset($_GET["area"]) && APPLICATION::valid_id("AREA_TYPE_ID", $_GET["area"])){
  67. $this->filters["AREA"] = $_GET["area"];
  68. }
  69. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  70. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  71. }
  72. return;
  73. }
  74. /**
  75. * Prepares the statement to execute against the database using the filter values.
  76. *
  77. * @return SQLite3Stmt prepared statement.
  78. */
  79. private function prepare_statement(){
  80. // Common items
  81. $query = "
  82. SELECT id
  83. FROM team
  84. WHERE player = :player
  85. ";
  86. if (APPLICATION::valid_id("AREA_TYPE_ID", $this->filters["AREA"])){
  87. $query .= " AND area_type = :area ";
  88. }
  89. if ($this->filters["NAME"] != ""){
  90. $query .= " AND upper(name) LIKE :name ";
  91. }
  92. $query .= " ORDER BY area_type, area;";
  93. $statement = get_context()->get_db()->prepare($query);
  94. $statement->bindValue(":player", get_context()->get_player()->get_id(), SQLITE3_TEXT);
  95. $statement->bindValue(":area", $this->filters["AREA"], SQLITE3_INTEGER);
  96. $statement->bindValue(":name", "%" . $this->filters["NAME"] . "%", SQLITE3_TEXT);
  97. return $statement;
  98. }
  99. /**
  100. * Retrieves the teams to show.
  101. *
  102. * @return Team[] Selected teams.
  103. */
  104. public function get_teams(){
  105. return $this->teams;
  106. }
  107. /**
  108. * Retrieves $all of the player units.
  109. *
  110. * @return Unit Player units.
  111. */
  112. public function get_units(){
  113. return $this->units;
  114. }
  115. /**
  116. * Retrieves the page filters.
  117. *
  118. * @return mixed[] Page filtes
  119. */
  120. public function get_filters(){
  121. return $this->filters;
  122. }
  123. /**
  124. * Retrieves one filter.
  125. *
  126. * @param string $key
  127. * @return mixed Filter value, null if if doesn't exist.
  128. */
  129. public function get_filter($key){
  130. if (array_key_exists($key, $this->filters)){
  131. return $this->filters[$key];
  132. }
  133. else{
  134. return null;
  135. }
  136. }
  137. }