From 33c6436c16b30414924a1dd1dc481465abff55bb Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Sat, 15 May 2021 22:46:46 +0200 Subject: [PATCH] move more emails into class NotificationService --- inc/inc.ClassNotificationService.php | 120 +++++++++++++++++++++++++++ op/op.AddFile.php | 18 +--- op/op.AddSubFolder.php | 3 + op/op.Ajax.php | 38 ++------- op/op.RemoveDocument.php | 23 ++--- op/op.RemoveFolder.php | 3 + op/op.SetWorkflow.php | 27 +----- 7 files changed, 141 insertions(+), 91 deletions(-) diff --git a/inc/inc.ClassNotificationService.php b/inc/inc.ClassNotificationService.php index 15626e544..728ef5077 100644 --- a/inc/inc.ClassNotificationService.php +++ b/inc/inc.ClassNotificationService.php @@ -302,6 +302,126 @@ class SeedDMS_NotificationService { } /* }}} */ + /** + * This notification is sent when a document is deleted. + * Keep in mind that $document refers to a document which has just been + * deleted from the database, but all the data needed is still in the + * object. + */ + public function sendDeleteDocumentMail($document, $user) { /* {{{ */ + $folder = $document->getFolder(); + $dnl = $document->getNotifyList(); + $fnl = $folder->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) + ); + $subject = "document_deleted_email_subject"; + $message = "document_deleted_email_body"; + $params = array(); + $params['name'] = $document->getName(); + $params['folder_path'] = $folder->getFolderPathPlain(); + $params['username'] = $user->getFullName(); + $params['sitename'] = $this->settings->_siteName; + $params['http_root'] = $this->settings->_httpRoot; + $params['url'] = getBaseUrl().$this->settings->_httpRoot."out/out.ViewFolder.php?folderid=".$folder->getID(); + $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 notification is sent when a new folder is created. + */ + public function sendNewFolderMail($folder, $user) { /* {{{ */ + $parent = $folder->getParent(); + $fnl = $parent->getNotifyList(); + $snl = $folder->getNotifyList(); + $nl = array( + 'users'=>array_unique(array_merge($snl['users'], $fnl['users']), SORT_REGULAR), + 'groups'=>array_unique(array_merge($snl['groups'], $fnl['groups']), SORT_REGULAR) + ); + + $subject = "new_subfolder_email_subject"; + $message = "new_subfolder_email_body"; + $params = array(); + $params['name'] = $folder->getName(); + $params['folder_name'] = $parent->getName(); + $params['folder_path'] = $parent->getFolderPathPlain(); + $params['username'] = $user->getFullName(); + $params['comment'] = $folder->getComment(); + $params['url'] = getBaseUrl().$this->settings->_httpRoot."out/out.ViewFolder.php?folderid=".$folder->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 notification is sent when a folder is deleted. + * Keep in mind that $folder refers to a folder which has just been + * deleted from the database, but all the data needed is still in the + * object. + */ + public function sendDeleteFolderMail($folder, $user) { /* {{{ */ + $parent = $folder->getParent(); + $fnl = $folder->getNotifyList(); + $pnl = $parent->getNotifyList(); + $nl = array( + 'users'=>array_unique(array_merge($fnl['users'], $pnl['users']), SORT_REGULAR), + 'groups'=>array_unique(array_merge($fnl['groups'], $pnl['groups']), SORT_REGULAR) + ); + + $subject = "folder_deleted_email_subject"; + $message = "folder_deleted_email_body"; + $params = array(); + $params['name'] = $folder->getName(); + $params['folder_path'] = $parent->getFolderPathPlain(); + $params['username'] = $user->getFullName(); + $params['sitename'] = $this->settings->_siteName; + $params['http_root'] = $this->settings->_httpRoot; + $params['url'] = getBaseUrl().$this->settings->_httpRoot."out/out.ViewFolder.php?folderid=".$parent->getID(); + $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 notification is sent when a new attachment is created. + */ + public function sendNewFileMail($file, $user) { /* {{{ */ + $document = $file->getDocument(); + $folder = $document->getFolder(); + $notifyList = $document->getNotifyList(); + + $subject = "new_file_email_subject"; + $message = "new_file_email_body"; + $params = array(); + $params['name'] = $file->getName(); + $params['document'] = $document->getName(); + $params['folder_name'] = $folder->getName(); + $params['folder_path'] = $folder->getFolderPathPlain(); + $params['username'] = $user->getFullName(); + $params['comment'] = $file->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; + // if user is not owner and owner not already in list of notifiers, then + // send notification to owner + if ($user->getID() != $document->getOwner()->getID() && + false === SeedDMS_Core_DMS::inList($document->getOwner(), $notifyList['users'])) { + $this->toIndividual($user, $document->getOwner(), $subject, $message, $params, SeedDMS_NotificationService::RECV_OWNER); + } + $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); + } + } /* }}} */ + public function sendChangedExpiryMail($document, $user, $oldexpires) { /* {{{ */ $folder = $document->getFolder(); $notifyList = $document->getNotifyList(); diff --git a/op/op.AddFile.php b/op/op.AddFile.php index 5f970afdd..305972af5 100644 --- a/op/op.AddFile.php +++ b/op/op.AddFile.php @@ -111,22 +111,7 @@ for ($file_num=0;$file_numgetNotifyList(); - - $subject = "new_file_email_subject"; - $message = "new_file_email_body"; - $params = array(); - $params['name'] = $name; - $params['document'] = $document->getName(); - $params['username'] = $user->getFullName(); - $params['comment'] = $comment; - $params['url'] = getBaseUrl().$settings->_httpRoot."out/out.ViewDocument.php?documentid=".$document->getID(); - $params['sitename'] = $settings->_siteName; - $params['http_root'] = $settings->_httpRoot; - $notifier->toList($user, $notifyList["users"], $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION); - foreach ($notifyList["groups"] as $grp) { - $notifier->toGroup($user, $grp, $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION); - } + $notifier->sendNewFileMail($res, $user); } } } @@ -135,5 +120,4 @@ add_log_line("?name=".$name."&documentid=".$documentid); header("Location:../out/out.ViewDocument.php?documentid=".$documentid."¤ttab=attachments"); - ?> diff --git a/op/op.AddSubFolder.php b/op/op.AddSubFolder.php index a98c9d7ac..efac68b40 100644 --- a/op/op.AddSubFolder.php +++ b/op/op.AddSubFolder.php @@ -121,6 +121,8 @@ if(!$subFolder = $controller->run()) { } else { // Send notification to subscribers. if($notifier) { + $notifier->sendNewFolderMail($subFolder, $user); + /* $fnl = $folder->getNotifyList(); $snl = $subFolder->getNotifyList(); $nl = array( @@ -143,6 +145,7 @@ if(!$subFolder = $controller->run()) { foreach ($nl["groups"] as $grp) { $notifier->toGroup($user, $grp, $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION); } + */ } } diff --git a/op/op.Ajax.php b/op/op.Ajax.php index 217316fa7..71dc69af3 100644 --- a/op/op.Ajax.php +++ b/op/op.Ajax.php @@ -74,7 +74,7 @@ if (isset($_COOKIE["mydms_session"])) { } if($settings->_enableEmail) { - $notifier->addService(new SeedDMS_EmailNotify($dms, $settings->_smtpSendFrom, $settings->_smtpServer, $settings->_smtpPort, $settings->_smtpUser, $settings->_smtpPassword)); + $notifier->addService(new SeedDMS_EmailNotify($dms, $settings->_smtpSendFrom, $settings->_smtpServer, $settings->_smtpPort, $settings->_smtpUser, $settings->_smtpPassword), 'email'); } if(isset($GLOBALS['SEEDDMS_HOOKS']['notification'])) { @@ -507,19 +507,7 @@ switch($command) { if($folder->remove()) { if ($notifier) { - $subject = "folder_deleted_email_subject"; - $message = "folder_deleted_email_body"; - $params = array(); - $params['name'] = $foldername; - $params['folder_path'] = $parent->getFolderPathPlain(); - $params['username'] = $user->getFullName(); - $params['sitename'] = $settings->_siteName; - $params['http_root'] = $settings->_httpRoot; - $params['url'] = getBaseUrl().$settings->_httpRoot."out/out.ViewFolder.php?folderid=".$parent->getID(); - $notifier->toList($user, $nl["users"], $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION); - foreach ($nl["groups"] as $grp) { - $notifier->toGroup($user, $grp, $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION); - } + $notifier->sendDeleteFolderMail($folder, $user); } header('Content-Type: application/json'); echo json_encode(array('success'=>true, 'message'=>'', 'data'=>'')); @@ -553,10 +541,6 @@ switch($command) { /* Get the notify list before removing the document */ $dnl = $document->getNotifyList(); $fnl = $folder->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) - ); $docname = $document->getName(); $controller = Controller::factory('RemoveDocument', array('dms'=>$dms, 'user'=>$user)); @@ -564,18 +548,10 @@ switch($command) { $controller->setParam('fulltextservice', $fulltextservice); if($controller->run()) { if ($notifier){ - $subject = "document_deleted_email_subject"; - $message = "document_deleted_email_body"; - $params = array(); - $params['name'] = $docname; - $params['folder_path'] = $folder->getFolderPathPlain(); - $params['username'] = $user->getFullName(); - $params['sitename'] = $settings->_siteName; - $params['http_root'] = $settings->_httpRoot; - $notifier->toList($user, $nl["users"], $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION); - foreach ($nl["groups"] as $grp) { - $notifier->toGroup($user, $grp, $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION); - } + /* $document still has the data from the just deleted document, + * which is just enough to send the email. + */ + $notifier->sendDeleteDocumentMail($document, $user); } header('Content-Type: application/json'); @@ -844,7 +820,7 @@ switch($command) { } break; /* }}} */ - case 'indexdocument': /* {{{ */ + case 'indexdocument': case 'indexfolder': /* {{{ */ if($user && $user->isAdmin()) { if($fulltextservice) { diff --git a/op/op.RemoveDocument.php b/op/op.RemoveDocument.php index 9d554b335..f700b96ac 100644 --- a/op/op.RemoveDocument.php +++ b/op/op.RemoveDocument.php @@ -69,13 +69,11 @@ $previewer->deleteDocumentPreviews($document); /* Get the notify list before removing the document * Also inform the users/groups of the parent folder + * Getting the list now will keep them in the document object + * even after the document has been deleted. */ $dnl = $document->getNotifyList(); $fnl = $folder->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) -); $docname = $document->getName(); $controller->setParam('document', $document); @@ -89,19 +87,10 @@ if(!$controller->run()) { } if ($notifier){ - $subject = "document_deleted_email_subject"; - $message = "document_deleted_email_body"; - $params = array(); - $params['name'] = $docname; - $params['folder_path'] = $folder->getFolderPathPlain(); - $params['username'] = $user->getFullName(); - $params['sitename'] = $settings->_siteName; - $params['http_root'] = $settings->_httpRoot; - $params['url'] = getBaseUrl().$settings->_httpRoot."out/out.ViewFolder.php?folderid=".$folder->getID(); - $notifier->toList($user, $nl["users"], $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION); - foreach ($nl["groups"] as $grp) { - $notifier->toGroup($user, $grp, $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION); - } + /* $document still has the data from the just deleted document, + * which is just enough to send the email. + */ + $notifier->sendDeleteDocumentMail($document, $user); } $session->setSplashMsg(array('type'=>'success', 'msg'=>getMLText('splash_rm_document'))); diff --git a/op/op.RemoveFolder.php b/op/op.RemoveFolder.php index 0d0c948e3..a2a25da55 100644 --- a/op/op.RemoveFolder.php +++ b/op/op.RemoveFolder.php @@ -85,6 +85,8 @@ if(!$controller->run()) { } if ($notifier) { + $notifier->sendDeleteFolderMail($folder, $user); + /* $subject = "folder_deleted_email_subject"; $message = "folder_deleted_email_body"; $params = array(); @@ -98,6 +100,7 @@ if ($notifier) { foreach ($nl["groups"] as $grp) { $notifier->toGroup($user, $grp, $subject, $message, $params, SeedDMS_NotificationService::RECV_NOTIFICATION); } + */ } add_log_line("?folderid=".$folderid."&name=".$foldername); diff --git a/op/op.SetWorkflow.php b/op/op.SetWorkflow.php index b679ab885..13a961c00 100644 --- a/op/op.SetWorkflow.php +++ b/op/op.SetWorkflow.php @@ -72,32 +72,7 @@ if (!$version->setWorkflow($workflow, $user)){ } if ($notifier) { - $nl = $document->getNotifyList(); - $folder = $document->getFolder(); - - if($settings->_enableNotificationWorkflow) { - $subject = "request_workflow_action_email_subject"; - $message = "request_workflow_action_email_body"; - $params = array(); - $params['name'] = $document->getName(); - $params['version'] = $version->getVersion(); - $params['workflow'] = $workflow->getName(); - $params['folder_path'] = $folder->getFolderPathPlain(); - $params['current_state'] = $workflow->getInitState()->getName(); - $params['username'] = $user->getFullName(); - $params['sitename'] = $settings->_siteName; - $params['http_root'] = $settings->_httpRoot; - $params['url'] = getBaseUrl().$settings->_httpRoot."out/out.ViewDocument.php?documentid=".$document->getID(); - - foreach($workflow->getNextTransitions($workflow->getInitState()) as $ntransition) { - foreach($ntransition->getUsers() as $tuser) { - $notifier->toIndividual($user, $tuser->getUser(), $subject, $message, $params, SeedDMS_NotificationService::RECV_WORKFLOW); - } - foreach($ntransition->getGroups() as $tuser) { - $notifier->toGroup($user, $tuser->getGroup(), $subject, $message, $params, SeedDMS_NotificationService::RECV_WORKFLOW); - } - } - } + $notifier->sendRequestWorkflowActionMail($version, $user); } add_log_line("?documentid=".$documentid);