<?php
/**
 * Implementation of notification service
 *
 * @category   DMS
 * @package    SeedDMS
 * @license    GPL 2
 * @version    @version@
 * @author     Uwe Steinmann <uwe@steinmann.cx>
 * @copyright  Copyright (C) 2016 Uwe Steinmann
 * @version    Release: @package_version@
 */

/**
 * Implementation of notification service
 *
 * @category   DMS
 * @package    SeedDMS
 * @author     Uwe Steinmann <uwe@steinmann.cx>
 * @copyright  Copyright (C) 2016 Uwe Steinmann
 * @version    Release: @package_version@
 */
class SeedDMS_NotificationService {
	/**
	 * List of services for sending notification
	 */
	protected $services;

	/*
	 * List of servives with errors
	 */
	protected $errors;

	/*
	 * Service for logging
	 */
	protected $logger;

	/*
	 * Configuration
	 */
	protected $settings;

	/*
	 * Possible types of receivers
	 */
	const RECV_ANY = 0;
	const RECV_NOTIFICATION = 1;
	const RECV_OWNER = 2;
	const RECV_REVIEWER = 3;
	const RECV_APPROVER = 4;
	const RECV_WORKFLOW = 5;

	public function __construct($logger = null, $settings = null) { /* {{{ */
		$this->services = array();
		$this->errors = array();
		$this->logger = $logger;
		$this->settings = $settings;
	} /* }}} */

	public function addService($service, $name='') { /* {{{ */
		if(!$name)
			$name = md5(uniqid());
		$this->services[$name] = $service;
		$this->errors[$name] = true;
	} /* }}} */

	public function getServices() { /* {{{ */
		return $this->services;
	} /* }}} */

	public function getErrors() { /* {{{ */
		return $this->errors;
	} /* }}} */

	public function toIndividual($sender, $recipient, $subject, $message, $params=array(), $recvtype=0) { /* {{{ */
		$error = true;
		foreach($this->services as $name => $service) {
			/* Set $to to email address of user or the string passed in $recipient
			 * This is only used for logging
			 */
			if(is_object($recipient) && $recipient->isType('user') && !$recipient->isDisabled() && $recipient->getEmail()!="") {
				$to = $recipient->getEmail();
			} elseif(is_string($recipient) && trim($recipient) != "") {
				$to = $recipient;
			} else {
				$to = '';
			}

			/* Call filter of notification service if set */
			if(!is_callable([$service, 'filter']) || $service->filter($sender, $recipient, $subject, $message, $params, $recvtype)) {
				if(!$service->toIndividual($sender, $recipient, $subject, $message, $params)) {
					$error = false;
					$this->errors[$name] = false;
					$this->logger->log('Notification service \''.$name.'\': Sending notification \''.$subject.'\' to user \''.$to.'\' ('.$recvtype.') failed.', PEAR_LOG_ERR);
				} else {
					$this->logger->log('Notification service \''.$name.'\': Sending notification \''.$subject.'\' to user \''.$to.'\' ('.$recvtype.') successful.', PEAR_LOG_INFO);
					$this->errors[$name] = true;
				}
			} else {
				$this->logger->log('Notification service \''.$name.'\': Notification \''.$subject.'\' to user \''.$to.'\' ('.$recvtype.') filtered out.', PEAR_LOG_INFO);
			}
		}
		return $error;
	} /* }}} */

	/**
	 * Send a notification to each user of a group
	 *
	 */
	public function toGroup($sender, $groupRecipient, $subject, $message, $params=array(), $recvtype=0) { /* {{{ */
		$error = true;
		foreach($this->services as $name => $service) {
			$ret = true;
			foreach ($groupRecipient->getUsers() as $recipient) {
				$ret &= $this->toIndividual($sender, $recipient, $subject, $message, $params, $recvtype);
			}
			$this->errors[$name] = $ret;
			if(!$ret) {
				$error = false;
			}
		}
		return $error;
	} /* }}} */

	/**
	 * Send a notification to a list of recipients
	 *
	 * The list of recipients may contain both email addresses and users
	 *
	 * @param string|object $sender either an email address or a user
	 * @param array $recipients list of recipients
	 * @param string $subject key of translatable phrase for the subject
	 * @param string $message key of translatable phrase for the message body
	 * @param array $params list of parameters filled into the subject and body
	 * @param int $recvtype type of receiver
	 * @return boolean true on success, otherwise false
	 */
	public function toList($sender, $recipients, $subject, $message, $params=array(), $recvtype=0) { /* {{{ */
		$error = true;
		foreach($this->services as $name => $service) {
			$ret = true;
			foreach ($recipients as $recipient) {
				$ret &= $this->toIndividual($sender, $recipient, $subject, $message, $params, $recvtype);
			}
			$this->errors[$name] = $ret;
			if(!$ret) {
				$error = false;
			}
		}
		return $error;
	} /* }}} */

	/**
	 *
	 */
	public function sendNewDocumentMail($document, $user) {
		$folder = $document->getFolder();
			$fnl = $folder->getNotifyList();
			$dnl = $document->getNotifyList();
			$nl = array(
				'users'=>array_unique(array_merge($dnl['users'], $fnl['users']), SORT_REGULAR),
				'groups'=>array_unique(array_merge($dnl['groups'], $fnl['groups']), SORT_REGULAR)
			);

			$lc = $document->getLatestContent();
			$subject = "new_document_email_subject";
			$message = "new_document_email_body";
			$params = array();
			$params['name'] = $document->getName();
			$params['folder_name'] = $folder->getName();
			$params['folder_path'] = $folder->getFolderPathPlain();
			$params['username'] = $user->getFullName();
			$params['comment'] = $document->getComment();
			$params['version_comment'] = $lc->getComment();
			$params['url'] = getBaseUrl().$this->settings->_httpRoot."out/out.ViewDocument.php?documentid=".$document->getID();
			$params['sitename'] = $this->settings->_siteName;
			$params['http_root'] = $this->settings->_httpRoot;
			$this->toList($user, $nl["users"], $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION);
			foreach ($nl["groups"] as $grp) {
				$this->toGroup($user, $grp, $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION);
			}

			/* Get workflow from controller in case it was modified in a hook */
//			$workflow = $controller->getParam('workflow');
			if($this->settings->_enableNotificationWorkflow && ($workflow = $lc->getWorkflow())) {
				$subject = "request_workflow_action_email_subject";
				$message = "request_workflow_action_email_body";
				$params = array();
				$params['name'] = $document->getName();
				$params['version'] = $lc->getVersion();
				$params['workflow'] = $workflow->getName();
				$params['folder_path'] = $folder->getFolderPathPlain();
				$params['current_state'] = $workflow->getInitState()->getName();
				$params['username'] = $user->getFullName();
				$params['sitename'] = $this->settings->_siteName;
				$params['http_root'] = $this->settings->_httpRoot;
				$params['url'] = getBaseUrl().$this->settings->_httpRoot."out/out.ViewDocument.php?documentid=".$document->getID();

				foreach($workflow->getNextTransitions($workflow->getInitState()) as $ntransition) {
					foreach($ntransition->getUsers() as $tuser) {
						$this->toIndividual($user, $tuser->getUser(), $subject, $message, $params, SeedDMS_NotificationService::RECV_WORKFLOW);
					}
					foreach($ntransition->getGroups() as $tuser) {
						$this->toGroup($user, $tuser->getGroup(), $subject, $message, $params, SeedDMS_NotificationService::RECV_WORKFLOW);
					}
				}
			}

			if($this->settings->_enableNotificationAppRev) {
				/* Reviewers and approvers will be informed about the new document */
				/* Get reviewers and approvers from controller in case it was
				 * modified in a hook
				 */
				$reviewers = $lc->getReviewers(); //$controller->getParam('reviewers');
        $approvers = $lc->getApprovers(); //$controller->getParam('approvers');
				if($reviewers['i'] || $reviewers['g']) {
					$subject = "review_request_email_subject";
					$message = "review_request_email_body";
					$params = array();
					$params['name'] = $document->getName();
					$params['folder_path'] = $folder->getFolderPathPlain();
					$params['version'] = $lc->getVersion();
					$params['comment'] = $document->getComment();
					$params['username'] = $user->getFullName();
					$params['url'] = getBaseUrl().$this->settings->_httpRoot."out/out.ViewDocument.php?documentid=".$document->getID();
					$params['sitename'] = $this->settings->_siteName;
					$params['http_root'] = $this->settings->_httpRoot;

					foreach($reviewers['i'] as $reviewer) {
						$this->toIndividual($user, $reviewer, $subject, $message, $params, SeedDMS_NotificationService::RECV_REVIEWER);
					}
					foreach($reviewers['g'] as $reviewergrp) {
						$this->toGroup($user, $reviewergrp, $subject, $message, $params, SeedDMS_NotificationService::RECV_REVIEWER);
					}
				}

				elseif($approvers['i'] || $approvers['g']) {
					$subject = "approval_request_email_subject";
					$message = "approval_request_email_body";
					$params = array();
					$params['name'] = $document->getName();
					$params['folder_path'] = $folder->getFolderPathPlain();
					$params['version'] = $lc->getVersion();
					$params['comment'] = $document->getComment();
					$params['username'] = $user->getFullName();
					$params['url'] = getBaseUrl().$this->settings->_httpRoot."out/out.ViewDocument.php?documentid=".$document->getID();
					$params['sitename'] = $this->settings->_siteName;
					$params['http_root'] = $this->settings->_httpRoot;

					foreach($approvers['i'] as $approver) {
						$this->toIndividual($user, $approver, $subject, $message, $params, SeedDMS_NotificationService::RECV_APPROVER);
					}
					foreach($approvers['g'] as $approvergrp) {
						$this->toGroup($user, $approvergrp, $subject, $message, $params, SeedDMS_NotificationService::RECV_APPROVER);
					}
				}
			}
	} /* }}} */
}