95 lines
2.9 KiB
PHP
95 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace KupShop\ContentBundle\Util;
|
|
|
|
use KupShop\I18nBundle\Translations\MenuLinksTranslation;
|
|
use Query\Operator;
|
|
use Query\QueryBuilder;
|
|
use Query\Translation;
|
|
|
|
class MenuUtil
|
|
{
|
|
/** @var array menu item types */
|
|
public static $TYPES = [
|
|
self::TYPE_PAGE => 'Page',
|
|
self::TYPE_LINK => 'Link',
|
|
self::TYPE_GROUP => 'Group',
|
|
];
|
|
|
|
public const TYPE_PAGE = 1;
|
|
public const TYPE_LINK = 2;
|
|
public const TYPE_GROUP = 3;
|
|
|
|
/** @var array menu item type classes */
|
|
public static $TYPES_CLASSES = [
|
|
self::TYPE_PAGE => 'bi bi-pencil-square',
|
|
self::TYPE_LINK => 'bi bi-link-45deg',
|
|
self::TYPE_GROUP => 'bi bi-folder',
|
|
];
|
|
|
|
/** @var array menu item type titles */
|
|
public static $TYPES_TITLES = [
|
|
self::TYPE_PAGE => 'Obsahová stránka',
|
|
self::TYPE_LINK => 'Odkaz',
|
|
self::TYPE_GROUP => 'Skupina',
|
|
];
|
|
|
|
/**
|
|
* @return array [menuID => menuRow]
|
|
*/
|
|
public static function getMenuRoots(): array
|
|
{
|
|
$result = sqlQueryBuilder()->select('*')->from('menu_links')
|
|
->where('parent IS NULL AND id=id_menu')
|
|
->orderBy('list_order')->execute();
|
|
$roots = [];
|
|
foreach ($result as $row) {
|
|
$roots[$row['id']] = $row;
|
|
}
|
|
|
|
return $roots;
|
|
}
|
|
|
|
public static function fixOrderTreeLevelPositions($parentID, $menuID, bool $sortMenus = false)
|
|
{
|
|
if ($sortMenus) {
|
|
$data = ['parent' => $parentID];
|
|
$menuID = null;
|
|
} else {
|
|
$data = ['parent' => $parentID, 'id_menu' => $menuID];
|
|
}
|
|
|
|
if (is_null($parentID)) {
|
|
if (is_null($menuID)) {
|
|
// ignore unclassified items when sortMenus enabled (root menu items has id_menu value)
|
|
// ignore root menu items when sortMenu disabled -> only sort unclassified root items
|
|
$where = $sortMenus ? ' id_menu IS NOT NULL' : ' id_menu IS NULL';
|
|
} else {
|
|
$where = ' id_menu = :id_menu';
|
|
}
|
|
$where .= ' AND parent IS NULL';
|
|
} else {
|
|
$where = ' parent = :parent';
|
|
}
|
|
|
|
sqlQuery("SELECT @i := -1; UPDATE menu_links SET list_order = (select @i := @i + 1) WHERE {$where} ORDER BY list_order, id", $data);
|
|
}
|
|
|
|
public function resolve($uri)
|
|
{
|
|
return sqlQueryBuilder()->select('ml.id')
|
|
->from('menu_links', 'ml')
|
|
->andWhere(
|
|
Translation::joinTranslatedFields(
|
|
MenuLinksTranslation::class,
|
|
function (QueryBuilder $qb, $columnName, $translatedField, $langID) use ($uri) {
|
|
$qb->andWhere(Operator::equals([Operator::coalesce($translatedField, 'ml.url') => $uri]));
|
|
|
|
return false;
|
|
},
|
|
['url']
|
|
))
|
|
->execute()->fetchColumn();
|
|
}
|
|
}
|