{"label":"Action","syntax":"php"}
<?php
global $_,$conf;
switch($_['action']){
	/** {{ENTITY}} **/
	//Récuperation d'une liste de {{entity}}
	case '{{plugin}}_{{entity}}_search':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('{{plugin}}','read')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'{{Entity}}.class.php');{{:links}}
			require_once(__DIR__.SLASH.'{{value.entity}}.class.php');{{/:links}}

			{{~! Recherche non avancée}}
			// OPTIONS DE RECHERCHE, A ACTIVER POUR UNE RECHERCHE AVANCEE
			$query = 'SELECT * FROM '.{{Entity}}::tableName().' WHERE 1';
			$data = array();
			//Recherche simple
			if(!empty($_['filters']['keyword'])){
				$query .= ' AND label LIKE ?';
				$data[] = '%'.$_['filters']['keyword'].'%';
			}

			//Recherche avancée
			if(isset($_['filters']['advanced'])) filter_secure_query($_['filters']['advanced'],array('label'),$query,$data);

			//Tri des colonnes
			if(isset($_['sort'])) sort_secure_query($_['sort'],array('label'),$query,$data);

			//Pagination
			$response['pagination'] = {{Entity}}::paginate(2,(!empty($_['page'])?$_['page']:0),$query,$data);

			${{entity}}s = {{Entity}}::staticQuery($query,$data,true,{{linksCount}});
			{{/~}}

			{{~ Recherche non avancée}}
			${{entity}}s = {{Entity}}::loadAll();
			{{/~}}
			foreach(${{entity}}s as ${{entity}}){
				$row = ${{entity}}->toArray();{{:links}}
				$row['{{value.key}}'] = ${{entity}}->join('{{value.key}}')->toArray();{{/:links}}
				$response['rows'][] = $row;
			}
		});
	break;
	
	//Ajout ou modification d'élément {{entity}}
	case '{{plugin}}_{{entity}}_save':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('{{plugin}}','edit')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'{{Entity}}.class.php');
			$item = {{Entity}}::provide();{{:fields}}
			$item->{{value.key}} = $_['{{value.key}}'];{{/:fields}}
			$item->save();
		});
	break;
	
	{{~ Formulaire dans le tableau de liste }}//Récuperation ou edition d'élément {{entity}}
	case '{{plugin}}_{{entity}}_edit':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('{{plugin}}','edit')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'{{Entity}}.class.php');{{:links}}
			require_once(__DIR__.SLASH.'{{value.entity}}.class.php');{{/:links}}
			$response = {{Entity}}::getById($_['id'],{{linksCount}});
		});
	break;{{/~}}

	//Suppression d'élement {{entity}}
	case '{{plugin}}_{{entity}}_delete':
		Action::write(function(&$response){
			global $myUser,$_;
			if(!$myUser->can('{{plugin}}','delete')) throw new Exception("Permissions insuffisantes",403);
			require_once(__DIR__.SLASH.'{{Entity}}.class.php');
			{{~! Suppression logique}}{{Entity}}::deleteById($_['id']);{{/~}}
			{{~ Suppression logique}}$item = {{Entity}}::getById($_['id']);
			$item->state = {{Entity}}::INACTIVE;
			$item->save();{{/~}}
		});
	break;

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

	{{~ Widget de dashboard }}
	case '{{plugin}}_widget_load':
		global $myUser;
		require_once(__DIR__.SLASH.'..'.SLASH.'dashboard'.SLASH.'DashboardWidget.class.php');
		$widget = DashboardWidget::current();
		$widget->title = 'Widget {{Plugin}}';
		ob_start();
		//Décommenter après avoir créé widget.php
		//require_once(__DIR__.SLASH.'widget.php');
		//$widget->content = ob_get_clean();
		$widget->content = 'Widget non développé';
		echo json_encode($widget);
	break;
	{{/~}}


}
?>