Files
kupshop/admin/lists/MenuList.php
2025-08-02 16:30:27 +02:00

232 lines
8.1 KiB
PHP

<?php
use KupShop\AdminBundle\AdminList\BaseList;
use KupShop\ContentBundle\Util\MenuUtil;
use KupShop\KupShopBundle\Util\HtmlBuilder\HTML;
class MenuList extends BaseList
{
protected $template = 'list/menu.tpl';
protected $pageDivide = 0;
protected $tableDef = [
'id' => 'id',
'fields' => [
'Název' => ['field' => 'name', 'render' => 'renderTitle', 'class' => 'nameContainer', 'size' => 3],
'Kód' => ['field' => 'code', 'render' => 'renderCode', 'size' => 2],
'Adresa odkazu' => ['field' => 'link', 'render' => 'renderUrl', 'class' => 'typeAndLinkContainer', 'size' => 3],
'Příznak' => ['field' => 'flags', 'render' => 'renderFlags', 'class' => 'columnCampaigns', 'size' => 1],
'Tlačítka' => ['field' => '', 'render' => 'renderButtons', 'class' => 'alignRight hidden-label overflow-visible hiddenTooltip', 'size' => 1.3],
],
];
public function renderUrl($values)
{
$result = HTML::create('span')->class('d-flex align-center');
$result->tag('span')->class(MenuUtil::$TYPES_CLASSES[$values['type']].' m-r-1')
->attr('title', MenuUtil::$TYPES_TITLES[$values['type']]);
$result->text(ltrim($values['url'] ?: $values['link'] ?: '', '/'));
return $result;
}
public function renderTitle($values, $column)
{
return [
HTML::create('span')
->class('drag-drop-mover')
->tag('i')
->class('bi bi-arrows-move handle')
->end(),
HTML::create('span')
->class('bi bi-dash-circle opener '
.(count($values['submenu']) > 0 ? '' : 'disabled')),
HTML::create('strong')
->attr('class', 'text-dark')
->text($this->getListRowValue($values, $column['field'])),
];
}
public function renderCode($values, $column)
{
$text = HTML::create('span');
if (isSuperuser() && $values['code'] ?? false) {
$label = ' ['.$values['code'].']';
$text->text($label)->end();
}
return $text->end();
}
public function renderFlags($values, $column)
{
$result = [];
if ($values['show_in_search'] === 'N') {
array_push($result, $this->renderBadge(translate('showInSearchN', 'menulinks'), 'badge-pastel-default', 'incognito'));
}
if ($values['figure'] === 'N') {
array_push($result, $this->renderBadge(translate('figureN', 'menulinks'), 'badge-pastel-default', 'eye-slash-fill'));
}
return $result;
}
public function renderButtons($values, $column)
{
$editBtn = HTML::create('a')->class('btn btn-primary btn-sm')
->attr('style', 'visibility:hidden')->attr('href', '#')
->tag('span')->class('bi bi-pencil-square')->end();
if ($values['type'] == MenuUtil::TYPE_PAGE) {
$editBtn = HTML::create('a')->class('btn btn-primary btn-sm')
->attr('href', '/'.ltrim($values['url'] ?? '', '/').'?inlineEditable=1')
->attr('target', '_blank')
->tag('span')->class('bi bi-pencil-square')->end();
}
$newItemBtn = HTML::create('div')->class('dropdown');
$ul = $newItemBtn->tag('a')->class('dropdown-toggle btn btn-sm btn-success')
->id('newItemDropdown_'.$values['id'])
->attr('title', translate('nestNewItem', 'menulinks'))
->attr('data-toggle', 'dropdown')
->tag('span')->class('bi bi-plus-lg')->end()
->end()
->tag('ul')->class('dropdown-menu dropdown-menu-right');
foreach (MenuUtil::$TYPES as $typeID => $name) {
$ul->tag('li')->tag('a')
->attr('href', "javascript:nw('menu', '0', '{$values['id_menu']}&data[type]={$typeID}&data[parent]={$values['id']}');")
->tag('span')->class(MenuUtil::$TYPES_CLASSES[$typeID])->end()
->text(' '.translate('add'.$name, 'menulinks'))
->end()->end(); // end a, end li
}
return [
$editBtn,
$newItemBtn,
];
}
public function createTreeMenuLinks($parentID, $level = 0)
{
if ($level > 20) {
exit('Rekurze');
}
$ret = [];
$qb = sqlQueryBuilder()->select('*')->from('menu_links', 'ml');
if (is_null($parentID)) {
$qb->where('ml.parent IS NULL AND ml.id_menu IS NULL');
} else {
$qb->where('ml.parent = :parent')->setParameter('parent', $parentID);
}
$SQL = $qb->orderBy('ml.list_order', 'ASC')->execute();
foreach ($SQL as $row) {
$row['level'] = $level;
// vytvoreni podsekci
$row['submenu'] = $this->createTreeMenuLinks($row['id'], $level + 1);
$ret[] = $row;
}
sqlFreeResult($SQL);
return $ret;
}
public function handleDrag()
{
$tree = getVal('tree');
$menuID = getVal('menuID');
if ($tree) {
$tree['position'] = (int) getVal('position', $tree);
$tree['id'] = (int) getVal('id', $tree);
if (!empty($tree['target'])) {
$target = (int) getVal('target', $tree);
$where = "parent = {$target}";
$where_insert = "parent = {$target}";
} else {
$target = null;
$where = 'parent IS NULL';
$where_insert = 'parent = NULL';
}
if ($menuID === 'null') {
$menuID = null;
$menuToSave = 'null';
$whereMenuID = 'id_menu IS NULL';
} else {
$menuToSave = $menuID = (int) $menuID;
$whereMenuID = "id_menu={$menuID}";
}
$old_parent = returnSQLResult('SELECT parent FROM '.getTableName('menu_links')." WHERE id={$tree['id']}");
sqlQuery("UPDATE menu_links SET list_order=list_order+1 WHERE {$where} AND {$whereMenuID} AND list_order >= {$tree['position']}");
sqlQuery("UPDATE menu_links SET {$where_insert}, id_menu={$menuToSave}, list_order={$tree['position']} WHERE id={$tree['id']}");
// fix id_menu for nested items in the dragged item
$parentIDsBuffer = [$tree['id']];
while ($tmpParentID = array_pop($parentIDsBuffer)) {
$result = sqlQueryBuilder()->select('id')->from('menu_links')
->where('parent=:parent')->setParameter('parent', $tmpParentID)->execute();
foreach ($result as $row) {
array_push($parentIDsBuffer, $row['id']);
}
$this->updateSQL('menu_links', ['id_menu' => $menuID], ['parent' => $tmpParentID]);
}
MenuUtil::fixOrderTreeLevelPositions($target, $menuID);
if ($old_parent != $tree['target']) {
MenuUtil::fixOrderTreeLevelPositions($old_parent, is_null($old_parent) ? null : $menuID);
}
clearCache('menu', true);
exit('OK');
}
exit('Err');
}
public function getSQL(Query\QueryBuilder $qb)
{
$data = parent::getSQL($qb);
$menu = [];
foreach (MenuUtil::getMenuRoots() as $row) {
$row['level'] = 0;
$row['submenu'] = $this->createTreeMenuLinks($row['id']);
$menu[$row['id']] = $row;
}
$menu[null] = [
'name' => translate('unclassifiedItems', 'menulinks'),
'level' => 0,
'submenu' => $this->createTreeMenuLinks(null),
];
$data['SQL'] = [
'menu' => $menu,
'menuItemTypes' => MenuUtil::$TYPES,
'menuItemTypesClasses' => MenuUtil::$TYPES_CLASSES,
];
return $data;
}
public function getQuery()
{
$qb = sqlQueryBuilder()
->select('id', 'name, link', 'target', 'list_order', 'id_menu')
->from('menu_links')
->orderBy('id_menu ASC, list_order ASC');
return $qb;
}
}