From 2458393d226cd4f6981af5dfdfae097bb383e50b Mon Sep 17 00:00:00 2001 From: slawkens Date: Sat, 6 Jun 2020 17:33:34 +0200 Subject: [PATCH] Add Plugins::installMenus function --- system/libs/plugins.php | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/system/libs/plugins.php b/system/libs/plugins.php index 04dfaf52..f77bfe0f 100644 --- a/system/libs/plugins.php +++ b/system/libs/plugins.php @@ -401,4 +401,62 @@ class Plugins { return $string; } + + /** + * Install menus + * Helper function for plugins + * + * @param string $templateName + * @param array $categories + */ + public static function installMenus($templateName, $categories) + { + global $db; + + // check if menu table exist (it does not on MyAAC 0.7.0 and lower) + if (!$db->hasTable(TABLE_PREFIX . 'menu')) { + return; + } + + // 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['color'])) { + $color = $link['color']; + } + if (isset($link['blank'])) { + $blank = $link['blank'] === true ? 1 : 0; + } + + $link = $link['link']; + } + + $insert_array = [ + 'template' => $templateName, + 'name' => $name, + 'link' => $link, + 'category' => $category, + 'ordering' => $i++, + ]; + + // support for color and blank attributes since 0.8.0 + if(version_compare(MYAAC_VERSION, '0.8.0', '>=')) { + $insert_array['blank'] = $blank; + $insert_array['color'] = $color; + } + + $db->insert(TABLE_PREFIX . 'menu', $insert_array); + } + } + } }