|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace app\models; |
| 4 | + |
| 5 | +use app\core\Application; |
| 6 | +use app\core\DbModel; |
| 7 | + |
| 8 | +class Admin extends DbModel |
| 9 | +{ |
| 10 | + public string $fname = ''; |
| 11 | + public string $lname = ''; |
| 12 | + public string $email = ''; |
| 13 | + public string $phone_no = ''; |
| 14 | + public string $address = ''; |
| 15 | + public string $password = ''; |
| 16 | + public string $confirmPassword = ''; |
| 17 | + |
| 18 | + public function tableName(): string |
| 19 | + { |
| 20 | + return 'admin'; |
| 21 | + } |
| 22 | + |
| 23 | + public function primaryKey(): string |
| 24 | + { |
| 25 | + return 'admin_id'; |
| 26 | + } |
| 27 | + |
| 28 | + public function save() |
| 29 | + { |
| 30 | + $this->password = password_hash($this->password, PASSWORD_DEFAULT); |
| 31 | + return parent::save(); |
| 32 | + } |
| 33 | + |
| 34 | + public function updateAdmin() |
| 35 | + { |
| 36 | + $sql = "UPDATE admin SET fname = :fname, lname = :lname, phone_no = :phone_no, address = :address WHERE admin_id = :admin_id"; |
| 37 | + $stmt = self::prepare($sql); |
| 38 | + $stmt->bindValue(':fname', $this->fname); |
| 39 | + $stmt->bindValue(':lname', $this->lname); |
| 40 | + $stmt->bindValue(':phone_no', $this->phone_no); |
| 41 | + $stmt->bindValue(':address', $this->address); |
| 42 | + $stmt->bindValue(':admin_id', Application::$app->admin->{'admin_id'}); |
| 43 | + return $stmt->execute(); |
| 44 | + } |
| 45 | + |
| 46 | + public function rules(): array |
| 47 | + { |
| 48 | + return [ |
| 49 | + 'fname' => [self::RULE_REQUIRED], |
| 50 | + 'lname' => [self::RULE_REQUIRED], |
| 51 | + 'email' => [self::RULE_REQUIRED, self::RULE_EMAIL, [ |
| 52 | + self::RULE_UNIQUE, |
| 53 | + 'class' => self::class |
| 54 | + ]], |
| 55 | + 'phone_no' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 10], [self::RULE_MAX, 'max' => 10]], |
| 56 | + 'address' => [self::RULE_REQUIRED], |
| 57 | + 'password' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 8]], |
| 58 | + 'confirmPassword' => [self::RULE_REQUIRED, [self::RULE_MATCH, 'match' => 'password']], |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + public function updateRules(): array |
| 63 | + { |
| 64 | + return [ |
| 65 | + 'fname' => [self::RULE_REQUIRED], |
| 66 | + 'lname' => [self::RULE_REQUIRED], |
| 67 | + 'phone_no' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 10], [self::RULE_MAX, 'max' => 10]], |
| 68 | + 'address' => [self::RULE_REQUIRED], |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + public function attributes(): array |
| 73 | + { |
| 74 | + return [ |
| 75 | + 'fname', |
| 76 | + 'lname', |
| 77 | + 'email', |
| 78 | + 'phone_no', |
| 79 | + 'address', |
| 80 | + 'password', |
| 81 | + ]; |
| 82 | + } |
| 83 | + |
| 84 | + public static function findAllCustomers() |
| 85 | + { |
| 86 | + $sql = "SELECT cus_id, fname, lname, email, phone_no, address, reg_date FROM customer"; |
| 87 | + $statement = (new Admin)->prepare($sql); |
| 88 | + $statement->execute(); |
| 89 | + return $statement->fetchAll(\PDO::FETCH_ASSOC); |
| 90 | + } |
| 91 | + |
| 92 | + public static function findAllTechnicians() |
| 93 | + { |
| 94 | + $sql = "SELECT cus_id, fname, lname, email, phone_no, address, reg_date FROM technician"; |
| 95 | + $statement = (new Admin)->prepare($sql); |
| 96 | + $statement->execute(); |
| 97 | + return $statement->fetchAll(\PDO::FETCH_ASSOC); |
| 98 | + } |
| 99 | + |
| 100 | + public static function deleteCustomerById($cus_id) |
| 101 | + { |
| 102 | + $db = Application::$app->db; // Ensure this points to the correct Database instance |
| 103 | + $sql = "DELETE FROM customer WHERE cus_id = :cus_id"; |
| 104 | + $stmt = $db->prepare($sql); |
| 105 | + $stmt->bindParam(':cus_id', $cus_id, \PDO::PARAM_INT); |
| 106 | + return $stmt->execute(); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + public static function deleteTechnicianById($cus_id) |
| 111 | + { |
| 112 | + $db = Application::$app->db; // Ensure this points to the correct Database instance |
| 113 | + $sql = "DELETE FROM technician WHERE tech_id = :tech_id"; |
| 114 | + $stmt = $db->prepare($sql); |
| 115 | + $stmt->bindParam(':tech_id', $tech_id, \PDO::PARAM_INT); |
| 116 | + return $stmt->execute(); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments