Teams_Page.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. * @global int Player ID.
  34. * @global resource Database connection.
  35. */
  36. public function __construct(){
  37. global $UID;
  38. global $db;
  39. $this->view = PATH::VIEW . "teams.php";
  40. $this->parse_filters();
  41. $s = $this->build_query();
  42. $q = $db->query($s);
  43. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  44. array_push($this->teams, new Team($r["id"]));
  45. }
  46. $statement = $db->prepare("
  47. SELECT id
  48. FROM unit
  49. WHERE unit.uid = :uid;
  50. ");
  51. $statement->bindValue(':uid', $UID, SQLITE3_INTEGER);
  52. $q = $statement->execute();
  53. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  54. array_push($this->units, new Unit($r["id"], false));
  55. }
  56. $this->title = "Teams - SWDB";
  57. $this->description = "Teams";
  58. $this->canonical = URL::BASE . "teams/";
  59. }
  60. /**
  61. * Parses the request looking for the selected filters, validates them
  62. * and adds them to the $filters array.
  63. */
  64. private function parse_filters(){
  65. $this->filters = FILTER::TEAM;
  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. * Builds the query to the rune table using the selected or default
  76. * filters.
  77. *
  78. * @return string The query to be executed.
  79. */
  80. private function build_query(){
  81. global $UID;
  82. $s = "
  83. SELECT id
  84. FROM team
  85. WHERE uid = '$UID'
  86. ORDER BY
  87. area_type,
  88. area
  89. ";
  90. if (APPLICATION::valid_id("AREA_ID", $this->filters["AREA"])){
  91. $s = $s . " AND area = '" . strtoupper($this->filters["AREA"]) . "') ";
  92. }
  93. if ($this->filters["NAME"] != ""){
  94. $s = $s . " AND upper(name) LIKE '%" . strtoupper($this->filters["NAME"]) . "%' ";
  95. }
  96. return $s;
  97. }
  98. }
  99. ?>