add method sendNewDocumentMail(), settings can be passed to construct

This commit is contained in:
Uwe Steinmann 2021-05-14 21:55:48 +02:00
parent e40fcf1c2c
commit ca52671bac

View File

@ -36,6 +36,11 @@ class SeedDMS_NotificationService {
*/
protected $logger;
/*
* Configuration
*/
protected $settings;
/*
* Possible types of receivers
*/
@ -46,28 +51,29 @@ class SeedDMS_NotificationService {
const RECV_APPROVER = 4;
const RECV_WORKFLOW = 5;
public function __construct($logger = null) {
public function __construct($logger = null, $settings = null) { /* {{{ */
$this->services = array();
$this->errors = array();
$this->logger = $logger;
}
$this->settings = $settings;
} /* }}} */
public function addService($service, $name='') {
public function addService($service, $name='') { /* {{{ */
if(!$name)
$name = md5(uniqid());
$this->services[$name] = $service;
$this->errors[$name] = true;
}
} /* }}} */
public function getServices() {
public function getServices() { /* {{{ */
return $this->services;
}
} /* }}} */
public function getErrors() {
public function getErrors() { /* {{{ */
return $this->errors;
}
} /* }}} */
public function toIndividual($sender, $recipient, $subject, $message, $params=array(), $recvtype=0) {
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
@ -96,13 +102,13 @@ class SeedDMS_NotificationService {
}
}
return $error;
}
} /* }}} */
/**
* Send a notification to each user of a group
*
*/
public function toGroup($sender, $groupRecipient, $subject, $message, $params=array(), $recvtype=0) {
public function toGroup($sender, $groupRecipient, $subject, $message, $params=array(), $recvtype=0) { /* {{{ */
$error = true;
foreach($this->services as $name => $service) {
$ret = true;
@ -115,7 +121,7 @@ class SeedDMS_NotificationService {
}
}
return $error;
}
} /* }}} */
/**
* Send a notification to a list of recipients
@ -130,7 +136,7 @@ class SeedDMS_NotificationService {
* @param int $recvtype type of receiver
* @return boolean true on success, otherwise false
*/
public function toList($sender, $recipients, $subject, $message, $params=array(), $recvtype=0) {
public function toList($sender, $recipients, $subject, $message, $params=array(), $recvtype=0) { /* {{{ */
$error = true;
foreach($this->services as $name => $service) {
$ret = true;
@ -143,7 +149,113 @@ class SeedDMS_NotificationService {
}
}
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);
}
}
}
} /* }}} */
}