Teams_Page.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * @param resource $db Connection to the database.
  34. * @global int Player ID.
  35. */
  36. public function __construct($db){
  37. global $UID;
  38. parent::__construct($db);
  39. $this->view = PATH::VIEW . "teams.php";
  40. $this->parse_filters();
  41. $s = $this->build_query();
  42. $q = $this->db->query($s);
  43. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  44. array_push($this->teams, new Team($db, $r["id"]));
  45. }
  46. $s = "
  47. SELECT id
  48. FROM unit
  49. WHERE unit.uid = '$UID';
  50. ";
  51. $q = $this->db->query($s);
  52. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  53. array_push($this->units, new Unit($this->db, $r["id"], false));
  54. }
  55. $this->title = "Teams - SWDB";
  56. $this->description = "Teams";
  57. $this->canonical = URL::BASE . "teams/";
  58. }
  59. /**
  60. * Parses the request looking for the selected filters, validates them
  61. * and adds them to the $filters array.
  62. */
  63. private function parse_filters(){
  64. $this->filters = FILTER::TEAM;
  65. if (isset($_GET["area"]) && property_exists(AREA_TYPE_ID, $_GET["area"])){
  66. $this->filters["area"] = $_GET["area"];
  67. }
  68. if (isset($_GET["name"]) && strlen($_GET["name"]) > 0){
  69. $this->filters["name"] = SQLite3::escapeString($_GET["name"]);
  70. }
  71. return;
  72. }
  73. /**
  74. * Builds the query to the rune table using the selected or default
  75. * filters.
  76. *
  77. * @return The query to be executed.
  78. */
  79. private function build_query(){
  80. global $ELEMENT;
  81. global $UID;
  82. $s = "
  83. SELECT id
  84. FROM team
  85. WHERE uid = '$UID'
  86. ";
  87. if (property_exists(AREA_ID, $this->filters["area"])){
  88. $s = $s . " AND area = '" . strtoupper($this->filters["area"]) . "') ";
  89. }
  90. if ($this->filters["name"] != ""){
  91. $s = $s . " AND upper(name) LIKE '%" . strtoupper($this->filters["name"]) . "%' ";
  92. }
  93. return $s;
  94. }
  95. }
  96. ?>