<?php
global $_,$conf;
switch($_['action']){
	/** MENUITEM **/
	
	//Récuperation d'une liste de menuitem
	case 'navigation_menuitem_search':
		Action::write(function(&$response){
			global $myUser,$_;
			if (!$myUser->connected()) return;
			if(!$myUser->can('navigation','read')) return;
			require_once(__DIR__.SLASH.'MenuItem.class.php');

			$items = MenuItem::staticQuery('SELECT * FROM {{table}} WHERE user="" OR user=? ORDER BY sort',array($myUser->login),true);

			$mainMenu = array();
			Plugin::callHook("menu_main", array(&$mainMenu));
			foreach ($mainMenu as $item) {
				foreach ($items as $key=>$existingitem) {
					if(isset($item['parent'])){
						if(isset($existingitem->slug) && $item['parent'] == $existingitem->slug){
							if(!isset($existingitem->childs)) $existingitem->childs = array();
							$existingitem->childs[] = $item;
							$items[$key] = $existingitem;
						}
					}
				}
			}
			foreach($items as $menuitem){
				$response['rows'][] = $menuitem;
			}
		});
	break;
	
	case 'navigation_menuitem_move':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('navigation','edit')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'MenuItem.class.php');
			if(isset($_['sort']) && !empty($_['sort'])){
				foreach ($_['sort'] as $sort => $id) {
					$item = MenuItem::getById($id);
					$item->sort = $sort;
					$item->save();
				}
			}
		});
	break;

	//Ajout ou modification d'élément menuitem
	case 'navigation_menuitem_save':
		Action::write(function(&$response){
			global $myUser,$_,$conf;
			if(!$myUser->can('navigation','edit')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'MenuItem.class.php');
			if(!isset($_['url']) || empty($_['url'])) throw new Exception("Adresse web obligatoire");
			if(!$conf->get('navigation_allow_custom_menu') && !$myUser->superadmin)  throw new Exception("La configuration du plugin n'autorise pas l'ajout de menu custom");

			$item = MenuItem::provide();
			if(!$item->editable && !$myUser->superadmin) throw new Exception("Ce menu n'est pas éditable");

			$item->icon = $_['icon'];
			$item->label = $_['label'];
			$item->url = str_replace(ROOT_URL.'/','',$_['url']);
			$item->target = $_['target'];

			$item->user = $myUser->login;
			if(isset($_['public']) && $_['public']=='1' && $myUser->can('navigation','configure'))
				$item->user = '';

			if($item->id==0){
				$item->slug = slugify($item->label);
				if(MenuItem::rowCount(array('slug'=>$item->slug)) <0 )
					$item->slug .='_'.time();
			}
			//$item->parent = $_['parent'];
			$item->save();
		});
	break;
	
	//Récuperation ou edition d'élément menuitem
	case 'navigation_menuitem_edit':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('navigation','edit')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'MenuItem.class.php');
			$response = MenuItem::getById($_['id']);
		});
	break;

	//Suppression d'élement menuitem
	case 'navigation_menuitem_delete':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('navigation','delete')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'MenuItem.class.php');
			
			MenuItem::deleteById($_['id']);

			// Décommenter pour une suppression logique
			// $item = MenuItem::getById($_['id']);
			// $item->state = MenuItem::INACTIVE;
			// $item->save();
		});
	break;

	//Sauvegarde des configurations de navigation
	case 'navigation_setting_save':
		Action::write(function(&$response){
			global $myUser,$_,$conf;
			if(!$myUser->can('navigation','configure')) throw new Exception("Permissions insuffisantes",403);
			foreach(Configuration::setting('navigation') as $key=>$value){
				if(!is_array($value)) continue;
				$allowed[] = $key;
			}
			foreach ($_['fields'] as $key => $value)
				if(in_array($key, $allowed)) $conf->put($key,$value);
		});
	break;
}
?>