User.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. */
  54. public function __construct($id){
  55. $statement = get_context()->get_db()->prepare("
  56. SELECT
  57. id,
  58. name,
  59. mail,
  60. api_key,
  61. admin
  62. FROM user
  63. WHERE id = :id;
  64. ");
  65. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  66. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  67. if ($r){
  68. $this->id = $r["id"];
  69. $this->name = $r["name"];
  70. $this->mail = $r["mail"];
  71. $this->api_key = $r["api_key"];
  72. $this->admin = $r["admin"];
  73. // Get all accounts
  74. $statement = get_context()->get_db()->prepare("
  75. SELECT id
  76. FROM player
  77. WHERE user = :user
  78. ");
  79. $statement->bindValue(':user', $this->id, SQLITE3_TEXT);
  80. $q = $statement->execute();
  81. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  82. array_push($this->account, new Player($r["id"]));
  83. }
  84. }
  85. }
  86. }
  87. ?>