<?php
global $_,$conf;
switch($_['action']){


	//Récuperation d'une liste de notification
	case 'notification_user_notification_search':
	Action::write(function(&$response){
		global $myUser,$_;
		if (!$myUser->connected()) return;
		if(!$myUser->can('notification','read')) return;
		require_once(__DIR__.SLASH.'UserNotification.class.php');
		require_once(__DIR__.SLASH.'Notification.class.php');
		$response['unread'] = 0;

		$query = 'SELECT *, un.id as id FROM {{table}} un LEFT JOIN '.Notification::tableName().' n ON un.notification=n.id WHERE un.user=? AND (n.start<? OR n.start IS NULL) AND (n.end>? OR n.end IS NULL) '.(isset($_['synthesis']) ? 'AND (un.read = 0)' : '').' ORDER BY n.created DESC LIMIT '.(isset($_['synthesis']) ? 5 : 100);

		foreach($userNotifications = UserNotification::staticQuery($query,array($myUser->login,time(),time())) as $infos){
			$infos['class'] = 'read';
			$infos['readState'] = 'non lu';
			if($infos['read']==0){
				$infos['class'] = 'unread'; 
				$response['unread']++;
				$infos['readState'] = 'lu';
			}
			$infos['created-relative'] = isset($_['synthesis']) ? relative_time($infos['created'], null, 7) : relative_time($infos['created'], null, 7, true);

			$meta = json_decode($infos['meta']);
			$infos['link'] = isset($meta->link) ? $meta->link : 'index.php?module=notification#'.$infos['id'];
			
			// if(isset($_['synthesis']))
			// 	$infos['html'] = truncate($infos['html'],80);
			
			$response['rows'][] = $infos;
		}
		// $query = 'SELECT COUNT(*) unread FROM {{table}} un LEFT JOIN '.Notification::tableName().' n ON un.notification=n.id WHERE un.user=? AND (n.start<? OR n.start IS NULL) AND (n.end>? OR n.end IS NULL) AND (un.read = 0) ORDER BY n.created DESC';
		// $result = UserNotification::staticQuery($query, array($myUser->login,time(),time()))->fetch();

		// $response['unread'] = $result['unread'] > 5 ? '5+' : $result['unread'];
	});
	break;

		//Active/Désactive l'état lu
	case 'notification_usernotification_toggle_read':
	Action::write(function(&$response){
		global $myUser,$_;
		require_once(__DIR__.SLASH.'UserNotification.class.php');
		$item = UserNotification::getById($_['id']);
		if($myUser->login != $item->user) throw new Exception("Permissions insuffisantes",403);
		$item->read = $item->read == true ? false : true;
		$item->save();
		$response['read'] = $item->read;
	});
	break;

	//Suppression d'élement usernotification
	case 'notification_usernotification_delete':
	Action::write(function(&$response){
		global $myUser,$_;
		require_once(__DIR__.SLASH.'UserNotification.class.php');
		$notification = UserNotification::getById($_['id']);
		if($myUser->login != $notification->user) throw new Exception("Permissions insuffisantes",403);
		UserNotification::deleteById($_['id']);
	});
	break;

	//Sauvegarde des préférences de notifications
	case 'notification_user_save_preference':
	Action::write(function(&$response){
		global $myUser,$_;
		if(!$myUser->connected()) throw new Exception("Vous devez être connecté pour enregistrer vos préférences");
		
		if (trim($_['notification_send_mail']) != '' && !check_mail($_['notification_send_mail'])) throw new Exception("Format de l'adresse mail incorrect");

		$myUser->preference('notification_send_mail',$_['notification_send_mail']);
		if(!isset($_['categories'])) $_['categories'] = array();
		$myUser->preference('notification_categories',json_encode($_['categories']));

	});
	break;


}


?>