first commit
This commit is contained in:
151
bundles/KupShop/ParameterGroupsBundle/Admin/ParameterGroups.php
Normal file
151
bundles/KupShop/ParameterGroupsBundle/Admin/ParameterGroups.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
|
||||
use Query\Operator;
|
||||
|
||||
class ParameterGroups extends Window
|
||||
{
|
||||
protected $tableName = 'parameter_groups';
|
||||
|
||||
public function get_vars()
|
||||
{
|
||||
$vars = parent::get_vars();
|
||||
$groupItems = $this->getGroupItems();
|
||||
$allParameters = $this->getAllParameters();
|
||||
$params = $this->generateParams($groupItems, $allParameters);
|
||||
|
||||
$vars['body']['data']['allParameters'] = $allParameters;
|
||||
$vars['body']['data']['params'] = $params;
|
||||
|
||||
return $vars;
|
||||
}
|
||||
|
||||
public function handleUpdate()
|
||||
{
|
||||
if (!parent::handleUpdate()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $this->getData();
|
||||
$selects = getVal('select', $data);
|
||||
unset($selects[0]);
|
||||
|
||||
$this->removeDeletedItems($selects);
|
||||
|
||||
$position = 0;
|
||||
foreach ($selects as $key => $value) {
|
||||
$value = array_unique($value);
|
||||
|
||||
if ($key < 0) {
|
||||
$key = $this->insertParameterGroupItem($value);
|
||||
}
|
||||
|
||||
$this->updateGroupParameters($value, $key, ++$position);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function getGroupItems(): array
|
||||
{
|
||||
return sqlQueryBuilder()->select('*')
|
||||
->from('parameter_groups_items')
|
||||
->andWhere(Operator::equals(['id_parameter_group' => $this->getID()]))
|
||||
->orderBy('position')
|
||||
->execute()->fetchAllAssociative();
|
||||
}
|
||||
|
||||
protected function getAllParameters(): array
|
||||
{
|
||||
return sqlQueryBuilder()->select('*')
|
||||
->from('parameters')
|
||||
->execute()
|
||||
->fetchAllAssociative();
|
||||
}
|
||||
|
||||
protected function generateParams(array $groupItems, array $allParameters): array
|
||||
{
|
||||
$qb = sqlQueryBuilder()->select('*')
|
||||
->from('parameter_groups_items_parameters')
|
||||
->orderBy('position')
|
||||
->andWhere('id_parameter_group_item = :itemId');
|
||||
|
||||
$params = [];
|
||||
foreach ($groupItems as $item) {
|
||||
$params[$item['id']] = [
|
||||
'name' => $item['name'],
|
||||
'params' => [],
|
||||
];
|
||||
$qb->setParameter('itemId', $item['id']);
|
||||
foreach ($qb->execute() as $param) {
|
||||
foreach ($allParameters as $singleParameter) {
|
||||
if ($singleParameter['id'] == $param['id_parameter']) {
|
||||
$params[$item['id']]['params'][] = $singleParameter;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
protected function insertParameterIntoGroup(int $groupItemId, int $parameter, int $position): void
|
||||
{
|
||||
sqlQueryBuilder()
|
||||
->insert('parameter_groups_items_parameters')
|
||||
->directValues([
|
||||
'id_parameter_group_item' => $groupItemId,
|
||||
'id_parameter' => $parameter,
|
||||
'position' => $position,
|
||||
])
|
||||
->execute();
|
||||
}
|
||||
|
||||
protected function removeDeletedItems(array $selects): void
|
||||
{
|
||||
sqlQueryBuilder()
|
||||
->delete('parameter_groups_items')
|
||||
->andWhere(Operator::andX(
|
||||
Operator::equals(['id_parameter_group' => $this->getID()]),
|
||||
Operator::not(Operator::inIntArray(array_keys($selects), 'id'))
|
||||
))
|
||||
->execute();
|
||||
}
|
||||
|
||||
protected function insertParameterGroupItem(array $value): int
|
||||
{
|
||||
return sqlGetConnection()->transactional(function () use ($value) {
|
||||
sqlQueryBuilder()
|
||||
->insert('parameter_groups_items')
|
||||
->directValues(['id_parameter_group' => $this->getID(), 'name' => $value['name'] ?? 'def_skupina'])
|
||||
->execute();
|
||||
|
||||
return sqlInsertId();
|
||||
});
|
||||
}
|
||||
|
||||
public function updateGroupParameters(array $parameters, int $groupId, int $position): void
|
||||
{
|
||||
sqlQueryBuilder()->delete('parameter_groups_items_parameters')
|
||||
->andWhere(Operator::equals(['id_parameter_group_item' => $groupId]))
|
||||
->execute();
|
||||
|
||||
// update name
|
||||
if (isset($parameters['name'])) {
|
||||
sqlQueryBuilder()->update('parameter_groups_items')
|
||||
->directValues([
|
||||
'name' => $parameters['name'],
|
||||
'position' => $position, ])
|
||||
->andWhere(Operator::equals(['id' => $groupId]))
|
||||
->execute();
|
||||
}
|
||||
|
||||
$parameterPosition = 0;
|
||||
foreach ($parameters as $parameter) {
|
||||
if (is_numeric($parameter)) {
|
||||
$this->insertParameterIntoGroup($groupId, intval($parameter), ++$parameterPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ParameterGroups::class;
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace KupShop\ParameterGroupsBundle\Admin;
|
||||
|
||||
use KupShop\AdminBundle\AdminRegister\AdminRegister;
|
||||
use KupShop\AdminBundle\AdminRegister\IAdminRegisterStatic;
|
||||
|
||||
class ParameterGroupsAdminRegister extends AdminRegister implements IAdminRegisterStatic
|
||||
{
|
||||
public static function getMenu(): array
|
||||
{
|
||||
return [
|
||||
static::createMenuItem('productsMenu', [
|
||||
'name' => 'ParameterGroups',
|
||||
'left' => 's=menu.php&type=ParameterGroups', 'right' => 's=list.php&type=ParameterGroups',
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPermissions(): array
|
||||
{
|
||||
return [
|
||||
static::createPermissions('ParameterGroups', [\Modules::PARAMETER_GROUPS], ['PARAM']),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
$txt_str['ParameterGroups'] = [
|
||||
'toolbar_list' => 'Skupiny parametrů',
|
||||
'toolbar_add' => 'Přidat skupinu parametrů',
|
||||
|
||||
'titleAdd' => 'Přidat skupinu parametrů',
|
||||
'titleEdit' => 'Upravit skupinu parametrů',
|
||||
|
||||
'ParameterGroups' => 'Skupiny parametrů',
|
||||
|
||||
'name' => 'Název',
|
||||
'addParameterGroup' => 'Přidat skupinu',
|
||||
'groupName' => 'Název skupiny',
|
||||
'addParameter' => 'Přidat parametr',
|
||||
'parameters' => 'Parametry',
|
||||
];
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
use KupShop\AdminBundle\AdminList\BaseList;
|
||||
|
||||
class ParameterGroupsList extends BaseList
|
||||
{
|
||||
use AdminListSortable;
|
||||
|
||||
protected $tableDef = [
|
||||
'id' => 'id',
|
||||
'fields' => [
|
||||
'Název' => ['field' => 'name'],
|
||||
],
|
||||
];
|
||||
|
||||
public function getQuery()
|
||||
{
|
||||
$qb = sqlQueryBuilder()->select('*')
|
||||
->from('parameter_groups');
|
||||
|
||||
return $qb;
|
||||
}
|
||||
}
|
||||
|
||||
return ParameterGroupsList::class;
|
||||
@@ -0,0 +1,205 @@
|
||||
{extends "[shared]window.tpl"}
|
||||
|
||||
{block tabs}
|
||||
{windowTab id='flapParameterGroups' label=translate('ParameterGroups')}
|
||||
{/block}
|
||||
|
||||
{block tabsContent}
|
||||
<div id="flapGlobalDiscount" class="tab-pane fade active in boxStatic">
|
||||
<h1 class="h4 main-panel-title">{'ParameterGroups'|translate}</h1>
|
||||
|
||||
<div class="form-group">
|
||||
<div class="col-md-2 control-label">
|
||||
<label>{'name'|translate}</label>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<input type="text" class="form-control input-sm" name="data[name]" value="{$body.data.name}" required>
|
||||
</div>
|
||||
</div>
|
||||
<div id="parameter_groups" class="panel-group">
|
||||
<div class="row bottom-space">
|
||||
<div class="col-md-3">
|
||||
<a href="#" data-form-add="addRegion" id="addGroup" class="btn btn-success btn-block"><span class="glyphicon glyphicon-plus"></span> {'addParameterGroup'|translate}</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="panel-group-lists">
|
||||
<div class="panel">
|
||||
<div class="panel-heading" style="padding-bottom:4px">
|
||||
<div class="row">
|
||||
<div class="col-md-1 text-center">
|
||||
<small><strong>Pořadí</strong></small>
|
||||
</div>
|
||||
<div class="col-md-10">
|
||||
<small><strong>Název skupiny</strong></small>
|
||||
</div>
|
||||
<div class="col-md-1 text-center">
|
||||
<small><strong>Akce</strong></small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="panel-group-lists treeDragDrop" data-tree-drag>
|
||||
<!--row-->
|
||||
{foreach array_replace([0 => []], $body.data.params) as $key => $param}
|
||||
<div data-form-id="{$param@index}" data-form-item class="panel" {if $key === 0}data-form-new="addGroup" style="display: none"{/if}>
|
||||
<div class="panel-heading row-green" style="background:lightgray" data-toggle="collapse" aria-expanded="{if $key === 0}true{else}false{/if}">
|
||||
<div class="row">
|
||||
<div class="col-md-1 text-center">
|
||||
<span class="drag-drop-mover ui-draggable-handle group-drag-drop-mover">
|
||||
<i class="bi bi-arrows-move"></i>
|
||||
</span>
|
||||
</div>
|
||||
<div class="col-md-10">
|
||||
<p class="input-height">{if $key === 0}{'addParameterGroup'|translate}{else}{$param.name}{/if}</p>
|
||||
</div>
|
||||
|
||||
<div class="col-md-1 text-center">
|
||||
<a class="btn-sm btn btn-danger" data-form-delete>
|
||||
<span class="glyphicon glyphicon-remove"></span>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel-collapse collapse{if $key === 0} in{/if}" style="background-color:#fff">
|
||||
<div class="panel-body border"
|
||||
style="border:1px solid rgba(0, 0, 0, 0.1);border-top:none;border-radius:3px;">
|
||||
<div class="row bottom-space">
|
||||
<div class="form-group">
|
||||
<div class="col-md-2 control-label"><label>{'groupName'|translate}</label></div>
|
||||
<div class="col-md-3">
|
||||
<input type="text" class="form-control input-sm" size="30" maxlength="255" value="{if $key !== 0}{$param.name}{/if}"
|
||||
name="data[select][{$key}][name]" required>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<!--movable row-->
|
||||
<div class="panel-group panel-group-lists">
|
||||
<div class="panel">
|
||||
<div class="panel-heading panel-heading-sm">
|
||||
<div class="row">
|
||||
<div class="col-md-11 col-md-offset-1">
|
||||
<strong>{'parameters'|translate}</strong>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="treeDragDrop" data-tree-drag>
|
||||
{foreach array_merge([0 => null], $param.params) as $pos => $item }
|
||||
<div class="panel" data-form-new="addPanel" data-row-panel="" data-form-item=""
|
||||
{if $pos === 0}style="display:none;background:#fff" data-form-new=""{else}style="background:#fff"{/if}>
|
||||
<div class="row bottom-space d-flex align-items-center">
|
||||
<div class="col-xs-1" style="text-align: center">
|
||||
<input type="hidden" data-sort="" value="{$pos}">
|
||||
<span class="drag-drop-mover ui-draggable-handle">
|
||||
<i class="bi bi-arrows-move"></i>
|
||||
</span>
|
||||
</div>
|
||||
<div class="col-xs-4">
|
||||
<select class="input-sm form-control selecter"
|
||||
name="data[select][{$key}][]">
|
||||
<option value="{$item.id}" selected="">{$item.name}</option>
|
||||
{foreach $body.data.allParameters as $index => $selectParam}
|
||||
{if $selectParam.id != $item.id}
|
||||
<option
|
||||
value="{$selectParam.id}">{$selectParam.name}</option> {/if}
|
||||
{/foreach}
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-xs-6">
|
||||
<a class="btn-sm btn btn-danger pull-right" data-form-delete>
|
||||
<input class="hidden" type="checkbox"/>
|
||||
<span class="glyphicon glyphicon-remove"></span>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/foreach}
|
||||
</div>
|
||||
<!--movable row-->
|
||||
</div>
|
||||
<div class="row bottom-space">
|
||||
<div class="col-md-3">
|
||||
<a href="#" class="btn btn-success btn-block" data-form-copy-row data-form-add=""><span
|
||||
class="glyphicon glyphicon-plus"></span> {'addParameter'|translate}
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{/foreach}
|
||||
<!--row-->
|
||||
</div>
|
||||
</div>
|
||||
{/block}
|
||||
<script type="text/javascript">
|
||||
{block onready append}
|
||||
const initSortable = function(subGroup, handleClass) {
|
||||
$(subGroup).find('[data-tree-drag]').sortable({
|
||||
helper: function(e, row) {
|
||||
var $row = $(row);
|
||||
var $helper = $row.clone().addClass('drag-drop');
|
||||
return $helper[0];
|
||||
},
|
||||
update: function(event, ui) {
|
||||
$(this).children().each(function(index, item) {
|
||||
$(item).find('[data-sort]').val($(item).index()).change();
|
||||
});
|
||||
},
|
||||
handle: handleClass || '.drag-drop-mover',
|
||||
placeholder: 'placeholder',
|
||||
});
|
||||
};
|
||||
|
||||
const initSubGroupCollapse = function(subGroupEl) {
|
||||
const $subGroupEl = $(subGroupEl);
|
||||
$subGroupEl.find('> .panel-heading[data-toggle="collapse"]').click(function(e) {
|
||||
e.preventDefault();
|
||||
$subGroupEl.find('> .panel-collapse').slideToggle(150);
|
||||
});
|
||||
};
|
||||
|
||||
const deleteCallback = function() {
|
||||
const $itemToDeletion = $(this).closest('[data-form-item]');
|
||||
$itemToDeletion.slideUp(300, function() {
|
||||
$itemToDeletion.remove();
|
||||
});
|
||||
};
|
||||
|
||||
// init main form
|
||||
initForm({
|
||||
selector: '#parameter_groups',
|
||||
beforeAdd: function (createNew) {
|
||||
const newSubGroup = createNew();
|
||||
initSubGroupCollapse(newSubGroup);
|
||||
initSortable(newSubGroup);
|
||||
initForm({
|
||||
selector: newSubGroup,
|
||||
beforeDelete: deleteCallback
|
||||
});
|
||||
},
|
||||
beforeDelete: deleteCallback
|
||||
});
|
||||
|
||||
// init sorting of subgroups
|
||||
initSortable($('#parameter_groups'), '.group-drag-drop-mover');
|
||||
|
||||
// init collapse and params sorting for existing subgroups
|
||||
$('[data-form-id]').each(function(index, el) {
|
||||
initSubGroupCollapse(el);
|
||||
initSortable(el);
|
||||
initForm({
|
||||
selector: el,
|
||||
beforeDelete: deleteCallback
|
||||
});
|
||||
});
|
||||
|
||||
{/block}
|
||||
|
||||
|
||||
</script>
|
||||
Reference in New Issue
Block a user