<?php

/**
 * Snippet to render the manager main menu.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS\snippet\managerMainMenu
 * @copyright Copyright (c) 2015 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

// Query for notices
$evt = new cfNoticesEvent();
clearFusionCMS::getEventManager()->notify($evt);

$db = clearFusionCMS::getDb();

/* @var $tplEngine cfTemplateEngine */
/* @var $controller cfBackendController */
$controller = clearFusionCMS::getController();
$user = clearFusionCMS::getUser();

$dashMgr = new cfDashboardMgr($db);
$grps = $dashMgr->getGroups();
$grpIcons = $dashMgr->getGroupFAIcons();
$menu = '<ul role="menu">';
$top = '<ul role="menu">';
$top .= '<li data-mainmenu="cfNotifications" title="' . __('Notifications', 'cfBackend') . '" id="notifyIcon"' . ($evt->hasMessages() ? '' : ' style="display: none;"') . '><i class="fa fa-exclamation-circle"></i></li>';
$top .= '<li title="' . __('Dashboard', 'cfBackend') . '"><a href="' . $controller->assemble('', 'dashboard') . '"><i class="fa fa-home"></i></a></li>';
$top .= '<li title="' . sprintf(__('My Profile: %s', 'cfBackend'), $user->getDisplayName()) . '"><a href="' . $controller->assemble('', 'myprofile') . '"><i class="fa fa-user"></i></a></li>';

$main = '';
foreach($grps as $id => $name) {
	$icons = $dashMgr->getIcons($id, $user);
	if(count($icons)) {
		$menu .=  "<li id=\"$id\"><strong>$name</strong><ul>";
		$top .= '<li data-mainmenu="' . $id . '" title="' . $name . '"><i class="fa ' . $grpIcons[$id] . '"></i></li>';

		$main .= '<ul id="' . $id . '" role="menu">';
		$main .= '<li><strong><i class="fa ' . $grpIcons[$id] . '"></i> ' . $name . '</strong>';
		foreach($icons as $icon) {
			$title = fusionLib::htmlChars($icon['displayname']);
			$t = '<li><a href="' . $controller->assemble($icon['module'], $icon['action']) . '"><img src="' . $icon['iconfile'] . "\" alt=\"$title\" /> <span>$title</span></a></li>\n";
			$main .= $t;
			$menu .= $t;
		}
		$main .= '</ul>';
		$menu .= '</ul></li>';
	}
}

$top .= '<li title="' . __('Logout', 'cfBackend') . '"><a href="' . $controller->assemble('', 'logout') . '"><i class="fa fa-power-off"></i></a></li>';
$top .= '</ul>';
$menu .= '</ul>';

echo $tplEngine->parseChunk('managerMenu', array(
	'mainMenuLevel1' => $top,
	'mainMenuLevel2' => $main . '<ul id="cfNotifications" role="menu"></ul>',
	'mainMenu' => $menu
));
