Add Plugins::installMenus function

This commit is contained in:
slawkens 2020-06-06 17:33:34 +02:00
parent 787416e552
commit 2458393d22

View File

@ -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);
}
}
}
}