Teams_Page.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. * @category Page
  8. */
  9. /**
  10. * Require dependent files if not present.
  11. */
  12. require_once(PATH::PAGE . "Page.php");
  13. require_once(PATH::ENTITY . "Team.php");
  14. /**
  15. * Team list page model.
  16. *
  17. * @category Page
  18. */
  19. class Teams_Page extends Page{
  20. /**
  21. * @var Team[] Teams to show.
  22. */
  23. public $teams = [];
  24. /**
  25. * @var Unit[] List of all units for team creation.
  26. */
  27. public $units = [];
  28. /**
  29. * Constructor.
  30. *
  31. * Retrieves the data and initializes the variables.
  32. */
  33. public function __construct(){
  34. $this->view = PATH::VIEW . "teams.php";
  35. $this->parse_filters();
  36. $s = $this->build_query();
  37. $q = get_context()->get_db()->query($s);
  38. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  39. array_push($this->teams, new Team($r["id"]));
  40. }
  41. $statement = get_context()->get_db()->prepare("
  42. SELECT id
  43. FROM unit
  44. WHERE player = :player;
  45. ");
  46. $statement->bindValue(':player', get_context()->get_player()->get_id(), SQLITE3_TEXT);
  47. $q = $statement->execute();
  48. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  49. array_push($this->units, new Unit($r["id"], false));
  50. }
  51. $this->title = "Teams - SWDB";
  52. $this->description = "Teams";
  53. $this->canonical = URL::BASE . "teams/";
  54. }
  55. /**
  56. * Parses the request looking for the selected filters, validates them
  57. * and adds them to the $filters array.
  58. */
  59. private function parse_filters(){
  60. $this->filters = FILTER::TEAM;
  61. if (isset($_GET["area"]) && APPLICATION::valid_id("AREA_TYPE_ID", $_GET["area"])){
  62. $this->filters["AREA"] = $_GET["area"];
  63. }
  64. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  65. $this->filters["NAME"] = SQLite3::escapeString($_GET["name"]);
  66. }
  67. return;
  68. }
  69. /**
  70. * Builds the query to the rune table using the selected or default
  71. * filters.
  72. *
  73. * @return string The query to be executed.
  74. */
  75. private function build_query(){
  76. $s = "
  77. SELECT id
  78. FROM team
  79. WHERE player = '" . get_context()->get_player()->get_id() . "'
  80. ORDER BY
  81. area_type,
  82. area
  83. ";
  84. if (APPLICATION::valid_id("AREA_ID", $this->filters["AREA"])){
  85. $s = $s . " AND area = '" . strtoupper($this->filters["AREA"]) . "') ";
  86. }
  87. if ($this->filters["NAME"] != ""){
  88. $s = $s . " AND upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%' ";
  89. }
  90. return $s;
  91. }
  92. }
  93. ?>