From d1c5a189c3b182a36933ed507c6ae36b61fe1d45 Mon Sep 17 00:00:00 2001 From: slawkens Date: Mon, 18 Nov 2024 23:46:37 +0100 Subject: [PATCH] Add Plugins::installMenus function --- system/libs/plugins.php | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/system/libs/plugins.php b/system/libs/plugins.php index 595b2d44..2272c630 100644 --- a/system/libs/plugins.php +++ b/system/libs/plugins.php @@ -405,4 +405,60 @@ class Plugins { return $string; } + + /** + * Install menus + * Helper function for plugins + * + * @param string $templateName + * @param array $categories + */ + public static function installMenus($templateName, $categories, $clearOld = true) + { + global $db; + + if ($clearOld) { + $db->query('DELETE FROM `' . TABLE_PREFIX . 'menu` WHERE `template` = ' . $db->quote($templateName)); + } + + // check if menus already exist + $query = $db->query('SELECT `id` FROM `' . TABLE_PREFIX . 'menu` WHERE `template` = ' . $db->quote($templateName) . ' LIMIT 1;'); + if ($query->rowCount() > 0) { + return; + } + + foreach ($categories as $category => $menus) { + $i = 0; + foreach ($menus as $name => $link) { + $color = ''; + $blank = 0; + + if (is_array($link)) { + if (isset($link['name'])) { + $name = $link['name']; + } + if (isset($link['color'])) { + $color = $link['color']; + } + if (isset($link['blank'])) { + $blank = $link['blank'] ? 1 : 0; + } + + $link = $link['link']; + } + + $insert_array = [ + 'template' => $templateName, + 'name' => $name, + 'link' => $link, + 'category' => $category, + 'ordering' => $i++, + 'blank' => $blank, + 'color' => $color, + ]; + + $db->insert(TABLE_PREFIX . 'menu', $insert_array); + } + } + } }