| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- class ContactPerson {
- private $conn;
- private $table_name = "contact_persons";
- public $id;
- public $client_id;
- public $first_name;
- public $last_name;
- public $email;
- public $phone;
- public $position;
- public $is_primary;
- public $created_at;
- public $updated_at;
- public function __construct($db) {
- $this->conn = $db;
- }
- public function create() {
- $query = "INSERT INTO " . $this->table_name . " SET client_id=:client_id, first_name=:first_name, last_name=:last_name, email=:email, phone=:phone, position=:position, is_primary=:is_primary, created_at=:created_at, updated_at=:updated_at";
- $stmt = $this->conn->prepare($query);
- $this->client_id = htmlspecialchars(strip_tags($this->client_id));
- $this->first_name = htmlspecialchars(strip_tags($this->first_name));
- $this->last_name = htmlspecialchars(strip_tags($this->last_name));
- $this->email = htmlspecialchars(strip_tags($this->email));
- $this->phone = htmlspecialchars(strip_tags($this->phone));
- $this->position = htmlspecialchars(strip_tags($this->position));
- $this->is_primary = $this->is_primary ? 1 : 0;
- $this->created_at = date('Y-m-d H:i:s');
- $this->updated_at = date('Y-m-d H:i:s');
- $stmt->bindParam(":client_id", $this->client_id);
- $stmt->bindParam(":first_name", $this->first_name);
- $stmt->bindParam(":last_name", $this->last_name);
- $stmt->bindParam(":email", $this->email);
- $stmt->bindParam(":phone", $this->phone);
- $stmt->bindParam(":position", $this->position);
- $stmt->bindParam(":is_primary", $this->is_primary);
- $stmt->bindParam(":created_at", $this->created_at);
- $stmt->bindParam(":updated_at", $this->updated_at);
- if($stmt->execute()) {
- return true;
- }
- return false;
- }
- public function read() {
- $query = "SELECT * FROM " . $this->table_name . " WHERE client_id = ? ORDER BY is_primary DESC, last_name ASC, first_name ASC";
- $stmt = $this->conn->prepare($query);
- $stmt->bindParam(1, $this->client_id);
- $stmt->execute();
- return $stmt;
- }
- public function readOne() {
- $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
- $stmt = $this->conn->prepare($query);
- $stmt->bindParam(1, $this->id);
- $stmt->execute();
- $row = $stmt->fetch(PDO::FETCH_ASSOC);
- $this->client_id = $row['client_id'];
- $this->first_name = $row['first_name'];
- $this->last_name = $row['last_name'];
- $this->email = $row['email'];
- $this->phone = $row['phone'];
- $this->position = $row['position'];
- $this->is_primary = $row['is_primary'];
- $this->created_at = $row['created_at'];
- $this->updated_at = $row['updated_at'];
- }
- public function update() {
- $query = "UPDATE " . $this->table_name . " SET client_id=:client_id, first_name=:first_name, last_name=:last_name, email=:email, phone=:phone, position=:position, is_primary=:is_primary, updated_at=:updated_at WHERE id=:id";
- $stmt = $this->conn->prepare($query);
- $this->client_id = htmlspecialchars(strip_tags($this->client_id));
- $this->first_name = htmlspecialchars(strip_tags($this->first_name));
- $this->last_name = htmlspecialchars(strip_tags($this->last_name));
- $this->email = htmlspecialchars(strip_tags($this->email));
- $this->phone = htmlspecialchars(strip_tags($this->phone));
- $this->position = htmlspecialchars(strip_tags($this->position));
- $this->is_primary = $this->is_primary ? 1 : 0;
- $this->updated_at = date('Y-m-d H:i:s');
- $stmt->bindParam(":client_id", $this->client_id);
- $stmt->bindParam(":first_name", $this->first_name);
- $stmt->bindParam(":last_name", $this->last_name);
- $stmt->bindParam(":email", $this->email);
- $stmt->bindParam(":phone", $this->phone);
- $stmt->bindParam(":position", $this->position);
- $stmt->bindParam(":is_primary", $this->is_primary);
- $stmt->bindParam(":updated_at", $this->updated_at);
- $stmt->bindParam(":id", $this->id);
- if($stmt->execute()) {
- return true;
- }
- return false;
- }
- public function delete() {
- $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
- $stmt = $this->conn->prepare($query);
- $stmt->bindParam(1, $this->id);
- if($stmt->execute()) {
- return true;
- }
- return false;
- }
- public function getFullName() {
- return trim($this->first_name . ' ' . $this->last_name);
- }
- public function getDisplayName() {
- $name = $this->getFullName();
- if (!empty($this->position)) {
- $name .= ' - ' . $this->position;
- }
- if ($this->is_primary) {
- $name .= ' (Primary)';
- }
- return $name;
- }
- }
- ?>
|