refactor(shop_category): migrate admin module to Domain+DI with routing and ajax cleanup

This commit is contained in:
2026-02-15 15:32:23 +01:00
parent c99b42af68
commit d0747b3e0a
63 changed files with 8998 additions and 625 deletions

View File

@@ -0,0 +1,163 @@
<?php
namespace admin\Controllers;
use Domain\Category\CategoryRepository;
use Domain\Languages\LanguagesRepository;
class ShopCategoryController
{
private CategoryRepository $repository;
private LanguagesRepository $languagesRepository;
public function __construct(CategoryRepository $repository, LanguagesRepository $languagesRepository)
{
$this->repository = $repository;
$this->languagesRepository = $languagesRepository;
}
public function view_list(): string
{
return \Tpl::view('shop-category/categories-list', [
'categories' => $this->repository->subcategories(null),
'level' => 0,
'dlang' => \front\factory\Languages::default_language(),
]);
}
public function list(): string
{
return $this->view_list();
}
public function category_edit(): string
{
return \Tpl::view('shop-category/category-edit', [
'category' => $this->repository->categoryDetails(\S::get('id')),
'pid' => \S::get('pid'),
'languages' => $this->languagesRepository->languagesList(),
'sort_types' => $this->repository->sortTypes(),
]);
}
public function edit(): string
{
return $this->category_edit();
}
public function save(): void
{
$response = [
'status' => 'error',
'msg' => 'Podczas zapisywania kategorii wystąpił błąd. Proszę spróbować ponownie.',
];
$values = json_decode((string)\S::get('values'), true);
if (is_array($values)) {
$savedId = $this->repository->save($values);
if (!empty($savedId)) {
$response = [
'status' => 'ok',
'msg' => 'Kategoria została zapisana.',
'id' => (int)$savedId,
];
}
}
echo json_encode($response);
exit;
}
public function category_delete(): void
{
if ($this->repository->categoryDelete(\S::get('id'))) {
\S::set_message('Kategoria została usunięta.');
} else {
\S::alert('Podczas usuwania kategorii wystąpił błąd. Aby usunąć kategorię nie może ona posiadać przypiętych podkategorii.');
}
header('Location: /admin/shop_category/view_list/');
exit;
}
public function delete(): void
{
$this->category_delete();
}
public function category_products(): string
{
return \Tpl::view('shop-category/category-products', [
'category_id' => \S::get('id'),
'products' => $this->repository->categoryProducts((int)\S::get('id')),
]);
}
public function products(): string
{
return $this->category_products();
}
public function category_url_browser(): void
{
echo \Tpl::view('shop-category/category-browse-list', [
'categories' => $this->repository->subcategories(null),
'level' => 0,
'dlang' => \front\factory\Languages::default_language(),
]);
exit;
}
public function save_categories_order(): void
{
$response = [
'status' => 'error',
'msg' => 'Podczas zapisywania kolejności kategorii wystąpił błąd. Proszę spróbować ponownie.',
];
if ( $this->repository->saveCategoriesOrder( \S::get( 'categories' ) ) ) {
$response = [ 'status' => 'ok' ];
}
echo json_encode( $response );
exit;
}
public function save_products_order(): void
{
$response = [
'status' => 'error',
'msg' => 'Podczas zapisywania kolejności wyświetlania produktów wystąpił błąd. Proszę spróbować ponownie.',
];
if ( $this->repository->saveProductOrder( \S::get( 'category_id' ), \S::get( 'products' ) ) ) {
$response = [ 'status' => 'ok' ];
}
echo json_encode( $response );
exit;
}
public function cookie_categories(): void
{
$categoryId = (string) \S::get( 'category_id' );
if ( $categoryId === '' ) {
echo json_encode( [ 'status' => 'error' ] );
exit;
}
$array = [];
if ( isset( $_COOKIE['cookie_categories'] ) ) {
$tmp = @unserialize( (string) $_COOKIE['cookie_categories'] );
if ( is_array( $tmp ) ) {
$array = $tmp;
}
}
$array[$categoryId] = isset( $array[$categoryId] ) && (int) $array[$categoryId] === 1 ? 0 : 1;
setcookie( 'cookie_categories', serialize( $array ), time() + 3600 * 24 * 365, '/' );
echo json_encode( [ 'status' => 'ok' ] );
exit;
}
}

View File

@@ -2,6 +2,7 @@
namespace admin\Controllers;
use Domain\Product\ProductRepository;
use Domain\Category\CategoryRepository;
/**
* Kontroler masowej edycji produktów.
@@ -23,9 +24,11 @@ class ShopProductController
*/
public function mass_edit(): string
{
$categoryRepository = new CategoryRepository( $GLOBALS['mdb'] );
return \Tpl::view( 'shop-product/mass-edit', [
'products' => $this->repository->allProductsForMassEdit(),
'categories' => \admin\factory\ShopCategory::subcategories( null ),
'categories' => $categoryRepository->subcategories( null ),
'dlang' => \front\factory\Languages::default_language()
] );
}

View File

@@ -377,6 +377,14 @@ class Site
new \Domain\Languages\LanguagesRepository( $mdb )
);
},
'ShopCategory' => function() {
global $mdb;
return new \admin\Controllers\ShopCategoryController(
new \Domain\Category\CategoryRepository( $mdb ),
new \Domain\Languages\LanguagesRepository( $mdb )
);
},
'ShopProduct' => function() {
global $mdb;

View File

@@ -1,66 +0,0 @@
<?php
namespace admin\controls;
class ShopCategory
{
public static function category_products()
{
return \Tpl::view( 'shop-category/category-products', [
'category_id' => \S::get( 'id' ),
'products' => \admin\factory\ShopCategory::category_products( (int)\S::get( 'id' ) )
] );
}
public static function category_url_browser() {
echo \Tpl::view( 'shop-category/category-browse-list', [
'categories' => \admin\factory\ShopCategory::subcategories( null ),
'level' => 0,
'dlang' => \front\factory\Languages::default_language()
] );
exit;
}
public static function category_delete()
{
if ( \admin\factory\ShopCategory::category_delete( \S::get( 'id' ) ) )
\S::set_message( 'Kategoria została usunięta.' );
else
\S::alert( 'Podczas usuwania kategorii wystąpił błąd. Aby usunąć kategorię nie może ona posiadać przypiętych podkategorii.' );
header( 'Location: /admin/shop_category/view_list/' );
exit;
}
static public function save()
{
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania kategorii wystąpił błąd. Proszę spróbować ponownie.' ];
$values = json_decode( \S::get( 'values' ), true );
if ( $id = \admin\factory\ShopCategory::save(
$values['id'], $values['title'], $values['text'], $values['text_hidden'], $values['seo_link'], $values['meta_title'], $values['meta_description'], $values['meta_keywords'], $values['parent_id'], $values['status'],
$values['noindex'], $values['category_title'], $values['sort_type'], $values['additional_text'], $values['view_subcategories']
) )
$response = [ 'status' => 'ok', 'msg' => 'Kategoria została zapisana.', 'id' => $id ];
echo json_encode( $response );
exit;
}
public static function category_edit()
{
return \admin\view\ShopCategory::category_edit(
\admin\factory\ShopCategory::category_details(
\S::get( 'id' )
),
\S::get( 'pid' ),
( new \Domain\Languages\LanguagesRepository( $GLOBALS['mdb'] ) )->languagesList()
);
}
public static function view_list()
{
return \Tpl::view( 'shop-category/categories-list', [
'categories' => \admin\factory\ShopCategory::subcategories( null ),
'level' => 0,
'dlang' => \front\factory\Languages::default_language()
] );
}
}

View File

@@ -193,7 +193,7 @@ class ShopProduct
return \Tpl::view( 'shop-product/product-edit', [
'product' => \admin\factory\ShopProduct::product_details( (int) \S::get( 'id' ) ),
'languages' => ( new \Domain\Languages\LanguagesRepository( $GLOBALS['mdb'] ) )->languagesList(),
'categories' => \admin\factory\ShopCategory::subcategories( null ),
'categories' => ( new \Domain\Category\CategoryRepository( $GLOBALS['mdb'] ) )->subcategories( null ),
'layouts' => self::layouts_for_product_edit( $mdb ),
'products' => \admin\factory\ShopProduct::products_list(),
'dlang' => \front\factory\Languages::default_language(),

View File

@@ -1,245 +0,0 @@
<?php
namespace admin\factory;
class ShopCategory
{
public static $_sort_types = [
0 => 'data dodania - najstarsze na początku',
1 => 'data dodania - najnowsze na początku',
2 => 'data modyfikacji - rosnąco',
3 => 'data mofyfikacji - malejąco',
4 => 'ręczne',
5 => 'alfabetycznie - A - Z',
6 => 'alfabetycznie - Z - A'
];
public static function save_product_order( $category_id, $products )
{
global $mdb;
if ( is_array( $products ) )
{
$mdb -> update( 'pp_shop_products_categories', [ 'o' => 0 ], [ 'category_id' => (int)$category_id ] );
for ( $i = 0; $i < count( $products ); $i++ )
{
if ( $products[$i]['item_id'] )
{
$x++;
$mdb -> update( 'pp_shop_products_categories', [ 'o' => $x ], [ 'AND' => [ 'category_id' => (int)$category_id, 'product_id' => $products[ $i ]['item_id'] ] ] );
}
}
}
return true;
}
public static function category_products( $category_id )
{
global $mdb;
$results = $mdb -> query( 'SELECT '
. 'product_id, o, status '
. 'FROM '
. 'pp_shop_products_categories AS pspc '
. 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
. 'WHERE '
. 'category_id = :category_id '
. 'ORDER BY '
. 'o ASC', [
':category_id' => $category_id
] ) -> fetchAll();
if ( is_array( $results ) ) foreach ( $results as $row )
{
$row['name'] = \admin\factory\ShopProduct::product_name( $row['product_id'] );
$products[] = $row;
}
return $products;
}
public static function save_categories_order( $categories )
{
global $mdb;
if ( is_array( $categories ) )
{
$mdb -> update( 'pp_shop_categories', [ 'o' => 0 ] );
for ( $i = 0; $i < count( $categories ); $i++ )
{
if ( $categories[$i]['item_id'] )
{
$categories[$i]['parent_id'] ? $parent_id = $categories[$i]['parent_id'] : $parent_id = null;
$x++;
$mdb -> update( 'pp_shop_categories', [ 'o' => $x, 'parent_id' => $parent_id ], [ 'id' => (int)$categories[ $i ]['item_id'] ] );
}
}
}
\S::delete_dir( '../temp/' );
return true;
}
public static function category_delete( $category_id )
{
global $mdb;
if ( $mdb -> count( 'pp_shop_categories', [ 'parent_id' => (int)$category_id ] ) )
return false;
return $mdb -> delete( 'pp_shop_categories', [ 'id' => (int)$category_id ] );
}
public static function category_languages( $category_id )
{
global $mdb;
return $mdb -> select( 'pp_shop_categories_langs', '*', [ 'AND' => [ 'category_id' => (int)$category_id, 'title[!]' => null ] ] );
}
public static function category_title( $category_id )
{
global $mdb;
$result = $mdb -> select( 'pp_shop_categories_langs', [
'[><]pp_langs' => [ 'lang_id' => 'id' ]
], 'title', [
'AND' => [
'category_id' => (int)$category_id, 'title[!]' => ''
],
'ORDER' => [ 'o' => 'ASC' ],
'LIMIT' => 1
] );
return $result[0];
}
public static function subcategories( $parent_id = null )
{
global $mdb;
$results = $mdb -> select( 'pp_shop_categories', [ 'id' ], [ 'parent_id' => $parent_id, 'ORDER' => [ 'o' => 'ASC' ] ] );
if ( is_array( $results ) ) foreach ( $results as $row )
$categories[] = self::category_details( $row['id'] );
return $categories;
}
public static function max_order()
{
global $mdb;
return $mdb -> max( 'pp_shop_categories', 'o' );
}
public static function save( $category_id, $title, $text, $text_hidden, $seo_link, $meta_title, $meta_description, $meta_keywords, $parent_id, $status, $noindex, $category_title, $sort_type, $additional_text, $view_subcategories )
{
global $mdb;
if ( !$parent_id )
$parent_id = null;
if ( !$category_id )
{
$order = self::max_order() + 1;
$mdb -> insert( 'pp_shop_categories', [
'status' => $status == 'on' ? 1 : 0,
'o' => (int)$order,
'parent_id' => $parent_id,
'sort_type' => $sort_type,
'view_subcategories' => $view_subcategories == 'on' ? 1 : 0,
] );
$id = $mdb -> id();
if ( $id )
{
foreach ( $title as $key => $val )
{
$mdb -> insert( 'pp_shop_categories_langs', [
'category_id' => (int)$id,
'lang_id' => $key,
'title' => $title[$key] != '' ? $title[$key] : null,
'text' => $text[$key] != '' ? $text[$key] : null,
'text_hidden' => $text_hidden[$key] != '' ? $text_hidden[$key] : null,
'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null,
'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null,
'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null,
'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null,
'noindex' => $noindex[$key],
'category_title' => $category_title[$key] != '' ? $category_title[$key] : null,
'additional_text' => $additional_text[$key] != '' ? $additional_text[$key] : null
] );
}
\S::htacces();
\S::delete_dir( '../temp/' );
return $id;
}
}
else
{
$mdb -> update( 'pp_shop_categories', [
'status' => $status == 'on' ? 1 : 0,
'parent_id' => $parent_id,
'sort_type' => $sort_type,
'view_subcategories' => $view_subcategories == 'on' ? 1 : 0,
], [
'id' => (int)$category_id
] );
foreach ( $title as $key => $val )
{
if ( $translation_id = $mdb -> get( 'pp_shop_categories_langs', 'id', [ 'AND' => [ 'category_id' => $category_id, 'lang_id' => $key ] ] ) )
$mdb -> update( 'pp_shop_categories_langs', [
'lang_id' => $key,
'title' => $title[$key] != '' ? $title[$key] : null,
'text' => $text[$key] != '' ? $text[$key] : null,
'text_hidden' => $text_hidden[$key] != '' ? $text_hidden[$key] : null,
'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null,
'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null,
'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null,
'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null,
'noindex' => $noindex[$key],
'category_title' => $category_title[$key] != '' ? $category_title[$key] : null,
'additional_text' => $additional_text[$key] != '' ? $additional_text[$key] : null
], [
'id' => $translation_id
] );
else
$mdb -> insert( 'pp_shop_categories_langs', [
'category_id' => (int)$category_id,
'lang_id' => $key,
'title' => $title[$key] != '' ? $title[$key] : null,
'text' => $text[$key] != '' ? $text[$key] : null,
'text_hidden' => $text_hidden[$key] != '' ? $text_hidden[$key] : null,
'meta_description' => $meta_description[$key] != '' ? $meta_description[$key] : null,
'meta_keywords' => $meta_keywords[$key] != '' ? $meta_keywords[$key] : null,
'meta_title' => $meta_title[$key] != '' ? $meta_title[$key] : null,
'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : null,
'noindex' => $noindex[$key],
'category_title' => $category_title[$key] != '' ? $category_title[$key] : null,
'additional_text' => $additional_text[$key] != '' ? $additional_text[$key] : null
] );
}
\S::htacces();
\S::delete_dir( '../temp/' );
return $category_id;
}
return false;
}
public static function category_details( $category_id = '' )
{
global $mdb;
$category = $mdb -> get( 'pp_shop_categories', '*', [ 'id' => (int)$category_id ] );
$results = $mdb -> select( 'pp_shop_categories_langs', '*', [ 'category_id' => (int)$category_id ] );
if ( is_array( $results ) ) foreach ( $results as $row )
$category['languages'][ $row['lang_id'] ] = $row;
return $category;
}
}

View File

@@ -697,7 +697,7 @@ class ShopProduct
$out .= ' - ';
}
$out .= \admin\factory\ShopCategory::category_title($row['category_id']);
$out .= ( new \Domain\Category\CategoryRepository( $mdb ) )->categoryTitle( (int) $row['category_id'] );
if (end($results) !== $row)
{

View File

@@ -1,21 +0,0 @@
<?php
namespace admin\view;
class ShopCategory
{
public static function category_edit( $category, $pid = '', $languages )
{
$tpl = new \Tpl;
$tpl -> languages = $languages;
$tpl -> pid = $pid;
$tpl -> category = $category;
return $tpl -> render( 'shop-category/category-edit' );
}
public static function subcategories_list( $categories_list, $level )
{
$tpl = new \Tpl;
$tpl -> level = $level;
$tpl -> categories = $categories_list;
return $tpl -> render( 'shop-category/subcategories-list' );
}
}