| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- /**
- * User entity file.
- *
- * Creates the entity and makes it available.
- *
- * @author Iñigo Valentin <i@inigovalentin.com>
- * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
- * @package SWDB
- */
- require_once(PATH::ENTITY . "Entity.php");
- require_once(PATH::ENTITY . "Player.php");
- /**
- * A User.
- *
- * Represents an application user.
- *
- * @category Entity
- */
- class User extends Entity{
- /**
- * @var int User ID.
- */
- private $id;
- /**
- * @var string User name.
- */
- private $name;
- /**
- * @var string User email.
- */
- private $mail;
- /**
- * @var string User API key.
- */
- private $api_key;
- /**
- * @var bool Indicates if the user is an administrator.
- */
- private $admin = false;
- /**
- * @var bool Internal flag to check if the user's accounts have been loaded.
- */
- private $flag_accounts = false;
- /**
- * @var Player[] All of the players accounts.
- */
- private $account = [];
- /**
- * Constructor.
- *
- * Searches the database and retrieves the information about the
- * user, populating it and it's items.
- *
- * @param int $id User identifier.
- */
- public function __construct($id){
- $statement = get_context()->get_db()->prepare("
- SELECT
- id,
- name,
- mail,
- api_key,
- admin
- FROM user
- WHERE id = :id;
- ");
- $statement->bindValue(':id', $id, SQLITE3_TEXT);
- $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
- if ($r){
- $this->id = $r["id"];
- $this->name = $r["name"];
- $this->mail = $r["mail"];
- $this->api_key = $r["api_key"];
- $this->admin = $r["admin"];
- }
- }
- /**
- * Loads the users accounts into the entity.
- *
- * Must be called only once before trying to get the acoounts. Sets the flag
- * {@link User:$flag_accounts} to true.
- */
- private function load_accounts(){
- $statement = get_context()->get_db()->prepare("
- SELECT id
- FROM player
- WHERE user = :user
- ORDER BY last_access DESC
- ");
- $statement->bindValue(':user', $this->id, SQLITE3_TEXT);
- $q = $statement->execute();
- while ($r = $q->fetchArray(SQLITE3_ASSOC)){
- array_push($this->account, new Player($r["id"]));
- }
- $this->flag_accounts = true;
- }
- /**
- * Retrieves the user identifier.
- *
- * @return int User ID.
- */
- public function get_id(){
- return $this->id;
- }
- /**
- * Retrieves the user name.
- *
- * @return string User name.
- */
- public function get_name(){
- return $this->name;
- }
- /**
- * Retrieves the user email address.
- *
- * @return string User mail.
- */
- public function get_mail(){
- return $this->mail;
- }
- /**
- * Retrieves the user's API key.
- *
- * @return string User's API key.
- */
- public function get_api_key(){
- return $this->api_key;
- }
- /**
- * Checks if the user is a site administrator.
- *
- * @return bool True if the user is administrator, false otherwise.
- */
- public function is_admin(){
- return $this->admin;
- }
- /**
- * Retrieves the user's accounts, sorted by the last account date in descending order.
- *
- * @return Player[] The user's accounts.
- */
- public function get_accounts(){
- if (! $this->flag_accounts){
- $this->load_accounts();
- }
- return $this->account;
- }
- }
|