1
0

user.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. class User extends Model {
  3. protected static $table = "user";
  4. protected static $id_field = "id";
  5. protected static $fillable_fields = ["email", "old_id", "password", "level", "score", "title", "first_name", "last_name", "gender", "date_of_birth", "phone", "bank_sort_code", "bank_account_number", "dts_registered", "dts_last_seen", "dts_verified", "verifier_id", "verifier_notes"];
  6. protected static $field_map = [
  7. ];
  8. // Constants
  9. const TITLES = ["Miss", "Mr", "Ms", "Dr"];
  10. const GENDERS = ["Female", "Male", "Unspecified"];
  11. const LEVEL_BANNED = 0;
  12. const LEVEL_REGISTERED = 1;
  13. const LEVEL_UNVERIFIED = 2;
  14. const LEVEL_DEACTIVATED = 4;
  15. const LEVEL_TESTER = 8;
  16. const LEVEL_VENUE = 16;
  17. const LEVEL_CUSTOMER = 32;
  18. const LEVEL_ADMIN = 64;
  19. const LEVEL_SUPERADMIN = 128;
  20. const LEVELS = [
  21. 0 => "Banned",
  22. 1 => "Registered",
  23. 2 => "Unverified",
  24. 4 => "Deactivated",
  25. 8 => "Tester",
  26. 16 => "Venue",
  27. 32 => "Customer",
  28. 64 => "Admin",
  29. 128 => "Super Admin",
  30. ];
  31. // Object methods
  32. public function canAdmin() {
  33. return $this->level >= User::LEVEL_ADMIN;
  34. }
  35. public function getDirectory() {
  36. $directory = ROOT_DIR . "/files/users/" . $this->id;
  37. if(!file_exists($directory)) mkdir($directory, 0777, true);
  38. return $directory;
  39. }
  40. public function getLevelText() {
  41. return User::LEVELS[$this->level];
  42. }
  43. public function getLevelClass() {
  44. switch($this->level) {
  45. case User::LEVEL_BANNED: return "danger";
  46. case User::LEVEL_REGISTERED: return "secondary";
  47. case User::LEVEL_UNVERIFIED: return "warning";
  48. case User::LEVEL_DEACTIVATED: return "secondary";
  49. case User::LEVEL_TESTER: return "success";
  50. case User::LEVEL_VENUE: return "info";
  51. case User::LEVEL_CUSTOMER: return "info";
  52. case User::LEVEL_ADMIN: return "info";
  53. case User::LEVEL_SUPERADMIN: return "info";
  54. default:
  55. return "primary";
  56. }
  57. }
  58. public function getGenderText() {
  59. if($this->gender == "M") return "Male";
  60. if($this->gender == "F") return "Female";
  61. return "Unspecified";
  62. }
  63. public function getAge() {
  64. if(is_null($this->date_of_birth)) return "N/A";
  65. $now = new DateTime("now");
  66. $dob = new DateTime($this->date_of_birth);
  67. return $dob->diff($now)->y;
  68. }
  69. public function getDisplayName() {
  70. $parts = array_filter([$this->first_name, $this->last_name]);
  71. return implode(" ", $parts);
  72. }
  73. public function hasPermission($object) {
  74. if($this->level == User::LEVEL_SUPERADMIN) return true;
  75. if($this->level == User::LEVEL_ADMIN) return true;
  76. $customers = $this->getCustomers();
  77. $customer_ids = array_column($customers, "id");
  78. if(is_a($object, "Customer")) {
  79. return in_array($object->id, $customer_ids);
  80. }
  81. if(is_a($object, "Location")) {
  82. return in_array($object->customer_id, $customer_ids);
  83. }
  84. if(is_a($object, "Project")) {
  85. return in_array($object->customer_id, $customer_ids);
  86. }
  87. if(is_a($object, "Template")) {
  88. return in_array($object->customer_id, $customer_ids);
  89. }
  90. return false;
  91. }
  92. public function deleteIDFiles() {
  93. $files = UserFile::query([
  94. "user_id" => $this->id,
  95. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  96. ]);
  97. foreach($files as $file) {
  98. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  99. if(file_exists($path)) unlink($path);
  100. $file->delete();
  101. }
  102. return true;
  103. }
  104. public function getAddresses() {
  105. $addresses = UserAddress::query([
  106. "user_id" => $this->id,
  107. ], [
  108. "sort" => "name"
  109. ]);
  110. return $addresses;
  111. }
  112. public function getCustomers() {
  113. if($this->level >= User::LEVEL_ADMIN) {
  114. return Customer::query([], ["sort" => "name"]);
  115. }
  116. if($this->level == User::LEVEL_CUSTOMER) {
  117. $links = UserCustomer::query(["user_id" => $this->id]);
  118. return Customer::query([
  119. "id" => array_column($links, "customer_id")
  120. ], ["sort" => "name"]);
  121. }
  122. return [];
  123. }
  124. public function getPendingTests() {
  125. return Test::query([
  126. "tester_id" => $this->id,
  127. "status" => Test::STATUS_ASSIGNED,
  128. ], [
  129. "sort" => "id",
  130. ]);
  131. }
  132. public function getCompletedTests() {
  133. return Test::query([
  134. "tester_id" => $this->id,
  135. "status" => Test::STATUS_COMPLETED,
  136. ], [
  137. "sort" => "id",
  138. ]);
  139. }
  140. }
  141. ?>