Release 0.265: ShopPromotion date_from and edit save fix
This commit is contained in:
327
autoload/admin/Controllers/ShopPromotionController.php
Normal file
327
autoload/admin/Controllers/ShopPromotionController.php
Normal file
@@ -0,0 +1,327 @@
|
||||
<?php
|
||||
namespace admin\Controllers;
|
||||
|
||||
use Domain\Promotion\PromotionRepository;
|
||||
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 ShopPromotionController
|
||||
{
|
||||
private PromotionRepository $repository;
|
||||
|
||||
public function __construct(PromotionRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
public function list(): string
|
||||
{
|
||||
$sortableColumns = ['id', 'name', 'status', 'condition_type', 'date_from', 'date_to'];
|
||||
$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,
|
||||
'id'
|
||||
);
|
||||
|
||||
$sortDir = $listRequest['sortDir'];
|
||||
if (trim((string)\S::get('sort')) === '') {
|
||||
$sortDir = 'DESC';
|
||||
}
|
||||
|
||||
$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);
|
||||
$conditionType = (int)($item['condition_type'] ?? 0);
|
||||
$dateFrom = trim((string)($item['date_from'] ?? ''));
|
||||
$dateTo = trim((string)($item['date_to'] ?? ''));
|
||||
|
||||
$rows[] = [
|
||||
'lp' => $lp++ . '.',
|
||||
'status' => $status === 1 ? 'tak' : '<span style="color: #FF0000;">nie</span>',
|
||||
'name' => '<a href="/admin/shop_promotion/edit/id=' . $id . '">' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '</a>',
|
||||
'condition_type' => htmlspecialchars((string)(\shop\Promotion::$condition_type[$conditionType] ?? '-'), ENT_QUOTES, 'UTF-8'),
|
||||
'date_from' => $dateFrom !== '' ? htmlspecialchars($dateFrom, ENT_QUOTES, 'UTF-8') : '-',
|
||||
'date_to' => $dateTo !== '' ? htmlspecialchars($dateTo, ENT_QUOTES, 'UTF-8') : '-',
|
||||
'_actions' => [
|
||||
[
|
||||
'label' => 'Edytuj',
|
||||
'url' => '/admin/shop_promotion/edit/id=' . $id,
|
||||
'class' => 'btn btn-xs btn-primary',
|
||||
],
|
||||
[
|
||||
'label' => 'Usun',
|
||||
'url' => '/admin/shop_promotion/delete/id=' . $id,
|
||||
'class' => 'btn btn-xs btn-danger',
|
||||
'confirm' => 'Na pewno chcesz usunac wybrana promocje?',
|
||||
'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' => 'status', 'sort_key' => 'status', 'label' => 'Aktywny', 'class' => 'text-center', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'name', 'sort_key' => 'name', 'label' => 'Nazwa', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'condition_type', 'sort_key' => 'condition_type', 'label' => 'Typ kuponu', 'sortable' => true],
|
||||
['key' => 'date_from', 'sort_key' => 'date_from', 'label' => 'Data od', 'class' => 'text-center', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'date_to', 'sort_key' => 'date_to', 'label' => 'Data do', '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_promotion/list/',
|
||||
'Brak danych w tabeli.',
|
||||
'/admin/shop_promotion/edit/',
|
||||
'Dodaj promocje'
|
||||
);
|
||||
|
||||
return \Tpl::view('shop-promotion/promotions-list', [
|
||||
'viewModel' => $viewModel,
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(): string
|
||||
{
|
||||
$promotion = $this->repository->find((int)\S::get('id'));
|
||||
$categories = $this->repository->categoriesTree(null);
|
||||
|
||||
return \Tpl::view('shop-promotion/promotion-edit', [
|
||||
'form' => $this->buildFormViewModel($promotion, $categories),
|
||||
]);
|
||||
}
|
||||
|
||||
public function save(): void
|
||||
{
|
||||
$legacyValues = \S::get('values');
|
||||
|
||||
if ($legacyValues) {
|
||||
$values = json_decode((string)$legacyValues, true);
|
||||
$response = [
|
||||
'status' => 'error',
|
||||
'msg' => 'Podczas zapisywania promocji wystapil blad. Prosze sprobowac ponownie.',
|
||||
];
|
||||
|
||||
if (is_array($values)) {
|
||||
$id = $this->repository->save($values);
|
||||
if (!empty($id)) {
|
||||
$response = [
|
||||
'status' => 'ok',
|
||||
'msg' => 'Promocja zostala zapisana.',
|
||||
'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;
|
||||
}
|
||||
}
|
||||
|
||||
$id = $this->repository->save($payload);
|
||||
if (!empty($id)) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'id' => (int)$id,
|
||||
'message' => 'Promocja zostala zapisana.',
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'errors' => ['general' => 'Podczas zapisywania promocji wystapil blad.'],
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
public function delete(): void
|
||||
{
|
||||
if ($this->repository->delete((int)\S::get('id'))) {
|
||||
\S::alert('Promocja zostala usunieta.');
|
||||
}
|
||||
|
||||
header('Location: /admin/shop_promotion/list/');
|
||||
exit;
|
||||
}
|
||||
|
||||
private function buildFormViewModel(array $promotion, array $categories): FormEditViewModel
|
||||
{
|
||||
$id = (int)($promotion['id'] ?? 0);
|
||||
$isNew = $id <= 0;
|
||||
|
||||
$data = [
|
||||
'id' => $id,
|
||||
'name' => (string)($promotion['name'] ?? ''),
|
||||
'status' => (int)($promotion['status'] ?? 1),
|
||||
'include_coupon' => (int)($promotion['include_coupon'] ?? 0),
|
||||
'include_product_promo' => (int)($promotion['include_product_promo'] ?? 0),
|
||||
'condition_type' => (int)($promotion['condition_type'] ?? 1),
|
||||
'discount_type' => (int)($promotion['discount_type'] ?? 1),
|
||||
'min_product_count' => $promotion['min_product_count'] ?? '',
|
||||
'price_cheapest_product' => $promotion['price_cheapest_product'] ?? '',
|
||||
'amount' => $promotion['amount'] ?? '',
|
||||
'date_from' => (string)($promotion['date_from'] ?? ''),
|
||||
'date_to' => (string)($promotion['date_to'] ?? ''),
|
||||
];
|
||||
|
||||
$fields = [
|
||||
FormField::hidden('id', $id),
|
||||
FormField::text('name', [
|
||||
'label' => 'Nazwa',
|
||||
'tab' => 'settings',
|
||||
'required' => true,
|
||||
]),
|
||||
FormField::switch('status', [
|
||||
'label' => 'Aktywna',
|
||||
'tab' => 'settings',
|
||||
'value' => true,
|
||||
]),
|
||||
FormField::switch('include_coupon', [
|
||||
'label' => 'Lacz z kuponami rabatowymi',
|
||||
'tab' => 'settings',
|
||||
]),
|
||||
FormField::switch('include_product_promo', [
|
||||
'label' => 'Uwzglednij produkty przecenione',
|
||||
'tab' => 'settings',
|
||||
]),
|
||||
FormField::select('condition_type', [
|
||||
'label' => 'Warunki promocji',
|
||||
'tab' => 'settings',
|
||||
'options' => \shop\Promotion::$condition_type,
|
||||
'required' => true,
|
||||
]),
|
||||
FormField::select('discount_type', [
|
||||
'label' => 'Typ rabatu',
|
||||
'tab' => 'settings',
|
||||
'options' => \shop\Promotion::$discount_type,
|
||||
'required' => true,
|
||||
]),
|
||||
FormField::text('min_product_count', [
|
||||
'label' => 'Min. ilosc produktow z danej kategorii',
|
||||
'tab' => 'settings',
|
||||
'attributes' => ['class' => 'int-format'],
|
||||
]),
|
||||
FormField::text('price_cheapest_product', [
|
||||
'label' => 'Cena najtanszego produktu',
|
||||
'tab' => 'settings',
|
||||
'attributes' => ['class' => 'number-format'],
|
||||
]),
|
||||
FormField::text('amount', [
|
||||
'label' => 'Wartosc',
|
||||
'tab' => 'settings',
|
||||
'attributes' => ['class' => 'number-format'],
|
||||
]),
|
||||
FormField::date('date_from', [
|
||||
'label' => 'Data od',
|
||||
'tab' => 'settings',
|
||||
]),
|
||||
FormField::date('date_to', [
|
||||
'label' => 'Data do',
|
||||
'tab' => 'settings',
|
||||
]),
|
||||
FormField::custom('categories_group_1', \Tpl::view('shop-promotion/promotion-categories-selector', [
|
||||
'label' => 'Kategorie grupa I',
|
||||
'inputName' => 'categories[]',
|
||||
'categories' => $categories,
|
||||
'selectedIds' => is_array($promotion['categories'] ?? null) ? $promotion['categories'] : [],
|
||||
]), [
|
||||
'tab' => 'categories',
|
||||
]),
|
||||
FormField::custom('categories_group_2', \Tpl::view('shop-promotion/promotion-categories-selector', [
|
||||
'label' => 'Kategorie grupa II',
|
||||
'inputName' => 'condition_categories[]',
|
||||
'categories' => $categories,
|
||||
'selectedIds' => is_array($promotion['condition_categories'] ?? null) ? $promotion['condition_categories'] : [],
|
||||
]), [
|
||||
'tab' => 'categories',
|
||||
]),
|
||||
];
|
||||
|
||||
$tabs = [
|
||||
new FormTab('settings', 'Ustawienia', 'fa-wrench'),
|
||||
new FormTab('categories', 'Kategorie', 'fa-folder-open'),
|
||||
];
|
||||
|
||||
$actionUrl = '/admin/shop_promotion/save/' . ($isNew ? '' : ('id=' . $id));
|
||||
$actions = [
|
||||
FormAction::save($actionUrl, '/admin/shop_promotion/list/'),
|
||||
FormAction::cancel('/admin/shop_promotion/list/'),
|
||||
];
|
||||
|
||||
return new FormEditViewModel(
|
||||
'shop-promotion-edit',
|
||||
$isNew ? 'Nowa promocja' : ('Edycja promocji: ' . (string)($promotion['name'] ?? '')),
|
||||
$data,
|
||||
$fields,
|
||||
$tabs,
|
||||
$actions,
|
||||
'POST',
|
||||
$actionUrl,
|
||||
'/admin/shop_promotion/list/',
|
||||
true,
|
||||
['id' => $id]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,6 +302,13 @@ class Site
|
||||
new \Domain\Languages\LanguagesRepository( $mdb )
|
||||
);
|
||||
},
|
||||
'ShopPromotion' => function() {
|
||||
global $mdb;
|
||||
|
||||
return new \admin\Controllers\ShopPromotionController(
|
||||
new \Domain\Promotion\PromotionRepository( $mdb )
|
||||
);
|
||||
},
|
||||
'Pages' => function() {
|
||||
global $mdb;
|
||||
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
<?php
|
||||
namespace admin\controls;
|
||||
class ShopPromotion
|
||||
{
|
||||
// usunięcie promocji
|
||||
static public function promotion_delete()
|
||||
{
|
||||
$promotion = \R::load( 'pp_shop_promotion', (int)\S::get( 'id' ) );
|
||||
if ( \R::trash( $promotion ) )
|
||||
\S::alert( 'Promocja została usunięta' );
|
||||
|
||||
header( 'Location: /admin/shop_promotion/view_list/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
// zapisanie promocji
|
||||
static public function save()
|
||||
{
|
||||
$response = [ 'status' => 'error', 'msg' => 'Podczas zapisywania promocji wystąpił błąd. Proszę spróbować ponownie' ];
|
||||
$values = json_decode( \S::get( 'values' ), true );
|
||||
|
||||
if ( $id = \admin\factory\ShopPromotion::save(
|
||||
$values['id'],
|
||||
$values['name'],
|
||||
$values['status'] == 'on' ? 1 : 0,
|
||||
$values['condition_type'],
|
||||
$values['discount_type'],
|
||||
$values['amount'],
|
||||
$values['date_to'],
|
||||
$values['categories'],
|
||||
$values['condition_categories'],
|
||||
$values['include_coupon'] == 'on' ? 1 : 0,
|
||||
$values['include_product_promo'] == 'on' ? 1 : 0,
|
||||
$values['min_product_count'],
|
||||
$values['price_cheapest_product']
|
||||
) )
|
||||
$response = [ 'status' => 'ok', 'msg' => 'Promocja została zapisana', 'id' => $id ];
|
||||
|
||||
echo json_encode( $response );
|
||||
exit;
|
||||
}
|
||||
|
||||
// edycja promocji
|
||||
static public function edit()
|
||||
{
|
||||
return \Tpl::view( 'shop-promotion/promotion-edit', [
|
||||
'promotion' => \admin\factory\ShopPromotion::promotion_details( (int)\S::get( 'id' ) ),
|
||||
'categories' => \admin\factory\ShopCategory::subcategories( null ),
|
||||
'dlang' => \front\factory\Languages::default_language()
|
||||
] );
|
||||
}
|
||||
|
||||
// lista promocji
|
||||
public static function view_list()
|
||||
{
|
||||
return \Tpl::view( 'shop-promotion/view-list' );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
<?php
|
||||
namespace admin\factory;
|
||||
class ShopPromotion
|
||||
{
|
||||
static public function promotion_details( int $promotion_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> get( 'pp_shop_promotion', '*', [ 'id' => $promotion_id ] );
|
||||
}
|
||||
|
||||
static public function save( $promotion_id, $name, $status, $condition_type, $discount_type, $amount, $date_to, $categories, $condition_categories, $include_coupon, $include_product_promo, $min_product_count, $price_cheapest_product )
|
||||
{
|
||||
global $mdb, $user;
|
||||
|
||||
if ( !$promotion_id )
|
||||
{
|
||||
$mdb -> insert( 'pp_shop_promotion', [
|
||||
'name' => $name,
|
||||
'status' => $status,
|
||||
'condition_type' => $condition_type,
|
||||
'discount_type' => $discount_type,
|
||||
'amount' => $amount,
|
||||
'date_to' => $date_to != '' ? $date_to : null,
|
||||
'categories' => $categories != null ? ( is_array( $categories ) ? json_encode( $categories ) : json_encode( [ $categories ] ) ) : null,
|
||||
'condition_categories' => $condition_categories != null ? ( is_array( $condition_categories ) ? json_encode( $condition_categories ) : json_encode( [ $condition_categories ] ) ) : null,
|
||||
'include_coupon' => $include_coupon,
|
||||
'include_product_promo' => $include_product_promo,
|
||||
'min_product_count' => $min_product_count ? $min_product_count : null,
|
||||
'price_cheapest_product' => $price_cheapest_product ? $price_cheapest_product : null
|
||||
] );
|
||||
|
||||
return $mdb -> id();
|
||||
}
|
||||
else
|
||||
{
|
||||
$mdb -> update( 'pp_shop_promotion', [
|
||||
'name' => $name,
|
||||
'status' => $status,
|
||||
'condition_type' => $condition_type,
|
||||
'discount_type' => $discount_type,
|
||||
'amount' => $amount,
|
||||
'date_to' => $date_to != '' ? $date_to : null,
|
||||
'categories' => $categories != null ? ( is_array( $categories ) ? json_encode( $categories ) : json_encode( [ $categories ] ) ) : null,
|
||||
'condition_categories' => $condition_categories != null ? ( is_array( $condition_categories ) ? json_encode( $condition_categories ) : json_encode( [ $condition_categories ] ) ) : null,
|
||||
'include_coupon' => $include_coupon,
|
||||
'include_product_promo' => $include_product_promo,
|
||||
'min_product_count' => $min_product_count ? $min_product_count : null,
|
||||
'price_cheapest_product' => $price_cheapest_product ? $price_cheapest_product : null
|
||||
], [
|
||||
'id' => $promotion_id
|
||||
] );
|
||||
return $promotion_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user