refactor(shop_category): migrate admin module to Domain+DI with routing and ajax cleanup
This commit is contained in:
422
autoload/Domain/Category/CategoryRepository.php
Normal file
422
autoload/Domain/Category/CategoryRepository.php
Normal file
@@ -0,0 +1,422 @@
|
||||
<?php
|
||||
namespace Domain\Category;
|
||||
|
||||
class CategoryRepository
|
||||
{
|
||||
private $db;
|
||||
|
||||
private const 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 function __construct($db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function sortTypes(): array
|
||||
{
|
||||
return self::SORT_TYPES;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function subcategories($parentId = null): array
|
||||
{
|
||||
$rows = $this->db->select('pp_shop_categories', ['id'], [
|
||||
'parent_id' => $this->toNullableInt($parentId),
|
||||
'ORDER' => ['o' => 'ASC'],
|
||||
]);
|
||||
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$categories = [];
|
||||
foreach ($rows as $row) {
|
||||
$categoryId = (int)($row['id'] ?? 0);
|
||||
if ($categoryId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$details = $this->categoryDetails($categoryId);
|
||||
if (!empty($details)) {
|
||||
$categories[] = $details;
|
||||
}
|
||||
}
|
||||
|
||||
return $categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function categoryDetails($categoryId = ''): array
|
||||
{
|
||||
$id = (int)$categoryId;
|
||||
if ($id <= 0) {
|
||||
return $this->defaultCategory();
|
||||
}
|
||||
|
||||
$category = $this->db->get('pp_shop_categories', '*', ['id' => $id]);
|
||||
if (!is_array($category)) {
|
||||
return $this->defaultCategory();
|
||||
}
|
||||
|
||||
$category['id'] = (int)($category['id'] ?? 0);
|
||||
$category['status'] = $this->toSwitchValue($category['status'] ?? 0);
|
||||
$category['view_subcategories'] = $this->toSwitchValue($category['view_subcategories'] ?? 0);
|
||||
$category['sort_type'] = (int)($category['sort_type'] ?? 0);
|
||||
$category['parent_id'] = $this->toNullableInt($category['parent_id'] ?? null);
|
||||
|
||||
$translations = $this->db->select('pp_shop_categories_langs', '*', ['category_id' => $id]);
|
||||
$category['languages'] = [];
|
||||
|
||||
if (is_array($translations)) {
|
||||
foreach ($translations as $translation) {
|
||||
$langId = (string)($translation['lang_id'] ?? '');
|
||||
if ($langId === '') {
|
||||
continue;
|
||||
}
|
||||
$category['languages'][$langId] = $translation;
|
||||
}
|
||||
}
|
||||
|
||||
return $category;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function categoryProducts(int $categoryId): array
|
||||
{
|
||||
if ($categoryId <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$rows = $this->db->query(
|
||||
'SELECT '
|
||||
. 'pspc.product_id, pspc.o, psp.status '
|
||||
. 'FROM '
|
||||
. 'pp_shop_products_categories AS pspc '
|
||||
. 'INNER JOIN pp_shop_products AS psp ON psp.id = pspc.product_id '
|
||||
. 'WHERE '
|
||||
. 'pspc.category_id = :category_id '
|
||||
. 'ORDER BY '
|
||||
. 'pspc.o ASC',
|
||||
[
|
||||
':category_id' => $categoryId,
|
||||
]
|
||||
);
|
||||
|
||||
if (!$rows) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$products = [];
|
||||
foreach ($rows->fetchAll() as $row) {
|
||||
$productId = (int)($row['product_id'] ?? 0);
|
||||
if ($productId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$products[] = [
|
||||
'product_id' => $productId,
|
||||
'o' => (int)($row['o'] ?? 0),
|
||||
'status' => $this->toSwitchValue($row['status'] ?? 0),
|
||||
'name' => $this->productName($productId),
|
||||
];
|
||||
}
|
||||
|
||||
return $products;
|
||||
}
|
||||
|
||||
public function categoryDelete($categoryId): bool
|
||||
{
|
||||
$id = (int)$categoryId;
|
||||
if ($id <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((int)$this->db->count('pp_shop_categories', ['parent_id' => $id]) > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$deleted = (bool)$this->db->delete('pp_shop_categories', ['id' => $id]);
|
||||
if ($deleted) {
|
||||
$this->refreshCategoryArtifacts();
|
||||
}
|
||||
|
||||
return $deleted;
|
||||
}
|
||||
|
||||
public function saveCategoriesOrder($categories): bool
|
||||
{
|
||||
if (!is_array($categories)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->db->update('pp_shop_categories', ['o' => 0]);
|
||||
|
||||
$position = 0;
|
||||
foreach ($categories as $item) {
|
||||
$itemId = (int)($item['item_id'] ?? 0);
|
||||
if ($itemId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$position++;
|
||||
$parentId = $this->toNullableInt($item['parent_id'] ?? null);
|
||||
$this->db->update('pp_shop_categories', [
|
||||
'o' => $position,
|
||||
'parent_id' => $parentId,
|
||||
], [
|
||||
'id' => $itemId,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->refreshCategoryArtifacts();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function saveProductOrder($categoryId, $products): bool
|
||||
{
|
||||
$id = (int)$categoryId;
|
||||
if ($id <= 0 || !is_array($products)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->db->update('pp_shop_products_categories', ['o' => 0], ['category_id' => $id]);
|
||||
|
||||
$position = 0;
|
||||
foreach ($products as $item) {
|
||||
$productId = (int)($item['item_id'] ?? 0);
|
||||
if ($productId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$position++;
|
||||
$this->db->update('pp_shop_products_categories', ['o' => $position], [
|
||||
'AND' => [
|
||||
'category_id' => $id,
|
||||
'product_id' => $productId,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function save(array $data): ?int
|
||||
{
|
||||
$categoryId = (int)($data['id'] ?? 0);
|
||||
$parentId = $this->toNullableInt($data['parent_id'] ?? null);
|
||||
|
||||
$row = [
|
||||
'status' => $this->toSwitchValue($data['status'] ?? 0),
|
||||
'parent_id' => $parentId,
|
||||
'sort_type' => (int)($data['sort_type'] ?? 0),
|
||||
'view_subcategories' => $this->toSwitchValue($data['view_subcategories'] ?? 0),
|
||||
];
|
||||
|
||||
if ($categoryId <= 0) {
|
||||
$row['o'] = $this->maxOrder() + 1;
|
||||
$this->db->insert('pp_shop_categories', $row);
|
||||
$categoryId = (int)$this->db->id();
|
||||
|
||||
if ($categoryId <= 0) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
$this->db->update('pp_shop_categories', $row, ['id' => $categoryId]);
|
||||
}
|
||||
|
||||
$title = is_array($data['title'] ?? null) ? $data['title'] : [];
|
||||
$text = is_array($data['text'] ?? null) ? $data['text'] : [];
|
||||
$textHidden = is_array($data['text_hidden'] ?? null) ? $data['text_hidden'] : [];
|
||||
$seoLink = is_array($data['seo_link'] ?? null) ? $data['seo_link'] : [];
|
||||
$metaTitle = is_array($data['meta_title'] ?? null) ? $data['meta_title'] : [];
|
||||
$metaDescription = is_array($data['meta_description'] ?? null) ? $data['meta_description'] : [];
|
||||
$metaKeywords = is_array($data['meta_keywords'] ?? null) ? $data['meta_keywords'] : [];
|
||||
$noindex = is_array($data['noindex'] ?? null) ? $data['noindex'] : [];
|
||||
$categoryTitle = is_array($data['category_title'] ?? null) ? $data['category_title'] : [];
|
||||
$additionalText = is_array($data['additional_text'] ?? null) ? $data['additional_text'] : [];
|
||||
|
||||
foreach ($title as $langId => $langTitle) {
|
||||
$translationData = [
|
||||
'lang_id' => (string)$langId,
|
||||
'title' => $this->toNullableString($langTitle),
|
||||
'text' => $this->toNullableString($text[$langId] ?? null),
|
||||
'text_hidden' => $this->toNullableString($textHidden[$langId] ?? null),
|
||||
'meta_description' => $this->toNullableString($metaDescription[$langId] ?? null),
|
||||
'meta_keywords' => $this->toNullableString($metaKeywords[$langId] ?? null),
|
||||
'meta_title' => $this->toNullableString($metaTitle[$langId] ?? null),
|
||||
'seo_link' => $this->normalizeSeoLink($seoLink[$langId] ?? null),
|
||||
'noindex' => (int)($noindex[$langId] ?? 0),
|
||||
'category_title' => $this->toNullableString($categoryTitle[$langId] ?? null),
|
||||
'additional_text' => $this->toNullableString($additionalText[$langId] ?? null),
|
||||
];
|
||||
|
||||
$translationId = $this->db->get('pp_shop_categories_langs', 'id', [
|
||||
'AND' => [
|
||||
'category_id' => $categoryId,
|
||||
'lang_id' => (string)$langId,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($translationId) {
|
||||
$this->db->update('pp_shop_categories_langs', $translationData, ['id' => $translationId]);
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->db->insert('pp_shop_categories_langs', array_merge($translationData, [
|
||||
'category_id' => $categoryId,
|
||||
]));
|
||||
}
|
||||
|
||||
$this->refreshCategoryArtifacts();
|
||||
|
||||
return $categoryId;
|
||||
}
|
||||
|
||||
public function categoryTitle(int $categoryId): string
|
||||
{
|
||||
if ($categoryId <= 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$title = $this->db->select('pp_shop_categories_langs', [
|
||||
'[><]pp_langs' => ['lang_id' => 'id'],
|
||||
], 'title', [
|
||||
'AND' => [
|
||||
'category_id' => $categoryId,
|
||||
'title[!]' => '',
|
||||
],
|
||||
'ORDER' => ['o' => 'ASC'],
|
||||
'LIMIT' => 1,
|
||||
]);
|
||||
|
||||
if (!is_array($title) || !isset($title[0])) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return (string)$title[0];
|
||||
}
|
||||
|
||||
private function maxOrder(): int
|
||||
{
|
||||
return (int)$this->db->max('pp_shop_categories', 'o');
|
||||
}
|
||||
|
||||
private function refreshCategoryArtifacts(): void
|
||||
{
|
||||
if (class_exists('\\S')) {
|
||||
\S::htacces();
|
||||
\S::delete_dir('../temp/');
|
||||
}
|
||||
}
|
||||
|
||||
private function normalizeSeoLink($value): ?string
|
||||
{
|
||||
if (!class_exists('\\S')) {
|
||||
return $this->toNullableString($value);
|
||||
}
|
||||
|
||||
$seo = \S::seo((string)$value);
|
||||
$seo = trim((string)$seo);
|
||||
|
||||
return $seo !== '' ? $seo : null;
|
||||
}
|
||||
|
||||
private function toNullableString($value): ?string
|
||||
{
|
||||
$text = trim((string)$value);
|
||||
return $text === '' ? null : $text;
|
||||
}
|
||||
|
||||
private function toSwitchValue($value): int
|
||||
{
|
||||
if (is_bool($value)) {
|
||||
return $value ? 1 : 0;
|
||||
}
|
||||
|
||||
if (is_numeric($value)) {
|
||||
return ((int)$value) === 1 ? 1 : 0;
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
$normalized = strtolower(trim($value));
|
||||
return in_array($normalized, ['1', 'on', 'true', 'yes'], true) ? 1 : 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function toNullableInt($value): ?int
|
||||
{
|
||||
if ($value === null || $value === '' || (int)$value <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (int)$value;
|
||||
}
|
||||
|
||||
private function defaultCategory(): array
|
||||
{
|
||||
return [
|
||||
'id' => 0,
|
||||
'status' => 1,
|
||||
'parent_id' => null,
|
||||
'sort_type' => 0,
|
||||
'view_subcategories' => 0,
|
||||
'languages' => [],
|
||||
];
|
||||
}
|
||||
|
||||
private function productName(int $productId): string
|
||||
{
|
||||
if ($productId <= 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$defaultLang = $this->db->get('pp_langs', 'id', ['start' => 1]);
|
||||
if (!$defaultLang) {
|
||||
$defaultLang = 'pl';
|
||||
}
|
||||
|
||||
$name = $this->db->get('pp_shop_products_langs', 'name', [
|
||||
'AND' => [
|
||||
'product_id' => $productId,
|
||||
'lang_id' => (string)$defaultLang,
|
||||
],
|
||||
]);
|
||||
|
||||
if ($name !== false && $name !== null && trim((string)$name) !== '') {
|
||||
return (string)$name;
|
||||
}
|
||||
|
||||
$fallback = $this->db->get('pp_shop_products_langs', 'name', [
|
||||
'AND' => [
|
||||
'product_id' => $productId,
|
||||
'name[!]' => '',
|
||||
],
|
||||
'LIMIT' => 1,
|
||||
]);
|
||||
|
||||
return $fallback ? (string)$fallback : '';
|
||||
}
|
||||
}
|
||||
163
autoload/admin/Controllers/ShopCategoryController.php
Normal file
163
autoload/admin/Controllers/ShopCategoryController.php
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
] );
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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()
|
||||
] );
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user