<?php


    /** DASHBOARDWIDGET / BLOC DE TABLEAU DE BORD **/

	Action::register('dashboard_widget_search',function(&$response){
		global $_;
		User::check_access('dashboard','read');
		require_once(__DIR__.SLASH.'DashboardWidget.class.php');
		require_once(__DIR__.SLASH.'Dashboard.class.php');
		
		$filters = array();

		if(empty($_['uid']))  throw new Exception('Scope non spécifié');

		$filters['scope'] = $_['scope'];
		if(empty($_['uid'])) $filters['uid'] = $_['uid'];

		$dashboard = Dashboard::load($filters);

		$widgets = DashboardWidget::loadAll(array('dashboard'=>$dashboard->id)); 
		$response['widgets'] = array();

		foreach($widgets as $widget){
			$row = $widget->toArray();
			$model = DashboardWidget::model($widget->model);
			$row = array_merge($model,$row);

			$response['widgets'][] = $row ;
		}
		
	});

	Action::register('dashboard_widget_add',function(&$response){
		global $_,$myUser;
		User::check_access('dashboard','edit');
		require_once(__DIR__.SLASH.'DashboardWidget.class.php');
		require_once(__DIR__.SLASH.'Dashboard.class.php');
		
		if(empty($_['scope'])) throw new Exception('Scope non sépcifié');
		$filters = array('scope'=>$_['scope']);

		if(!empty($_['uid'])) $filters['uid'] = $_['uid'];


		// $dashboard = new Dashboard();
		// $dashboard->uid = $myUser->login;
		// $dashboard->scope = "home";
		// $dashboard->save();

		$dashboard = Dashboard::load($filters);
		if(!$dashboard) throw new Exception("Dashboard introuvable");

		$widget = new DashboardWidget();
		$widget->dashboard = $dashboard->id;
		$widget->save();

		$row = $widget->toArray();
		$model = DashboardWidget::model($widget->model);
		$row = array_merge($model,$row);

		$response = $row;
	});

	Action::register('dashboard_widget_move',function(&$response){
		global $_;
		User::check_access('dashboard','edit');
		require_once(__DIR__.SLASH.'DashboardWidget.class.php');
		require_once(__DIR__.SLASH.'Dashboard.class.php');
		
		if(empty($_['widget']['id'])) throw new Exception('Id non spécifié');
		if(!isset($_['widget']['column'])) throw new Exception('Colonne non spécifié');
		if(!isset($_['widget']['row'])) throw new Exception('Ligne non spécifié');

		$widget = DashboardWidget::getById($_['widget']['id'],1);
		if(!$widget) throw new Exception('Widget introuvable');
		
		$widget->column = $_['widget']['column'];
		$widget->row = $_['widget']['row'];
		$widget->save();
	});
	
	Action::register('dashboard_widget_resize',function(&$response){
		global $_;
		User::check_access('dashboard','edit');
		require_once(__DIR__.SLASH.'DashboardWidget.class.php');
		require_once(__DIR__.SLASH.'Dashboard.class.php');
		
		if(empty($_['widget']['id'])) throw new Exception('Id non spécifié');
		if(!isset($_['widget']['width'])) throw new Exception('Largeur non spécifiée');
		if(!isset($_['widget']['height'])) throw new Exception('Hauteur non spécifiée');

		$widget = DashboardWidget::getById($_['widget']['id'],1);
		if(!$widget) throw new Exception('Widget introuvable');
		
		$widget->width = $_['widget']['width'];
		$widget->height = $_['widget']['height'];
		$widget->save();
	});

	Action::register('dashboard_widget_delete',function(&$response){
		global $_;
		User::check_access('dashboard','delete');
		require_once(__DIR__.SLASH.'DashboardWidget.class.php');
		require_once(__DIR__.SLASH.'Dashboard.class.php');
		
		if(empty($_['widget']['id'])) throw new Exception('Id non spécifié');

		$widget = DashboardWidget::getById($_['widget']['id'],1);
		if(!$widget) throw new Exception('Widget introuvable');
		
		DashboardWidget::deleteById($widget->id);
	});

?>