ContactPerson.php 5.6 KB

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