user.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 getGenderText() {
  44. if($this->gender == "M") return "Male";
  45. if($this->gender == "F") return "Female";
  46. return "Unspecified";
  47. }
  48. public function getAge() {
  49. if(is_null($this->date_of_birth)) return "N/A";
  50. $now = new DateTime("now");
  51. $dob = new DateTime($this->date_of_birth);
  52. return $dob->diff($now)->y;
  53. }
  54. public function getDisplayName() {
  55. $parts = array_filter([$this->title, $this->first_name, $this->last_name]);
  56. return implode(" ", $parts);
  57. }
  58. public function hasPermission($object) {
  59. if($this->level == User::LEVEL_SUPERADMIN) return true;
  60. if($this->level == User::LEVEL_ADMIN) return true;
  61. $customers = $this->getCustomers();
  62. $customer_ids = array_column($customers, "id");
  63. if(is_a($object, "Customer")) {
  64. return in_array($object->id, $customer_ids);
  65. }
  66. if(is_a($object, "Location")) {
  67. return in_array($object->customer_id, $customer_ids);
  68. }
  69. if(is_a($object, "Project")) {
  70. return in_array($object->customer_id, $customer_ids);
  71. }
  72. if(is_a($object, "Template")) {
  73. return in_array($object->customer_id, $customer_ids);
  74. }
  75. return false;
  76. }
  77. public function deleteIDFiles() {
  78. $files = UserFile::query([
  79. "user_id" => $this->id,
  80. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  81. ]);
  82. foreach($files as $file) {
  83. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  84. if(file_exists($path)) unlink($path);
  85. $file->delete();
  86. }
  87. return true;
  88. }
  89. public function getAddresses() {
  90. $addresses = UserAddress::query([
  91. "user_id" => $this->id,
  92. ], [
  93. "sort" => "name"
  94. ]);
  95. return $addresses;
  96. }
  97. public function getCustomers() {
  98. if($this->level >= User::LEVEL_ADMIN) {
  99. return Customer::query([], ["sort" => "name"]);
  100. }
  101. if($this->level == User::LEVEL_CUSTOMER) {
  102. $links = UserCustomer::query(["user_id" => $this->id]);
  103. return Customer::query([
  104. "id" => array_column($links, "customer_id")
  105. ], ["sort" => "name"]);
  106. }
  107. return [];
  108. }
  109. public function getPendingTests() {
  110. return Test::query([
  111. "tester_id" => $this->id,
  112. "status" => Test::STATUS_ASSIGNED,
  113. ], [
  114. "sort" => "id",
  115. ]);
  116. }
  117. public function getCompletedTests() {
  118. return Test::query([
  119. "tester_id" => $this->id,
  120. "status" => Test::STATUS_COMPLETED,
  121. ], [
  122. "sort" => "id",
  123. ]);
  124. }
  125. }
  126. ?>