refactor(shop-coupon): migrate admin module to DI and release 0.266
This commit is contained in:
@@ -602,6 +602,7 @@ class ArticleRepository
|
||||
]);
|
||||
}
|
||||
|
||||
\S::delete_dir('../temp/');
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -636,6 +637,7 @@ class ArticleRepository
|
||||
}
|
||||
}
|
||||
|
||||
\S::delete_dir('../temp/');
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -102,6 +102,8 @@ class BannerRepository
|
||||
$this->saveTranslations($bannerId, $data['src'], $data['url'], $data['html'], $data['text']);
|
||||
}
|
||||
|
||||
\S::delete_dir('../temp/');
|
||||
|
||||
return (int)$bannerId;
|
||||
}
|
||||
|
||||
|
||||
391
autoload/Domain/Coupon/CouponRepository.php
Normal file
391
autoload/Domain/Coupon/CouponRepository.php
Normal file
@@ -0,0 +1,391 @@
|
||||
<?php
|
||||
namespace Domain\Coupon;
|
||||
|
||||
class CouponRepository
|
||||
{
|
||||
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 = 'name',
|
||||
string $sortDir = 'ASC',
|
||||
int $page = 1,
|
||||
int $perPage = 15
|
||||
): array {
|
||||
$allowedSortColumns = [
|
||||
'id' => 'sc.id',
|
||||
'status' => 'sc.status',
|
||||
'used_count' => 'sc.used_count',
|
||||
'name' => 'sc.name',
|
||||
'type' => 'sc.type',
|
||||
'amount' => 'sc.amount',
|
||||
'one_time' => 'sc.one_time',
|
||||
'send' => 'sc.send',
|
||||
'used' => 'sc.used',
|
||||
'date_used' => 'sc.date_used',
|
||||
];
|
||||
|
||||
$sortSql = $allowedSortColumns[$sortColumn] ?? 'sc.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[] = 'sc.name LIKE :name';
|
||||
$params[':name'] = '%' . $name . '%';
|
||||
}
|
||||
|
||||
$status = trim((string)($filters['status'] ?? ''));
|
||||
if ($status === '0' || $status === '1') {
|
||||
$where[] = 'sc.status = :status';
|
||||
$params[':status'] = (int)$status;
|
||||
}
|
||||
|
||||
$used = trim((string)($filters['used'] ?? ''));
|
||||
if ($used === '0' || $used === '1') {
|
||||
$where[] = 'sc.used = :used';
|
||||
$params[':used'] = (int)$used;
|
||||
}
|
||||
|
||||
$send = trim((string)($filters['send'] ?? ''));
|
||||
if ($send === '0' || $send === '1') {
|
||||
$where[] = 'sc.send = :send';
|
||||
$params[':send'] = (int)$send;
|
||||
}
|
||||
|
||||
$whereSql = implode(' AND ', $where);
|
||||
|
||||
$sqlCount = "
|
||||
SELECT COUNT(0)
|
||||
FROM pp_shop_coupon AS sc
|
||||
WHERE {$whereSql}
|
||||
";
|
||||
|
||||
$stmtCount = $this->db->query($sqlCount, $params);
|
||||
$countRows = $stmtCount ? $stmtCount->fetchAll() : [];
|
||||
$total = isset($countRows[0][0]) ? (int)$countRows[0][0] : 0;
|
||||
|
||||
$sql = "
|
||||
SELECT
|
||||
sc.id,
|
||||
sc.name,
|
||||
sc.status,
|
||||
sc.used,
|
||||
sc.type,
|
||||
sc.amount,
|
||||
sc.one_time,
|
||||
sc.send,
|
||||
sc.include_discounted_product,
|
||||
sc.categories,
|
||||
sc.date_used,
|
||||
sc.used_count
|
||||
FROM pp_shop_coupon AS sc
|
||||
WHERE {$whereSql}
|
||||
ORDER BY {$sortSql} {$sortDir}, sc.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);
|
||||
$item['used'] = $this->toSwitchValue($item['used'] ?? 0);
|
||||
$item['one_time'] = $this->toSwitchValue($item['one_time'] ?? 0);
|
||||
$item['send'] = $this->toSwitchValue($item['send'] ?? 0);
|
||||
$item['used_count'] = (int)($item['used_count'] ?? 0);
|
||||
$item['type'] = (int)($item['type'] ?? 1);
|
||||
$item['categories'] = $this->decodeIdList($item['categories'] ?? null);
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return [
|
||||
'items' => $items,
|
||||
'total' => $total,
|
||||
];
|
||||
}
|
||||
|
||||
public function find(int $couponId): array
|
||||
{
|
||||
if ($couponId <= 0) {
|
||||
return $this->defaultCoupon();
|
||||
}
|
||||
|
||||
$coupon = $this->db->get('pp_shop_coupon', '*', ['id' => $couponId]);
|
||||
if (!is_array($coupon)) {
|
||||
return $this->defaultCoupon();
|
||||
}
|
||||
|
||||
$coupon['id'] = (int)($coupon['id'] ?? 0);
|
||||
$coupon['status'] = $this->toSwitchValue($coupon['status'] ?? 0);
|
||||
$coupon['send'] = $this->toSwitchValue($coupon['send'] ?? 0);
|
||||
$coupon['used'] = $this->toSwitchValue($coupon['used'] ?? 0);
|
||||
$coupon['one_time'] = $this->toSwitchValue($coupon['one_time'] ?? 0);
|
||||
$coupon['include_discounted_product'] = $this->toSwitchValue($coupon['include_discounted_product'] ?? 0);
|
||||
$coupon['type'] = (int)($coupon['type'] ?? 1);
|
||||
$coupon['used_count'] = (int)($coupon['used_count'] ?? 0);
|
||||
$coupon['categories'] = $this->decodeIdList($coupon['categories'] ?? null);
|
||||
|
||||
return $coupon;
|
||||
}
|
||||
|
||||
public function save(array $data): ?int
|
||||
{
|
||||
$couponId = (int)($data['id'] ?? 0);
|
||||
|
||||
$row = [
|
||||
'name' => trim((string)($data['name'] ?? '')),
|
||||
'status' => $this->toSwitchValue($data['status'] ?? 0),
|
||||
'send' => $this->toSwitchValue($data['send'] ?? 0),
|
||||
'used' => $this->toSwitchValue($data['used'] ?? 0),
|
||||
'type' => (int)($data['type'] ?? 1),
|
||||
'amount' => $this->toNullableNumeric($data['amount'] ?? null),
|
||||
'one_time' => $this->toSwitchValue($data['one_time'] ?? 0),
|
||||
'include_discounted_product' => $this->toSwitchValue($data['include_discounted_product'] ?? 0),
|
||||
'categories' => $this->encodeIdList($data['categories'] ?? null),
|
||||
];
|
||||
|
||||
if ($couponId <= 0) {
|
||||
$this->db->insert('pp_shop_coupon', $row);
|
||||
$id = (int)$this->db->id();
|
||||
return $id > 0 ? $id : null;
|
||||
}
|
||||
|
||||
$this->db->update('pp_shop_coupon', $row, ['id' => $couponId]);
|
||||
return $couponId;
|
||||
}
|
||||
|
||||
public function delete(int $couponId): bool
|
||||
{
|
||||
if ($couponId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)$this->db->delete('pp_shop_coupon', ['id' => $couponId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 defaultCoupon(): array
|
||||
{
|
||||
return [
|
||||
'id' => 0,
|
||||
'name' => '',
|
||||
'status' => 1,
|
||||
'send' => 0,
|
||||
'used' => 0,
|
||||
'type' => 1,
|
||||
'amount' => null,
|
||||
'one_time' => 1,
|
||||
'include_discounted_product' => 0,
|
||||
'categories' => [],
|
||||
'used_count' => 0,
|
||||
'date_used' => 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 toNullableNumeric($value): ?string
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$stringValue = trim((string)$value);
|
||||
if ($stringValue === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return str_replace(',', '.', $stringValue);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,6 +249,7 @@ class DictionariesRepository
|
||||
private function clearCache(): void
|
||||
{
|
||||
if (class_exists('\S') && method_exists('\S', 'delete_dir')) {
|
||||
\S::delete_dir('../temp/');
|
||||
\S::delete_dir('../temp/dictionaries');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ class IntegrationsRepository
|
||||
} else {
|
||||
$this->db->insert( $table, [ 'name' => $name, 'value' => $value ] );
|
||||
}
|
||||
\S::delete_dir('../temp/');
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,13 @@ class LayoutsRepository
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)$this->db->delete('pp_layouts', ['id' => $layoutId]);
|
||||
$deleted = (bool)$this->db->delete('pp_layouts', ['id' => $layoutId]);
|
||||
if ($deleted) {
|
||||
\S::delete_dir('../temp/');
|
||||
$this->clearFrontLayoutsCache();
|
||||
}
|
||||
|
||||
return $deleted;
|
||||
}
|
||||
|
||||
public function find(int $layoutId): array
|
||||
@@ -77,6 +83,7 @@ class LayoutsRepository
|
||||
$this->syncCategories($layoutId, $data['categories'] ?? []);
|
||||
|
||||
\S::delete_dir('../temp/');
|
||||
$this->clearFrontLayoutsCache();
|
||||
|
||||
return $layoutId;
|
||||
}
|
||||
@@ -288,6 +295,22 @@ class LayoutsRepository
|
||||
];
|
||||
}
|
||||
|
||||
private function clearFrontLayoutsCache(): void
|
||||
{
|
||||
if (!class_exists('\CacheHandler')) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$cacheHandler = new \CacheHandler();
|
||||
if (method_exists($cacheHandler, 'deletePattern')) {
|
||||
$cacheHandler->deletePattern('*Layouts::*');
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
// Inwalidacja cache nie moze blokowac zapisu/usuwania.
|
||||
}
|
||||
}
|
||||
|
||||
private function menuPages(int $menuId, $parentId = null): array
|
||||
{
|
||||
if ($menuId <= 0) {
|
||||
|
||||
@@ -25,6 +25,7 @@ class NewsletterRepository
|
||||
{
|
||||
$this->settingsRepository->updateSetting('newsletter_footer', (string)($values['newsletter_footer'] ?? ''));
|
||||
$this->settingsRepository->updateSetting('newsletter_header', (string)($values['newsletter_header'] ?? ''));
|
||||
\S::delete_dir('../temp/');
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -289,4 +290,3 @@ class NewsletterRepository
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -172,6 +172,9 @@ class PagesRepository
|
||||
'status' => $statusValue,
|
||||
]);
|
||||
|
||||
if ($result) {
|
||||
\S::delete_dir('../temp/');
|
||||
}
|
||||
return (bool)$result;
|
||||
}
|
||||
|
||||
@@ -182,6 +185,7 @@ class PagesRepository
|
||||
'id' => $menuId,
|
||||
]);
|
||||
|
||||
\S::delete_dir('../temp/');
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -278,6 +282,7 @@ class PagesRepository
|
||||
]);
|
||||
}
|
||||
|
||||
\S::delete_dir('../temp/');
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -148,11 +148,13 @@ class PromotionRepository
|
||||
}
|
||||
|
||||
$this->invalidateActivePromotionsCache();
|
||||
\S::delete_dir('../temp/');
|
||||
return $id;
|
||||
}
|
||||
|
||||
$this->db->update('pp_shop_promotion', $row, ['id' => $promotionId]);
|
||||
$this->invalidateActivePromotionsCache();
|
||||
\S::delete_dir('../temp/');
|
||||
|
||||
return $promotionId;
|
||||
}
|
||||
|
||||
@@ -90,6 +90,7 @@ class SettingsRepository
|
||||
// Zachowanie zgodne z dotychczasowym flow: pelna podmiana zestawu ustawien.
|
||||
$this->db->query('TRUNCATE pp_settings');
|
||||
$this->updateSettings($settingsToSave);
|
||||
\S::delete_dir('../temp/');
|
||||
|
||||
\S::set_message('Ustawienia zostaly zapisane');
|
||||
|
||||
|
||||
@@ -154,6 +154,7 @@ class UserRepository
|
||||
]);
|
||||
|
||||
if ($inserted) {
|
||||
\S::delete_dir('../temp/');
|
||||
return ['status' => 'ok', 'msg' => 'Uzytkownik zostal zapisany.'];
|
||||
}
|
||||
|
||||
@@ -186,6 +187,7 @@ class UserRepository
|
||||
'id' => $userId,
|
||||
]);
|
||||
|
||||
\S::delete_dir('../temp/');
|
||||
return ['status' => 'ok', 'msg' => 'Uzytkownik zostal zapisany.'];
|
||||
}
|
||||
|
||||
|
||||
@@ -210,7 +210,6 @@ class BannerController
|
||||
$savedId = $this->repository->save($data);
|
||||
|
||||
if ($savedId) {
|
||||
\S::delete_dir('../temp/');
|
||||
$response = [
|
||||
'success' => true,
|
||||
'id' => $savedId,
|
||||
|
||||
353
autoload/admin/Controllers/ShopCouponController.php
Normal file
353
autoload/admin/Controllers/ShopCouponController.php
Normal file
@@ -0,0 +1,353 @@
|
||||
<?php
|
||||
namespace admin\Controllers;
|
||||
|
||||
use Domain\Coupon\CouponRepository;
|
||||
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 ShopCouponController
|
||||
{
|
||||
private CouponRepository $repository;
|
||||
|
||||
public function __construct(CouponRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
public function list(): string
|
||||
{
|
||||
$sortableColumns = ['id', 'status', 'used_count', 'name', 'type', 'amount', 'one_time', 'send', 'used', 'date_used'];
|
||||
$filterDefinitions = [
|
||||
[
|
||||
'key' => 'name',
|
||||
'label' => 'Nazwa',
|
||||
'type' => 'text',
|
||||
],
|
||||
[
|
||||
'key' => 'status',
|
||||
'label' => 'Aktywny',
|
||||
'type' => 'select',
|
||||
'options' => [
|
||||
'' => '- aktywny -',
|
||||
'1' => 'tak',
|
||||
'0' => 'nie',
|
||||
],
|
||||
],
|
||||
[
|
||||
'key' => 'used',
|
||||
'label' => 'Uzyty',
|
||||
'type' => 'select',
|
||||
'options' => [
|
||||
'' => '- uzyty -',
|
||||
'1' => 'tak',
|
||||
'0' => 'nie',
|
||||
],
|
||||
],
|
||||
[
|
||||
'key' => 'send',
|
||||
'label' => 'Wyslany',
|
||||
'type' => 'select',
|
||||
'options' => [
|
||||
'' => '- wyslany -',
|
||||
'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);
|
||||
$used = (int)($item['used'] ?? 0);
|
||||
$send = (int)($item['send'] ?? 0);
|
||||
$oneTime = (int)($item['one_time'] ?? 0);
|
||||
$type = (int)($item['type'] ?? 0);
|
||||
$amount = (string)($item['amount'] ?? '');
|
||||
$dateUsed = trim((string)($item['date_used'] ?? ''));
|
||||
|
||||
$rows[] = [
|
||||
'lp' => $lp++ . '.',
|
||||
'status' => $status === 1 ? 'tak' : '<span style="color: #FF0000;">nie</span>',
|
||||
'used_count' => (int)($item['used_count'] ?? 0),
|
||||
'name' => '<a href="/admin/shop_coupon/edit/id=' . $id . '">' . htmlspecialchars($name, ENT_QUOTES, 'UTF-8') . '</a>',
|
||||
'type' => htmlspecialchars((string)($type === 1 ? 'Rabat procentowy na koszyk' : '-'), ENT_QUOTES, 'UTF-8'),
|
||||
'amount' => $type === 1 && $amount !== '' ? htmlspecialchars($amount, ENT_QUOTES, 'UTF-8') . '%' : '-',
|
||||
'one_time' => $oneTime === 1 ? 'tak' : 'nie',
|
||||
'send' => $send === 1 ? 'tak' : 'nie',
|
||||
'used' => $used === 1 ? 'tak' : 'nie',
|
||||
'date_used' => $dateUsed !== '' ? htmlspecialchars($dateUsed, ENT_QUOTES, 'UTF-8') : '-',
|
||||
'_actions' => [
|
||||
[
|
||||
'label' => 'Edytuj',
|
||||
'url' => '/admin/shop_coupon/edit/id=' . $id,
|
||||
'class' => 'btn btn-xs btn-primary',
|
||||
],
|
||||
[
|
||||
'label' => 'Usun',
|
||||
'url' => '/admin/shop_coupon/delete/id=' . $id,
|
||||
'class' => 'btn btn-xs btn-danger',
|
||||
'confirm' => 'Na pewno chcesz usunac wybrany kupon?',
|
||||
'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' => 'used_count', 'sort_key' => 'used_count', 'label' => 'Uzyto X razy', 'class' => 'text-center', 'sortable' => true],
|
||||
['key' => 'name', 'sort_key' => 'name', 'label' => 'Nazwa', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'type', 'sort_key' => 'type', 'label' => 'Typ kuponu', 'sortable' => true],
|
||||
['key' => 'amount', 'sort_key' => 'amount', 'label' => 'Wartosc', 'class' => 'text-center', 'sortable' => true, 'raw' => true],
|
||||
['key' => 'one_time', 'sort_key' => 'one_time', 'label' => 'Kupon jednorazowy', 'class' => 'text-center', 'sortable' => true],
|
||||
['key' => 'send', 'sort_key' => 'send', 'label' => 'Wyslany', 'class' => 'text-center', 'sortable' => true],
|
||||
['key' => 'used', 'sort_key' => 'used', 'label' => 'Uzyty', 'class' => 'text-center', 'sortable' => true],
|
||||
['key' => 'date_used', 'sort_key' => 'date_used', 'label' => 'Data uzycia', '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_coupon/list/',
|
||||
'Brak danych w tabeli.',
|
||||
'/admin/shop_coupon/edit/',
|
||||
'Dodaj kupon'
|
||||
);
|
||||
|
||||
return \Tpl::view('shop-coupon/coupons-list', [
|
||||
'viewModel' => $viewModel,
|
||||
]);
|
||||
}
|
||||
|
||||
public function view_list(): string
|
||||
{
|
||||
return $this->list();
|
||||
}
|
||||
|
||||
public function edit(): string
|
||||
{
|
||||
$coupon = $this->repository->find((int)\S::get('id'));
|
||||
$categories = $this->repository->categoriesTree(null);
|
||||
|
||||
return \Tpl::view('shop-coupon/coupon-edit-new', [
|
||||
'form' => $this->buildFormViewModel($coupon, $categories),
|
||||
]);
|
||||
}
|
||||
|
||||
public function coupon_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 kuponu wystapil blad. Prosze sprobowac ponownie.',
|
||||
];
|
||||
|
||||
if (is_array($values)) {
|
||||
$id = $this->repository->save($values);
|
||||
if (!empty($id)) {
|
||||
$response = [
|
||||
'status' => 'ok',
|
||||
'msg' => 'Kupon 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;
|
||||
}
|
||||
}
|
||||
|
||||
$id = $this->repository->save($payload);
|
||||
if (!empty($id)) {
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'id' => (int)$id,
|
||||
'message' => 'Kupon zostal zapisany.',
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'errors' => ['general' => 'Podczas zapisywania kuponu wystapil blad.'],
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
public function coupon_save(): void
|
||||
{
|
||||
$this->save();
|
||||
}
|
||||
|
||||
public function delete(): void
|
||||
{
|
||||
if ($this->repository->delete((int)\S::get('id'))) {
|
||||
\S::alert('Kupon zostal usuniety.');
|
||||
}
|
||||
|
||||
header('Location: /admin/shop_coupon/list/');
|
||||
exit;
|
||||
}
|
||||
|
||||
public function coupon_delete(): void
|
||||
{
|
||||
$this->delete();
|
||||
}
|
||||
|
||||
private function buildFormViewModel(array $coupon, array $categories): FormEditViewModel
|
||||
{
|
||||
$id = (int)($coupon['id'] ?? 0);
|
||||
$isNew = $id <= 0;
|
||||
|
||||
$data = [
|
||||
'id' => $id,
|
||||
'name' => (string)($coupon['name'] ?? ''),
|
||||
'send' => (int)($coupon['send'] ?? 0),
|
||||
'status' => (int)($coupon['status'] ?? 1),
|
||||
'used' => (int)($coupon['used'] ?? 0),
|
||||
'type' => (int)($coupon['type'] ?? 1),
|
||||
'amount' => (string)($coupon['amount'] ?? ''),
|
||||
'one_time' => (int)($coupon['one_time'] ?? 1),
|
||||
'include_discounted_product' => (int)($coupon['include_discounted_product'] ?? 0),
|
||||
];
|
||||
|
||||
$fields = [
|
||||
FormField::hidden('id', $id),
|
||||
FormField::text('name', [
|
||||
'label' => 'Nazwa',
|
||||
'tab' => 'settings',
|
||||
'required' => true,
|
||||
]),
|
||||
FormField::switch('send', [
|
||||
'label' => 'Wyslany',
|
||||
'tab' => 'settings',
|
||||
]),
|
||||
FormField::switch('status', [
|
||||
'label' => 'Aktywny',
|
||||
'tab' => 'settings',
|
||||
'value' => true,
|
||||
]),
|
||||
FormField::switch('used', [
|
||||
'label' => 'Uzyty',
|
||||
'tab' => 'settings',
|
||||
]),
|
||||
FormField::select('type', [
|
||||
'label' => 'Typ kuponu',
|
||||
'tab' => 'settings',
|
||||
'options' => [
|
||||
1 => 'Rabat procentowy na koszyk',
|
||||
],
|
||||
'required' => true,
|
||||
]),
|
||||
FormField::text('amount', [
|
||||
'label' => 'Wartosc',
|
||||
'tab' => 'settings',
|
||||
'attributes' => ['class' => 'number-format'],
|
||||
]),
|
||||
FormField::switch('one_time', [
|
||||
'label' => 'Kupon jednorazowy',
|
||||
'tab' => 'settings',
|
||||
'value' => true,
|
||||
]),
|
||||
FormField::switch('include_discounted_product', [
|
||||
'label' => 'Dotyczy rowniez produktow przecenionych',
|
||||
'tab' => 'settings',
|
||||
]),
|
||||
FormField::custom('coupon_categories', \Tpl::view('shop-coupon/coupon-categories-selector', [
|
||||
'label' => 'Ogranicz promocje do wybranych kategorii',
|
||||
'inputName' => 'categories[]',
|
||||
'categories' => $categories,
|
||||
'selectedIds' => is_array($coupon['categories'] ?? null) ? $coupon['categories'] : [],
|
||||
]), [
|
||||
'tab' => 'categories',
|
||||
]),
|
||||
];
|
||||
|
||||
$tabs = [
|
||||
new FormTab('settings', 'Ustawienia', 'fa-wrench'),
|
||||
new FormTab('categories', 'Kategorie', 'fa-folder-open'),
|
||||
];
|
||||
|
||||
$actionUrl = '/admin/shop_coupon/save/' . ($isNew ? '' : ('id=' . $id));
|
||||
$actions = [
|
||||
FormAction::save($actionUrl, '/admin/shop_coupon/list/'),
|
||||
FormAction::cancel('/admin/shop_coupon/list/'),
|
||||
];
|
||||
|
||||
return new FormEditViewModel(
|
||||
'shop-coupon-edit',
|
||||
$isNew ? 'Nowy kupon' : ('Edycja kuponu: ' . (string)($coupon['name'] ?? '')),
|
||||
$data,
|
||||
$fields,
|
||||
$tabs,
|
||||
$actions,
|
||||
'POST',
|
||||
$actionUrl,
|
||||
'/admin/shop_coupon/list/',
|
||||
true,
|
||||
['id' => $id]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -309,6 +309,13 @@ class Site
|
||||
new \Domain\Promotion\PromotionRepository( $mdb )
|
||||
);
|
||||
},
|
||||
'ShopCoupon' => function() {
|
||||
global $mdb;
|
||||
|
||||
return new \admin\Controllers\ShopCouponController(
|
||||
new \Domain\Coupon\CouponRepository( $mdb )
|
||||
);
|
||||
},
|
||||
'Pages' => function() {
|
||||
global $mdb;
|
||||
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
<?php
|
||||
namespace admin\controls;
|
||||
|
||||
class ShopCoupon
|
||||
{
|
||||
public static function coupon_delete()
|
||||
{
|
||||
$coupon = new \shop\Coupon( (int)\S::get( 'id' ) );
|
||||
if ( $coupon -> delete() )
|
||||
\S::alert( 'Kupon został usunięty.' );
|
||||
header( 'Location: /admin/shop_coupon/view_list/' );
|
||||
exit;
|
||||
}
|
||||
|
||||
public static function coupon_save()
|
||||
{
|
||||
$response = ['status' => 'error', 'msg' => 'Podczas zapisywania kuponu wystąpił błąd. Proszę spróbować ponownie.'];
|
||||
$values = json_decode( \S::get( 'values' ), true );
|
||||
|
||||
if ( $values['categories'] != null )
|
||||
$categories = is_array( $values['categories'] ) ? json_encode( $values['categories'] ) : json_encode( [ $values['categories'] ] );
|
||||
else
|
||||
$categories = null;
|
||||
|
||||
if ( \admin\factory\ShopCoupon::save(
|
||||
$values['id'],
|
||||
$values['name'],
|
||||
$values['status'] == 'on' ? 1 : 0,
|
||||
$values['send'] == 'on' ? 1 : 0,
|
||||
$values['used'] == 'on' ? 1 : 0,
|
||||
$values['type'],
|
||||
$values['amount'],
|
||||
$values['one_time'] == 'on' ? 1 : 0,
|
||||
$values['include_discounted_product'] == 'on' ? 1 : 0,
|
||||
$categories
|
||||
)
|
||||
)
|
||||
$response = [ 'status' => 'ok', 'msg' => 'Kupon został zapisany.', 'id' => $values['id'] ];
|
||||
|
||||
echo json_encode( $response );
|
||||
exit;
|
||||
}
|
||||
|
||||
public static function coupon_edit()
|
||||
{
|
||||
return \Tpl::view( 'shop-coupon/coupon-edit', [
|
||||
'coupon' => \admin\factory\ShopCoupon::details( (int)\S::get( 'id' ) ),
|
||||
'categories' => \admin\factory\ShopCategory::subcategories( null ),
|
||||
'dlang' => \front\factory\Languages::default_language()
|
||||
] );
|
||||
}
|
||||
|
||||
public static function view_list()
|
||||
{
|
||||
return \Tpl::view( 'shop-coupon/view-list' );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
<?php
|
||||
namespace admin\factory;
|
||||
class ShopCoupon
|
||||
{
|
||||
static public function details( int $coupon_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> get( 'pp_shop_coupon', '*', [ 'id' => $coupon_id ] );
|
||||
}
|
||||
|
||||
public static function coupon_delete( $coupon_id )
|
||||
{
|
||||
global $mdb;
|
||||
return $mdb -> delete( 'pp_shop_coupon', [ 'id' => $coupon_id ] );
|
||||
}
|
||||
|
||||
public static function save( $coupon_id, $name, $status, $send, $used, $type, $amount, $one_time, $include_discounted_product, $categories )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
if ( !$coupon_id )
|
||||
{
|
||||
if ( $mdb -> insert( 'pp_shop_coupon', [
|
||||
'name' => $name,
|
||||
'status' => $status,
|
||||
'used' => $used,
|
||||
'type' => $type,
|
||||
'amount' => $amount,
|
||||
'one_time' => $one_time,
|
||||
'send' => $send,
|
||||
'include_discounted_product' => $include_discounted_product,
|
||||
'categories' => $categories
|
||||
] ) )
|
||||
return $mdb -> id();
|
||||
}
|
||||
else
|
||||
{
|
||||
$mdb -> update( 'pp_shop_coupon', [
|
||||
'name' => $name,
|
||||
'status' => $status,
|
||||
'used' => $used,
|
||||
'type' => $type,
|
||||
'amount' => $amount,
|
||||
'one_time' => $one_time,
|
||||
'send' => $send,
|
||||
'include_discounted_product' => $include_discounted_product,
|
||||
'categories' => $categories
|
||||
], [
|
||||
'id' => $coupon_id
|
||||
] );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user