| Server IP : 202.61.199.114 / Your IP : 216.73.217.139 Web Server : nginx/1.22.1 System : Linux de.arni-solutions.de 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64 User : web20 ( 1018) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/clients/client2/c.pacim.de/web/app/Models/ |
Upload File : |
<?php
declare(strict_types=1);
namespace App\Models;
use App\Database;
use PDO;
final class Customer
{
private PDO $db;
public function __construct()
{
$this->db = Database::connection();
}
public function search(string $q = '', int $page = 1, int $limit = 20): array
{
$offset = ($page - 1) * $limit;
$params = [];
$where = '1=1';
if ($q !== '') {
$where = "(first_name LIKE :q OR last_name LIKE :q OR email LIKE :q OR phone LIKE :q OR company LIKE :q OR city LIKE :q)";
$params['q'] = '%' . $q . '%';
}
$sql = "SELECT c.*, COUNT(b.id) AS bookings_count, MAX(b.start_date) AS last_booking
FROM customers c
LEFT JOIN bookings b ON b.customer_id = c.id
WHERE $where
GROUP BY c.id
ORDER BY c.last_name ASC
LIMIT :limit OFFSET :offset";
$stmt = $this->db->prepare($sql);
foreach ($params as $k => $v) {
$stmt->bindValue(':' . $k, $v);
}
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll();
}
public function find(int $id): ?array
{
$stmt = $this->db->prepare('SELECT * FROM customers WHERE id = :id');
$stmt->execute(['id' => $id]);
return $stmt->fetch() ?: null;
}
public function update(int $id, array $data): bool
{
$allowed = ['first_name', 'last_name', 'company', 'phone', 'email', 'city', 'street', 'zip'];
$set = [];
$params = ['id' => $id];
foreach ($allowed as $field) {
if (isset($data[$field])) {
$set[] = "$field = :$field";
$params[$field] = $data[$field];
}
}
if (!$set) {
return false;
}
$stmt = $this->db->prepare('UPDATE customers SET ' . implode(', ', $set) . ' WHERE id = :id');
return $stmt->execute($params);
}
public function getBookings(int $customerId): array
{
$stmt = $this->db->prepare('SELECT * FROM bookings WHERE customer_id = :id ORDER BY start_date DESC');
$stmt->execute(['id' => $customerId]);
return $stmt->fetchAll();
}
}