<?php
global $_,$conf;
switch($_['action']){
	/** DASHBOARD **/
	//Récuperation d'une liste de dashboard
	case 'dashboard_dashboard_search':
	Action::write(function(&$response){
		global $myUser,$_;
		if(!$myUser->can('dashboard','read')) throw new Exception("Permissions insuffisantes",403);
		require_once(__DIR__.SLASH.'Dashboard.class.php');
		foreach(Dashboard::populate() as $dashboard){
			$response['rows'][] = $dashboard;
		}
	});
	break;
	
	//Ajout ou modification d'élément dashboard
	case 'dashboard_dashboard_save':
	Action::write(function(&$response){
		global $myUser,$_;
		if(!$myUser->can('dashboard','edit')) throw new Exception("Permissions insuffisantes",403);
		require_once(__DIR__.SLASH.'Dashboard.class.php');
		$item = Dashboard::provide();
		$item->user = $myUser->login;
		$item->label = $_['label'];
		$item->icon = $_['icon'];
		$item->default = $_['default'];
		$item->save();
	});
	break;
	
	//Récuperation ou edition d'élément dashboard
	case 'dashboard_dashboard_edit':
	Action::write(function(&$response){
		global $myUser,$_;
		if(!$myUser->can('dashboard','edit')) throw new Exception("Permissions insuffisantes",403);
		require_once(__DIR__.SLASH.'Dashboard.class.php');
		$response = Dashboard::getById($_['id']);
	});
	break;

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

	});
	break;

	
	//Sauvegarde des configurations de dashboard
	case 'dashboard_setting_save':
	Action::write(function(&$response){
		global $myUser,$_,$conf;
		if(!$myUser->can('dashboard','configure')) throw new Exception("Permissions insuffisantes",403);
		foreach(Configuration::setting('dashboard') 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;
	
	/** DASHBOARDWIDGET **/
	//Récuperation d'une liste de dashboardwidget
	case 'dashboard_dashboardwidget_search':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('dashboard','read')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'DashboardWidget.class.php');

			$models = array();
			Plugin::callHook('widget',array(&$models));
			foreach($models as $model):
				$models[$model->model] = $model;
			endforeach;

			$widgets = DashboardWidget::loadAll(array('dashboard'=>$_['dashboard']),array('position'));
			
			foreach($widgets as $widget):
				if(!isset($models[$widget->model])) continue;
				$model = clone $models[$widget->model];
				$model->id = $widget->id;
				$model->position = $widget->position;
				$model->minified = $widget->minified;
				$model->dashboard = $widget->dashboard;

				$response['rows'][] = $model;
			endforeach;
		});
	break;
	
	//Ajout ou modification d'élément dashboardwidget
	case 'dashboard_dashboardwidget_save':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('dashboard','edit')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'DashboardWidget.class.php');
			$item = DashboardWidget::provide();
			$item->model = $_['model'];
			$item->data = $_['data'];
			$item->position = $_['position'];
			$item->minified = $_['minified'];
			$item->dashboard = $_['dashboard'];
			$item->save();
		});
	break;
	
	//Récuperation ou edition d'élément dashboardwidget
	case 'dashboard_dashboardwidget_edit':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('dashboard','edit')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'DashboardWidget.class.php');
			$response = DashboardWidget::getById($_['id']);
		});
	break;

	//Suppression d'élement dashboardwidget
	case 'dashboard_dashboardwidget_delete':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('dashboard','delete')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'DashboardWidget.class.php');
			require_once(__DIR__.SLASH.'Dashboard.class.php');
			if(!$myUser->can('widget','delete')) throw new Exception("Permissions insuffisantes",403);
			$widget = DashboardWidget::getById($_['widget']);
			$dashboard = Dashboard::getById($_['dashboard']);
			if($widget->dashboard!=$dashboard->id || $dashboard->user!=$myUser->login) 
				throw new Exception("Vous ne pouvez supprimer que vos propres widgets");

			$widget->deleteById($widget->id);
			$response['message'] = 'Widget supprimé';
		});
	break;

	case 'dashboard_dashboardwidget_refresh':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('dashboard','read')) throw new Exception("Permissions insuffisantes",403);
			$widgets = array();
			Plugin::callHook('widget_refresh',array(&$widgets));
			$response['rows'] = $widgets;
		});
	break;

	
	//Sauvegarde des configurations de dashboard
	case 'dashboard_setting_save':
		Action::write(function(&$response){
			global $myUser,$_,$conf;
			if(!$myUser->can('dashboard','configure')) throw new Exception("Permissions insuffisantes",403);
			foreach(Configuration::setting('dashboard') 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;

	case 'dashboard_dashboardwidget_add':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('dashboard','edit')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'DashboardWidget.class.php');
			$widget = new DashboardWidget();
			$widget->model = $_['widget'];
			$widget->position = 666;
			$widget->minified = false;
			$widget->dashboard = $_['dashboard'];
			$widget->save();
			$response['message'] = 'Widget ajouté';
		});
	break;

	case 'dashboard_dashboardwidget_save_position':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('dashboard','update')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'Dashboard.class.php');
			require_once(__DIR__.SLASH.'DashboardWidget.class.php');
			$dashboard = Dashboard::getById($_['dashboard']);

			if( $dashboard->user!=$myUser->login) 
				throw new Exception("Vous ne pouvez modifier que vos propres widgets");

			$dashboard_widgets = DashboardWidget::loadAll( array('dashboard' => $dashboard->id ) );

			foreach($_['positions'] as $move){
				foreach($dashboard_widgets as $dashboard_widget){
					if($dashboard_widget->id!=$move['id']) continue;
					$dashboard_widget->position = $move['position'];
					$dashboard_widget->save();
				}
			}
		});
	break;


	case 'dashboard_widget_clock_load':
		require_once('DashboardWidget.class.php');
		$widget = DashboardWidget::current();
		$widget->title = 'Horloge';
		$widget->content = '<div class="clockContainer">
		<div class="clock" id="clock"></div>
		</div>';
		echo json_encode($widget);
	break;

	case 'dashboard_widget_log_load':
		global $myUser;
		require_once('DashboardWidget.class.php');
		if(!$myUser->can('log','read')) throw new Exception("Permissions insuffisantes",403);
		$widget = DashboardWidget::current();
		$logs = Log::loadAll(array(),array('created DESC'),array(30));

		$widget->title = '30 derniers logs';
		$widget->content = '<table class="table table-stripped table-hover">';

		$widget->content .= '<tr><th style="width:90px">Date</th><th>Libellé</th><th>Utilisateur</th></tr>';
		foreach($logs as $log){
			$widget->content .= '<tr><td><i class="fa fa-calendar-o"></i> '.date('d-m-y',$log->created).'<i class="fa fa-clock-o"></i> '.date('H:i:s',$log->created).'</td><td>'.$log->label().'</td><td><i class="fa fa-user"></i> '.$log->creator.'</td></tr>';
		}
		$widget->content .= '</ul>';

		echo json_encode($widget);
	break;

	case 'dashboard_widget_profile_load':
		global $myUser;
		require_once('DashboardWidget.class.php');
		$widget = DashboardWidget::current();
		$widget->title = 'Profile';
		$widget->content = '<div class="profileContainer">
		<div class="profileHeader"></div>
		<a href="account.php" class="profileImage"><img class="avatar-mini" src="'.$myUser->getAvatar().'"></a>
		<h3>'.$myUser->fullname().'</h3>
		<small>'.$myUser->function.'</small>
		<a href="account.php" class="btn btn-dark block ml-2  mr-2" ><i class="far fa-meh-blank"></i> Editer mon profil</a>
		</div>';
		echo json_encode($widget);
	break;


}
?>