Client.php 7.5 KB

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