Rework menus: Different categories can have different colors

This commit is contained in:
slawkens 2025-02-08 23:56:58 +01:00
parent ae1161d770
commit 73de93a561
5 changed files with 52 additions and 31 deletions

View File

@ -82,21 +82,25 @@ if (isset($_POST['template'])) {
return;
}
if (isset($_GET['reset_colors'])) {
if (isset($config['menu_default_color'])) {
Menu::where('template', $template)->update(['color' => str_replace('#', '', $config['menu_default_color'])]);
success('Colors has been reset.');
}
else {
warning('There is no default color defined, cannot reset colors.');
}
}
if (!isset($config['menu_categories'])) {
echo "No menu categories set in template config.php.<br/>This template doesn't support dynamic menus.";
return;
}
if (isset($_GET['reset_colors'])) {
foreach ($config['menu_categories'] as $id => $options) {
$color = $options['default_links_color'] ?? $config['menu_default_color'] ?? '#ffffff';
Menu::where('template', $template)->where('category', $id)->update(['color' => str_replace('#', '', $color)]);
}
$cache = Cache::getInstance();
if ($cache->enabled()) {
$cache->delete('template_menus');
}
success('Colors has been reset.');
}
$title = 'Menus - ' . $template;
?>
<div align="center" class="text-center">
@ -143,12 +147,13 @@ if (isset($_POST['template'])) {
if (isset($menus[$id])) {
$i = 0;
foreach ($menus[$id] as $menu):
$color = (empty($menu['color']) ? ($cat['default_links_color'] ?? ($config['menu_default_color'] ?? '#ffffff')) : '#' . $menu['color']);
?>
<li class="ui-state-default" id="list-<?php echo $id ?>-<?php echo $i ?>"><label>Name:</label> <input type="text" name="menu[<?php echo $id ?>][]" value="<?php echo escapeHtml($menu['name']); ?>"/>
<label>Link:</label> <input type="text" name="menu_link[<?php echo $id ?>][]" value="<?php echo $menu['link'] ?>"/>
<input type="hidden" name="menu_blank[<?php echo $id ?>][]" value="0"/>
<label><input class="blank-checkbox" type="checkbox" <?php echo($menu['blank'] == 1 ? 'checked' : '') ?>/><span title="Open in New Window">New Window</span></label>
<input class="color-picker" type="text" name="menu_color[<?php echo $id ?>][]" value="<?php echo (empty($menu['color']) ? ($config['menu_default_color'] ?? '#ffffff') : $menu['color']); ?>"/>
<input class="color-picker" type="text" name="menu_color[<?php echo $id ?>][]" value="<?php echo $color; ?>"/>
<a class="remove-button" id="remove-button-<?php echo $id ?>-<?php echo $i ?>"><i class="fas fa-trash"></a></i></li>
<?php $i++; $last_id[$id] = $i;
endforeach;
@ -172,7 +177,6 @@ if (isset($_POST['template'])) {
$twig->display('admin.menus.js.html.twig', array(
'menus' => $menus,
'last_id' => $last_id,
'menu_default_color' => $config['menu_default_color'] ?? '#ffffff'
));
?>
<?php

View File

@ -153,17 +153,34 @@ function get_template_menus(): array
return $result->toArray();
});
$menus = array();
$configMenuCategories = config('menu_categories');
$configMenuDefaultColor = config('menu_default_color');
$menus = [];
foreach($result as $menu) {
$link_full = strpos(trim($menu['link']), 'http') === 0 ? $menu['link'] : getLink($menu['link']);
$menus[$menu['category']][] = array('name' => $menu['name'], 'link' => $menu['link'], 'link_full' => $link_full, 'blank' => $menu['blank'] == 1, 'target_blank' => ($menu['blank'] == 1 ? ' target="blank"' : ''), 'color' => $menu['color']);
if (empty($menu['link'])) {
$menu['link'] = 'news';
}
$new_menus = array();
$link_full = (str_starts_with(trim($menu['link']), 'http') ? $menu['link'] : getLink($menu['link']));
$target_blank = ($menu['blank'] == 1 ? ' target="blank"' : '');
$color = (empty($menu['color']) ? ($configMenuCategories[$menu['category']]['default_links_color'] ?? ($configMenuDefaultColor ?? '')) : $menu['color']);
$color = str_replace('#', '', $color);
$style_color = (empty($color) ? '' : 'style="color: #' . $color . '"');
$menus[$menu['category']][] = [
'name' => $menu['name'],
'link' => $menu['link'], 'link_full' => $link_full,
'blank' => $menu['blank'] == 1, 'target_blank' => $target_blank,
'color' => $color, 'style_color' => $style_color,
];
}
$new_menus = [];
/**
* @var array $configMenuCategories
*/
$configMenuCategories = config('menu_categories');
if($configMenuCategories === null) {
return [];
}

View File

@ -1,9 +1,16 @@
<link type="text/css" rel="stylesheet" href="{{ constant('BASE_URL') }}tools/ext/jquery-ui/themes/base/jquery-ui.min.css">
<script>
var last_id = [];
let last_id = [];
let colors = [];
{% for cat, menu in menus %}
last_id[{{ cat }}] = {{ last_id[cat] }};
{% endfor %}
{% for cat, options in config('menu_categories') %}
colors[{{ cat }}] = '{{ options['default_links_color'] ?? config('menu_default_color') }}';
{% endfor %}
$(function () {
$(".sortable").sortable();
$(".sortable").disableSelection();
@ -17,7 +24,8 @@
var cat = $(this).attr("id").replace('add-button-', '');
var id = last_id[cat];
last_id[cat]++;
$('#sortable-' + cat).append('<li class="ui-state-default" id="list-' + cat + '-' + id + '"><label>Name:</label> <input type="text" name="menu[' + cat + '][]" value=""/> <label>Link:</label> <input type="text" name="menu_link[' + cat + '][]" value=""/><input type="hidden" name="menu_blank[' + cat + '][]" value="0" /> <label><input class="blank-checkbox" type="checkbox"/><span title="Open in New Window">New Window</span></label> <input class="color-picker" type="text" name="menu_color[' + cat + '][]" value="{{ menu_default_color }}" /><a class="remove-button" id="remove-button-' + cat + '-' + id + '"><i class="fas fa-trash"></i></a></li>'); //add input bo
const color = colors[cat];
$('#sortable-' + cat).append('<li class="ui-state-default" id="list-' + cat + '-' + id + '"><label>Name:</label> <input type="text" name="menu[' + cat + '][]" value=""/> <label>Link:</label> <input type="text" name="menu_link[' + cat + '][]" value=""/><input type="hidden" name="menu_blank[' + cat + '][]" value="0" /> <label><input class="blank-checkbox" type="checkbox"/><span title="Open in New Window">New Window</span></label> <input class="color-picker" type="text" name="menu_color[' + cat + '][]" value="#' + color + '" /><a class="remove-button" id="remove-button-' + cat + '-' + id + '"><i class="fas fa-trash"></i></a></li>'); //add input bo
$('#remove-button-' + cat + '-' + id).click(function () {
$('#list-' + $(this).attr("id").replace('remove-button-', '')).remove();
});

View File

@ -42,8 +42,6 @@ defined('MYAAC') or die('Direct access not allowed!');
<div id="mainsubmenu">
<?php
$default_menu_color = "ffffff";
foreach($menus as $category => $menu) {
if(!isset($menus[$category])) {
continue;
@ -54,8 +52,8 @@ defined('MYAAC') or die('Direct access not allowed!');
$size = count($menus[$category]);
$i = 0;
foreach($menus[$category] as $menu) {
echo '<a href="' . $menu['link_full'] . '"' . ($menu['blank'] ? ' target="_blank"' : '') . ' style="color: #' . (strlen($menu['color']) == 0 ? $default_menu_color : $menu['color']) . ';">' . $menu['name'] . '</a>';
foreach($menus[$category] as $link) {
echo '<a href="' . $link['link_full'] . '" ' . $link['target_blank'] . ' ' . $link['style_color'] . '>' . $link['name'] . '</a>';
if(++$i != $size) {
echo '<span class="separator"></span>';

View File

@ -372,19 +372,13 @@ foreach($config['menu_categories'] as $id => $cat) {
</span>
<div id='<?php echo $cat['id']; ?>_Submenu' class='Submenu'>
<?php
$default_menu_color = "ffffff";
foreach($menus[$id] as $category => $menu) {
if (empty($menu['link'])) {
$menu['link'] = 'news';
}
$link_color = '#' . (strlen($menu['color']) == 0 ? $default_menu_color : $menu['color']);
?>
<a href='<?php echo $menu['link_full']; ?>'<?= $menu['target_blank']?>>
<div id='submenu_<?php echo str_replace('/', '_', $menu['link']); ?>' class='Submenuitem' onMouseOver='MouseOverSubmenuItem(this)' onMouseOut='MouseOutSubmenuItem(this)' style="color: <?php echo $link_color; ?>;">
<div id='submenu_<?php echo str_replace('/', '_', $menu['link']); ?>' class='Submenuitem' onMouseOver='MouseOverSubmenuItem(this)' onMouseOut='MouseOutSubmenuItem(this)' >
<div class='LeftChain' style='background-image:url(<?php echo $template_path; ?>/images/general/chain.gif);'></div>
<div id='ActiveSubmenuItemIcon_<?php echo str_replace('/', '_', $menu['link']); ?>' class='ActiveSubmenuItemIcon' style='background-image:url(<?php echo $template_path; ?>/images/menu/icon-activesubmenu.gif);'></div>
<div class='SubmenuitemLabel' style="color: <?php echo $link_color; ?>;"><?php echo $menu['name']; ?></div>
<div class='SubmenuitemLabel' <?php echo $menu['style_color']; ?>><?php echo $menu['name']; ?></div>
<div class='RightChain' style='background-image:url(<?php echo $template_path; ?>/images/general/chain.gif);'></div>
</div>
</a>