From 4d7589a934682f83f079c1d13cb1ee6b53aeb55e Mon Sep 17 00:00:00 2001 From: slawkens Date: Wed, 3 Dec 2025 23:53:00 +0100 Subject: [PATCH] [WIP] Add access option to Menus Thanks @joelslamospersson for idea --- admin/pages/menus.php | 39 ++++++++++-- common.php | 2 +- system/migrations/48.php | 16 +++++ system/src/Models/Menu.php | 2 +- system/template.php | 8 ++- system/templates/admin.menus.js.html.twig | 74 +++++++++++++---------- 6 files changed, 100 insertions(+), 41 deletions(-) create mode 100644 system/migrations/48.php diff --git a/admin/pages/menus.php b/admin/pages/menus.php index 19d7b486..e85f237a 100644 --- a/admin/pages/menus.php +++ b/admin/pages/menus.php @@ -23,6 +23,7 @@ if (!hasFlag(FLAG_CONTENT_MENUS) && !superAdmin()) { } $pluginThemes = Plugins::getThemes(); +$groups = new OTS_Groups_List(); if (isset($_POST['template'])) { $template = $_POST['template']; @@ -32,6 +33,8 @@ if (isset($_POST['template'])) { $post_menu_link = $_POST['menu_link'] ?? []; $post_menu_blank = $_POST['menu_blank'] ?? []; $post_menu_color = $_POST['menu_color'] ?? []; + $post_menu_access = $_POST['menu_access'] ?? []; + if (count($post_menu) != count($post_menu_link)) { echo 'Menu count is not equal menu links. Something went wrong when sending form.'; return; @@ -50,6 +53,7 @@ if (isset($_POST['template'])) { 'link' => $post_menu_link[$category][$i], 'blank' => $post_menu_blank[$category][$i] == 'on' ? 1 : 0, 'color' => str_replace('#', '', $post_menu_color[$category][$i]), + 'access' => $post_menu_access[$category][$i], 'category' => $category, 'ordering' => $i ]); @@ -122,7 +126,7 @@ if (isset($_POST['template'])) { ?> select('name', 'link', 'blank', 'color', 'category', 'ordering') + ->select('name', 'link', 'access', 'blank', 'color', 'category', 'ordering') ->where('enabled', 1) ->where('template', $template) ->orderBy('ordering') @@ -151,11 +155,34 @@ if (isset($_POST['template'])) { foreach ($menus[$id] as $menu): $color = (empty($menu['color']) ? ($cat['default_links_color'] ?? ($config['menu_default_links_color'] ?? ($config['menu_default_color'] ?? '#ffffff'))) : '#' . $menu['color']); ?> -
  • - - - - +
  • + + + + +
    + + +
  • hasColumn(TABLE_PREFIX . 'menu', 'access')) { + $db->addColumn(TABLE_PREFIX . 'menu', 'access', 'TINYINT NOT NULL DEFAULT 0 AFTER `link`'); + } +}; + +$down = function () use ($db) { + if ($db->hasColumn(TABLE_PREFIX . 'menu', 'access')) { + $db->dropColumn(TABLE_PREFIX . 'menu', 'access'); + } +}; diff --git a/system/src/Models/Menu.php b/system/src/Models/Menu.php index 6e3b6d03..670cccc3 100644 --- a/system/src/Models/Menu.php +++ b/system/src/Models/Menu.php @@ -9,6 +9,6 @@ class Menu extends Model { public $timestamps = false; - protected $fillable = ['template', 'name', 'link', 'blank', 'color', 'category', 'ordering', 'enabled']; + protected $fillable = ['template', 'name', 'link', 'access', 'blank', 'color', 'category', 'ordering', 'enabled']; } diff --git a/system/template.php b/system/template.php index 30cb5535..dc89ef04 100644 --- a/system/template.php +++ b/system/template.php @@ -146,10 +146,10 @@ if($twig_loader) { function get_template_menus(): array { - global $template_name; + global $template_name, $logged_access; $result = Cache::remember('template_menus_' . $template_name, 10 * 60, function () use ($template_name) { - $result = Menu::select(['name', 'link', 'blank', 'color', 'category']) + $result = Menu::select(['name', 'link', 'access', 'blank', 'color', 'category']) ->where('template', $template_name) ->orderBy('category') ->orderBy('ordering') @@ -163,6 +163,10 @@ function get_template_menus(): array $menus = []; foreach($result as $menu) { + if ($menu['access'] > $logged_access) { + continue; + } + if (empty($menu['link'])) { $menu['link'] = 'news'; } diff --git a/system/templates/admin.menus.js.html.twig b/system/templates/admin.menus.js.html.twig index 9f7ea45e..48035539 100644 --- a/system/templates/admin.menus.js.html.twig +++ b/system/templates/admin.menus.js.html.twig @@ -14,42 +14,62 @@ colors[{{ cat }}] = '{{ options['default_links_color'] ?? (menuDefaultLinksColor ?? config('menu_default_color')) }}'; {% endfor %} + function confirmRemoveMenuItem(that) + { + let id = $(that).attr("id"); + if (confirm('Are you sure, that you want to remove this element?')) { + $('#list-' + id.replace('remove-button-', '')).remove(); + } + } + $(function () { const $sortable = $(".sortable"); $sortable.sortable(); $sortable.disableSelection(); $(".remove-button").on('click', function () { - var id = $(this).attr("id"); - $('#list-' + id.replace('remove-button-', '')).remove(); + confirmRemoveMenuItem(this); }); $(".add-button").on('click', function () { - var cat = $(this).attr("id").replace('add-button-', ''); - var id = last_id[cat]; + let cat = $(this).attr("id").replace('add-button-', ''); + let id = last_id[cat]; last_id[cat]++; const color = colors[cat]; - $('#sortable-' + cat).append('
  • '); //add input bo - $('#remove-button-' + cat + '-' + id).on('click', function () { - $('#list-' + $(this).attr("id").replace('remove-button-', '')).remove(); - }); - initializeSpectrum(); + let copy = $('.ui-state-default:first').clone(); + + copy.attr('id', 'list-' + cat + '-' + id); + + copy.find('.menu-name').val('').attr('name', 'menu[' + cat + '][]'); + copy.find('.menu-link').val('').attr('name', 'menu_link[' + cat + '][]'); + copy.find('.menu-access').val('0').attr('name', 'menu_access[' + cat + '][]'); + copy.find('.menu-color').val(color).attr('name', 'menu_color[' + cat + '][]'); + copy.find('.menu-blank').attr('name', 'menu_blank[' + cat + '][]'); + copy.find('.menu-blank-checkbox').prop('checked', false); + + copy.find('.remove-button').attr('id', 'remove-button-' + cat + '-' + id); + + $('#sortable-' + cat).append(copy); + + $('#remove-button-' + cat + '-' + id).on('click', function () { + confirmRemoveMenuItem(this); + }); }); $("#menus-form").on('submit', function (e) { - $('.blank-checkbox:not(:checked)').each(function (i, obj) { + $('.menu-blank-checkbox:not(:checked)').each(function (i, obj) { $(obj).parent().prev().val("off"); }); - $('.blank-checkbox:checked').each(function (i, obj) { + $('.menu-blank-checkbox:checked').each(function (i, obj) { $(obj).parent().prev().val("on"); }); }); }); - - - - + + .label_menu_name, .label_menu_link { + width: 45%; + } + + .menu-name, .menu-link { + width: 100%; + } +