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

@@ -15,6 +15,26 @@ class CategoryRepository
6 => 'alfabetycznie - Z - A',
];
private const SORT_ORDER_SQL = [
0 => 'q1.date_add ASC',
1 => 'q1.date_add DESC',
2 => 'q1.date_modify ASC',
3 => 'q1.date_modify DESC',
4 => 'q1.o ASC',
5 => 'q1.name ASC',
6 => 'q1.name DESC',
];
private const PRODUCTS_PER_PAGE = 12;
private const LANGUAGE_FALLBACK_NAME_SQL = '(CASE '
. 'WHEN copy_from IS NULL THEN name '
. 'WHEN copy_from IS NOT NULL THEN ('
. 'SELECT name FROM pp_shop_products_langs '
. 'WHERE lang_id = pspl.copy_from AND product_id = psp.id'
. ') '
. 'END) AS name';
public function __construct($db)
{
$this->db = $db;
@@ -317,6 +337,330 @@ class CategoryRepository
return (string)$title[0];
}
// ===== Frontend methods =====
public function getCategorySort(int $categoryId): int
{
if ($categoryId <= 0) {
return 0;
}
$cacheHandler = new \Shared\Cache\CacheHandler();
$cacheKey = "get_category_sort:$categoryId";
$objectData = $cacheHandler->get($cacheKey);
if ($objectData) {
return (int)unserialize($objectData);
}
$sortType = (int)$this->db->get('pp_shop_categories', 'sort_type', ['id' => $categoryId]);
$cacheHandler->set($cacheKey, $sortType);
return $sortType;
}
public function categoryName(int $categoryId, string $langId): string
{
if ($categoryId <= 0 || $langId === '') {
return '';
}
$cacheHandler = new \Shared\Cache\CacheHandler();
$cacheKey = "category_name:{$langId}:{$categoryId}";
$objectData = $cacheHandler->get($cacheKey);
if ($objectData) {
return (string)unserialize($objectData);
}
$name = $this->db->get('pp_shop_categories_langs', 'title', [
'AND' => [
'category_id' => $categoryId,
'lang_id' => $langId,
],
]);
$cacheHandler->set($cacheKey, $name);
return (string)$name;
}
public function categoryUrl(int $categoryId, string $langId): string
{
if ($categoryId <= 0) {
return '#';
}
$category = $this->frontCategoryDetails($categoryId, $langId);
if (empty($category)) {
return '#';
}
$url = !empty($category['language']['seo_link'])
? '/' . $category['language']['seo_link']
: '/k-' . $category['id'] . '-' . \Shared\Helpers\Helpers::seo($category['language']['title'] ?? '');
$currentLang = \Shared\Helpers\Helpers::get_session('current-lang');
$defaultLang = (new \Domain\Languages\LanguagesRepository($this->db))->defaultLanguage();
if ($currentLang != $defaultLang && $url !== '#') {
$url = '/' . $currentLang . $url;
}
return $url;
}
/**
* @return array<string, mixed>
*/
public function frontCategoryDetails(int $categoryId, string $langId): array
{
if ($categoryId <= 0) {
return [];
}
$cacheHandler = new \Shared\Cache\CacheHandler();
$cacheKey = "front_category_details:{$categoryId}:{$langId}";
$objectData = $cacheHandler->get($cacheKey);
if ($objectData) {
return unserialize($objectData);
}
$category = $this->db->get('pp_shop_categories', '*', ['id' => $categoryId]);
if (!is_array($category)) {
return [];
}
$category['language'] = $this->db->get('pp_shop_categories_langs', '*', [
'AND' => [
'category_id' => $categoryId,
'lang_id' => $langId,
],
]);
$cacheHandler->set($cacheKey, $category);
return $category;
}
/**
* @return array<int, array<string, mixed>>
*/
public function categoriesTree(string $langId, ?int $parentId = null): array
{
$cacheHandler = new \Shared\Cache\CacheHandler();
$cacheKey = "categories_tree:{$langId}:{$parentId}";
$objectData = $cacheHandler->get($cacheKey);
if ($objectData) {
return unserialize($objectData);
}
$categories = [];
$results = $this->db->select('pp_shop_categories', 'id', [
'parent_id' => $parentId,
'ORDER' => ['o' => 'ASC'],
]);
if (is_array($results)) {
foreach ($results as $row) {
$category = $this->frontCategoryDetails((int)$row, $langId);
$category['categories'] = $this->categoriesTree($langId, (int)$row);
$categories[] = $category;
}
}
$cacheHandler->set($cacheKey, $categories);
return $categories;
}
/**
* @return array<int, mixed>
*/
public function blogCategoryProducts(int $categoryId, string $langId, int $limit): array
{
if ($categoryId <= 0 || $langId === '' || $limit <= 0) {
return [];
}
$cacheHandler = new \Shared\Cache\CacheHandler();
$cacheKey = "blog_category_products:{$categoryId}:{$langId}:{$limit}";
$objectData = $cacheHandler->get($cacheKey);
if ($objectData) {
return unserialize($objectData);
}
$rows = $this->db->query(
'SELECT * FROM ('
. 'SELECT '
. 'psp.id, date_modify, date_add, o, '
. self::LANGUAGE_FALLBACK_NAME_SQL . ' '
. 'FROM '
. 'pp_shop_products_categories AS pspc '
. 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
. 'INNER JOIN pp_shop_products_langs AS pspl ON pspl.product_id = pspc.product_id '
. 'WHERE '
. 'status = 1 AND category_id = :category_id AND lang_id = :lang_id'
. ') AS q1 '
. 'WHERE '
. 'q1.name IS NOT NULL '
. 'ORDER BY '
. 'RAND() '
. 'LIMIT ' . (int)$limit,
[
':category_id' => $categoryId,
':lang_id' => $langId,
]
);
$output = [];
if ($rows) {
foreach ($rows->fetchAll() as $row) {
$output[] = $row['id'];
}
}
$cacheHandler->set($cacheKey, $output);
return $output;
}
public function categoryProductsCount(int $categoryId, string $langId): int
{
if ($categoryId <= 0 || $langId === '') {
return 0;
}
$cacheHandler = new \Shared\Cache\CacheHandler();
$cacheKey = "category_products_count:{$categoryId}:{$langId}";
$objectData = $cacheHandler->get($cacheKey);
if ($objectData) {
return (int)unserialize($objectData);
}
$rows = $this->db->query(
'SELECT COUNT(0) FROM ('
. 'SELECT '
. 'psp.id, '
. self::LANGUAGE_FALLBACK_NAME_SQL . ' '
. 'FROM '
. 'pp_shop_products_categories AS pspc '
. 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
. 'INNER JOIN pp_shop_products_langs AS pspl ON pspl.product_id = pspc.product_id '
. 'WHERE '
. 'status = 1 AND category_id = :category_id AND lang_id = :lang_id'
. ') AS q1 '
. 'WHERE '
. 'q1.name IS NOT NULL',
[
':category_id' => $categoryId,
':lang_id' => $langId,
]
);
$productsCount = 0;
if ($rows) {
$result = $rows->fetchAll();
$productsCount = (int)($result[0][0] ?? 0);
}
$cacheHandler->set($cacheKey, $productsCount);
return $productsCount;
}
/**
* @return array<int, mixed>
*/
public function productsId(int $categoryId, int $sortType, string $langId, int $productsLimit, int $from): array
{
if ($categoryId <= 0 || $langId === '') {
return [];
}
$order = self::SORT_ORDER_SQL[$sortType] ?? self::SORT_ORDER_SQL[0];
$today = date('Y-m-d');
$cacheHandler = new \Shared\Cache\CacheHandler();
$cacheKey = "products_id:{$categoryId}:{$sortType}:{$langId}:{$productsLimit}:{$from}";
$objectData = $cacheHandler->get($cacheKey);
if ($objectData) {
return unserialize($objectData);
}
$rows = $this->db->query(
'SELECT * FROM ('
. 'SELECT '
. 'psp.id, date_modify, date_add, o, '
. self::LANGUAGE_FALLBACK_NAME_SQL . ', '
. '(CASE '
. 'WHEN new_to_date >= :today THEN new_to_date '
. 'WHEN new_to_date < :today2 THEN null '
. 'END) AS new_to_date, '
. '(CASE WHEN (quantity + (SELECT IFNULL(SUM(quantity),0) FROM pp_shop_products WHERE parent_id = psp.id)) > 0 THEN 1 ELSE 0 END) AS total_quantity '
. 'FROM '
. 'pp_shop_products_categories AS pspc '
. 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
. 'INNER JOIN pp_shop_products_langs AS pspl ON pspl.product_id = pspc.product_id '
. 'WHERE '
. 'status = 1 AND category_id = :category_id AND lang_id = :lang_id'
. ') AS q1 '
. 'WHERE '
. 'q1.name IS NOT NULL '
. 'ORDER BY '
. $order . ' '
. 'LIMIT ' . (int)$from . ',' . (int)$productsLimit,
[
':category_id' => $categoryId,
':lang_id' => $langId,
':today' => $today,
':today2' => $today,
]
);
$output = [];
if ($rows) {
foreach ($rows->fetchAll() as $row) {
$output[] = $row['id'];
}
}
$cacheHandler->set($cacheKey, $output);
return $output;
}
/**
* @return array{products: array, ls: int}
*/
public function paginatedCategoryProducts(int $categoryId, int $sortType, string $langId, int $page): array
{
$count = $this->categoryProductsCount($categoryId, $langId);
if ($count <= 0) {
return ['products' => [], 'ls' => 0];
}
$totalPages = (int)ceil($count / self::PRODUCTS_PER_PAGE);
if ($page < 1) {
$page = 1;
} elseif ($page > $totalPages) {
$page = $totalPages;
}
$from = self::PRODUCTS_PER_PAGE * ($page - 1);
return [
'products' => $this->productsId($categoryId, $sortType, $langId, self::PRODUCTS_PER_PAGE, $from),
'ls' => $totalPages,
];
}
private function maxOrder(): int
{
return (int)$this->db->max('pp_shop_categories', 'o');

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);