Client.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. class Client {
  3. private $conn;
  4. private $table_name = "clients";
  5. public $id;
  6. public $company_name;
  7. public $y_tunnus;
  8. public $first_name;
  9. public $last_name;
  10. public $email;
  11. public $phone;
  12. public $address;
  13. public $city;
  14. public $state;
  15. public $postal_code;
  16. public $country;
  17. public $notes;
  18. public $created_at;
  19. public $updated_at;
  20. public function __construct($db) {
  21. $this->conn = $db;
  22. }
  23. public function create() {
  24. $query = "INSERT INTO " . $this->table_name . " SET company_name=:company_name, y_tunnus=:y_tunnus, first_name=:first_name, last_name=:last_name, email=:email, phone=:phone, address=:address, city=:city, state=:state, postal_code=:postal_code, country=:country, notes=:notes, created_at=:created_at, updated_at=:updated_at";
  25. $stmt = $this->conn->prepare($query);
  26. $this->company_name = htmlspecialchars(strip_tags($this->company_name));
  27. $this->y_tunnus = htmlspecialchars(strip_tags($this->y_tunnus));
  28. $this->first_name = htmlspecialchars(strip_tags($this->first_name));
  29. $this->last_name = htmlspecialchars(strip_tags($this->last_name));
  30. $this->email = htmlspecialchars(strip_tags($this->email));
  31. $this->phone = htmlspecialchars(strip_tags($this->phone));
  32. $this->address = htmlspecialchars(strip_tags($this->address));
  33. $this->city = htmlspecialchars(strip_tags($this->city));
  34. $this->state = htmlspecialchars(strip_tags($this->state));
  35. $this->postal_code = htmlspecialchars(strip_tags($this->postal_code));
  36. $this->country = htmlspecialchars(strip_tags($this->country));
  37. $this->notes = htmlspecialchars(strip_tags($this->notes));
  38. $this->created_at = date('Y-m-d H:i:s');
  39. $this->updated_at = date('Y-m-d H:i:s');
  40. $stmt->bindParam(":company_name", $this->company_name);
  41. $stmt->bindParam(":y_tunnus", $this->y_tunnus);
  42. $stmt->bindParam(":first_name", $this->first_name);
  43. $stmt->bindParam(":last_name", $this->last_name);
  44. $stmt->bindParam(":email", $this->email);
  45. $stmt->bindParam(":phone", $this->phone);
  46. $stmt->bindParam(":address", $this->address);
  47. $stmt->bindParam(":city", $this->city);
  48. $stmt->bindParam(":state", $this->state);
  49. $stmt->bindParam(":postal_code", $this->postal_code);
  50. $stmt->bindParam(":country", $this->country);
  51. $stmt->bindParam(":notes", $this->notes);
  52. $stmt->bindParam(":created_at", $this->created_at);
  53. $stmt->bindParam(":updated_at", $this->updated_at);
  54. if($stmt->execute()) {
  55. return true;
  56. }
  57. return false;
  58. }
  59. public function read() {
  60. $query = "SELECT * FROM " . $this->table_name . " ORDER BY last_name ASC, first_name ASC";
  61. $stmt = $this->conn->prepare($query);
  62. $stmt->execute();
  63. return $stmt;
  64. }
  65. public function readOne() {
  66. $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";
  67. $stmt = $this->conn->prepare($query);
  68. $stmt->bindParam(1, $this->id);
  69. $stmt->execute();
  70. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  71. $this->company_name = $row['company_name'];
  72. $this->y_tunnus = $row['y_tunnus'];
  73. $this->first_name = $row['first_name'];
  74. $this->last_name = $row['last_name'];
  75. $this->email = $row['email'];
  76. $this->phone = $row['phone'];
  77. $this->address = $row['address'];
  78. $this->city = $row['city'];
  79. $this->state = $row['state'];
  80. $this->postal_code = $row['postal_code'];
  81. $this->country = $row['country'];
  82. $this->notes = $row['notes'];
  83. $this->created_at = $row['created_at'];
  84. $this->updated_at = $row['updated_at'];
  85. }
  86. public function update() {
  87. $query = "UPDATE " . $this->table_name . " SET company_name=:company_name, y_tunnus=:y_tunnus, first_name=:first_name, last_name=:last_name, email=:email, phone=:phone, address=:address, city=:city, state=:state, postal_code=:postal_code, country=:country, notes=:notes, updated_at=:updated_at WHERE id=:id";
  88. $stmt = $this->conn->prepare($query);
  89. $this->company_name = htmlspecialchars(strip_tags($this->company_name));
  90. $this->y_tunnus = htmlspecialchars(strip_tags($this->y_tunnus));
  91. $this->first_name = htmlspecialchars(strip_tags($this->first_name));
  92. $this->last_name = htmlspecialchars(strip_tags($this->last_name));
  93. $this->email = htmlspecialchars(strip_tags($this->email));
  94. $this->phone = htmlspecialchars(strip_tags($this->phone));
  95. $this->address = htmlspecialchars(strip_tags($this->address));
  96. $this->city = htmlspecialchars(strip_tags($this->city));
  97. $this->state = htmlspecialchars(strip_tags($this->state));
  98. $this->postal_code = htmlspecialchars(strip_tags($this->postal_code));
  99. $this->country = htmlspecialchars(strip_tags($this->country));
  100. $this->notes = htmlspecialchars(strip_tags($this->notes));
  101. $this->updated_at = date('Y-m-d H:i:s');
  102. $stmt->bindParam(":company_name", $this->company_name);
  103. $stmt->bindParam(":y_tunnus", $this->y_tunnus);
  104. $stmt->bindParam(":first_name", $this->first_name);
  105. $stmt->bindParam(":last_name", $this->last_name);
  106. $stmt->bindParam(":email", $this->email);
  107. $stmt->bindParam(":phone", $this->phone);
  108. $stmt->bindParam(":address", $this->address);
  109. $stmt->bindParam(":city", $this->city);
  110. $stmt->bindParam(":state", $this->state);
  111. $stmt->bindParam(":postal_code", $this->postal_code);
  112. $stmt->bindParam(":country", $this->country);
  113. $stmt->bindParam(":notes", $this->notes);
  114. $stmt->bindParam(":updated_at", $this->updated_at);
  115. $stmt->bindParam(":id", $this->id);
  116. if($stmt->execute()) {
  117. return true;
  118. }
  119. return false;
  120. }
  121. public function delete() {
  122. $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";
  123. $stmt = $this->conn->prepare($query);
  124. $stmt->bindParam(1, $this->id);
  125. if($stmt->execute()) {
  126. return true;
  127. }
  128. return false;
  129. }
  130. public function search($search_term) {
  131. $query = "SELECT * FROM " . $this->table_name . " WHERE
  132. first_name LIKE ? OR
  133. last_name LIKE ? OR
  134. company_name LIKE ? OR
  135. email LIKE ? OR
  136. phone LIKE ?
  137. ORDER BY last_name ASC, first_name ASC";
  138. $stmt = $this->conn->prepare($query);
  139. $search_term = "%{$search_term}%";
  140. $stmt->bindParam(1, $search_term);
  141. $stmt->bindParam(2, $search_term);
  142. $stmt->bindParam(3, $search_term);
  143. $stmt->bindParam(4, $search_term);
  144. $stmt->bindParam(5, $search_term);
  145. $stmt->execute();
  146. return $stmt;
  147. }
  148. public function getFullName() {
  149. return trim($this->first_name . ' ' . $this->last_name);
  150. }
  151. public function getDisplayName() {
  152. if (!empty($this->company_name)) {
  153. return $this->company_name . ' (' . $this->getFullName() . ')';
  154. }
  155. return $this->getFullName();
  156. }
  157. }
  158. ?>