User.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. require_once(PATH::ENTITY . "Entity.php");
  3. require_once(PATH::ENTITY . "Lang.php");
  4. require_once(PATH::ENTITY . "Social.php");
  5. /**
  6. * User.
  7. *
  8. * Represents an object from the table 'user'.
  9. */
  10. class User extends Entity{
  11. /**
  12. * @var int User identifier.
  13. */
  14. private $id;
  15. /**
  16. * @var string User's display name.
  17. */
  18. private $display_name;
  19. /**
  20. * @var string User's image path.
  21. */
  22. private $image;
  23. /**
  24. * @var Lang[] List of the user languages.
  25. */
  26. private $langs = [];
  27. /**
  28. * @var Email[] List of the user emails.
  29. */
  30. private $email = [];
  31. /**
  32. * @var Social[] List of the user social links.
  33. */
  34. private $social = [];
  35. /**
  36. * @var string[] List uf user-defined texts.
  37. */
  38. private $texts = [];
  39. /**
  40. * @var bool[] Control flags to check loading status.
  41. */
  42. private $load_flags = [
  43. "email" => false,
  44. "social" => false,
  45. "lang" => false
  46. ];
  47. /**
  48. * Constructor.
  49. *
  50. * Searches the database and retrieves the information about the user, populating it. The
  51. * user to be loaded must be defined in the configuration file.
  52. */
  53. public function __construct(){
  54. $this->id = parse_ini_file(__DIR__ . "/../config/config.ini", true)["user"]["id"];
  55. $statement = get_context()->get_db()->prepare(
  56. "SELECT id, name, image FROM user WHERE id = :id"
  57. );
  58. $statement->bindValue(':id', $this->id, PDO::PARAM_INT);
  59. $statement->execute();
  60. $r_user = $statement->fetch(PDO::FETCH_ASSOC);
  61. if ($r_user !== false){
  62. $this->id = $r_user["id"];
  63. $this->display_name = $r_user["name"];
  64. $this->image = $r_user["image"];
  65. }
  66. else Log::error("User with id '$this->id' doesn't exist");
  67. }
  68. /**
  69. * Retrieves the user identifier
  70. *
  71. * @return int User ID.
  72. */
  73. public function get_id(){return $this->id;}
  74. /**
  75. * Retrieves the user's display name.
  76. *
  77. * @return string The user's display name.
  78. */
  79. public function get_display_name(){return $this->display_name;}
  80. /**
  81. * Retrieves a user defined text text.
  82. *
  83. * @param string $key Text key.
  84. * @return string The defined text, or an empty string if it doesn't exist.
  85. */
  86. public function get_text($key){
  87. if (isset($this->texts[$key])) return $this->texts[$key];
  88. $statement = get_context()->get_db()->prepare(
  89. "SELECT text FROM user_text WHERE user = :user AND upper(name) = upper(:key)"
  90. );
  91. $statement->bindValue(':user', $this->id, PDO::PARAM_INT);
  92. $statement->bindValue(':key', $key, PDO::PARAM_STR);
  93. $statement->execute();
  94. $r = $statement->fetch(PDO::FETCH_ASSOC);
  95. if ($r == false) return "";
  96. $this->texts[$key] = Text::get($r["text"], false);
  97. return $this->texts[$key];
  98. }
  99. /**
  100. * Retrieves the user's profile image file path.
  101. *
  102. * @return string The user's profile image.
  103. */
  104. public function get_image(){return $this->image;}
  105. /**
  106. * Retrieves the user's list of emails.
  107. *
  108. * @return string[] The user's list of emails.
  109. */
  110. public function get_emails(){
  111. if ($this->load_flags["email"] === false) $this->load_email();
  112. return $this->email;
  113. }
  114. /**
  115. * Retrieves the user's first visible email address.
  116. *
  117. * @return string The user's first visible email, null if there are none.
  118. */
  119. public function get_first_email(){
  120. if ($this->load_flags["email"] === false) $this->load_email();
  121. if (sizeof($this->email) == 0) return null;
  122. else return $this->email[0];
  123. }
  124. /**
  125. * Retrieves the user's list of supported languages.
  126. *
  127. * @return string[] The user's list of languages, sorted by priority.
  128. */
  129. public function get_langs(){
  130. if ($this->load_flags["lang"] === false) $this->load_lang();
  131. return $this->langs;
  132. }
  133. /**
  134. * Retrieves the user's list of social links.
  135. *
  136. * @return Social[] The user's list of social links.
  137. */
  138. public function get_social(){
  139. if ($this->load_flags["social"] === false) $this->load_social();
  140. return $this->social;
  141. }
  142. /**
  143. * Checks if a language is supported by the user.
  144. *
  145. * @param sting $code Two letter language code to check, case insensitive.
  146. * @return True if the language is supported by the user, false otherwise.
  147. */
  148. public function is_valid_language($code){
  149. if ($this->load_flags["lang"] === false) $this->load_lang();
  150. foreach ($this->langs as $lang){
  151. if (strtolower($lang->get_code()) == strtolower($code))
  152. return true;
  153. }
  154. return false;
  155. }
  156. /**
  157. * Loads the user emails.
  158. *
  159. * Not exposed, must be called before accessing the user email list.
  160. */
  161. private function load_email(){
  162. if ($this->load_flags["email"] === false){
  163. $statement = get_context()->get_db()->prepare(
  164. "SELECT email FROM user_email WHERE user = :id AND visible = 1"
  165. );
  166. $statement->bindValue(':id', $this->id, PDO::PARAM_INT);
  167. $statement->execute();
  168. while ($r_email = $statement->fetch(PDO::FETCH_ASSOC))
  169. array_push($this->email, $r_email["email"]);
  170. $this->load_flags["email"] = true;
  171. }
  172. }
  173. /**
  174. * Loads the user supported languages.
  175. *
  176. * Not exposed, must be called before accessing the user languages.
  177. */
  178. private function load_lang(){
  179. if ($this->load_flags["lang"] === false){
  180. $statement = get_context()->get_db()->prepare(
  181. "SELECT lang FROM user_lang WHERE user = :id ORDER BY priority"
  182. );
  183. $statement->bindValue(':id', $this->id, PDO::PARAM_INT);
  184. $statement->execute();
  185. while ($r_lang = $statement->fetch(PDO::FETCH_ASSOC))
  186. array_push($this->langs, new Lang($r_lang["lang"]));
  187. $this->load_flags["lang"] = true;
  188. }
  189. }
  190. /**
  191. * Loads the user social links.
  192. *
  193. * Not exposed, must be called before accessing the user links.
  194. */
  195. private function load_social(){
  196. if ($this->load_flags["social"] === false){
  197. $statement = get_context()->get_db()->prepare(
  198. "SELECT id FROM user_social WHERE user = :id ORDER BY priority"
  199. );
  200. $statement->bindValue(':id', $this->id, PDO::PARAM_INT);
  201. $statement->execute();
  202. while ($r_social = $statement->fetch(PDO::FETCH_ASSOC))
  203. array_push($this->social, new Social($r_social["id"]));
  204. $this->load_flags["social"] = true;
  205. }
  206. }
  207. }