ver. 0.289: ShopCategory + ShopClient frontend migration to Domain + Views + Controllers

ShopCategory: 9 frontend methods in CategoryRepository, front\Views\ShopCategory (3 methods),
deleted factory + view, updated 6 callers, +17 tests.

ShopClient: 13 frontend methods in ClientRepository, front\Views\ShopClient (8 methods),
front\Controllers\ShopClientController (15 methods + buildEmailBody helper),
deleted factory + view + controls, updated 7 callers, +36 tests.

Security fix: removed hardcoded password bypass 'Legia1916'.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-17 10:41:40 +01:00
parent 437d4c78dc
commit d29d396197
34 changed files with 2049 additions and 961 deletions

View File

@@ -234,6 +234,287 @@ class ClientRepository
];
}
// ===== Frontend methods =====
/**
* @return array<string, mixed>|null
*/
public function clientDetails(int $clientId): ?array
{
if ($clientId <= 0) {
return null;
}
return $this->db->get('pp_shop_clients', '*', ['id' => $clientId]) ?: null;
}
public function clientEmail(int $clientId): ?string
{
if ($clientId <= 0) {
return null;
}
$email = $this->db->get('pp_shop_clients', 'email', ['id' => $clientId]);
return $email ? (string)$email : null;
}
/**
* @return array<int, array<string, mixed>>
*/
public function clientAddresses(int $clientId): array
{
if ($clientId <= 0) {
return [];
}
$rows = $this->db->select('pp_shop_clients_addresses', '*', ['client_id' => $clientId]);
return is_array($rows) ? $rows : [];
}
/**
* @return array<string, mixed>|null
*/
public function addressDetails(int $addressId): ?array
{
if ($addressId <= 0) {
return null;
}
return $this->db->get('pp_shop_clients_addresses', '*', ['id' => $addressId]) ?: null;
}
public function addressDelete(int $addressId): bool
{
if ($addressId <= 0) {
return false;
}
return (bool)$this->db->delete('pp_shop_clients_addresses', ['id' => $addressId]);
}
/**
* @param array<string, string> $data Keys: name, surname, street, postal_code, city, phone
*/
public function addressSave(int $clientId, ?int $addressId, array $data): bool
{
if ($clientId <= 0) {
return false;
}
$row = [
'name' => (string)($data['name'] ?? ''),
'surname' => (string)($data['surname'] ?? ''),
'street' => (string)($data['street'] ?? ''),
'postal_code' => (string)($data['postal_code'] ?? ''),
'city' => (string)($data['city'] ?? ''),
'phone' => (string)($data['phone'] ?? ''),
];
if (!$addressId || $addressId <= 0) {
$row['client_id'] = $clientId;
return (bool)$this->db->insert('pp_shop_clients_addresses', $row);
}
return (bool)$this->db->update('pp_shop_clients_addresses', $row, [
'AND' => [
'client_id' => $clientId,
'id' => $addressId,
],
]);
}
public function markAddressAsCurrent(int $clientId, int $addressId): bool
{
if ($clientId <= 0 || $addressId <= 0) {
return false;
}
$this->db->update('pp_shop_clients_addresses', ['current' => 0], ['client_id' => $clientId]);
$this->db->update('pp_shop_clients_addresses', ['current' => 1], [
'AND' => [
'client_id' => $clientId,
'id' => $addressId,
],
]);
return true;
}
/**
* @return array<int, array<string, mixed>>
*/
public function clientOrders(int $clientId): array
{
if ($clientId <= 0) {
return [];
}
$rows = $this->db->select('pp_shop_orders', 'id', [
'client_id' => $clientId,
'ORDER' => ['date_order' => 'DESC'],
]);
$orders = [];
if (is_array($rows)) {
foreach ($rows as $row) {
$orders[] = \front\factory\ShopOrder::order_details($row);
}
}
return $orders;
}
/**
* @return array{status: string, client?: array, hash?: string, code?: string}
*/
public function authenticate(string $email, string $password): array
{
$email = trim($email);
$password = trim($password);
if ($email === '' || $password === '') {
return ['status' => 'error', 'code' => 'logowanie-nieudane'];
}
$client = $this->db->get('pp_shop_clients', [
'id', 'password', 'register_date', 'hash', 'status',
], ['email' => $email]);
if (!$client) {
return ['status' => 'error', 'code' => 'logowanie-nieudane'];
}
if (!(int)$client['status']) {
return ['status' => 'inactive', 'hash' => $client['hash']];
}
if ($client['password'] !== md5($client['register_date'] . $password)) {
return ['status' => 'error', 'code' => 'logowanie-blad-nieprawidlowe-haslo'];
}
$fullClient = $this->clientDetails((int)$client['id']);
return ['status' => 'ok', 'client' => $fullClient];
}
/**
* @return array{id: int, hash: string}|null Null when email already taken
*/
public function createClient(string $email, string $password, bool $agreementMarketing): ?array
{
$email = trim($email);
if ($email === '' || $password === '') {
return null;
}
if ($this->db->count('pp_shop_clients', ['email' => $email])) {
return null;
}
$hash = md5(time() . $email);
$registerDate = date('Y-m-d H:i:s');
$inserted = $this->db->insert('pp_shop_clients', [
'email' => $email,
'password' => md5($registerDate . $password),
'hash' => $hash,
'agremment_marketing' => $agreementMarketing ? 1 : 0,
'register_date' => $registerDate,
]);
if (!$inserted) {
return null;
}
return [
'id' => (int)$this->db->id(),
'hash' => $hash,
];
}
/**
* Confirms registration. Returns client email on success, null on failure.
*/
public function confirmRegistration(string $hash): ?string
{
$hash = trim($hash);
if ($hash === '') {
return null;
}
$id = $this->db->get('pp_shop_clients', 'id', [
'AND' => ['hash' => $hash, 'status' => 0],
]);
if (!$id) {
return null;
}
$this->db->update('pp_shop_clients', ['status' => 1], ['id' => $id]);
$email = $this->db->get('pp_shop_clients', 'email', ['id' => $id]);
return $email ? (string)$email : null;
}
/**
* Generates new password. Returns [email, password] on success, null on failure.
*
* @return array{email: string, password: string}|null
*/
public function generateNewPassword(string $hash): ?array
{
$hash = trim($hash);
if ($hash === '') {
return null;
}
$data = $this->db->get('pp_shop_clients', ['id', 'email', 'register_date'], [
'AND' => ['hash' => $hash, 'status' => 1, 'password_recovery' => 1],
]);
if (!$data) {
return null;
}
$newPassword = substr(md5(time()), 0, 10);
$this->db->update('pp_shop_clients', [
'password_recovery' => 0,
'password' => md5($data['register_date'] . $newPassword),
], ['id' => $data['id']]);
return [
'email' => (string)$data['email'],
'password' => $newPassword,
];
}
/**
* Initiates password recovery. Returns hash on success, null on failure.
*/
public function initiatePasswordRecovery(string $email): ?string
{
$email = trim($email);
if ($email === '') {
return null;
}
$hash = $this->db->get('pp_shop_clients', 'hash', [
'AND' => ['email' => $email, 'status' => 1],
]);
if (!$hash) {
return null;
}
$this->db->update('pp_shop_clients', ['password_recovery' => 1], ['email' => $email]);
return (string)$hash;
}
private function normalizeTextFilter($value): string
{
$value = trim((string)$value);