<?php
/**
 * Define a notification.
 * @author Valentin CARRUESCO
 * @category Plugin
 * @license copyright
 */
class Notification extends Entity{
	public $id,$label,$html,$type,$meta,$end,$start;
	protected $TABLE_NAME = 'notification';
	public $fields =
	array(
		'id' => 'key',
		'label' => 'string',
		'html' => 'longstring',
		'type' => 'string',
		'meta' => 'longstring',
		'start' => 'date',
		'end' => 'date'
	);

	public static function types($key = null){
		$types = array(
			'notice' => array(
				'label' =>'Information',
				'color' =>'#007fdc',
				'icon'  =>'far fa-flag',
				'description'  =>"Notifications d'informations mineures",
			),
			'alert' => array(
				'label' =>'Alerte',
				'color' =>'#ff3100',
				'icon'  =>'fas fa-fire',
				'description'  =>"Notifications importantes ou critiques",
			)
		);

		Plugin::callHook('notification_type',array(&$types));
		
		return $key ? $types[$key] : $types; 
	}

	public static function emit($infos,$recipients = array()){
		require_once(__DIR__.SLASH.'UserNotification.class.php');

		//Nettoyage des notifications périmées
		self::clear();

		$default = array(
			'label' => "Notification sans titre",
			'html' => "",
			'type' => "notice",
			'meta' => array(),
			'start' => time(),
			'end' => strtotime('+3month')
		);

		$infos = array_merge($default,$infos);

		$notification = new Notification();
		$notification->label = $infos['label'];
		$notification->html = $infos['html'];
		$notification->type = $infos['type'];
		$notification->meta = json_encode($infos['meta']);
		$notification->end = $infos['end'];
		$notification->start = $infos['start'];
		$notification->save();
			
		foreach($recipients as $recipient) {
			$userNotification = new UserNotification();
			$userNotification->notification = $notification->id;
			$userNotification->user = $recipient;
			$userNotification->read = 0;
			$userNotification->save();
		}

		$preferences = array();
		foreach(UserPreference::staticQuery("SELECT * FROM {{table}} up WHERE user IN ('".implode("','",$recipients)."') AND up.key IN('notification_send_mail','notification_categories')",array(),true) as $row){
			if(!isset($preferences[$row->user])) $preferences[$row->user] = array();
			$preferences[$row->user][$row->key] = $row->value;
		}

		foreach($preferences as $user => $preference){
			
			if(trim($preference['notification_send_mail']) == '') continue;
			if(trim($preference['notification_categories']) == '') continue;
			
			$categories = json_decode($preference['notification_categories'],true);
			
			if(!is_array($categories) || !in_array($notification->type, $categories)) continue;

			$mail = new Mail();
			$mail->expeditor = 'IdleCorp <no-reply@idleman.fr>';
			$mail->title = $notification->label;
			$mail->recipients['to'][] = $preference['notification_send_mail'];
			$mail->message = $notification->html;
			$mail->message .= '<br><a href="'.ROOT_URL.'/index.php?module=notification#'.$userNotification->id.'">Voir la notification</a>';
			$mail->send();
		}
			
	}

	public static function clear(){
		foreach (Notification::loadAll(array('end:<'=>time())) as $notification) {
			UserNotification::delete(array('notification'=>$notification->id));
			Notification::deleteById($notification->id);
		}
	}
}
?>