seeddms-code/inc/inc.ClassNotificationService.php

138 lines
4.5 KiB
PHP

<?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;
/*
* Possible types of receivers
*/
const RECV_ANY = 0;
const RECV_NOTIFICATION = 1;
const RECV_REVIEWER = 2;
const RECV_APPROVER = 3;
public function __construct($logger = null) {
$this->services = array();
$this->errors = array();
$this->logger = $logger;
}
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) {
if(!is_callable([$service, 'filter']) || $service->filter($sender, $recipient, $subject, $message, $params, $recvtype)) {
if(is_object($recipient) && ($dms = $recipient->getDMS()) && !strcasecmp(get_class($recipient), $dms->getClassname('user')) && !$recipient->isDisabled() && $recipient->getEmail()!="") {
$to = $recipient->getEmail();
} elseif(is_string($recipient) && trim($recipient) != "") {
$to = $recipient;
}
if(!$service->toIndividual($sender, $recipient, $subject, $message, $params, $recvtype)) {
$error = false;
$this->errors[$name] = false;
$this->logger->log('Notification service: '.$name.'. Sending mail \''.$subject.'\' to user \''.$to.'\' failed.', PEAR_LOG_ERR);
} else {
$this->logger->log('Notification service: '.$name.'. Sending mail \''.$subject.'\' to user \''.$to.'\' successful.', PEAR_LOG_INFO);
$this->errors[$name] = true;
}
}
}
return $error;
}
public function toGroup($sender, $groupRecipient, $subject, $message, $params=array(), $recvtype=0) {
$error = true;
foreach($this->services as $name => $service) {
if(!is_callable([$service, 'filter']) || $service->filter($sender, $groupRecipient, $subject, $message, $params, $recvtype)) {
if(is_object($groupRecipient) && ($dms = $recipient->getDMS()) && strcasecmp(get_class($groupRecipient), $dms->getClassname('group'))) {
$to = $groupRecipient->getName();
}
if(!$service->toGroup($sender, $groupRecipient, $subject, $message, $params, $recvtype)) {
$error = false;
$this->errors[$name] = false;
$this->logger->log('Notification service: '.$name.'. Sending mail \''.$subject.'\' to group \''.$to.'\' failed.', PEAR_LOG_ERR);
} else {
$this->errors[$name] = true;
$this->logger->log('Notification service: '.$name.'. Sending mail \''.$subject.'\' to group \''.$to.'\' successful.', PEAR_LOG_INFO);
}
}
}
return $error;
}
public function toList($sender, $recipients, $subject, $message, $params=array(), $recvtype=0) {
$error = true;
foreach($this->services as $name => $service) {
if(!is_callable([$service, 'filter']) || $service->filter($sender, $recipients, $subject, $message, $params, $recvtype)) {
$to = [];
foreach ($recipients as $recipient) {
if(is_object($recipient) && ($dms = $recipient->getDMS()) && !strcasecmp(get_class($recipient), $dms->getClassname('user')) && !$recipient->isDisabled() && $recipient->getEmail()!="") {
$to[] = $recipient->getEmail();
} elseif(is_string($recipient) && trim($recipient) != "") {
$to[] = $recipient;
}
}
$to = implode($to, ', ');
if(!$service->toList($sender, $recipients, $subject, $message, $params, $recvtype)) {
$error = false;
$this->errors[$name] = false;
$this->logger->log('Notification service: '.$name.'. Sending mail \''.$subject.'\' to users \''.$to.'\' failed.', PEAR_LOG_ERR);
} else {
$this->errors[$name] = true;
$this->logger->log('Notification service: '.$name.'. Sending mail \''.$subject.'\' to users \''.$to.'\' successful.', PEAR_LOG_INFO);
}
}
}
return $error;
}
}