refactor(shop-coupon): migrate admin module to DI and release 0.266

This commit is contained in:
2026-02-14 00:05:23 +01:00
parent 88e5673569
commit 40e777afe6
40 changed files with 1668 additions and 426 deletions

View File

@@ -0,0 +1,19 @@
<?php
$label = trim((string)($this->label ?? 'Kategorie'));
$inputName = trim((string)($this->inputName ?? 'categories[]'));
$categories = is_array($this->categories ?? null) ? $this->categories : [];
$selectedIds = is_array($this->selectedIds ?? null) ? $this->selectedIds : [];
?>
<div class="form-group row js-coupon-row js-coupon-row-categories">
<label class="col-lg-4 control-label"><?= htmlspecialchars($label, ENT_QUOTES, 'UTF-8'); ?>:</label>
<div class="col-lg-8">
<div class="coupon-categories-box">
<?= \Tpl::view('shop-coupon/coupon-categories-tree', [
'categories' => $categories,
'selectedIds' => $selectedIds,
'inputName' => $inputName,
]); ?>
</div>
</div>
</div>

View File

@@ -0,0 +1,68 @@
<?php
$categories = is_array($this->categories ?? null) ? $this->categories : [];
$inputName = trim((string)($this->inputName ?? 'categories[]'));
$selectedRaw = is_array($this->selectedIds ?? null) ? $this->selectedIds : [];
$selected = [];
foreach ($selectedRaw as $value) {
$id = (int)$value;
if ($id > 0) {
$selected[$id] = true;
}
}
?>
<?php if (!empty($categories)): ?>
<ol class="coupon-categories-tree">
<?php foreach ($categories as $category): ?>
<?php
$categoryId = (int)($category['id'] ?? 0);
if ($categoryId <= 0) {
continue;
}
$title = trim((string)($category['title'] ?? ''));
if ($title === '') {
$title = 'Kategoria #' . $categoryId;
}
$isActive = (int)($category['status'] ?? 0) === 1;
$children = is_array($category['subcategories'] ?? null) ? $category['subcategories'] : [];
$hasChildren = !empty($children);
?>
<li class="coupon-category-item<?= $hasChildren ? ' has-children is-collapsed' : ''; ?>">
<div class="coupon-category-row">
<?php if ($hasChildren): ?>
<button type="button" class="coupon-tree-toggle" aria-expanded="false" title="Rozwin / zwin">
<i class="fa fa-caret-right"></i>
</button>
<?php else: ?>
<span class="coupon-tree-spacer"></span>
<?php endif; ?>
<?php if (!$isActive): ?>
<i class="fa fa-ban text-danger" title="Kategoria nieaktywna"></i>
<?php endif; ?>
<label class="coupon-category-label">
<input
type="checkbox"
class="g-checkbox"
name="<?= htmlspecialchars($inputName, ENT_QUOTES, 'UTF-8'); ?>"
value="<?= $categoryId; ?>"
<?= isset($selected[$categoryId]) ? 'checked="checked"' : ''; ?>
/>
<span><?= htmlspecialchars($title, ENT_QUOTES, 'UTF-8'); ?></span>
</label>
</div>
<?php if ($hasChildren): ?>
<?= \Tpl::view('shop-coupon/coupon-categories-tree', [
'categories' => $children,
'selectedIds' => array_keys($selected),
'inputName' => $inputName,
]); ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ol>
<?php endif; ?>

View File

@@ -0,0 +1,109 @@
<style type="text/css">
.coupon-categories-box {
max-height: 420px;
overflow: auto;
border: 1px solid #d6d6d6;
border-radius: 4px;
padding: 8px 10px;
background: #fff;
}
.coupon-categories-tree {
margin: 0;
padding: 0 0 0 16px;
list-style: none;
}
.coupon-category-item {
margin: 2px 0;
}
.coupon-category-item.is-collapsed > ol {
display: none;
}
.coupon-category-row {
display: flex;
align-items: center;
gap: 6px;
min-height: 28px;
}
.coupon-tree-toggle,
.coupon-tree-spacer {
width: 18px;
height: 18px;
display: inline-flex;
align-items: center;
justify-content: center;
border: 0;
background: transparent;
padding: 0;
}
.coupon-tree-toggle {
cursor: pointer;
color: #666;
}
.coupon-tree-toggle:focus,
.coupon-tree-toggle:active,
.coupon-tree-toggle:focus-visible {
outline: none;
box-shadow: none;
}
.coupon-category-item:not(.is-collapsed) > .coupon-category-row .coupon-tree-toggle i {
transform: rotate(90deg);
}
.coupon-category-label {
margin: 0;
font-weight: 600;
cursor: pointer;
display: inline-flex;
align-items: center;
gap: 6px;
}
.coupon-category-row .icheckbox_minimal-blue {
margin-top: 0;
}
</style>
<script type="text/javascript">
(function($) {
if (!$) {
return;
}
$(document).off('click.shopCouponTree', '.coupon-tree-toggle');
$(document).on('click.shopCouponTree', '.coupon-tree-toggle', function(e) {
e.preventDefault();
var $item = $(this).closest('.coupon-category-item');
$item.toggleClass('is-collapsed');
$(this).attr('aria-expanded', $item.hasClass('is-collapsed') ? 'false' : 'true');
this.blur();
});
function initCouponCategoryCheckboxes() {
if (!$.fn || typeof $.fn.iCheck !== 'function') {
return;
}
$('.coupon-categories-box .g-checkbox').each(function() {
var $checkbox = $(this);
if ($checkbox.parent().hasClass('icheckbox_minimal-blue')) {
return;
}
$checkbox.iCheck({
checkboxClass: 'icheckbox_minimal-blue',
radioClass: 'iradio_minimal-blue'
});
});
}
initCouponCategoryCheckboxes();
})(window.jQuery);
</script>

View File

@@ -0,0 +1,3 @@
<?= \Tpl::view('components/form-edit', ['form' => $this->form]); ?>
<?= \Tpl::view('shop-coupon/coupon-edit-custom-script'); ?>

View File

@@ -1,151 +0,0 @@
<script type="text/javascript" src="/libraries/framework/vendor/plugins/ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="/libraries/framework/vendor/plugins/ckeditor/adapters/jquery.js"></script>
<script type="text/javascript" src="/libraries/jquery/sortable/sortable.js"></script>
<?
global $db;
ob_start();
?>
<div id="settings-tabs">
<ul class="resp-tabs-list settings-tabs">
<li><i class="fa fa-wrench"></i>Ustawienia</li>
</ul>
<div class="resp-tabs-container settings-tabs">
<div>
<?= \Html::input([
'label' => 'Nazwa',
'name' => 'name',
'id' => 'name',
'value' => $this -> coupon['name']
]); ?>
<?= \Html::input_switch([
'label' => 'Wysłany',
'name' => 'send',
'checked' => $this -> coupon['send'] == 1 ? true : false
]); ?>
<?= \Html::input_switch([
'label' => 'Aktywny',
'name' => 'status',
'checked' => $this -> coupon['status'] == 1 ? true : false
]); ?>
<?= \Html::input_switch( [
'label' => 'Użyty',
'name' => 'used',
'checked' => $this -> coupon['used'] == 1 ? true : false,
]); ?>
<?= \Html::select([
'label' => 'Typ kuponu',
'name' => 'type',
'values' => [ 1 => 'Rabat procentowy na koszyk'],
'value' => $this -> coupo['type'],
]); ?>
<?= \Html::input([
'label' => 'Wartość',
'class' => 'number-format',
'name' => 'amount',
'id' => 'amount',
'value' => $this -> coupon['amount']
]); ?>
<?= \Html::input_switch([
'label' => 'Kupon jednorazowy',
'name' => 'one_time',
'checked' => $this -> coupon['one_time'] == 1 ? true : false
]); ?>
<?= \Html::input_switch([
'label' => 'Dotyczy również produktów przecenionych',
'name' => 'include_discounted_product',
'checked' => $this -> coupon['include_discounted_product'] == 1 ? true : false,
]); ?>
<div class="form-group row">
<label class="col-lg-4 control-label">Ogranicz promocję do wybranych kategorii:</label>
<div class="col-lg-8">
<div class="menu_sortable">
<ol class="sortable" id="sortable">
<?
if ( is_array( $this -> categories ) )
{
foreach ( $this -> categories as $category )
{
?>
<li id="list_<?= $category[ 'id' ];?>" class="category_<?= $category[ 'id' ];?>" category="<?= $category[ 'id' ];?>">
<div class="context_0 content content_menu">
<span class="disclose"><span></span></span>
<? if ( !$category[ 'status' ] ) echo '<i class="fa fa-ban fa-lg text-danger" title="Kategoria nieaktywna"></i>';?>
<input type="checkbox" class="g-checkbox" name="categories[]" value="<?= $category[ 'id' ];?>" <? if ( is_array( json_decode( $this -> coupon['categories'] ) ) and in_array( $category[ 'id' ], json_decode( $this -> coupon['categories'] ) ) ):?>checked="checked"<? endif;?> />
<b><?= $category[ 'languages' ][ $this -> dlang ][ 'title' ];?></b>
</div>
<?=
\Tpl::view( 'shop-product/subcategories-list', [
'categories' => \admin\factory\ShopCategory::subcategories( $category[ 'id' ] ),
'product_categories' => json_decode( $this -> coupon['categories'] ),
'dlang' => $this -> dlang
] );
?>
</li>
<?
}
}
?>
</ol>
</div>
</div>
<div class="clear"></div>
</div>
</div>
</div>
</div>
<?
$out = ob_get_clean();
$grid = new \gridEdit;
$grid->id = 'coupon-edit';
$grid->gdb_opt = $gdb;
$grid->include_plugins = true;
$grid->title = $this -> coupon['id'] ? 'Edycja kuponu: <u>' . $this -> coupon['name'] . '</u>' : 'Nowy kupon';
$grid->fields = [
[
'db' => 'id',
'type' => 'hidden',
'value' => $this -> coupon['id']
]
];
$grid->actions = [
'save' => ['url' => '/admin/shop_coupon/coupon_save/', 'back_url' => '/admin/shop_coupon/view_list/'],
'cancel' => ['url' => '/admin/shop_coupon/view_list/']
];
$grid->external_code = $out;
$grid->persist_edit = true;
$grid->id_param = 'id';
echo $grid->draw();
?>
<link type="text/css" rel="stylesheet" href="/libraries/plupload/jquery.plupload.queue/css/jquery.plupload.queue.css">
<link type="text/css" rel="stylesheet" href="/libraries/selectize/css/selectize.css">
<link type="text/css" rel="stylesheet" href="/libraries/selectize/css/selectize.default.css">
<script type="text/javascript" src="/libraries/plupload/plupload.js"></script>
<script type="text/javascript" src="/libraries/plupload/plupload.flash.js"></script>
<script type="text/javascript" src="/libraries/plupload/plupload.html5.js"></script>
<script type="text/javascript" src="/libraries/plupload/plupload.html4.js"></script>
<script type="text/javascript" src="/libraries/plupload/jquery.plupload.queue/jquery.plupload.queue.js"></script>
<script type="text/javascript" src="/libraries/plupload/i18n/pl.js"></script>
<script type="text/javascript" src="/libraries/jquery-nested-sortable/jquery.mjs.nestedSortable.js"></script>
<script type="text/javascript" src="/libraries/jquery/lozad.js"></script>
<script type="text/javascript" src="/libraries/selectize/js/standalone/selectize.js"></script>
<script type="text/javascript">
$(function() {
const observer = lozad();
observer.observe();
disable_menu();
$('#settings-tabs').easyResponsiveTabs({
width: 'auto',
fit: true,
tabidentify: 'settings-tabs',
type: 'vertical'
});
});
</script>

View File

@@ -0,0 +1,2 @@
<?= \Tpl::view('components/table-list', ['list' => $this->viewModel]); ?>

View File

@@ -1,88 +0,0 @@
<?php
global $gdb;
$grid = new \grid( 'pp_shop_coupon' );
$grid -> gdb_opt = $gdb;
$grid -> debug = true;
$grid -> order = [ 'column' => 'name', 'type' => 'ASC' ];
$grid -> search = [
[ 'name' => 'Nazwa', 'db' => 'name', 'type' => 'text' ],
[ 'name' => 'Aktywny', 'db' => 'status', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ],
[ 'name' => 'Użyty', 'db' => 'used', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ],
[ 'name' => 'Wysłany', 'db' => 'send', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ]
];
$grid -> columns_view = [
[
'name' => 'Lp.',
'th' => [ 'class' => 'g-lp' ],
'td' => [ 'class' => 'g-center' ],
'autoincrement' => true
], [
'name' => 'Aktywny',
'db' => 'status',
'replace' => [ 'array' => [ 0 => '<span style="color: #FF0000;">nie</span>', 1 => 'tak' ] ],
'td' => [ 'class' => 'g-center' ],
'th' => [ 'class' => 'g-center', 'style' => 'width: 150px;' ],
'sort' => true
], [
'name' => 'Użyto X razy',
'db' => 'used_count',
'td' => [ 'class' => 'g-center' ],
'th' => [ 'class' => 'g-center', 'style' => 'width: 150px;' ],
], [
'name' => 'Nazwa',
'db' => 'name',
'sort' => true,
'php' => 'echo "<a href=\'/admin/shop_coupon/coupon_edit/id=[id]\'>[name]</a>";'
], [
'name' => 'Typ kuponu',
'db' => 'type',
'replace' => [ 'array' => [ 1 => 'Rabat procentowy na koszyk' ] ]
], [
'name' => 'Wartość',
'db' => 'amount',
'php' => 'if ( [type] == 1 ) echo "[amount]%";'
], [
'name' => 'Kupon jednorazowy',
'db' => 'one_time',
'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ],
'td' => [ 'class' => 'g-center' ],
'th' => [ 'class' => 'g-center', 'style' => 'width: 150px;' ]
], [
'name' => 'Wysłany',
'db' => 'send',
'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ],
'td' => [ 'class' => 'g-center' ],
'th' => [ 'class' => 'g-center', 'style' => 'width: 150px;' ]
], [
'name' => 'Użyty',
'db' => 'used',
'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ],
'td' => [ 'class' => 'g-center' ],
'th' => [ 'class' => 'g-center', 'style' => 'width: 150px;' ]
], [
'name' => 'Data użycia',
'db' => 'date_used',
'td' => [ 'class' => 'g-center' ],
'th' => [ 'class' => 'g-center', 'style' => 'width: 150px;' ]
], [
'name' => 'Edytuj',
'action' => [ 'type' => 'edit', 'url' => '/admin/shop_coupon/coupon_edit/id=[id]' ],
'th' => [ 'class' => 'g-center', 'style' => 'width: 70px;' ],
'td' => [ 'class' => 'g-center' ]
], [
'name' => 'Usuń',
'action' => [ 'type' => 'delete', 'url' => '/admin/shop_coupon/coupon_delete/id=[id]' ],
'th' => [ 'class' => 'g-center', 'style' => 'width: 70px;' ],
'td' => [ 'class' => 'g-center' ]
]
];
$grid -> buttons = [
[
'label' => 'Dodaj kupon',
'url' => '/admin/shop_coupon/coupon_edit/',
'icon' => 'fa-plus-circle',
'class' => 'btn-success'
]
];
echo $grid -> draw();