User.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * User entity file.
  4. *
  5. * Creates the entity and makes it available.
  6. *
  7. * @category Entity
  8. */
  9. /**
  10. * Require dependent entities if not present.
  11. */
  12. require_once(PATH::ENTITY . "Entity.php");
  13. require_once(PATH::ENTITY . "Player.php");
  14. /**
  15. * A User.
  16. *
  17. * Represents an object from the table 'user'.
  18. *
  19. * @category Entity
  20. */
  21. class User extends Entity{
  22. /**
  23. * @var int User ID.
  24. */
  25. public $id;
  26. /**
  27. * @var string User name.
  28. */
  29. public $name;
  30. /**
  31. * @var string User email.
  32. */
  33. public $mail;
  34. /**
  35. * @var string User API key.
  36. */
  37. public $api_key;
  38. /**
  39. * @var boolean Indicates if the user is an administrator.
  40. */
  41. public $admin = false;
  42. /**
  43. * @var \Player[] All of the players accounts.
  44. */
  45. public $account = [];
  46. /**
  47. * Constructor.
  48. *
  49. * Searches the database and retrieves the information about the
  50. * user, populating it and it's items.
  51. *
  52. * @param int $id User identifier.
  53. * @global resource Database connection.
  54. */
  55. public function __construct($id){
  56. global $db;
  57. $statement = $db->prepare("
  58. SELECT
  59. id,
  60. name,
  61. mail,
  62. api_key,
  63. admin
  64. FROM user
  65. WHERE id = :id;
  66. ");
  67. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  68. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  69. if ($r){
  70. $this->id = $r["id"];
  71. $this->name = $r["name"];
  72. $this->mail = $r["mail"];
  73. $this->api_key = $r["api_key"];
  74. $this->admin = $r["admin"];
  75. // Get all accounts
  76. $statement = $db->prepare("
  77. SELECT id
  78. FROM player
  79. WHERE user = :user
  80. ");
  81. $statement->bindValue(':user', $this->id, SQLITE3_TEXT);
  82. $q = $statement->execute();
  83. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  84. array_push($this->account, new Player($r["id"]));
  85. }
  86. }
  87. }
  88. }
  89. ?>