ContactPerson.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. class ContactPerson {
  3. private $conn;
  4. private $table_name = "contact_persons";
  5. public $id;
  6. public $client_id;
  7. public $first_name;
  8. public $last_name;
  9. public $email;
  10. public $phone;
  11. public $position;
  12. public $is_primary;
  13. public $created_at;
  14. public $updated_at;
  15. public function __construct($db) {
  16. $this->conn = $db;
  17. }
  18. public function create() {
  19. $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";
  20. $stmt = $this->conn->prepare($query);
  21. $this->client_id = htmlspecialchars(strip_tags($this->client_id));
  22. $this->first_name = htmlspecialchars(strip_tags($this->first_name));
  23. $this->last_name = htmlspecialchars(strip_tags($this->last_name));
  24. $this->email = htmlspecialchars(strip_tags($this->email));
  25. $this->phone = htmlspecialchars(strip_tags($this->phone));
  26. $this->position = htmlspecialchars(strip_tags($this->position));
  27. $this->is_primary = $this->is_primary ? 1 : 0;
  28. $this->created_at = date('Y-m-d H:i:s');
  29. $this->updated_at = date('Y-m-d H:i:s');
  30. $stmt->bindParam(":client_id", $this->client_id);
  31. $stmt->bindParam(":first_name", $this->first_name);
  32. $stmt->bindParam(":last_name", $this->last_name);
  33. $stmt->bindParam(":email", $this->email);
  34. $stmt->bindParam(":phone", $this->phone);
  35. $stmt->bindParam(":position", $this->position);
  36. $stmt->bindParam(":is_primary", $this->is_primary);
  37. $stmt->bindParam(":created_at", $this->created_at);
  38. $stmt->bindParam(":updated_at", $this->updated_at);
  39. if($stmt->execute()) {
  40. return true;
  41. }
  42. return false;
  43. }
  44. public function read() {
  45. $query = "SELECT * FROM " . $this->table_name . " WHERE client_id = ? ORDER BY is_primary DESC, last_name ASC, first_name ASC";
  46. $stmt = $this->conn->prepare($query);
  47. $stmt->bindParam(1, $this->client_id);
  48. $stmt->execute();
  49. return $stmt;
  50. }
  51. public function readOne() {
  52. $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
  53. $stmt = $this->conn->prepare($query);
  54. $stmt->bindParam(1, $this->id);
  55. $stmt->execute();
  56. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  57. $this->client_id = $row['client_id'];
  58. $this->first_name = $row['first_name'];
  59. $this->last_name = $row['last_name'];
  60. $this->email = $row['email'];
  61. $this->phone = $row['phone'];
  62. $this->position = $row['position'];
  63. $this->is_primary = $row['is_primary'];
  64. $this->created_at = $row['created_at'];
  65. $this->updated_at = $row['updated_at'];
  66. }
  67. public function update() {
  68. $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";
  69. $stmt = $this->conn->prepare($query);
  70. $this->client_id = htmlspecialchars(strip_tags($this->client_id));
  71. $this->first_name = htmlspecialchars(strip_tags($this->first_name));
  72. $this->last_name = htmlspecialchars(strip_tags($this->last_name));
  73. $this->email = htmlspecialchars(strip_tags($this->email));
  74. $this->phone = htmlspecialchars(strip_tags($this->phone));
  75. $this->position = htmlspecialchars(strip_tags($this->position));
  76. $this->is_primary = $this->is_primary ? 1 : 0;
  77. $this->updated_at = date('Y-m-d H:i:s');
  78. $stmt->bindParam(":client_id", $this->client_id);
  79. $stmt->bindParam(":first_name", $this->first_name);
  80. $stmt->bindParam(":last_name", $this->last_name);
  81. $stmt->bindParam(":email", $this->email);
  82. $stmt->bindParam(":phone", $this->phone);
  83. $stmt->bindParam(":position", $this->position);
  84. $stmt->bindParam(":is_primary", $this->is_primary);
  85. $stmt->bindParam(":updated_at", $this->updated_at);
  86. $stmt->bindParam(":id", $this->id);
  87. if($stmt->execute()) {
  88. return true;
  89. }
  90. return false;
  91. }
  92. public function delete() {
  93. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  94. $stmt = $this->conn->prepare($query);
  95. $stmt->bindParam(1, $this->id);
  96. if($stmt->execute()) {
  97. return true;
  98. }
  99. return false;
  100. }
  101. public function getFullName() {
  102. return trim($this->first_name . ' ' . $this->last_name);
  103. }
  104. public function getDisplayName() {
  105. $name = $this->getFullName();
  106. if (!empty($this->position)) {
  107. $name .= ' - ' . $this->position;
  108. }
  109. if ($this->is_primary) {
  110. $name .= ' (Primary)';
  111. }
  112. return $name;
  113. }
  114. }
  115. ?>