user.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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", "preferred_payment_method", "bank_name", "bank_sort_code", "bank_account_number", "paypal_email", "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. const PAYMENT_METHODS = [
  32. 1 => "Bank Transfer",
  33. 2 => "PayPal",
  34. ];
  35. // Object methods
  36. public function canAdmin() {
  37. return $this->level >= User::LEVEL_ADMIN;
  38. }
  39. public function getDirectory() {
  40. $directory = ROOT_DIR . "/files/users/" . $this->id;
  41. if(!file_exists($directory)) mkdir($directory, 0777, true);
  42. return $directory;
  43. }
  44. public function getLevelText() {
  45. return User::LEVELS[$this->level];
  46. }
  47. public function getLevelClass() {
  48. switch($this->level) {
  49. case User::LEVEL_BANNED: return "danger";
  50. case User::LEVEL_REGISTERED: return "secondary";
  51. case User::LEVEL_UNVERIFIED: return "warning";
  52. case User::LEVEL_DEACTIVATED: return "secondary";
  53. case User::LEVEL_TESTER: return "success";
  54. case User::LEVEL_VENUE: return "info";
  55. case User::LEVEL_CUSTOMER: return "info";
  56. case User::LEVEL_ADMIN: return "info";
  57. case User::LEVEL_SUPERADMIN: return "info";
  58. default:
  59. return "primary";
  60. }
  61. }
  62. public function getGenderText() {
  63. if($this->gender == "M") return "Male";
  64. if($this->gender == "F") return "Female";
  65. return "Unspecified";
  66. }
  67. public function getAge() {
  68. if(is_null($this->date_of_birth)) return "N/A";
  69. $now = new DateTime("now");
  70. $dob = new DateTime($this->date_of_birth);
  71. return $dob->diff($now)->y;
  72. }
  73. public function getDisplayName() {
  74. $parts = array_filter([$this->first_name, $this->last_name]);
  75. return implode(" ", $parts);
  76. }
  77. public function hasPermission($object) {
  78. if($this->level == User::LEVEL_SUPERADMIN) return true;
  79. if($this->level == User::LEVEL_ADMIN) return true;
  80. $customers = $this->getCustomers();
  81. $customer_ids = array_column($customers, "id");
  82. if(is_a($object, "Customer")) {
  83. return in_array($object->id, $customer_ids);
  84. }
  85. if(is_a($object, "Location")) {
  86. return in_array($object->customer_id, $customer_ids);
  87. }
  88. if(is_a($object, "Project")) {
  89. return in_array($object->customer_id, $customer_ids);
  90. }
  91. if(is_a($object, "Template")) {
  92. return in_array($object->customer_id, $customer_ids);
  93. }
  94. return false;
  95. }
  96. public function deleteIDFiles() {
  97. $files = UserFile::query([
  98. "user_id" => $this->id,
  99. "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
  100. ]);
  101. foreach($files as $file) {
  102. $path = $this->getDirectory() . "/" . $file->id . ".jpg";
  103. if(file_exists($path)) unlink($path);
  104. $file->delete();
  105. }
  106. return true;
  107. }
  108. public function getAddresses() {
  109. $addresses = UserAddress::query([
  110. "user_id" => $this->id,
  111. ], [
  112. "sort" => "name"
  113. ]);
  114. return $addresses;
  115. }
  116. public function getCustomers() {
  117. if($this->level >= User::LEVEL_ADMIN) {
  118. return Customer::query([], ["sort" => "name"]);
  119. }
  120. if($this->level == User::LEVEL_CUSTOMER) {
  121. $links = UserCustomer::query(["user_id" => $this->id]);
  122. return Customer::query([
  123. "id" => array_column($links, "customer_id")
  124. ], ["sort" => "name"]);
  125. }
  126. return [];
  127. }
  128. public function getPendingTests() {
  129. return Test::query([
  130. "tester_id" => $this->id,
  131. "status" => [Test::STATUS_ASSIGNED, Test::STATUS_CONFIRMED],
  132. ], [
  133. "sort" => "id",
  134. ]);
  135. }
  136. public function getCompletedTests() {
  137. return Test::query([
  138. "tester_id" => $this->id,
  139. "status" => Test::STATUS_COMPLETED,
  140. ], [
  141. "sort" => "id",
  142. ]);
  143. }
  144. public function getPreferredPaymentMethodText() {
  145. return User::PAYMENT_METHODS[$this->preferred_payment_method];
  146. }
  147. }
  148. ?>