2016-03-09 16:57:38 +00:00
< ? 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 ;
2020-12-18 13:36:51 +00:00
/*
* List of servives with errors
*/
protected $errors ;
2021-01-28 20:08:33 +00:00
/*
* Service for logging
*/
protected $logger ;
2021-05-14 19:55:48 +00:00
/*
* Configuration
*/
protected $settings ;
2021-01-28 20:08:33 +00:00
/*
* Possible types of receivers
*/
const RECV_ANY = 0 ;
const RECV_NOTIFICATION = 1 ;
2021-01-29 07:57:37 +00:00
const RECV_OWNER = 2 ;
const RECV_REVIEWER = 3 ;
const RECV_APPROVER = 4 ;
const RECV_WORKFLOW = 5 ;
2021-01-28 20:08:33 +00:00
2021-05-14 19:55:48 +00:00
public function __construct ( $logger = null , $settings = null ) { /* {{{ */
2016-03-09 16:57:38 +00:00
$this -> services = array ();
2020-12-18 13:36:51 +00:00
$this -> errors = array ();
2021-01-28 20:08:33 +00:00
$this -> logger = $logger ;
2021-05-14 19:55:48 +00:00
$this -> settings = $settings ;
} /* }}} */
2016-03-09 16:57:38 +00:00
2021-05-14 19:55:48 +00:00
public function addService ( $service , $name = '' ) { /* {{{ */
2020-12-18 13:36:51 +00:00
if ( ! $name )
$name = md5 ( uniqid ());
$this -> services [ $name ] = $service ;
$this -> errors [ $name ] = true ;
2021-05-14 19:55:48 +00:00
} /* }}} */
2020-12-01 17:21:18 +00:00
2021-05-14 19:55:48 +00:00
public function getServices () { /* {{{ */
2020-12-01 17:21:18 +00:00
return $this -> services ;
2021-05-14 19:55:48 +00:00
} /* }}} */
2016-03-09 16:57:38 +00:00
2021-05-14 19:55:48 +00:00
public function getErrors () { /* {{{ */
2020-12-18 13:36:51 +00:00
return $this -> errors ;
2021-05-14 19:55:48 +00:00
} /* }}} */
2020-12-18 13:36:51 +00:00
2021-05-14 19:55:48 +00:00
public function toIndividual ( $sender , $recipient , $subject , $message , $params = array (), $recvtype = 0 ) { /* {{{ */
2020-12-18 13:36:51 +00:00
$error = true ;
2020-12-01 17:21:18 +00:00
foreach ( $this -> services as $name => $service ) {
2021-01-29 09:34:01 +00:00
/* 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 */
2021-01-28 20:08:33 +00:00
if ( ! is_callable ([ $service , 'filter' ]) || $service -> filter ( $sender , $recipient , $subject , $message , $params , $recvtype )) {
2021-01-29 07:57:37 +00:00
if ( ! $service -> toIndividual ( $sender , $recipient , $subject , $message , $params )) {
2020-12-18 13:36:51 +00:00
$error = false ;
$this -> errors [ $name ] = false ;
2021-02-01 12:42:16 +00:00
$this -> logger -> log ( 'Notification service \'' . $name . '\': Sending notification \'' . $subject . '\' to user \'' . $to . '\' (' . $recvtype . ') failed.' , PEAR_LOG_ERR );
2020-12-18 13:36:51 +00:00
} else {
2021-02-01 12:42:16 +00:00
$this -> logger -> log ( 'Notification service \'' . $name . '\': Sending notification \'' . $subject . '\' to user \'' . $to . '\' (' . $recvtype . ') successful.' , PEAR_LOG_INFO );
2020-12-18 13:36:51 +00:00
$this -> errors [ $name ] = true ;
}
2021-01-29 09:34:01 +00:00
} else {
2021-02-01 12:42:16 +00:00
$this -> logger -> log ( 'Notification service \'' . $name . '\': Notification \'' . $subject . '\' to user \'' . $to . '\' (' . $recvtype . ') filtered out.' , PEAR_LOG_INFO );
2020-12-01 17:21:18 +00:00
}
2016-03-09 16:57:38 +00:00
}
2020-12-18 13:36:51 +00:00
return $error ;
2021-05-14 19:55:48 +00:00
} /* }}} */
2016-03-09 16:57:38 +00:00
2021-01-29 07:57:37 +00:00
/**
* Send a notification to each user of a group
*
*/
2021-05-14 19:55:48 +00:00
public function toGroup ( $sender , $groupRecipient , $subject , $message , $params = array (), $recvtype = 0 ) { /* {{{ */
2020-12-18 13:36:51 +00:00
$error = true ;
2020-12-01 17:21:18 +00:00
foreach ( $this -> services as $name => $service ) {
2021-01-29 07:57:37 +00:00
$ret = true ;
foreach ( $groupRecipient -> getUsers () as $recipient ) {
$ret &= $this -> toIndividual ( $sender , $recipient , $subject , $message , $params , $recvtype );
}
$this -> errors [ $name ] = $ret ;
if ( ! $ret ) {
$error = false ;
2020-12-01 17:21:18 +00:00
}
2016-03-09 16:57:38 +00:00
}
2020-12-18 13:36:51 +00:00
return $error ;
2021-05-14 19:55:48 +00:00
} /* }}} */
2016-03-09 16:57:38 +00:00
2021-01-29 07:57:37 +00:00
/**
* 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
*/
2021-05-14 19:55:48 +00:00
public function toList ( $sender , $recipients , $subject , $message , $params = array (), $recvtype = 0 ) { /* {{{ */
2020-12-18 13:36:51 +00:00
$error = true ;
2020-12-01 17:21:18 +00:00
foreach ( $this -> services as $name => $service ) {
2021-01-29 07:57:37 +00:00
$ret = true ;
foreach ( $recipients as $recipient ) {
2021-02-02 20:54:29 +00:00
$ret &= $this -> toIndividual ( $sender , $recipient , $subject , $message , $params , $recvtype );
2021-01-29 07:57:37 +00:00
}
$this -> errors [ $name ] = $ret ;
if ( ! $ret ) {
$error = false ;
2020-12-01 17:21:18 +00:00
}
2016-03-09 16:57:38 +00:00
}
2020-12-18 13:36:51 +00:00
return $error ;
2021-05-14 19:55:48 +00:00
} /* }}} */
/**
2021-05-15 05:38:46 +00:00
* This notification is sent when a workflow action is needed .
2021-05-14 19:55:48 +00:00
*/
2021-05-15 05:38:46 +00:00
public function sendRequestWorkflowActionMail ( $content , $user ) { /* {{{ */
$document = $content -> getDocument ();
2021-05-14 19:55:48 +00:00
$folder = $document -> getFolder ();
2021-05-15 05:38:46 +00:00
/* Send mail only if enabled in the configuration */
if ( $this -> settings -> _enableNotificationWorkflow && ( $workflow = $content -> getWorkflow ())) {
$subject = " request_workflow_action_email_subject " ;
$message = " request_workflow_action_email_body " ;
2021-05-14 19:55:48 +00:00
$params = array ();
$params [ 'name' ] = $document -> getName ();
2021-05-15 05:38:46 +00:00
$params [ 'version' ] = $content -> getVersion ();
$params [ 'workflow' ] = $workflow -> getName ();
2021-05-14 19:55:48 +00:00
$params [ 'folder_path' ] = $folder -> getFolderPathPlain ();
2021-05-15 05:38:46 +00:00
$params [ 'current_state' ] = $workflow -> getInitState () -> getName ();
2021-05-14 19:55:48 +00:00
$params [ 'username' ] = $user -> getFullName ();
$params [ 'sitename' ] = $this -> settings -> _siteName ;
$params [ 'http_root' ] = $this -> settings -> _httpRoot ;
2021-05-15 05:38:46 +00:00
$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 );
}
2021-05-14 19:55:48 +00:00
}
2021-05-15 05:38:46 +00:00
}
} /* }}} */
/**
* This notification is sent when a review or approval is needed .
*/
public function sendRequestRevAppActionMail ( $content , $user ) { /* {{{ */
$document = $content -> getDocument ();
$folder = $document -> getFolder ();
2021-05-14 19:55:48 +00:00
2021-05-15 05:38:46 +00:00
if ( $this -> settings -> _enableNotificationAppRev ) {
/* Reviewers and approvers will be informed about the new document */
$reviewers = $content -> getReviewers (); //$controller->getParam('reviewers');
$approvers = $content -> getApprovers (); //$controller->getParam('approvers');
if ( $reviewers [ 'i' ] || $reviewers [ 'g' ]) {
$subject = " review_request_email_subject " ;
$message = " review_request_email_body " ;
2021-05-14 19:55:48 +00:00
$params = array ();
$params [ 'name' ] = $document -> getName ();
$params [ 'folder_path' ] = $folder -> getFolderPathPlain ();
2021-05-15 05:38:46 +00:00
$params [ 'version' ] = $content -> getVersion ();
$params [ 'comment' ] = $document -> getComment ();
2021-05-14 19:55:48 +00:00
$params [ 'username' ] = $user -> getFullName ();
2021-05-15 05:38:46 +00:00
$params [ 'url' ] = getBaseUrl () . $this -> settings -> _httpRoot . " out/out.ViewDocument.php?documentid= " . $document -> getID ();
2021-05-14 19:55:48 +00:00
$params [ 'sitename' ] = $this -> settings -> _siteName ;
$params [ 'http_root' ] = $this -> settings -> _httpRoot ;
2016-03-09 16:57:38 +00:00
2021-05-15 05:38:46 +00:00
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 );
2021-05-14 19:55:48 +00:00
}
}
2021-05-15 05:38:46 +00:00
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' ] = $content -> 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 ;
2021-05-14 19:55:48 +00:00
2021-05-15 05:38:46 +00:00
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 );
2021-05-14 19:55:48 +00:00
}
}
2021-05-15 05:38:46 +00:00
}
} /* }}} */
/**
* This notification is sent when a new document is created .
*/
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 );
}
$this -> sendRequestWorkflowActionMail ( $lc , $user );
$this -> sendRequestRevAppActionMail ( $lc , $user );
} /* }}} */
/**
* This notification is sent when a new document version is created .
*/
public function sendNewDocumentVersionMail ( $document , $user ) { /* {{{ */
$lc = $document -> getLatestContent ();
$folder = $document -> getFolder ();
$notifyList = $document -> getNotifyList ();
$subject = " document_updated_email_subject " ;
$message = " document_updated_email_body " ;
$params = array ();
$params [ 'name' ] = $document -> 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 , $notifyList [ " users " ], $subject , $message , $params , SeedDMS_NotificationService :: RECV_NOTIFICATION );
foreach ( $notifyList [ " groups " ] as $grp ) {
$this -> toGroup ( $user , $grp , $subject , $message , $params , SeedDMS_NotificationService :: RECV_NOTIFICATION );
}
// if user is not owner send notification to owner
// if ($user->getID() != $document->getOwner()->getID())
// $this->toIndividual($user, $document->getOwner(), $subject, $message, $params, SeedDMS_NotificationService::RECV_OWNER);
$this -> sendRequestWorkflowActionMail ( $lc , $user );
$this -> sendRequestRevAppActionMail ( $lc , $user );
} /* }}} */
public function sendChangedExpiryMail ( $document , $user , $oldexpires ) { /* {{{ */
$folder = $document -> getFolder ();
$notifyList = $document -> getNotifyList ();
if ( $oldexpires != $document -> getExpires ()) {
// Send notification to subscribers.
$subject = " expiry_changed_email_subject " ;
$message = " expiry_changed_email_body " ;
$params = array ();
$params [ 'name' ] = $document -> getName ();
$params [ 'folder_path' ] = $folder -> getFolderPathPlain ();
$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 ;
$this -> toList ( $user , $notifyList [ " users " ], $subject , $message , $params , SeedDMS_NotificationService :: RECV_NOTIFICATION );
foreach ( $notifyList [ " groups " ] as $grp ) {
$this -> toGroup ( $user , $grp , $subject , $message , $params , SeedDMS_NotificationService :: RECV_NOTIFICATION );
}
}
2021-05-14 19:55:48 +00:00
} /* }}} */
2021-05-15 05:38:46 +00:00
2016-03-09 16:57:38 +00:00
}