pass logger to constructor, introduce type of receiver

This commit is contained in:
Uwe Steinmann 2021-01-28 21:08:33 +01:00
parent 4ad9638e33
commit d0c1b16332

View File

@ -31,9 +31,23 @@ class SeedDMS_NotificationService {
*/
protected $errors;
public function __construct() {
/*
* 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='') {
@ -51,14 +65,21 @@ class SeedDMS_NotificationService {
return $this->errors;
}
public function toIndividual($sender, $recipient, $subject, $message, $params=array()) {
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)) {
if(!$service->toIndividual($sender, $recipient, $subject, $message, $params)) {
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;
}
}
@ -66,30 +87,46 @@ class SeedDMS_NotificationService {
return $error;
}
public function toGroup($sender, $groupRecipient, $subject, $message, $params=array()) {
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)) {
if(!$service->toGroup($sender, $groupRecipient, $subject, $message, $params)) {
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()) {
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)) {
if(!$service->toList($sender, $recipients, $subject, $message, $params)) {
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);
}
}
}