ver. 0.272 - ShopProductSets refactor + update package
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
250
autoload/Domain/ProductSet/ProductSetRepository.php
Normal file
250
autoload/Domain/ProductSet/ProductSetRepository.php
Normal file
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
namespace Domain\ProductSet;
|
||||
|
||||
class ProductSetRepository
|
||||
{
|
||||
private const MAX_PER_PAGE = 100;
|
||||
|
||||
private $db;
|
||||
|
||||
public function __construct($db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{items: array<int, array<string, mixed>>, total: int}
|
||||
*/
|
||||
public function listForAdmin(
|
||||
array $filters,
|
||||
string $sortColumn = 'name',
|
||||
string $sortDir = 'ASC',
|
||||
int $page = 1,
|
||||
int $perPage = 15
|
||||
): array {
|
||||
$allowedSortColumns = [
|
||||
'id' => 'ps.id',
|
||||
'name' => 'ps.name',
|
||||
'status' => 'ps.status',
|
||||
];
|
||||
|
||||
$sortSql = $allowedSortColumns[$sortColumn] ?? 'ps.name';
|
||||
$sortDir = strtoupper(trim($sortDir)) === 'DESC' ? 'DESC' : 'ASC';
|
||||
$page = max(1, $page);
|
||||
$perPage = min(self::MAX_PER_PAGE, max(1, $perPage));
|
||||
$offset = ($page - 1) * $perPage;
|
||||
|
||||
$where = ['1 = 1'];
|
||||
$params = [];
|
||||
|
||||
$name = trim((string)($filters['name'] ?? ''));
|
||||
if ($name !== '') {
|
||||
if (strlen($name) > 255) {
|
||||
$name = substr($name, 0, 255);
|
||||
}
|
||||
$where[] = 'ps.name LIKE :name';
|
||||
$params[':name'] = '%' . $name . '%';
|
||||
}
|
||||
|
||||
$status = trim((string)($filters['status'] ?? ''));
|
||||
if ($status === '0' || $status === '1') {
|
||||
$where[] = 'ps.status = :status';
|
||||
$params[':status'] = (int)$status;
|
||||
}
|
||||
|
||||
$whereSql = implode(' AND ', $where);
|
||||
|
||||
$sqlCount = "
|
||||
SELECT COUNT(0)
|
||||
FROM pp_shop_product_sets AS ps
|
||||
WHERE {$whereSql}
|
||||
";
|
||||
|
||||
$stmtCount = $this->db->query($sqlCount, $params);
|
||||
$countRows = $stmtCount ? $stmtCount->fetchAll() : [];
|
||||
$total = isset($countRows[0][0]) ? (int)$countRows[0][0] : 0;
|
||||
|
||||
$sql = "
|
||||
SELECT
|
||||
ps.id,
|
||||
ps.name,
|
||||
ps.status
|
||||
FROM pp_shop_product_sets AS ps
|
||||
WHERE {$whereSql}
|
||||
ORDER BY {$sortSql} {$sortDir}, ps.id DESC
|
||||
LIMIT {$perPage} OFFSET {$offset}
|
||||
";
|
||||
|
||||
$stmt = $this->db->query($sql, $params);
|
||||
$items = $stmt ? $stmt->fetchAll() : [];
|
||||
|
||||
if (!is_array($items)) {
|
||||
$items = [];
|
||||
}
|
||||
|
||||
foreach ($items as &$item) {
|
||||
$item['id'] = (int)($item['id'] ?? 0);
|
||||
$item['status'] = $this->toSwitchValue($item['status'] ?? 0);
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return [
|
||||
'items' => $items,
|
||||
'total' => $total,
|
||||
];
|
||||
}
|
||||
|
||||
public function find(int $id): array
|
||||
{
|
||||
if ($id <= 0) {
|
||||
return $this->defaultSet();
|
||||
}
|
||||
|
||||
$set = $this->db->get('pp_shop_product_sets', '*', ['id' => $id]);
|
||||
if (!is_array($set)) {
|
||||
return $this->defaultSet();
|
||||
}
|
||||
|
||||
$set['id'] = (int)($set['id'] ?? 0);
|
||||
$set['status'] = $this->toSwitchValue($set['status'] ?? 0);
|
||||
|
||||
$products = $this->db->select('pp_shop_product_sets_products', 'product_id', ['set_id' => $id]);
|
||||
$set['products'] = is_array($products) ? array_map('intval', $products) : [];
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
public function save(int $id, string $name, int $status, array $productIds): ?int
|
||||
{
|
||||
$row = [
|
||||
'name' => trim($name),
|
||||
'status' => $status === 1 ? 1 : 0,
|
||||
];
|
||||
|
||||
if ($id <= 0) {
|
||||
$this->db->insert('pp_shop_product_sets', $row);
|
||||
$id = (int)$this->db->id();
|
||||
if ($id <= 0) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
$this->db->update('pp_shop_product_sets', $row, ['id' => $id]);
|
||||
}
|
||||
|
||||
$this->syncProducts($id, $productIds);
|
||||
$this->clearTempAndCache();
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function delete(int $id): bool
|
||||
{
|
||||
if ($id <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->db->delete('pp_shop_product_sets_products', ['set_id' => $id]);
|
||||
$result = (bool)$this->db->delete('pp_shop_product_sets', ['id' => $id]);
|
||||
|
||||
if ($result) {
|
||||
$this->clearTempAndCache();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{id: int, name: string}>
|
||||
*/
|
||||
public function allSets(): array
|
||||
{
|
||||
$rows = $this->db->select('pp_shop_product_sets', ['id', 'name'], ['ORDER' => ['name' => 'ASC']]);
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$sets = [];
|
||||
foreach ($rows as $row) {
|
||||
$sets[] = [
|
||||
'id' => (int)($row['id'] ?? 0),
|
||||
'name' => (string)($row['name'] ?? ''),
|
||||
];
|
||||
}
|
||||
|
||||
return $sets;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public function allProductsMap(): array
|
||||
{
|
||||
$rows = $this->db->select('pp_shop_products', 'id', ['parent_id' => null]);
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$products = [];
|
||||
foreach ($rows as $productId) {
|
||||
$name = $this->db->get('pp_shop_products_langs', 'name', [
|
||||
'AND' => ['product_id' => $productId, 'lang_id' => 'pl'],
|
||||
]);
|
||||
if ($name) {
|
||||
$products[(int)$productId] = (string)$name;
|
||||
}
|
||||
}
|
||||
|
||||
return $products;
|
||||
}
|
||||
|
||||
private function syncProducts(int $setId, array $productIds): void
|
||||
{
|
||||
$this->db->delete('pp_shop_product_sets_products', ['set_id' => $setId]);
|
||||
|
||||
$seen = [];
|
||||
foreach ($productIds as $productId) {
|
||||
$pid = (int)$productId;
|
||||
if ($pid > 0 && !isset($seen[$pid])) {
|
||||
$this->db->insert('pp_shop_product_sets_products', [
|
||||
'set_id' => $setId,
|
||||
'product_id' => $pid,
|
||||
]);
|
||||
$seen[$pid] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function defaultSet(): array
|
||||
{
|
||||
return [
|
||||
'id' => 0,
|
||||
'name' => '',
|
||||
'status' => 1,
|
||||
'products' => [],
|
||||
];
|
||||
}
|
||||
|
||||
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 clearTempAndCache(): void
|
||||
{
|
||||
\S::delete_dir('../temp/');
|
||||
\S::delete_dir('../thumbs/');
|
||||
}
|
||||
}
|
||||
328
autoload/admin/Controllers/ShopProductSetsController.php
Normal file
328
autoload/admin/Controllers/ShopProductSetsController.php
Normal file
@@ -0,0 +1,328 @@
|
||||
<?php
|
||||
namespace admin\Controllers;
|
||||
|
||||
use Domain\ProductSet\ProductSetRepository;
|
||||
use admin\ViewModels\Common\PaginatedTableViewModel;
|
||||
use admin\ViewModels\Forms\FormAction;
|
||||
use admin\ViewModels\Forms\FormEditViewModel;
|
||||
use admin\ViewModels\Forms\FormField;
|
||||
use admin\ViewModels\Forms\FormTab;
|
||||
|
||||
class ShopProductSetsController
|
||||
{
|
||||
private ProductSetRepository $repository;
|
||||
|
||||
public function __construct(ProductSetRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
public function list(): string
|
||||
{
|
||||
$sortableColumns = ['id', 'name', 'status'];
|
||||
$filterDefinitions = [
|
||||
[
|
||||
'key' => 'name',
|
||||
'label' => 'Nazwa',
|
||||
'type' => 'text',
|
||||
],
|
||||
[
|
||||
'key' => 'status',
|
||||
'label' => 'Aktywny',
|
||||
'type' => 'select',
|
||||
'options' => [
|
||||
'' => '- aktywny -',
|
||||
'1' => 'tak',
|
||||
'0' => 'nie',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$listRequest = \admin\Support\TableListRequestFactory::fromRequest(
|
||||
$filterDefinitions,
|
||||
$sortableColumns,
|
||||
'name'
|
||||
);
|
||||
|
||||
$sortDir = $listRequest['sortDir'];
|
||||
if (trim((string)\S::get('sort')) === '') {
|
||||
$sortDir = 'ASC';
|
||||
}
|
||||
|
||||
$result = $this->repository->listForAdmin(
|
||||
$listRequest['filters'],
|
||||
$listRequest['sortColumn'],
|
||||
$sortDir,
|
||||
$listRequest['page'],
|
||||
$listRequest['perPage']
|
||||
);
|
||||
|
||||
$rows = [];
|
||||
$lp = ($listRequest['page'] - 1) * $listRequest['perPage'] + 1;
|
||||
foreach ($result['items'] as $item) {
|
||||
$id = (int)($item['id'] ?? 0);
|
||||
$name = trim((string)($item['name'] ?? ''));
|
||||
$status = (int)($item['status'] ?? 0);
|
||||
|
||||
$rows[] = [
|
||||
'lp' => $lp++ . '.',
|
||||
'name' => '<a href="/admin/shop_product_sets/edit/id=' . $id . '">' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '</a>',
|
||||
'status' => $status === 1 ? 'tak' : '<span style="color: #FF0000;">nie</span>',
|
||||
'_actions' => [
|
||||
[
|
||||
'label' => 'Edytuj',
|
||||
'url' => '/admin/shop_product_sets/edit/id=' . $id,
|
||||
'class' => 'btn btn-xs btn-primary',
|
||||
],
|
||||
[
|
||||
'label' => 'Usun',
|
||||
'url' => '/admin/shop_product_sets/delete/id=' . $id,
|
||||
'class' => 'btn btn-xs btn-danger',
|
||||
'confirm' => 'Na pewno chcesz usunac wybrany komplet produktow?',
|
||||
'confirm_ok' => 'Usun',
|
||||
'confirm_cancel' => 'Anuluj',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
$total = (int)$result['total'];
|
||||
$totalPages = max(1, (int)ceil($total / $listRequest['perPage']));
|
||||
|
||||
$viewModel = new PaginatedTableViewModel(
|
||||
[
|
||||
['key' => 'lp', 'label' => 'Lp.', 'class' => 'text-center', 'sortable' => false],
|
||||
['key' => 'name', 'sort_key' => 'name', 'label' => 'Nazwa', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'status', 'sort_key' => 'status', 'label' => 'Aktywny', 'class' => 'text-center', 'sortable' => true, 'raw' => true],
|
||||
],
|
||||
$rows,
|
||||
$listRequest['viewFilters'],
|
||||
[
|
||||
'column' => $listRequest['sortColumn'],
|
||||
'dir' => $sortDir,
|
||||
],
|
||||
[
|
||||
'page' => $listRequest['page'],
|
||||
'per_page' => $listRequest['perPage'],
|
||||
'total' => $total,
|
||||
'total_pages' => $totalPages,
|
||||
],
|
||||
array_merge($listRequest['queryFilters'], [
|
||||
'sort' => $listRequest['sortColumn'],
|
||||
'dir' => $sortDir,
|
||||
'per_page' => $listRequest['perPage'],
|
||||
]),
|
||||
$listRequest['perPageOptions'],
|
||||
$sortableColumns,
|
||||
'/admin/shop_product_sets/list/',
|
||||
'Brak danych w tabeli.',
|
||||
'/admin/shop_product_sets/edit/',
|
||||
'Dodaj komplet produktow'
|
||||
);
|
||||
|
||||
return \Tpl::view('shop-product-sets/product-sets-list', [
|
||||
'viewModel' => $viewModel,
|
||||
]);
|
||||
}
|
||||
|
||||
public function view_list(): string
|
||||
{
|
||||
return $this->list();
|
||||
}
|
||||
|
||||
public function edit(): string
|
||||
{
|
||||
$set = $this->repository->find((int)\S::get('id'));
|
||||
$products = $this->repository->allProductsMap();
|
||||
|
||||
return \Tpl::view('shop-product-sets/product-set-edit', [
|
||||
'form' => $this->buildFormViewModel($set, $products),
|
||||
]);
|
||||
}
|
||||
|
||||
public function set_edit(): string
|
||||
{
|
||||
return $this->edit();
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$legacyValues = \S::get('values');
|
||||
|
||||
if ($legacyValues) {
|
||||
$values = json_decode((string)$legacyValues, true);
|
||||
$response = [
|
||||
'status' => 'error',
|
||||
'msg' => 'Podczas zapisywania kompletu produktow wystapil blad. Prosze sprobowac ponownie.',
|
||||
];
|
||||
|
||||
if (is_array($values)) {
|
||||
$productIds = $values['set_products_id'] ?? [];
|
||||
if (!is_array($productIds)) {
|
||||
$productIds = $productIds ? [$productIds] : [];
|
||||
}
|
||||
|
||||
$id = $this->repository->save(
|
||||
(int)($values['id'] ?? 0),
|
||||
(string)($values['name'] ?? ''),
|
||||
$this->toSwitchValue($values['status'] ?? 0),
|
||||
$productIds
|
||||
);
|
||||
|
||||
if (!empty($id)) {
|
||||
$response = [
|
||||
'status' => 'ok',
|
||||
'msg' => 'Komplet produktow zostal zapisany.',
|
||||
'id' => (int)$id,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode($response);
|
||||
exit;
|
||||
}
|
||||
|
||||
$payload = $_POST;
|
||||
if (empty($payload['id'])) {
|
||||
$routeId = (int)\S::get('id');
|
||||
if ($routeId > 0) {
|
||||
$payload['id'] = $routeId;
|
||||
}
|
||||
}
|
||||
|
||||
$productIds = $payload['set_products_id'] ?? [];
|
||||
if (!is_array($productIds)) {
|
||||
$productIds = $productIds ? [$productIds] : [];
|
||||
}
|
||||
|
||||
$id = $this->repository->save(
|
||||
(int)($payload['id'] ?? 0),
|
||||
(string)($payload['name'] ?? ''),
|
||||
$this->toSwitchValue($payload['status'] ?? 0),
|
||||
$productIds
|
||||
);
|
||||
|
||||
if (!empty($id)) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'id' => (int)$id,
|
||||
'message' => 'Komplet produktow zostal zapisany.',
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'errors' => ['general' => 'Podczas zapisywania kompletu produktow wystapil blad.'],
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
public function delete(): void
|
||||
{
|
||||
if ($this->repository->delete((int)\S::get('id'))) {
|
||||
\S::alert('Komplet produktow zostal usuniety.');
|
||||
}
|
||||
|
||||
header('Location: /admin/shop_product_sets/list/');
|
||||
exit;
|
||||
}
|
||||
|
||||
public function set_delete(): void
|
||||
{
|
||||
$this->delete();
|
||||
}
|
||||
|
||||
private function buildFormViewModel(array $set, array $products = []): FormEditViewModel
|
||||
{
|
||||
$id = (int)($set['id'] ?? 0);
|
||||
$isNew = $id <= 0;
|
||||
$selectedProducts = $set['products'] ?? [];
|
||||
|
||||
$data = [
|
||||
'id' => $id,
|
||||
'name' => (string)($set['name'] ?? ''),
|
||||
'status' => (int)($set['status'] ?? 1),
|
||||
];
|
||||
|
||||
$productsSelectHtml = $this->renderProductsSelect($products, $selectedProducts);
|
||||
|
||||
$fields = [
|
||||
FormField::hidden('id', $id),
|
||||
FormField::text('name', [
|
||||
'label' => 'Nazwa',
|
||||
'tab' => 'settings',
|
||||
'required' => true,
|
||||
]),
|
||||
FormField::switch('status', [
|
||||
'label' => 'Aktywny',
|
||||
'tab' => 'settings',
|
||||
'value' => true,
|
||||
]),
|
||||
FormField::custom('set_products', $productsSelectHtml, [
|
||||
'tab' => 'settings',
|
||||
]),
|
||||
];
|
||||
|
||||
$tabs = [
|
||||
new FormTab('settings', 'Ustawienia', 'fa-wrench'),
|
||||
];
|
||||
|
||||
$actionUrl = '/admin/shop_product_sets/save/' . ($isNew ? '' : ('id=' . $id));
|
||||
$actions = [
|
||||
FormAction::save($actionUrl, '/admin/shop_product_sets/list/'),
|
||||
FormAction::cancel('/admin/shop_product_sets/list/'),
|
||||
];
|
||||
|
||||
return new FormEditViewModel(
|
||||
'shop-product-set-edit',
|
||||
$isNew ? 'Nowy komplet produktow' : ('Edycja kompletu produktow: ' . (string)($set['name'] ?? '')),
|
||||
$data,
|
||||
$fields,
|
||||
$tabs,
|
||||
$actions,
|
||||
'POST',
|
||||
$actionUrl,
|
||||
'/admin/shop_product_sets/list/',
|
||||
true,
|
||||
['id' => $id]
|
||||
);
|
||||
}
|
||||
|
||||
private function toSwitchValue($value): int
|
||||
{
|
||||
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 renderProductsSelect(array $products, array $selectedProducts): string
|
||||
{
|
||||
$html = '<div class="form-group row">';
|
||||
$html .= '<label class="col-lg-4 control-label">Produkty do kompletu:</label>';
|
||||
$html .= '<div class="col-lg-8">';
|
||||
$html .= '<select id="set_products_id" multiple name="set_products_id[]" placeholder="produkty do kompletu">';
|
||||
$html .= '<option value="">wybierz produkt...</option>';
|
||||
|
||||
foreach ($products as $productId => $productName) {
|
||||
$pid = (int)$productId;
|
||||
$selected = in_array($pid, $selectedProducts, true) ? ' selected' : '';
|
||||
$html .= '<option value="' . $pid . '"' . $selected . '>'
|
||||
. htmlspecialchars((string)$productName, ENT_QUOTES, 'UTF-8')
|
||||
. '</option>';
|
||||
}
|
||||
|
||||
$html .= '</select>';
|
||||
$html .= '</div>';
|
||||
$html .= '</div>';
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
@@ -362,6 +362,13 @@ class Site
|
||||
new \Domain\ShopStatus\ShopStatusRepository( $mdb )
|
||||
);
|
||||
},
|
||||
'ShopProductSets' => function() {
|
||||
global $mdb;
|
||||
|
||||
return new \admin\Controllers\ShopProductSetsController(
|
||||
new \Domain\ProductSet\ProductSetRepository( $mdb )
|
||||
);
|
||||
},
|
||||
];
|
||||
|
||||
return self::$newControllers;
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
<?php
|
||||
namespace admin\controls;
|
||||
class ShopProductSets
|
||||
{
|
||||
|
||||
static public function set_delete()
|
||||
{
|
||||
if ( \shop\ProductSet::set_delete( \S::get( 'id' ) ) )
|
||||
\S::set_message( 'Komplet produktów usunięty.' );
|
||||
else
|
||||
\S::alert( 'Podczas usuwania kompletu produktów wystąpił błąd. Proszę spróbować ponownie' );
|
||||
header( 'Location: /admin/shop_product_sets/view_list/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
static public function save()
|
||||
{
|
||||
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania kompletu produktów wystąpił błąd. Proszę spróbować ponownie.' ];
|
||||
$values = json_decode( \S::get( 'values' ), true );
|
||||
|
||||
if ( $id = \admin\factory\ShopProductSet::save(
|
||||
(int)$values['id'], $values['name'], (string) $values['status'], $values['set_products_id']
|
||||
) ) {
|
||||
$response = [ 'status' => 'ok', 'msg' => 'Komplet produktów został zapisany.', 'id' => $id ];
|
||||
}
|
||||
|
||||
echo json_encode( $response );
|
||||
exit;
|
||||
}
|
||||
|
||||
static public function set_edit()
|
||||
{
|
||||
return \Tpl::view( 'shop-product-sets/set-edit', [
|
||||
'set' => new \shop\ProductSet( (int) \S::get( 'id' ) ),
|
||||
'products' => \admin\factory\ShopProduct::products_list()
|
||||
] );
|
||||
}
|
||||
|
||||
static public function view_list()
|
||||
{
|
||||
return \Tpl::view( 'shop-product-sets/view-list' );
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
<?
|
||||
namespace admin\factory;
|
||||
|
||||
class ShopProductSet
|
||||
{
|
||||
// zapisywanie kompletu produktów
|
||||
static public function save( int $set_id, string $name, string $status, $set_products_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if ( !$set_id )
|
||||
{
|
||||
$mdb -> insert('pp_shop_product_sets', [
|
||||
'name' => $name,
|
||||
'status' => 'on' === $status ? 1 : 0
|
||||
] );
|
||||
|
||||
$id = $mdb -> id();
|
||||
|
||||
if ( $set_products_id == null )
|
||||
$not_in = [ 0 ];
|
||||
elseif ( !is_array( $set_products_id ) )
|
||||
$not_in = [ 0, $set_products_id ];
|
||||
elseif ( is_array( $set_products_id ) )
|
||||
$not_in = $set_products_id;
|
||||
|
||||
foreach ( $not_in as $product_id )
|
||||
{
|
||||
if ( $product_id != 0 )
|
||||
$mdb -> insert( 'pp_shop_product_sets_products', [ 'set_id' => $id, 'product_id' => $product_id ] );
|
||||
}
|
||||
|
||||
\S::delete_dir('../temp/');
|
||||
\S::delete_dir('../thumbs/');
|
||||
|
||||
return $id;
|
||||
}
|
||||
else
|
||||
{
|
||||
$mdb -> update('pp_shop_product_sets', [
|
||||
'name' => $name,
|
||||
'status' => 'on' === $status ? 1 : 0
|
||||
], [
|
||||
'id' => (int)$set_id,
|
||||
] );
|
||||
|
||||
$mdb -> delete( 'pp_shop_product_sets_products', [ 'set_id' => $set_id ] );
|
||||
|
||||
if ( $set_products_id == null )
|
||||
$not_in = [ 0 ];
|
||||
elseif ( !is_array( $set_products_id ) )
|
||||
$not_in = [ 0, $set_products_id ];
|
||||
elseif ( is_array( $set_products_id ) )
|
||||
$not_in = $set_products_id;
|
||||
|
||||
foreach ( $not_in as $product_id )
|
||||
{
|
||||
if ( $product_id != 0 )
|
||||
$mdb -> insert( 'pp_shop_product_sets_products', [ 'set_id' => $set_id, 'product_id' => $product_id ] );
|
||||
}
|
||||
|
||||
\S::delete_dir('../temp/');
|
||||
\S::delete_dir('../thumbs/');
|
||||
|
||||
return $set_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,32 +23,26 @@ class ProductSet implements \ArrayAccess
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$result = $mdb -> get( 'pp_shop_product_sets', '*', [ 'id' => $set_id ] );
|
||||
if ( \S::is_array_fix( $result ) ) foreach ( $result as $key => $val )
|
||||
$this -> $key = $val;
|
||||
$repo = new \Domain\ProductSet\ProductSetRepository( $mdb );
|
||||
$data = $repo->find( $set_id );
|
||||
|
||||
$this -> products = $mdb -> select( 'pp_shop_product_sets_products', 'product_id', [ 'set_id' => $set_id ] );
|
||||
foreach ( $data as $key => $val )
|
||||
$this->$key = $val;
|
||||
}
|
||||
|
||||
//lista dostępnych kompletów
|
||||
//lista dostepnych kompletow (fasada do repozytorium)
|
||||
static public function sets_list()
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> select( 'pp_shop_product_sets', [ 'id', 'name' ], [ 'ORDER' => [ 'name' => 'ASC' ] ] );
|
||||
$repo = new \Domain\ProductSet\ProductSetRepository( $mdb );
|
||||
return $repo->allSets();
|
||||
}
|
||||
|
||||
// usuwanie kompletu produktów
|
||||
// usuwanie kompletu produktow (fasada do repozytorium)
|
||||
static public function set_delete( int $set_id )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if (
|
||||
$mdb -> delete( 'pp_shop_product_sets_products', [ 'set_id' => $set_id ] )
|
||||
and
|
||||
$mdb -> delete( 'pp_shop_product_sets', [ 'id' => $set_id ] )
|
||||
)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
$repo = new \Domain\ProductSet\ProductSetRepository( $mdb );
|
||||
return $repo->delete( $set_id );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user