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