Release 0.265: ShopPromotion date_from and edit save fix
This commit is contained in:
420
autoload/Domain/Promotion/PromotionRepository.php
Normal file
420
autoload/Domain/Promotion/PromotionRepository.php
Normal file
@@ -0,0 +1,420 @@
|
||||
<?php
|
||||
namespace Domain\Promotion;
|
||||
|
||||
class PromotionRepository
|
||||
{
|
||||
private const MAX_PER_PAGE = 100;
|
||||
|
||||
private $db;
|
||||
private ?string $defaultLangId = null;
|
||||
|
||||
public function __construct($db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{items: array<int, array<string, mixed>>, total: int}
|
||||
*/
|
||||
public function listForAdmin(
|
||||
array $filters,
|
||||
string $sortColumn = 'id',
|
||||
string $sortDir = 'DESC',
|
||||
int $page = 1,
|
||||
int $perPage = 15
|
||||
): array {
|
||||
$allowedSortColumns = [
|
||||
'id' => 'sp.id',
|
||||
'name' => 'sp.name',
|
||||
'status' => 'sp.status',
|
||||
'condition_type' => 'sp.condition_type',
|
||||
'date_from' => 'sp.date_from',
|
||||
'date_to' => 'sp.date_to',
|
||||
];
|
||||
|
||||
$sortSql = $allowedSortColumns[$sortColumn] ?? 'sp.id';
|
||||
$sortDir = strtoupper(trim($sortDir)) === 'ASC' ? 'ASC' : 'DESC';
|
||||
$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[] = 'sp.name LIKE :name';
|
||||
$params[':name'] = '%' . $name . '%';
|
||||
}
|
||||
|
||||
$status = trim((string)($filters['status'] ?? ''));
|
||||
if ($status === '0' || $status === '1') {
|
||||
$where[] = 'sp.status = :status';
|
||||
$params[':status'] = (int)$status;
|
||||
}
|
||||
|
||||
$whereSql = implode(' AND ', $where);
|
||||
|
||||
$sqlCount = "
|
||||
SELECT COUNT(0)
|
||||
FROM pp_shop_promotion AS sp
|
||||
WHERE {$whereSql}
|
||||
";
|
||||
|
||||
$stmtCount = $this->db->query($sqlCount, $params);
|
||||
$countRows = $stmtCount ? $stmtCount->fetchAll() : [];
|
||||
$total = isset($countRows[0][0]) ? (int)$countRows[0][0] : 0;
|
||||
|
||||
$sql = "
|
||||
SELECT
|
||||
sp.id,
|
||||
sp.name,
|
||||
sp.status,
|
||||
sp.condition_type,
|
||||
sp.discount_type,
|
||||
sp.amount,
|
||||
sp.date_from,
|
||||
sp.date_to,
|
||||
sp.include_coupon,
|
||||
sp.include_product_promo,
|
||||
sp.min_product_count,
|
||||
sp.price_cheapest_product
|
||||
FROM pp_shop_promotion AS sp
|
||||
WHERE {$whereSql}
|
||||
ORDER BY {$sortSql} {$sortDir}, sp.id DESC
|
||||
LIMIT {$perPage} OFFSET {$offset}
|
||||
";
|
||||
|
||||
$stmt = $this->db->query($sql, $params);
|
||||
$items = $stmt ? $stmt->fetchAll() : [];
|
||||
|
||||
return [
|
||||
'items' => is_array($items) ? $items : [],
|
||||
'total' => $total,
|
||||
];
|
||||
}
|
||||
|
||||
public function find(int $promotionId): array
|
||||
{
|
||||
if ($promotionId <= 0) {
|
||||
return $this->defaultPromotion();
|
||||
}
|
||||
|
||||
$promotion = $this->db->get('pp_shop_promotion', '*', ['id' => $promotionId]);
|
||||
if (!is_array($promotion)) {
|
||||
return $this->defaultPromotion();
|
||||
}
|
||||
|
||||
$promotion['id'] = (int)($promotion['id'] ?? 0);
|
||||
$promotion['status'] = $this->toSwitchValue($promotion['status'] ?? 0);
|
||||
$promotion['include_coupon'] = $this->toSwitchValue($promotion['include_coupon'] ?? 0);
|
||||
$promotion['include_product_promo'] = $this->toSwitchValue($promotion['include_product_promo'] ?? 0);
|
||||
$promotion['condition_type'] = (int)($promotion['condition_type'] ?? 1);
|
||||
$promotion['discount_type'] = (int)($promotion['discount_type'] ?? 1);
|
||||
$promotion['categories'] = $this->decodeIdList($promotion['categories'] ?? null);
|
||||
$promotion['condition_categories'] = $this->decodeIdList($promotion['condition_categories'] ?? null);
|
||||
|
||||
return $promotion;
|
||||
}
|
||||
|
||||
public function save(array $data): ?int
|
||||
{
|
||||
$promotionId = (int)($data['id'] ?? 0);
|
||||
|
||||
$row = [
|
||||
'name' => trim((string)($data['name'] ?? '')),
|
||||
'status' => $this->toSwitchValue($data['status'] ?? 0),
|
||||
'condition_type' => (int)($data['condition_type'] ?? 1),
|
||||
'discount_type' => (int)($data['discount_type'] ?? 1),
|
||||
'amount' => $this->toNullableNumeric($data['amount'] ?? null),
|
||||
'date_from' => $this->toNullableDate($data['date_from'] ?? null),
|
||||
'date_to' => $this->toNullableDate($data['date_to'] ?? null),
|
||||
'categories' => $this->encodeIdList($data['categories'] ?? null),
|
||||
'condition_categories' => $this->encodeIdList($data['condition_categories'] ?? null),
|
||||
'include_coupon' => $this->toSwitchValue($data['include_coupon'] ?? 0),
|
||||
'include_product_promo' => $this->toSwitchValue($data['include_product_promo'] ?? 0),
|
||||
'min_product_count' => $this->toNullableInt($data['min_product_count'] ?? null),
|
||||
'price_cheapest_product' => $this->toNullableNumeric($data['price_cheapest_product'] ?? null),
|
||||
];
|
||||
|
||||
if ($promotionId <= 0) {
|
||||
$this->db->insert('pp_shop_promotion', $row);
|
||||
$id = (int)$this->db->id();
|
||||
if ($id <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->invalidateActivePromotionsCache();
|
||||
return $id;
|
||||
}
|
||||
|
||||
$this->db->update('pp_shop_promotion', $row, ['id' => $promotionId]);
|
||||
$this->invalidateActivePromotionsCache();
|
||||
|
||||
return $promotionId;
|
||||
}
|
||||
|
||||
public function delete(int $promotionId): bool
|
||||
{
|
||||
if ($promotionId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$deleted = $this->db->delete('pp_shop_promotion', ['id' => $promotionId]);
|
||||
$ok = (bool)$deleted;
|
||||
|
||||
if ($ok) {
|
||||
$this->invalidateActivePromotionsCache();
|
||||
}
|
||||
|
||||
return $ok;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function categoriesTree($parentId = null): array
|
||||
{
|
||||
$rows = $this->db->select('pp_shop_categories', ['id'], [
|
||||
'parent_id' => $parentId,
|
||||
'ORDER' => ['o' => 'ASC'],
|
||||
]);
|
||||
|
||||
if (!is_array($rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$categories = [];
|
||||
foreach ($rows as $row) {
|
||||
$categoryId = (int)($row['id'] ?? 0);
|
||||
if ($categoryId <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$category = $this->db->get('pp_shop_categories', '*', ['id' => $categoryId]);
|
||||
if (!is_array($category)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$translations = $this->db->select('pp_shop_categories_langs', '*', ['category_id' => $categoryId]);
|
||||
$category['languages'] = [];
|
||||
if (is_array($translations)) {
|
||||
foreach ($translations as $translation) {
|
||||
$langId = (string)($translation['lang_id'] ?? '');
|
||||
if ($langId !== '') {
|
||||
$category['languages'][$langId] = $translation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$category['title'] = $this->categoryTitle($category['languages']);
|
||||
$category['subcategories'] = $this->categoriesTree($categoryId);
|
||||
$categories[] = $category;
|
||||
}
|
||||
|
||||
return $categories;
|
||||
}
|
||||
|
||||
private function defaultPromotion(): array
|
||||
{
|
||||
return [
|
||||
'id' => 0,
|
||||
'name' => '',
|
||||
'status' => 1,
|
||||
'condition_type' => 1,
|
||||
'discount_type' => 1,
|
||||
'amount' => null,
|
||||
'date_from' => null,
|
||||
'date_to' => null,
|
||||
'categories' => [],
|
||||
'condition_categories' => [],
|
||||
'include_coupon' => 0,
|
||||
'include_product_promo' => 0,
|
||||
'min_product_count' => null,
|
||||
'price_cheapest_product' => null,
|
||||
];
|
||||
}
|
||||
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
$value = trim($value);
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$intValue = (int)$value;
|
||||
return $intValue > 0 ? $intValue : null;
|
||||
}
|
||||
|
||||
private function toNullableNumeric($value): ?string
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$stringValue = trim((string)$value);
|
||||
if ($stringValue === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return str_replace(',', '.', $stringValue);
|
||||
}
|
||||
|
||||
private function toNullableDate($value): ?string
|
||||
{
|
||||
$date = trim((string)$value);
|
||||
if ($date === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $date;
|
||||
}
|
||||
|
||||
private function encodeIdList($values): ?string
|
||||
{
|
||||
$ids = $this->normalizeIdList($values);
|
||||
if (empty($ids)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return json_encode($ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[]
|
||||
*/
|
||||
private function decodeIdList($raw): array
|
||||
{
|
||||
if (is_array($raw)) {
|
||||
return $this->normalizeIdList($raw);
|
||||
}
|
||||
|
||||
$text = trim((string)$raw);
|
||||
if ($text === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
$decoded = json_decode($text, true);
|
||||
if (!is_array($decoded)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->normalizeIdList($decoded);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int[]
|
||||
*/
|
||||
private function normalizeIdList($values): array
|
||||
{
|
||||
if ($values === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (!is_array($values)) {
|
||||
$text = trim((string)$values);
|
||||
if ($text === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (strpos($text, ',') !== false) {
|
||||
$values = explode(',', $text);
|
||||
} else {
|
||||
$values = [$text];
|
||||
}
|
||||
}
|
||||
|
||||
$ids = [];
|
||||
foreach ($values as $value) {
|
||||
$id = (int)$value;
|
||||
if ($id > 0) {
|
||||
$ids[$id] = $id;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($ids);
|
||||
}
|
||||
|
||||
private function categoryTitle(array $languages): string
|
||||
{
|
||||
$defaultLang = $this->defaultLanguageId();
|
||||
if ($defaultLang !== '' && isset($languages[$defaultLang]['title'])) {
|
||||
$title = trim((string)$languages[$defaultLang]['title']);
|
||||
if ($title !== '') {
|
||||
return $title;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($languages as $language) {
|
||||
$title = trim((string)($language['title'] ?? ''));
|
||||
if ($title !== '') {
|
||||
return $title;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
private function defaultLanguageId(): string
|
||||
{
|
||||
if ($this->defaultLangId !== null) {
|
||||
return $this->defaultLangId;
|
||||
}
|
||||
|
||||
$rows = $this->db->select('pp_langs', ['id', 'start', 'o'], [
|
||||
'status' => 1,
|
||||
'ORDER' => ['start' => 'DESC', 'o' => 'ASC'],
|
||||
]);
|
||||
|
||||
if (is_array($rows) && !empty($rows)) {
|
||||
$this->defaultLangId = (string)($rows[0]['id'] ?? '');
|
||||
} else {
|
||||
$this->defaultLangId = '';
|
||||
}
|
||||
|
||||
return $this->defaultLangId;
|
||||
}
|
||||
|
||||
private function invalidateActivePromotionsCache(): void
|
||||
{
|
||||
if (!class_exists('\CacheHandler')) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$cache = new \CacheHandler();
|
||||
if (method_exists($cache, 'delete')) {
|
||||
$cache->delete('\shop\Promotion::get_active_promotions');
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// Cache invalidation should not block save/delete.
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ class Promotion extends DbModel
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$results = $mdb -> select( 'pp_shop_promotion', 'id', [ 'AND' => [ 'status' => 1, 'OR' => [ 'date_to' => null, 'date_to[>=]' => date( 'Y-m-d' ) ] ], 'ORDER' => [ 'id' => 'DESC' ] ] );
|
||||
$results = $mdb -> select( 'pp_shop_promotion', 'id', [ 'AND' => [ 'status' => 1, 'OR #date_from' => [ 'date_from' => null, 'date_from[<=]' => date( 'Y-m-d' ) ], 'OR #date_to' => [ 'date_to' => null, 'date_to[>=]' => date( 'Y-m-d' ) ] ], 'ORDER' => [ 'id' => 'DESC' ] ] );
|
||||
|
||||
$cacheHandler -> set( $cacheKey, $results );
|
||||
}
|
||||
@@ -84,4 +84,4 @@ class Promotion extends DbModel
|
||||
}
|
||||
return $basket;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user