Files
drivetimeplaner/www/fahrschultermin.de/app/Repositories/InstructorRepository.php

120 lines
4.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Repositories;
final class InstructorRepository extends BaseRepository
{
public function allForTenant(int $tenantId): array
{
$statement = $this->db->prepare('SELECT * FROM instructors WHERE tenant_id = :tenant_id ORDER BY last_name, first_name');
$statement->execute(['tenant_id' => $tenantId]);
return $statement->fetchAll();
}
public function findByUserId(int $tenantId, int $userId): ?array
{
$statement = $this->db->prepare(
'SELECT * FROM instructors WHERE tenant_id = :tenant_id AND user_id = :user_id LIMIT 1'
);
$statement->execute(['tenant_id' => $tenantId, 'user_id' => $userId]);
return $statement->fetch() ?: null;
}
public function create(int $tenantId, array $data): array
{
$timestamp = $this->now();
$statement = $this->db->prepare(
'INSERT INTO instructors
(tenant_id, user_id, first_name, last_name, color, notes, is_active, pre_start_buffer_minutes, created_at, updated_at)
VALUES (:tenant_id, :user_id, :first_name, :last_name, :color, :notes, :is_active, :pre_start_buffer_minutes, :created_at, :updated_at)'
);
$statement->execute([
'tenant_id' => $tenantId,
'user_id' => $data['user_id'] ?? null,
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'color' => $data['color'],
'notes' => $data['notes'] ?? '',
'is_active' => !empty($data['is_active']) ? 1 : 0,
'pre_start_buffer_minutes' => max(0, (int) ($data['pre_start_buffer_minutes'] ?? 0)),
'created_at' => $timestamp,
'updated_at' => $timestamp,
]);
return $this->find($tenantId, (int) $this->lastInsertId());
}
public function update(int $tenantId, int $id, array $data): ?array
{
$statement = $this->db->prepare(
'UPDATE instructors
SET user_id = :user_id, first_name = :first_name, last_name = :last_name, color = :color,
notes = :notes, is_active = :is_active, pre_start_buffer_minutes = :pre_start_buffer_minutes, updated_at = :updated_at
WHERE tenant_id = :tenant_id AND id = :id'
);
$statement->execute([
'tenant_id' => $tenantId,
'id' => $id,
'user_id' => $data['user_id'] ?? null,
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'color' => $data['color'],
'notes' => $data['notes'] ?? '',
'is_active' => !empty($data['is_active']) ? 1 : 0,
'pre_start_buffer_minutes' => max(0, (int) ($data['pre_start_buffer_minutes'] ?? 0)),
'updated_at' => $this->now(),
]);
return $this->find($tenantId, $id);
}
public function find(int $tenantId, int $id): ?array
{
$statement = $this->db->prepare('SELECT * FROM instructors WHERE tenant_id = :tenant_id AND id = :id');
$statement->execute(['tenant_id' => $tenantId, 'id' => $id]);
return $statement->fetch() ?: null;
}
public function findById(int $id): ?array
{
$statement = $this->db->prepare('SELECT * FROM instructors WHERE id = :id');
$statement->execute(['id' => $id]);
return $statement->fetch() ?: null;
}
public function listForTenant(int $tenantId): array
{
$statement = $this->db->prepare('SELECT * FROM instructors WHERE tenant_id = :tenant_id AND is_active = 1 ORDER BY last_name, first_name');
$statement->execute(['tenant_id' => $tenantId]);
return $statement->fetchAll();
}
public function updateSettings(int $instructorId, array $settings): bool
{
$allowed = ['max_daily_units', 'max_daily_override', 'max_weekly_units', 'max_weekly_override', 'booking_enabled'];
$sets = [];
$params = ['id' => $instructorId];
foreach ($allowed as $key) {
if (array_key_exists($key, $settings)) {
$sets[] = "{$key} = :{$key}";
$params[$key] = $settings[$key];
}
}
if (empty($sets)) {
return false;
}
$sql = 'UPDATE instructors SET ' . implode(', ', $sets) . ', updated_at = :updated_at WHERE id = :id';
$params['updated_at'] = $this->now();
$statement = $this->db->prepare($sql);
$statement->execute($params);
return (bool) $statement->rowCount();
}
}