| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- class User extends Model {
- protected static $table = "user";
- protected static $id_field = "id";
- 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"];
- protected static $field_map = [
- ];
- // Constants
- const TITLES = ["Miss", "Mr", "Ms", "Dr"];
- const GENDERS = ["Female", "Male", "Unspecified"];
- const LEVEL_BANNED = 0;
- const LEVEL_REGISTERED = 1;
- const LEVEL_UNVERIFIED = 2;
- const LEVEL_DEACTIVATED = 4;
- const LEVEL_TESTER = 8;
- const LEVEL_VENUE = 16;
- const LEVEL_CUSTOMER = 32;
- const LEVEL_ADMIN = 64;
- const LEVEL_SUPERADMIN = 128;
- const LEVELS = [
- 0 => "Banned",
- 1 => "Registered",
- 2 => "Unverified",
- 4 => "Deactivated",
- 8 => "Tester",
- 16 => "Venue",
- 32 => "Customer",
- 64 => "Admin",
- 128 => "Super Admin",
- ];
- // Object methods
- public function canAdmin() {
- return $this->level >= User::LEVEL_ADMIN;
- }
- public function getDirectory() {
- $directory = ROOT_DIR . "/files/users/" . $this->id;
- if(!file_exists($directory)) mkdir($directory, 0777, true);
- return $directory;
- }
- public function getLevelText() {
- return User::LEVELS[$this->level];
- }
- public function getLevelClass() {
- switch($this->level) {
- case User::LEVEL_BANNED: return "danger";
- case User::LEVEL_REGISTERED: return "secondary";
- case User::LEVEL_UNVERIFIED: return "warning";
- case User::LEVEL_DEACTIVATED: return "secondary";
- case User::LEVEL_TESTER: return "success";
- case User::LEVEL_VENUE: return "info";
- case User::LEVEL_CUSTOMER: return "info";
- case User::LEVEL_ADMIN: return "info";
- case User::LEVEL_SUPERADMIN: return "info";
- default:
- return "primary";
- }
- }
- public function getGenderText() {
- if($this->gender == "M") return "Male";
- if($this->gender == "F") return "Female";
- return "Unspecified";
- }
- public function getAge() {
- if(is_null($this->date_of_birth)) return "N/A";
- $now = new DateTime("now");
- $dob = new DateTime($this->date_of_birth);
- return $dob->diff($now)->y;
- }
- public function getDisplayName() {
- $parts = array_filter([$this->title, $this->first_name, $this->last_name]);
- return implode(" ", $parts);
- }
- public function hasPermission($object) {
- if($this->level == User::LEVEL_SUPERADMIN) return true;
- if($this->level == User::LEVEL_ADMIN) return true;
- $customers = $this->getCustomers();
- $customer_ids = array_column($customers, "id");
- if(is_a($object, "Customer")) {
- return in_array($object->id, $customer_ids);
- }
- if(is_a($object, "Location")) {
- return in_array($object->customer_id, $customer_ids);
- }
- if(is_a($object, "Project")) {
- return in_array($object->customer_id, $customer_ids);
- }
- if(is_a($object, "Template")) {
- return in_array($object->customer_id, $customer_ids);
- }
- return false;
- }
- public function deleteIDFiles() {
- $files = UserFile::query([
- "user_id" => $this->id,
- "type" => [UserFile::TYPE_ID, UserFile::TYPE_PHOTO]
- ]);
- foreach($files as $file) {
- $path = $this->getDirectory() . "/" . $file->id . ".jpg";
- if(file_exists($path)) unlink($path);
- $file->delete();
- }
- return true;
- }
- public function getAddresses() {
- $addresses = UserAddress::query([
- "user_id" => $this->id,
- ], [
- "sort" => "name"
- ]);
- return $addresses;
- }
- public function getCustomers() {
- if($this->level >= User::LEVEL_ADMIN) {
- return Customer::query([], ["sort" => "name"]);
- }
- if($this->level == User::LEVEL_CUSTOMER) {
- $links = UserCustomer::query(["user_id" => $this->id]);
- return Customer::query([
- "id" => array_column($links, "customer_id")
- ], ["sort" => "name"]);
- }
- return [];
- }
- public function getPendingTests() {
- return Test::query([
- "tester_id" => $this->id,
- "status" => Test::STATUS_ASSIGNED,
- ], [
- "sort" => "id",
- ]);
- }
- public function getCompletedTests() {
- return Test::query([
- "tester_id" => $this->id,
- "status" => Test::STATUS_COMPLETED,
- ], [
- "sort" => "id",
- ]);
- }
- }
- ?>
|