<?php
//Déclaration d'un item de menu dans le menu principal
function navigation_menu(){
	global $_,$myUser,$conf;
	// if(!$myUser->can('navigation','read')) return;
	$page = basename($_SERVER['REQUEST_URI']);
	$menuItems = array();

	$menuItems[] = array(
		'sort'=>299,
		'id'=>'{{id}}',
		'url'=>'{{url}}',
		'title'=>'{{title}}',
		'target'=>'{{target}}',
		'label'=>'{{{label}}}',
		'icon'=> '{{icon}}',
		'classes' =>'navigation-template-item hide',
		'html' =>'<span class="navigation-edit-item-button hide" data-id="{{id}}" onclick="navigation_menuitem_edit(this,event)"><i class="fas fa-pencil-alt"></i></span>'
	);

	$menuItems[] = array(
		'sort'=>400,
		'label'=>'Affaires',
		'slug'=>'case',
		'icon'=> 'fas fa-suitcase',
		'classes' =>'pointer'
	);
	$menuItems[] = array(
		'sort'=>401,
		'label'=>'Organisation',
		'slug'=>'organization',
		'icon'=> 'far fa-calendar-alt',
		'classes' =>'pointer'
	);
	$menuItems[] = array(
		'sort'=>402,
		'label'=>'Statistiques',
		'slug'=>'statistics',
		'icon'=> 'fas fa-chart-line',
		'classes' =>'pointer',
	);
	$menuItems[] = array(
		'sort'=>403,
		'label'=>'Exploitation',
		'slug'=>'exploitation',
		'icon'=> 'fas fa-cubes',
		'classes' =>'pointer'
	);
	uasort($menuItems , function($a,$b){return $a['sort']>$b['sort']?1:-1;});

	//Menus ajoutés par les plugins
	$mainMenu = array();
	Plugin::callHook("menu_main", array(&$mainMenu));
	uasort ($mainMenu , function($a,$b){return $a['sort']>$b['sort']?1:-1;});

	foreach ($mainMenu as $item) {
		foreach ($menuItems as $key=>$existingitem) {
			if(!isset($item['parent'])) continue;
			if(!isset($existingitem['slug']) || $item['parent'] != $existingitem['slug']) continue;
			if(!isset($existingitem['childs'])) 
				$existingitem['childs'] = array();
			$existingitem['childs'][] = $item;
			$menuItems[$key] = $existingitem;
		}
	}

	if($conf->get('navigation_show_uncategorized')){
		foreach ($mainMenu as $item)
			if(!isset($item['parent'])) $menuItems[] = $item;
	}

	if($myUser->can('navigation','edit')){
		$menuItems[] = array(
			'sort'=>300,
			'onclick'=>'navigation_navigation_edit()',
			'title'=>'Modifier le menu',
			'icon'=> 'fas fa-edit',
			'classes' =>'navigation-edit-button pointer' 
		);
		$menuItems[] = array(
			'sort'=>300,
			'onclick'=>'navigation_navigation_save()',
			'title'=>'Arrêter les modifications',
			'icon'=> 'fas fa-check',
			'classes' =>'navigation-validate-button pointer' 
		);
		$menuItems[] = array(
			'sort'=>300,
			'onclick'=>'navigation_menuitem_edit()',
			'title'=>'Ajouter un item',
			'icon'=> 'fas fa-plus',
			'classes' =>'navigation-plus-button pointer' 
		);
	}
 ?>
	<ul class="navbar-nav navigation-menu">
	<?php if ($myUser->connected()): ?>
		<?php foreach($menuItems as $item): ?>
			<?php if (isset($item['label']) && $item['label'] == 'Réglages') $page = basename($_SERVER['PHP_SELF']); 
			$classes = isset($item['classes'])? $item['classes']: '';
			if( isset($item['url']) && (
				(!empty($item['url']) 
				&& strpos($page, $item['url']) !== false 
				&& $item['url'] != 'index.php') 
				|| $page == $item['url'])
			){
				$classes .= ' active';
			} 

			if(!isset($item['childs']) && empty($item['url']) && empty($item['onclick'])) continue;
			?>
			
			<li data-id="<?php echo isset($item['id'])?$item['id']:''; ?>" class="nav-item <?php echo $classes; ?>  <?php echo isset($item['childs'])? 'dropdown':''; ?>">
				<a class="nav-link<?php echo isset($item['childs'])? ' dropdown-toggle':''?>"  
				<?php echo isset($item['childs'])? 'data-toggle="dropdown"':''; ?>
				title="<?php echo isset($item['title'])?$item['title']:''; ?>" 
				target="<?php echo isset($item['target'])?$item['target']:''; ?>" 
				onclick="<?php echo isset($item['onclick'])?$item['onclick']:''; ?>" 
				<?php echo isset($item['url'])? 'href="'.$item['url'].'"':''; ?>>
				<?php echo (isset($item['icon'])?'<i class="'.$item['icon'].'"></i> ':'').'<span>'.(isset($item['label'])?$item['label']:'').'</span>'; ?>
				<?php echo isset($item['html'])? $item['html']:''; ?>
				</a>
				<?php if(isset($item['childs'])): ?>
				 <div class="dropdown-menu" aria-labelledby="navbarDropdown">
				 	<?php foreach($item['childs'] as $child): ?>
			      		<a class="dropdown-item" href="<?php echo $child['url']; ?>">
							<?php echo (isset($child['icon'])?'<i class="'.$child['icon'].'"></i> ':'') ?>
			      			<?php echo $child['label']; ?></a>
			    	<?php endforeach; ?>
			    </div>
			    <?php endif; ?>
			</li>
		<?php endforeach; ?>
	<?php endif; ?>
	</ul>
	<?php
}

//Cette fonction va generer une page quand on clique sur navigation dans menu
function navigation_page(){
	global $_,$myUser;
	if(!isset($_['module']) || $_['module'] !='navigation') return;
	$page = !isset($_['page']) ? 'list' : $_['page'];
	$file = __DIR__.SLASH.'page.'.$page.'.php';
	if(!file_exists($file)) throw new Exception("Page ".$page." inexistante");
	
	require_once($file);
}

//Fonction executée lors de l'activation du plugin
function navigation_install($id){
	if($id != 'fr.idleman.navigation') return;
	Entity::install(__DIR__);

}

//Fonction executée lors de la désactivation du plugin
function navigation_uninstall($id){
	if($id != 'fr.idleman.navigation') return;
	Entity::uninstall(__DIR__);
}

//Déclaration des sections de droits du plugin
function navigation_section(&$sections){
	$sections['navigation'] = "Gestion des droits sur le plugin navigation";
}

//cette fonction comprends toutes les actions du plugin qui ne nécessitent pas de vue html
function navigation_action(){
	require_once(__DIR__.SLASH.'action.php');
}

//Déclaration du menu de réglages
function navigation_menu_setting(&$settingMenu){
	global $_, $myUser;
	
	if(!$myUser->can('navigation','configure')) return;
	$settingMenu[]= array(
		'sort' =>1,
		'url' => 'setting.php?section=global.navigation',
		'icon' => 'fas fa-angle-right',
		'label' => 'Menu & Navigation'
	);
}

//Déclaration des pages de réglages
function navigation_content_setting(){
	global $_;
	if(file_exists(__DIR__.SLASH.'setting.'.$_['section'].'.php'))
		require_once(__DIR__.SLASH.'setting.'.$_['section'].'.php');
}

//Déclaration des settings de base
//Types possibles : text,select ( + "values"=> array('1'=>'Val 1'),password,checkbox. Un simple string définit une catégorie.
Configuration::setting('navigation',array(
    "Général",
    'navigation_show_uncategorized' => array("label"=>"Afficher les menus natifs non catégorisés","legend"=>"Affiche les menus de plugins qui n'ont pas de catégorie parente","type"=>"checkbox"),
    'navigation_allow_custom_menu' => array("label"=>"Autoriser la création de menu personnalisés","legend"=>"Autorise aux utilisateurs la création de menu custom dans la navbar","type"=>"checkbox"),
));

//Déclation des assets
Plugin::addCss("/css/main.css?v=1"); 
Plugin::addJs("/js/main.js?v=1"); 

//Mapping hook / fonctions
Plugin::addHook("install", "navigation_install");
Plugin::addHook("uninstall", "navigation_uninstall"); 
Plugin::addHook("section", "navigation_section");
Plugin::addHook("header", "navigation_menu"); 
Plugin::addHook("page", "navigation_page");  
Plugin::addHook("action", "navigation_action");    
Plugin::addHook("menu_setting", "navigation_menu_setting");    
Plugin::addHook("content_setting", "navigation_content_setting");  

?>