conn = $db; } public function create() { $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"; $stmt = $this->conn->prepare($query); $this->company_name = htmlspecialchars(strip_tags($this->company_name)); $this->y_tunnus = htmlspecialchars(strip_tags($this->y_tunnus)); $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->address = htmlspecialchars(strip_tags($this->address)); $this->city = htmlspecialchars(strip_tags($this->city)); $this->state = htmlspecialchars(strip_tags($this->state)); $this->postal_code = htmlspecialchars(strip_tags($this->postal_code)); $this->country = htmlspecialchars(strip_tags($this->country)); $this->notes = htmlspecialchars(strip_tags($this->notes)); $this->hour_price = htmlspecialchars(strip_tags($this->hour_price)); $this->created_at = date('Y-m-d H:i:s'); $this->updated_at = date('Y-m-d H:i:s'); $stmt->bindParam(":company_name", $this->company_name); $stmt->bindParam(":y_tunnus", $this->y_tunnus); $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(":address", $this->address); $stmt->bindParam(":city", $this->city); $stmt->bindParam(":state", $this->state); $stmt->bindParam(":postal_code", $this->postal_code); $stmt->bindParam(":country", $this->country); $stmt->bindParam(":notes", $this->notes); $stmt->bindParam(":hour_price", $this->hour_price); $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 . " ORDER BY last_name ASC, first_name ASC"; $stmt = $this->conn->prepare($query); $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->company_name = $row['company_name']; $this->y_tunnus = $row['y_tunnus']; $this->first_name = $row['first_name']; $this->last_name = $row['last_name']; $this->email = $row['email']; $this->phone = $row['phone']; $this->address = $row['address']; $this->city = $row['city']; $this->state = $row['state']; $this->postal_code = $row['postal_code']; $this->country = $row['country']; $this->notes = $row['notes']; $this->hour_price = $row['hour_price']; $this->created_at = $row['created_at']; $this->updated_at = $row['updated_at']; } public function update() { $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"; $stmt = $this->conn->prepare($query); $this->company_name = htmlspecialchars(strip_tags($this->company_name)); $this->y_tunnus = htmlspecialchars(strip_tags($this->y_tunnus)); $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->address = htmlspecialchars(strip_tags($this->address)); $this->city = htmlspecialchars(strip_tags($this->city)); $this->state = htmlspecialchars(strip_tags($this->state)); $this->postal_code = htmlspecialchars(strip_tags($this->postal_code)); $this->country = htmlspecialchars(strip_tags($this->country)); $this->notes = htmlspecialchars(strip_tags($this->notes)); $this->hour_price = htmlspecialchars(strip_tags($this->hour_price)); $this->updated_at = date('Y-m-d H:i:s'); $stmt->bindParam(":company_name", $this->company_name); $stmt->bindParam(":y_tunnus", $this->y_tunnus); $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(":address", $this->address); $stmt->bindParam(":city", $this->city); $stmt->bindParam(":state", $this->state); $stmt->bindParam(":postal_code", $this->postal_code); $stmt->bindParam(":country", $this->country); $stmt->bindParam(":notes", $this->notes); $stmt->bindParam(":hour_price", $this->hour_price); $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 search($search_term) { $query = "SELECT * FROM " . $this->table_name . " WHERE first_name LIKE ? OR last_name LIKE ? OR company_name LIKE ? OR email LIKE ? OR phone LIKE ? ORDER BY last_name ASC, first_name ASC"; $stmt = $this->conn->prepare($query); $search_term = "%{$search_term}%"; $stmt->bindParam(1, $search_term); $stmt->bindParam(2, $search_term); $stmt->bindParam(3, $search_term); $stmt->bindParam(4, $search_term); $stmt->bindParam(5, $search_term); $stmt->execute(); return $stmt; } public function getFullName() { return trim($this->first_name . ' ' . $this->last_name); } public function getDisplayName() { if (!empty($this->company_name)) { return $this->company_name . ' (' . $this->getFullName() . ')'; } return $this->getFullName(); } } ?>