diff --git a/lib/dashboard/Notification.inc.php b/lib/dashboard/Notification.inc.php index bd3d21b..55c7f17 100644 --- a/lib/dashboard/Notification.inc.php +++ b/lib/dashboard/Notification.inc.php @@ -164,7 +164,7 @@ class KTSubscriptionNotification extends KTNotificationHandler { "ArchivedDocument" => _('Document archived'), // can go through and request un-archival (?) "RestoredArchivedDocument" => _('Document restored') ); - parent::KTNotificationHandler(); + //parent::KTNotificationHandler(); } // helper method to extract / set the various pieces of information function _getSubscriptionData($oKTNotification) { @@ -219,13 +219,8 @@ class KTSubscriptionNotification extends KTNotificationHandler { } function handleNotification($oKTNotification) { - $oTemplating = new KTTemplating; - $oTemplate = $oTemplating->loadTemplate("kt3/notifications/subscriptions"); - $aTemplateData = array( - "context" => $oKTNotification, - "info" => $this->_getSubscriptionData($oKTNotification), - ); - return $oTemplate->render($aTemplateData); + $oSubscriptionContent = new SubscriptionContent(); + return $oSubscriptionContent->getNotificationAlertContent($oKTNotification); } // helper to _create_ a notification, in a way that is slightly less opaque. diff --git a/lib/documentmanagement/documentutil.inc.php b/lib/documentmanagement/documentutil.inc.php index 9125525..2686f38 100644 --- a/lib/documentmanagement/documentutil.inc.php +++ b/lib/documentmanagement/documentutil.inc.php @@ -30,16 +30,15 @@ // LEGACY PATHS require_once(KT_LIB_DIR . '/documentmanagement/DocumentFieldLink.inc'); require_once(KT_LIB_DIR . '/documentmanagement/DocumentTransaction.inc'); -require_once(KT_LIB_DIR . '/subscriptions/SubscriptionEngine.inc'); -require_once(KT_LIB_DIR . '/subscriptions/SubscriptionConstants.inc'); // NEW PATHS require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php'); require_once(KT_LIB_DIR . '/filelike/filelikeutil.inc.php'); require_once(KT_LIB_DIR . '/metadata/metadatautil.inc.php'); require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php'); - +require_once(KT_LIB_DIR . "/subscriptions/subscriptions.inc.php"); require_once(KT_LIB_DIR . '/triggers/triggerregistry.inc.php'); +require_once(KT_LIB_DIR . "/foldermanagement/Folder.inc"); class KTDocumentUtil { function createMetadataVersion($oDocument) { @@ -164,12 +163,10 @@ class KTDocumentUtil { } // fire subscription alerts for the checked in document - $count = SubscriptionEngine::fireSubscription($oDocument->getID(), SubscriptionConstants::subscriptionAlertType("CheckInDocument"), - SubscriptionConstants::subscriptionType("DocumentSubscription"), - array( "folderID" => $oDocument->getFolderID(), - "modifiedDocumentName" => $oDocument->getName() )); - global $default; - $default->log->info("checkInDocumentBL.php fired $count subscription alerts for checked out document " . $oDocument->getName()); + $oSubscriptionEvent = new SubscriptionEvent(); + $oFolder = Folder::get($oDocument->getFolderID()); + $oSubscriptionEvent->CheckinDocument($oDocument, $oFolder); + return true; } @@ -420,12 +417,9 @@ class KTDocumentUtil { } // fire subscription alerts for the checked in document - $count = SubscriptionEngine::fireSubscription($oDocument->getID(), SubscriptionConstants::subscriptionAlertType("AddDocument"), - SubscriptionConstants::subscriptionType("DocumentSubscription"), - array( "folderID" => $oDocument->getFolderID(), - "newDocumentName" => $oDocument->getName() )); - global $default; - $default->log->info("checkInDocumentBL.php fired $count subscription alerts for checked out document " . $oDocument->getName()); + $oSubscriptionEvent = new SubscriptionEvent(); + $oFolder = Folder::get($oDocument->getFolderID()); + $oSubscriptionEvent->AddDocument($oDocument, $oFolder); return $oDocument; } diff --git a/lib/foldermanagement/folderutil.inc.php b/lib/foldermanagement/folderutil.inc.php index a0d586a..c3fcc83 100644 --- a/lib/foldermanagement/folderutil.inc.php +++ b/lib/foldermanagement/folderutil.inc.php @@ -25,6 +25,7 @@ */ require_once(KT_LIB_DIR . '/storage/storagemanager.inc.php'); +require_once(KT_LIB_DIR . "/subscriptions/subscriptions.inc.php"); class KTFolderUtil { function _add ($oParentFolder, $sFolderName, $oUser) { @@ -54,14 +55,8 @@ class KTFolderUtil { } // fire subscription alerts for the new folder - /* $count = SubscriptionEngine::fireSubscription( - $oParentFolder->getID(), SubscriptionConstants::subscriptionAlertType("AddFolder"), - SubscriptionConstants::subscriptionType("FolderSubscription"), - array( - "newFolderName" => $sFolderName, - "parentFolderName" => $oParentFolder->getName(), - ) - );*/ + $oSubscriptionEvent = new SubscriptionEvent(); + $oSubscriptionEvent->AddFolder($oFolder, $oParentFolder); return $oFolder; } diff --git a/lib/subscriptions/Subscription.inc b/lib/subscriptions/Subscription.inc index 8aae6a5..0c32ba7 100644 --- a/lib/subscriptions/Subscription.inc +++ b/lib/subscriptions/Subscription.inc @@ -2,6 +2,7 @@ require_once(KT_LIB_DIR . "/subscriptions/SubscriptionConstants.inc"); require_once(KT_LIB_DIR . "/foldermanagement/Folder.inc"); require_once(KT_LIB_DIR . "/documentmanagement/Document.inc"); +require_once(KT_LIB_DIR . "/subscriptions/subscriptions.inc.php"); /** * $Id$ * @@ -318,9 +319,9 @@ class Subscription extends KTEntity { * @return string the subscription id field name to use */ function getIdFieldName($iSubscriptionType) { - if ($iSubscriptionType == SubscriptionConstants::subscriptionType("DocumentSubscription")) { + if ($iSubscriptionType == SubscriptionEvent::subTypes("Document")) { return "document_id"; - } else if($iSubscriptionType == SubscriptionConstants::subscriptionType("FolderSubscription")) { + } else if($iSubscriptionType == SubscriptionEvent::subTypes("Folder")) { return "folder_id"; } diff --git a/lib/subscriptions/SubscriptionEngine.inc b/lib/subscriptions/SubscriptionEngine.inc deleted file mode 100644 index 61628dc..0000000 --- a/lib/subscriptions/SubscriptionEngine.inc +++ /dev/null @@ -1,298 +0,0 @@ -, Jam Warehouse (Pty) Ltd, South Africa - * @package lib.subscriptions - */ -class SubscriptionEngine { - - /** - * Fires a subscription alert for this subscription content - * - * @param int the id of the subscription content - * @param int the alert type (document change, new folder, etc.) - * @param int the subscription content type (folder, document) - * @param array any dynamic values that should be sent with the alert (eg. document name, path to modified document) - * @param int the original object id (e.g. if fired on a document, and chained to a folder. - */ - function fireSubscription($iExternalID, $iSubscriptionAlertType, $iSubscriptionType, $aValues, $iOriginalId = null) { - global $default; - $default->log->info("fireSubscription ($iExternalID, $iSubscriptionAlertType, $iSubscriptionType, values)"); - // get the list of subscriber addresses that we need to alert - $aSubscribers = SubscriptionEngine::retrieveSubscribers($iExternalID, $iSubscriptionType); - - // count the number of subscriptions we've sent - $iSubscriptionsSent = 0; - - // if the subscription type is document, fire the folder subscriptions also - if ($iSubscriptionType == SubscriptionConstants::subscriptionType("DocumentSubscription")) { - $iSubscriptionsSent = SubscriptionEngine::fireSubscription($aValues["folderID"], - ($iSubscriptionAlertType == SubscriptionConstants::subscriptionAlertType("RemoveSubscribedDocument") ? - SubscriptionConstants::subscriptionAlertType("RemoveChildDocument") : - $iSubscriptionAlertType), - SubscriptionConstants::subscriptionType("FolderSubscription"), - $aValues, - $iExternalId); - $default->log->info("SubscriptionEngine::fireSubscription fired folder subscribers, count=$iSubscriptionsSent"); - } - - // for each subscriber, construct an address based on their notification preferences - for ($i=0; $igetID(), $aValues['folderID'], $iSubscriptionType); - if (empty($oSubscription) || PEAR::isError($oSubscription)) { - $oSubscription =& new Subscription($aSubscribers[$i]->getID(), $aValues["folderID"], $iSubscriptionType); - $res = $oSubscription->create(); - if (empty($res) || PEAR::isError($res)) { - continue; - } - } - } else { - $oSubscription = & Subscription::getByIDs($aSubscribers[$i]->getID(), $iExternalID, $iSubscriptionType); - } - - // update the alerted status - $oSubscription->setIsAlerted(true); - - // write it back to the db - if ($oSubscription->update()) { - - // get the subscription id - $aValues["subscriptionID"] = $oSubscription->getID(); - // and type - $aValues["subscriptionType"] = $iSubscriptionType; - // and subscriber name - $aValues["subscriberName"] = $aSubscribers[$i]->getName(); - - // retrieve the appropriate content - // using the values array to customise the notification message - $sAlertContent = AlertContent::getSubscriptionAlert($iSubscriptionAlertType, $aValues); - - // construct alerts - - // dashboard notification. - $aNotificationOptions = array(); - - $aNotificationOptions['target_user'] = $aSubscribers[$i]->getID(); - $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. - - // location name: ditto. - // target_name: ditto. - - //$default->log->debug('subscriptionengine: received ' . print_r($aValues, true)); - - // sweet lord this is _hideous_. Please, oh please kill the subscriptionsconstants - if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "AddFolder") { - $aNotificationOptions['target_name'] = $aValues["newFolderName"]; - $aNotificationOptions['location_name'] = $aValues["parentFolderName"]; - $aNotificationOptions['object_id'] = $iExternalId; // parent folder_id, in this case. - $aNotificationOptions['event_type'] = "AddFolder"; - } else if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "RemoveSubscribedFolder") { - $aNotificationOptions['target_name'] = $aValues["removedFolderName"]; - $aNotificationOptions['location_name'] = $aValues["parentFolderName"]; - $aNotificationOptions['object_id'] = $iExternalId; // parent folder_id, in this case. - $aNotificationOptions['event_type'] = "RemoveSubscribedFolder"; - } else if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "RemoveChildFolder") { - $aNotificationOptions['target_name'] = $aValues["removedFolderName"]; - $aNotificationOptions['location_name'] = $aValues["parentFolderName"]; - $aNotificationOptions['object_id'] = $iExternalId; // parent folder_id, in this case, where user is subscribed. - $aNotificationOptions['event_type'] = "RemoveChildFolder"; - } else if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "AddDocument") { - $aNotificationOptions['target_name'] = $aValues["newDocumentName"]; - $aNotificationOptions['location_name'] = $aValues["parentFolderName"]; - if ($iOriginalId !== null) { - $aNotificationOptions['object_id'] = $iOriginalId; // folder subscription, this _was_ the document. - } else { - $aNotificationOptions['object_id'] = $iExternalId; // this path _shouldn't_ be hit since its a new document - user _can't_ be subscribed to it already. - } - $aNotificationOptions['event_type'] = "AddDocument"; - } else if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "RemoveChildDocument") { - $aNotificationOptions['target_name'] = $aValues["removedDocumentName"]; - $aNotificationOptions['location_name'] = $aValues["folderName"]; - $aNotificationOptions['object_id'] = $iExternalId; // parent folder_id, in this case, where user is subscribed... is it? - $aNotificationOptions['event_type'] = "RemoveChildDocument"; - } else if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "RemoveSubscribedDocument") { - $aNotificationOptions['target_name'] = $aValues["removedDocumentName"]; - $aNotificationOptions['location_name'] = $aValues["folderName"]; - $aNotificationOptions['object_id'] = $iExternalId; // not used. - $aNotificationOptions['event_type'] = "RemoveSubscribedDocument"; - } else if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "ModifyDocument") { - $aNotificationOptions['target_name'] = $aValues["modifiedDocumentName"]; - $aNotificationOptions['location_name'] = null; // not used... why? don't we have the folder name? why not? - if ($iOriginalId !== null) { - $aNotificationOptions['object_id'] = $iOriginalId; // folder subscription, this _was_ the document. - } else { - $aNotificationOptions['object_id'] = $iExternalId; // this path _shouldn't_ be hit since its a new document - user _can't_ be subscribed to it already. - } - $aNotificationOptions['event_type'] = "ModifyDocument"; - } else if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "CheckInDocument") { - $aNotificationOptions['target_name'] = $aValues["modifiedDocumentName"]; - $aNotificationOptions['location_name'] = null; // not used... why? don't we have the folder name? why not? - if ($iOriginalId !== null) { - $aNotificationOptions['object_id'] = $iOriginalId; // folder subscription, this _was_ the document. - } else { - $aNotificationOptions['object_id'] = $iExternalId; // this path _shouldn't_ be hit since its a new document - user _can't_ be subscribed to it already. - }$aNotificationOptions['event_type'] = "CheckInDocument"; - } else if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "CheckOutDocument") { - $aNotificationOptions['target_name'] = $aValues["modifiedDocumentName"]; - $aNotificationOptions['location_name'] = null; // not used... why? don't we have the folder name? why not? - if ($iOriginalId !== null) { - $aNotificationOptions['object_id'] = $iOriginalId; // folder subscription, this _was_ the document. - } else { - $aNotificationOptions['object_id'] = $iExternalId; // this path _shouldn't_ be hit since its a new document - user _can't_ be subscribed to it already. - }$aNotificationOptions['event_type'] = "CheckOutDocument"; - } else if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "MovedDocument") { - $aNotificationOptions['target_name'] = $aValues["modifiedDocumentName"]; - $aNotificationOptions['location_name'] = $aValues["oldFolderName"]; - if ($iOriginalId !== null) { - $aNotificationOptions['object_id'] = $iOriginalId; // folder subscription, this _was_ the document. - } else { - $aNotificationOptions['object_id'] = $iExternalId; - }$aNotificationOptions['event_type'] = "MovedDocument"; - } else if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "ArchivedDocument") { - $aNotificationOptions['target_name'] = $aValues["modifiedDocumentName"]; - $aNotificationOptions['location_name'] = $aValues["folderName"]; - if ($iOriginalId !== null) { - $aNotificationOptions['object_id'] = $iOriginalId; // folder subscription, this _was_ the document. - } else { - $aNotificationOptions['object_id'] = $iExternalId; - } - $aNotificationOptions['event_type'] = "ArchivedDocument"; - } else if (SubscriptionConstants::subscriptionAlertTypeString($iSubscriptionAlertType) == "RestoredArchivedDocument") { - $aNotificationOptions['target_name'] = $aValues["modifiedDocumentName"]; - $aNotificationOptions['location_name'] = null; // $aValues["folderName"]; // not reachable. - if ($iOriginalId !== null) { - $aNotificationOptions['object_id'] = $iOriginalId; // folder subscription, this _was_ the document. - } else { - $aNotificationOptions['object_id'] = $iExternalId; - } - $aNotificationOptions['event_type'] = "RestoredArchivedDocument"; - } - - $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); - - // email alert. - if ($aSubscribers[$i]->getEmailNotification() && (strlen($aSubscribers[$i]->getEmail()) > 0)) { - - $oEmail = new EmailAlert($aSubscribers[$i]->getEmail(), $sAlertContent["subject"], $sAlertContent["text"]); - if ($oEmail->send()) { - $iSubscriptionsSent++; - $default->log->debug("SubscriptionEngine::fireSubscription successfully sent email alert to " . $aSubscribers[$i]->getEmail() . " for subscriptionID=" . $aSubscribers[$i]->getID()); - } else { - $default->log->error("SubscriptionEngine::fireSubscription failed sending email alert to " . $aSubscribers[$i]->getEmail() . " for subscriptionID=" . $aSubscribers[$i]->getID() . "; text=$sAlertContent"); - } - } - - // if sms notification is enabled, sms them - if ($aSubscribers[$i]->getSmsNotification() && strlen($aSubscribers[$i]->getMobile()) > 0) { - $oSms = new SMSAlert($aSubscribers[$i]->getMobile(), $sAlertContent["text"]); - if ($oSms->send()) { - $iSubscriptionsSent++; - $default->log->debug("SubscriptionEngine::fireSubscription successfully sent sms for folderID=$iFolderID, subscriber=" . $aSubscribers[$i]->getID() . "; text=$sNotificationText"); - } else { - $default->log->error("SubscriptionEngine::fireSubscription failed sending sms for folderID=$iFolderID, subscriber=" . $aSubscribers[$i]->getID() . "; text=$sNotificationText"); - } - } - } else { - $default->log->error("SubscriptionEngine::fireSubscription could not update subscription- db error?"); - } - } - - if ($iSubscriptionType == SubscriptionConstants::subscriptionType("FolderSubscription")) { - $oFolder =& Folder::get($iExternalID); - if ($oFolder && !PEAR::isError($oFolder)) { - $iThisSubscriptionsSent = SubscriptionEngine::fireSubscription($oFolder->getParentId(), - ($iSubscriptionAlertType == SubscriptionConstants::subscriptionAlertType("RemoveSubscribedDocument") ? - SubscriptionConstants::subscriptionAlertType("RemoveChildDocument") : - $iSubscriptionAlertType), - SubscriptionConstants::subscriptionType("FolderSubscription"), - $aValues); - $default->log->info("SubscriptionEngine::fireSubscription fired folder subscribers, count=$iThisSubscriptionsSent"); - $iSubscriptionsSent += $iThisSubscriptionsSent; - } - } - - // return the number of processed subscriptions - return $iSubscriptionsSent; - } - - /** - * Retrieves the users that are subscribed to this subscription content - * - * @param integer the ID of the subscription content to retrieve subscribers for - * @param int the subscription content type (folder, document) - * @return array of users objects representing the subscribers, false on error - */ - function retrieveSubscribers($iExternalID, $iSubscriptionType) { - global $default; - global $aAlreadySent; - if (!isset($aAlreadySent)) { - $aAlreadySent = array(); - } - - $sql = $default->db; - $aUsers = array(); - $default->log->debug("retrieveSubscribers(id=$iExternalID, type=$iSubscriptionType); table=" .Subscription::getTableName($iSubscriptionType). "; id=" .Subscription::getIdFieldName($iSubscriptionType)); - $sQuery = "SELECT user_id FROM " . Subscription::getTableName($iSubscriptionType) . " WHERE " . Subscription::getIdFieldName($iSubscriptionType) . " = ?";/*ok*/ - $aParams = array($iExternalID); - if ($sql->query(array($sQuery, $aParams))) { - while ($sql->next_record()) { - $iUserID = $sql->f("user_id"); - if (in_array($iUserID, $aAlreadySent)) { - continue; - } - $aAlreadySent[] = $iUserID; - $oUser = & User::get($iUserID ); - if ($oUser) { - $aUsers[] = $oUser; - } else { - $default->log->error("SubscriptionEngine::fireSubscription subscriber id=$iUserID doesn't exist"); - // remove this users subscription - if ($sql->query("DELETE FROM " . Subscription::getTableName($iSubscriptionType) . " " . - "WHERE user_id = $iUserID")) { - $default->log->info("SubscriptionEngine::fireSubscription successfully removed subscription for removed user id=$iUserID"); - } else { - $default->log->error("SubscriptionEngine::fireSubscription error removing subscription for user id=$iUserID"); - } - } - } - } else { - $_SESSION["errorMessage"] = $lang_err_database; - return false; - } - $default->log->debug('retrieveSubscribers found count=' . count($aUsers)); - return $aUsers; - } -} diff --git a/lib/subscriptions/SubscriptionManager.inc b/lib/subscriptions/SubscriptionManager.inc index 073a67d..a3d1ba8 100644 --- a/lib/subscriptions/SubscriptionManager.inc +++ b/lib/subscriptions/SubscriptionManager.inc @@ -1,5 +1,8 @@ fileSystemRoot/lib/subscriptions/Subscription.inc"); + +require_once(KT_LIB_DIR . "/subscriptions/Subscription.inc"); +require_once(KT_LIB_DIR . "/subscriptions/subscriptions.inc.php"); + /** * $Id$ * @@ -136,8 +139,8 @@ class SubscriptionManager { * @return array of subscription objects */ function listSubscriptions($iUserID) { - return $aSubscriptions = array_merge(SubscriptionManager::retrieveUserSubscriptions($iUserID, SubscriptionConstants::subscriptionType("FolderSubscription")), - SubscriptionManager::retrieveUserSubscriptions($iUserID, SubscriptionConstants::subscriptionType("DocumentSubscription"))); + return $aSubscriptions = array_merge(SubscriptionManager::retrieveUserSubscriptions($iUserID, SubscriptionEvent::subTypes('Folder')), + SubscriptionManager::retrieveUserSubscriptions($iUserID, SubscriptionEvent::subTypes('Document'))); } /** @@ -170,8 +173,8 @@ class SubscriptionManager { * @return array of subscription objects */ function listSubscriptionAlerts($iUserID) { - return $aSubscriptions = array_merge(SubscriptionManager::retrieveSubscriptionAlerts($iUserID, SubscriptionConstants::subscriptionType("FolderSubscription")), - SubscriptionManager::retrieveSubscriptionAlerts($iUserID, SubscriptionConstants::subscriptionType("DocumentSubscription"))); + return $aSubscriptions = array_merge(SubscriptionManager::retrieveSubscriptionAlerts($iUserID, SubscriptionEvent::subTypes("Folder")), + SubscriptionManager::retrieveSubscriptionAlerts($iUserID, SubscriptionEvent::subTypes("Document"))); } /** diff --git a/lib/subscriptions/subscriptions.inc.php b/lib/subscriptions/subscriptions.inc.php new file mode 100644 index 0000000..8645071 --- /dev/null +++ b/lib/subscriptions/subscriptions.inc.php @@ -0,0 +1,756 @@ + Jam Warehouse Software (Pty) Ltd. + */ + +require_once(KT_LIB_DIR . "/database/dbutil.inc"); +require_once(KT_LIB_DIR . "/subscriptions/Subscription.inc"); +require_once(KT_LIB_DIR . "/users/User.inc"); +require_once(KT_LIB_DIR . "/dashboard/Notification.inc.php"); +require_once(KT_LIB_DIR . "/alert/delivery/EmailAlert.inc"); + +require_once(KT_LIB_DIR . "/templating/templating.inc.php"); + +class SubscriptionEvent { + var $eventTypes = array( + "AddFolder", + "RemoveSubscribedFolder", + "RemoveChildFolder", + "AddDocument", + "RemoveSubscribedDocument", + "RemoveChildDocument", + "ModifyDocument", + "CheckInDocument", + "CheckOutDocument", + "MovedDocument", + "ArchivedDocument", + "RestoredArchivedDocument", + ); + + var $subscriptionTypes = array( + "Document" => 1, + "Folder" => 2, + ); + + function &subTypes($sType) { + $subscriptionTypes = array( + "Document" => 1, + "Folder" => 2, + ); + + return KTUtil::arrayGet($subscriptionTypes, $sType, null); + } + + var $alertedUsers = array(); // per-instance (e.g. per-event) list of users who were contacted. + var $_parameters = array(); // internal storage for + var $child = -1; // the child object-id (e.g. which initiated the event: document OR folder) + var $parent = -1; // the folder-id of the parent + + // FIXME stubs. + /* Each of these functions handles appropriate propogation (e.g. both + * folder and document subscription) without calling secondary functions. + * Every attempt is made to be as explicit as possible. + */ + + // alerts users who are subscribed to $iParentFolderId. + function AddFolder($oAddedFolder, $oParentFolder) { + $content = new SubscriptionContent(); // needed for i18n + + // only useful for folder subscriptions. + $aUsers = $this->_getSubscribers($oParentFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oAddedFolder->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oAddedFolder->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "AddFolder"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + } + function AddDocument ($oAddedDocument, $oParentFolder) { + $content = new SubscriptionContent(); // needed for i18n + // two parts to this: + $aUsers = $this->_getSubscribers($oParentFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null - is this valid? + $aNotificationOptions['target_name'] = $oAddedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oAddedDocument->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "AddDocument"; + + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + } + function RemoveFolder($oRemovedFolder, $oParentFolder) { + $content = new SubscriptionContent(); // needed for i18n + // two cases to consider here: + // - notify people who are subscribed to the parent folder. + // - notify and unsubscribe people who are subscribed to the actual folder. + + // we need to start with the latter, so we don't "lose" any. + $aUsers = $this->_getSubscribers($oRemovedFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oRemovedFolder->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oParentFolder->getId(); // parent folder_id, since the removed one is removed. + $aNotificationOptions['event_type'] = "RemoveSubscribedFolder"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + + // now grab each oSubscribers oSubscription, and delete. + $oSubscription = Subscription::getByIds($oSubscriber->getId(), $oRemovedFolder->getId(), $this->subscriptionTypes["Folder"]); + if (!(PEAR::isError($oSubscription) || ($oSubscription == false))) { + $oSubscription->delete(); + } + } + + // now handle (for those who haven't been alerted) users watching the folder. + $aUsers = $this->_getSubscribers($oParentFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oRemovedFolder->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oParentFolder->getId(); // parent folder_id, since the removed one is removed. + $aNotificationOptions['event_type'] = "RemoveChildFolder"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + + } + function RemoveDocument($oRemovedDocument, $oParentFolder) { + $content = new SubscriptionContent(); // needed for i18n + // two cases to consider here: + // - notify people who are subscribed to the parent folder. + // - notify and unsubscribe people who are subscribed to the actual folder. + + // we need to start with the latter, so we don't "lose" any. + $aUsers = $this->_getSubscribers($oRemovedDocument->getId(), $this->subscriptionTypes["Document"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oRemovedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oParentFolder->getId(); // parent folder_id, since the removed one is removed. + $aNotificationOptions['event_type'] = "RemoveSubscribedDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + + // now grab each oSubscribers oSubscription, and delete. + $oSubscription = Subscription::getByIds($oSubscriber->getId(), $oRemovedDocument->getId(), $this->subscriptionTypes["Document"]); + if (!(PEAR::isError($oSubscription) || ($oSubscription == false))) { + $oSubscription->delete(); + } + } + + // now handle (for those who haven't been alerted) users watching the folder. + $aUsers = $this->_getSubscribers($oParentFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oRemovedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oParentFolder->getId(); // parent folder_id, since the removed one is removed. + $aNotificationOptions['event_type'] = "RemoveChildDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + + } + function ModifyDocument($oModifiedDocument, $oParentFolder) { + $content = new SubscriptionContent(); // needed for i18n + // OK: two actions: document registrants, folder registrants. + $aUsers = $this->_getSubscribers($oModifiedDocument->getId(), $this->subscriptionTypes["Document"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oModifiedDocument->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "ModifyDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + + + $aUsers = $this->_getSubscribers($oParentFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oModifiedDocument->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "ModifyDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + } + function CheckinDocument($oModifiedDocument, $oParentFolder) { + $content = new SubscriptionContent(); // needed for i18n + // OK: two actions: document registrants, folder registrants. + $aUsers = $this->_getSubscribers($oModifiedDocument->getId(), $this->subscriptionTypes["Document"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oModifiedDocument->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "CheckinDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + + + $aUsers = $this->_getSubscribers($oParentFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oModifiedDocument->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "CheckinDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + + } + } + } + function CheckoutDocument($oModifiedDocument, $oParentFolder) { + $content = new SubscriptionContent(); // needed for i18n + // OK: two actions: document registrants, folder registrants. + $aUsers = $this->_getSubscribers($oModifiedDocument->getId(), $this->subscriptionTypes["Document"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oModifiedDocument->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "CheckoutDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + + + $aUsers = $this->_getSubscribers($oParentFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oModifiedDocument->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "CheckoutDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + } + function MoveDocument($oMovedDocument, $oToFolder, $oFromFolder) { + $content = new SubscriptionContent(); // needed for i18n + // OK: two actions: document registrants, folder registrants. + $aUsers = $this->_getSubscribers($oModifiedDocument->getId(), $this->subscriptionTypes["Document"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oToFolder->getName(); + $aNotificationOptions['object_id'] = $oToFolder->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "MovedDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + + + $aUsers = $this->_getSubscribers($oFromFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oToFolder->getName(); + $aNotificationOptions['object_id'] = $oToFolder->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "MovedDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + + $aUsers = $this->_getSubscribers($oToFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oToFolder->getName(); + $aNotificationOptions['object_id'] = $oToFolder->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "MovedDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + } + function ArchivedDocument($oModifiedDocument, $oParentFolder) { + $content = new SubscriptionContent(); // needed for i18n + // OK: two actions: document registrants, folder registrants. + $aUsers = $this->_getSubscribers($oModifiedDocument->getId(), $this->subscriptionTypes["Document"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oModifiedDocument->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "ArchivedDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + + + $aUsers = $this->_getSubscribers($oParentFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oModifiedDocument->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "ArchivedDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + } + + function RestoreDocument($oModifiedDocument, $oParentFolder) { + $content = new SubscriptionContent(); // needed for i18n + // OK: two actions: document registrants, folder registrants. + $aUsers = $this->_getSubscribers($oModifiedDocument->getId(), $this->subscriptionTypes["Document"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oModifiedDocument->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "RestoreArchivedDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + + + $aUsers = $this->_getSubscribers($oParentFolder->getId(), $this->subscriptionTypes["Folder"]); + $aUsers = $this->_pruneAlertedUsers($aUsers); // setup the alerted users. _might_ be a singleton. + foreach ($aUsers as $oSubscriber) { + + // notification object first. + $aNotificationOptions = array(); + $aNotificationOptions['target_user'] = $oSubscriber->getID(); + $aNotificationOptions['actor_id'] = KTUtil::arrayGet($_SESSION,"userID", null); // _won't_ be null. + $aNotificationOptions['target_name'] = $oModifiedDocument->getName(); + $aNotificationOptions['location_name'] = $oParentFolder->getName(); + $aNotificationOptions['object_id'] = $oModifiedDocument->getId(); // parent folder_id, in this case. + $aNotificationOptions['event_type'] = "RestoreArchivedDocument"; + $oNotification =& KTSubscriptionNotification::generateSubscriptionNotification($aNotificationOptions); + + // now the email content. + // FIXME this needs to be handled entirely within notifications from now on. + if ($oSubscriber->getEmailNotification() && (strlen($oSubscriber->getEmail()) > 0)) { + $emailContent = $content->getEmailAlertContent($oNotification); + $emailSubject = $content->getEmailAlertSubject($oNotification); + $oEmail = new EmailAlert($oSubscriber->getEmail(), $emailSubject, $emailContent); + $oEmail->send(); + } + } + } + + // small helper function to assist in identifying the numeric id. + function _getKeyForType($sEventType) { + foreach ($this->eventTypes as $key => $val) { + if ($val == $sSubType) { return $key; } + } + return -1; + } + + // helper function to get & adjust the $alertedUsers + // note that this has side-effects: $this->alertedUsers is a merged version + // after this has been called. + function _pruneAlertedUsers($aUserIds) { + $returnArray = array_diff($aUserIds, $this->alertedUsers); + $this->alertedUsers = array_merge($returnArray, $this->alertedUsers); // now contains all users who will have been alerted. + return $returnArray; + } + + // gets subscribers to object, with appropriate type (e.g. folder or document). + // need the second part because docs and folders have separate ids. + // based on the old SubscriptionEngine::retrieveSubscribers. + function _getSubscribers($iObjectId, $iSubType) { + global $default; // for the logging. + $default->log->debug("_getSubscribers(id=$iObjectId, type=$iSubType); table=" .Subscription::getTableName($iSubType). "; id=" .Subscription::getIdFieldName($iSubType)); + + $aUsers = array(); + $sQuery = "SELECT user_id FROM " . Subscription::getTableName($iSubType) . " WHERE " . Subscription::getIdFieldName($iSubType) . " = ?"; + $aParams = array($iObjectId); + + $aNewUsers = DBUtil::getResultArrayKey(array($sQuery, $aParams), "user_id"); + + // notionally less efficient than the old code. if its a big issue, can easily + // be refactored. + $default->log->error("SubscriptionEvent:: " . print_r($iSubType, true)); + foreach ($aNewUsers as $iUserId) { + $oUser = & User::get($iUserId); + + // do a quick prune here, for performance/maintenance reasons. + if (PEAR::isError($oUser) || ($oUser == false)) { + $sQuery = "DELETE FROM " . Subscription::getTableName($iSubType) . " WHERE user_id = ?"; + $aParams = array($iUserId); + DBUtil::runQuery(array($sQuery, $sParams)); + $default->log->error("SubscriptionEvent::fireSubscription error removing subscription for user id=$iUserID"); + } else { + $aUsers[] = $oUser; + } + } + + $default->log->debug('retrieveSubscribers found count=' . count($aUsers)); + return $aUsers; + } +} + +// interesting: how do we want to generate email & notification content? +// first suggestion: +// - generate this content here. +// - alternatively, generate the content inside the notification environment (for part 2). + +/* very simple class to handle and hold the various and sundry event types content for emails. */ +class SubscriptionContent { + // have to be instantiated, or the i18n can't work. + function SubscriptionContent() { + $this->_eventTypeNames = array( + "AddFolder" => _('Folder added'), + "RemoveSubscribedFolder" => _('Folder removed'), // nothing. your subscription is now gone. + "RemoveChildFolder" => _('Folder removed'), + "AddDocument" => _('Document added'), + "RemoveSubscribedDocument" => _('Document removed'), // nothing. your subscription is now gone. + "RemoveChildDocument" => _('Document removed'), + "ModifyDocument" => _('Document modified'), + "CheckInDocument" => _('Document checked in'), + "CheckOutDocument" => _('Document checked out'), + "MovedDocument" => _('Document moved'), + "ArchivedDocument" => _('Document archived'), // can go through and request un-archival (?) + "RestoredArchivedDocument" => _('Document restored') + ); + } + + function getEmailAlertContent($oKTNotification) { + // we can re-use the normal template. + // however, we need to wrap it - no need for a second template here. + $str = '' . $this->getNotificationAlertContent($oKTNotification) . ''; + return $str; + } + + function getEmailAlertSubject($oKTNotification) { + $info = $this->_getSubscriptionData($oKTNotification); + return $info["title"]; + } + + function getNotificationAlertContent($oKTNotification) { + $info = $this->_getSubscriptionData($oKTNotification); + $oTemplating = new KTTemplating; + + $oTemplate = $oTemplating->loadTemplate("kt3/notifications/subscriptions." . $info['event_type']); + // if, for some reason, this doesn't actually work, use the "generic" title. + if (PEAR::isError($oTemplate)) { + $oTemplate = $oTemplating->loadTemplate("kt3/notifications/subscriptions.generic"); + } + // FIXME we need to specify the i18n by user. + $aTemplateData = array( + "context" => $oKTNotification, + "info" => $info, + ); + return $oTemplate->render($aTemplateData); + } + // no separate subject function, its rolled into get...Content() + + var $_eventObjectMap = array( + "AddFolder" => 'folder', + "RemoveSubscribedFolder" => '', // nothing. your subscription is now gone. + "RemoveChildFolder" => 'folder', + "AddDocument" => 'document', + "RemoveSubscribedDocument" => '', // nothing. your subscription is now gone. + "RemoveChildDocument" => 'folder', + "ModifyDocument" => 'document', + "CheckInDocument" => 'document', + "CheckOutDocument" => 'document', + "MovedDocument" => 'document', + "ArchivedDocument" => 'document', // can go through and request un-archival (?) + "RestoredArchivedDocument" => 'document'); + + + + function _getSubscriptionData($oKTNotification) { + $info = array( + 'object_name' => $oKTNotification->getLabel(), + 'event_type' => $oKTNotification->getStrData1(), + 'location_name' => $oKTNotification->getStrData2(), + 'object_id' => $oKTNotification->getIntData1(), + 'actor_id' => $oKTNotification->getIntData2(), + 'has_actor' => false, + 'notify_id' => $oKTNotification->getId(), + ); + + $info['title'] = KTUtil::arrayGet($this->_eventTypeNames, $info['event_type'], 'Subscription alert:') .': ' . $info['object_name']; + + if ($info['actor_id'] !== null) { + $oTempUser = User::get($info['actor_id']); + if (PEAR::isError($oTempUser) || ($oTempUser == false)) { + // no-act + $info['actor'] = null; + } else { + $info['actor'] = $oTempUser; + $info['has_actor'] = true; + } + } + + if ($info['object_id'] !== null) { + $info['object'] = $this->_getEventObject($info['event_type'], $info['object_id']); + } + + return $info; + } + + // resolve the object type based on the alert type. + function _getEventObject($sAlertType, $id) { + $t = KTUtil::arrayGet($this->_eventObjectMap, $sAlertType ,''); + if ($t == 'document') { + $o = Document::get($id); + if (PEAR::isError($o) || ($o == false)) { return null; + } else { return $o; } + } else if ($t == 'folder') { + $o = Folder::get($id); + if (PEAR::isError($o) || ($o == false)) { return null; + } else { return $o; } + } else { + return null; + } + } + + function _getEventObjectType($sAlertType) { + return KTUtil::arrayGet($this->_eventObjectMap, $sAlertType ,''); + } +} + +?> \ No newline at end of file diff --git a/plugins/ktstandard/KTSubscriptions.php b/plugins/ktstandard/KTSubscriptions.php index d6c2e39..5f48b39 100644 --- a/plugins/ktstandard/KTSubscriptions.php +++ b/plugins/ktstandard/KTSubscriptions.php @@ -1,9 +1,10 @@ oUser->getID(), $this->oDocument->getID(), SubscriptionConstants::subscriptionType("DocumentSubscription"))) { + if (Subscription::exists($this->oUser->getID(), $this->oDocument->getID(), SubscriptionEvent::subTypes('Document'))) { return null; } return parent::getInfo(); } function do_main() { - $iSubscriptionType = SubscriptionConstants::subscriptionType("DocumentSubscription"); + $iSubscriptionType = SubscriptionEvent::subTypes('Document'); if (Subscription::exists($this->oUser->getId(), $this->oDocument->getId(), $iSubscriptionType)) { $_SESSION['KTErrorMessage'][] = _("You are already subscribed to that document"); } else { @@ -106,14 +107,14 @@ class KTDocumentUnsubscriptionAction extends KTDocumentAction { var $sName = 'ktstandard.subscription.documentunsubscription'; var $sDisplayName = 'Unsubscribe from document'; function getInfo() { - if (Subscription::exists($this->oUser->getID(), $this->oDocument->getID(), SubscriptionConstants::subscriptionType("DocumentSubscription"))) { + if (Subscription::exists($this->oUser->getID(), $this->oDocument->getID(), SubscriptionEvent::subTypes('Document'))) { return parent::getInfo(); } return null; } function do_main() { - $iSubscriptionType = SubscriptionConstants::subscriptionType("DocumentSubscription"); + $iSubscriptionType = SubscriptionEvent::subTypes('Document'); if (!Subscription::exists($this->oUser->getId(), $this->oDocument->getId(), $iSubscriptionType)) { $_SESSION['KTErrorMessage'][] = _("You were not subscribed to that document"); } else { @@ -143,11 +144,12 @@ class KTCheckoutSubscriptionTrigger { global $default; $oDocument =& $this->aInfo["document"]; // fire subscription alerts for the checked out document - $count = SubscriptionEngine::fireSubscription($oDocument->getId(), SubscriptionConstants::subscriptionAlertType("CheckOutDocument"), - SubscriptionConstants::subscriptionType("DocumentSubscription"), - array( "folderID" => $oDocument->getFolderID(), - "modifiedDocumentName" => $oDocument->getName() )); - $default->log->info("checkOutDocumentBL.php fired $count subscription alerts for checked out document " . $oDocument->getName()); + + // fire subscription alerts for the checked in document + $oSubscriptionEvent = new SubscriptionEvent(); + $oFolder = Folder::get($oDocument->getFolderID()); + $oSubscriptionEvent->CheckoutDocument($oDocument, $oFolder); + } } $oPlugin->registerTrigger('checkout', 'postValidate', 'KTCheckoutSubscriptionTrigger', 'ktstandard.triggers.subscription.checkout'); @@ -165,22 +167,11 @@ class KTDeleteSubscriptionTrigger { $oDocument =& $this->aInfo["document"]; // fire subscription alerts for the deleted document - $count = SubscriptionEngine::fireSubscription($oDocument->getId(), - SubscriptionConstants::subscriptionAlertType("RemoveSubscribedDocument"), - SubscriptionConstants::subscriptionType("DocumentSubscription"), - array( - "folderID" => $oDocument->getFolderID(), - "removedDocumentName" => $oDocument->getName(), - "folderName" => Folder::getFolderDisplayPath($oDocument->getFolderID()), - )); - $default->log->info("deleteDocumentBL.php fired $count subscription alerts for removed document " . $oDocument->getName()); - - // remove all document subscriptions for this document - if (SubscriptionManager::removeSubscriptions($oDocument->getId(), SubscriptionConstants::subscriptionType("DocumentSubscription"))) { - $default->log->info("deleteDocumentBL.php removed all subscriptions for this document"); - } else { - $default->log->error("deleteDocumentBL.php couldn't remove document subscriptions"); - } + + // fire subscription alerts for the checked in document + $oSubscriptionEvent = new SubscriptionEvent(); + $oFolder = Folder::get($oDocument->getFolderID()); + $oSubscriptionEvent->RemoveDocument($oDocument, $oFolder); } } $oPlugin->registerTrigger('delete', 'postValidate', 'KTDeleteSubscriptionTrigger', 'ktstandard.triggers.subscription.delete'); @@ -199,29 +190,10 @@ class KTDocumentMoveSubscriptionTrigger { $oOldFolder =& $this->aInfo["old_folder"]; $oNewFolder =& $this->aInfo["new_folder"]; - // fire subscription alerts for the moved document (and the folder its in) - $count = SubscriptionEngine::fireSubscription($oDocument->getId(), SubscriptionConstants::subscriptionAlertType("MovedDocument"), - SubscriptionConstants::subscriptionType("DocumentSubscription"), - array( - "folderID" => $oOldFolder->getId(), - "modifiedDocumentName" => $oDocument->getName(), - "oldFolderName" => Folder::getFolderName($oOldFolder->getId()), - "newFolderName" => Folder::getFolderName($oNewFolder->getID()), - ) - ); - $default->log->info("moveDocumentBL.php fired $count (folderID=$fFolderID) folder subscription alerts for moved document " . $oDocument->getName()); - - // fire folder subscriptions for the destination folder - $count = SubscriptionEngine::fireSubscription($oNewFolder->getId(), SubscriptionConstants::subscriptionAlertType("MovedDocument"), - SubscriptionConstants::subscriptionType("FolderSubscription"), - array( - "folderID" => $oOldFolder->getId(), - "modifiedDocumentName" => $oDocument->getName(), - "oldFolderName" => Folder::getFolderName($oOldFolder->getId()), - "newFolderName" => Folder::getFolderName($oNewFolder->getId()), - ) - ); - $default->log->info("moveDocumentBL.php fired $count (folderID=$fFolderID) folder subscription alerts for moved document " . $oDocument->getName()); + + // fire subscription alerts for the checked in document + $oSubscriptionEvent = new SubscriptionEvent(); + $oSubscriptionEvent->MoveDocument($oDocument, $oNewFolder, $oNewFolder); } } $oPlugin->registerTrigger('moveDocument', 'postValidate', 'KTDocumentMoveSubscriptionTrigger', 'ktstandard.triggers.subscription.moveDocument'); @@ -238,14 +210,10 @@ class KTArchiveSubscriptionTrigger { global $default; $oDocument =& $this->aInfo["document"]; - $count = SubscriptionEngine::fireSubscription($fDocumentID, SubscriptionConstants::subscriptionAlertType("ArchivedDocument"), - SubscriptionConstants::subscriptionType("DocumentSubscription"), - array( - "folderID" => $oDocument->getFolderID(), - "modifiedDocumentName" => $oDocument->getName(), - "folderName" => $oDocument->getFolderName(), - )); - $default->log->info("archiveDocumentBL.php fired $count subscription alerts for archived document " . $oDocument->getName()); + // fire subscription alerts for the checked in document + $oSubscriptionEvent = new SubscriptionEvent(); + $oFolder = Folder::get($oDocument->getFolderID()); + $oSubscriptionEvent->ArchiveDocument($oDocument, $oFolder); } } $oPlugin->registerTrigger('archive', 'postValidate', 'KTArchiveSubscriptionTrigger', 'ktstandard.triggers.subscription.archive'); @@ -256,7 +224,7 @@ class KTFolderSubscriptionAction extends KTFolderAction { var $sName = 'ktstandard.subscription.foldersubscription'; var $sDisplayName = 'Subscribe to folder'; function getInfo() { - if (Subscription::exists($this->oUser->getID(), $this->oFolder->getID(), SubscriptionConstants::subscriptionType("FolderSubscription"))) { + if (Subscription::exists($this->oUser->getID(), $this->oFolder->getID(), SubscriptionEvent::subTypes('Folder'))) { // KTFolderUnsubscriptionAction will display instead. return null; } @@ -264,7 +232,7 @@ class KTFolderSubscriptionAction extends KTFolderAction { } function do_main() { - $iSubscriptionType = SubscriptionConstants::subscriptionType("FolderSubscription"); + $iSubscriptionType = SubscriptionEvent::subTypes('Folder'); if (Subscription::exists($this->oUser->getId(), $this->oFolder->getId(), $iSubscriptionType)) { $_SESSION['KTErrorMessage'][] = _("You are already subscribed to that document"); } else { @@ -289,14 +257,14 @@ class KTFolderUnsubscriptionAction extends KTFolderAction { var $sDisplayName = 'Unsubscribe from folder'; function getInfo() { - if (Subscription::exists($this->oUser->getID(), $this->oFolder->getID(), SubscriptionConstants::subscriptionType("FolderSubscription"))) { + if (Subscription::exists($this->oUser->getID(), $this->oFolder->getID(), SubscriptionEvent::subTypes('Folder'))) { return parent::getInfo(); } return null; } function do_main() { - $iSubscriptionType = SubscriptionConstants::subscriptionType("FolderSubscription"); + $iSubscriptionType = SubscriptionEvent::subTypes('Folder'); if (!Subscription::exists($this->oUser->getId(), $this->oFolder->getId(), $iSubscriptionType)) { $_SESSION['KTErrorMessage'][] = _("You were not subscribed to that folder"); } else { @@ -320,9 +288,9 @@ class KTSubscriptionManagePage extends KTStandardDispatcher { function do_main() { $this->aBreadcrumbs[] = array("name" => _("Subscription Management")); $aFolderSubscriptions = SubscriptionManager::retrieveUserSubscriptions( - $this->oUser->getId(), SubscriptionConstants::subscriptionType("FolderSubscription")); + $this->oUser->getId(), SubscriptionEvent::subTypes('Folder')); $aDocumentSubscriptions = SubscriptionManager::retrieveUserSubscriptions( - $this->oUser->getId(), SubscriptionConstants::subscriptionType("DocumentSubscription")); + $this->oUser->getId(), SubscriptionEvent::subTypes('Document')); $bNoSubscriptions = ((count($aFolderSubscriptions) == 0) && (count($aDocumentSubscriptions) == 0)) ? true : false; $oTemplate = $this->oValidator->validateTemplate('ktstandard/subscriptions/manage'); @@ -347,7 +315,7 @@ class KTSubscriptionManagePage extends KTStandardDispatcher { if (!empty($foldersubscriptions)) { foreach ($foldersubscriptions as $iSubscriptionId) { - $oSubscription = Subscription::get($iSubscriptionId, SubscriptionConstants::subscriptionType('FolderSubscription')); + $oSubscription = Subscription::get($iSubscriptionId, SubscriptionEvent::subTypes('Folder')); if ($oSubscription) { $oSubscription->delete(); $iSuccesses++; @@ -359,7 +327,7 @@ class KTSubscriptionManagePage extends KTStandardDispatcher { if (!empty($documentsubscriptions)) { foreach ($documentsubscriptions as $iSubscriptionId) { - $oSubscription = Subscription::get($iSubscriptionId, SubscriptionConstants::subscriptionType('DocumentSubscription')); + $oSubscription = Subscription::get($iSubscriptionId, SubscriptionEvent::subTypes('Document')); if ($oSubscription) { $oSubscription->delete(); $iSuccesses++; diff --git a/templates/kt3/notifications/subscriptions.smarty b/templates/kt3/notifications/subscriptions.AddDocument.smarty index 9f82a76..7b8779b 100644 --- a/templates/kt3/notifications/subscriptions.smarty +++ b/templates/kt3/notifications/subscriptions.AddDocument.smarty @@ -1,15 +1,9 @@
{$info.title}
-{if ($info.event_type == 'AddDocument')} The document "{$info.object_name}" was added{if ($info.location_name !== null)} to "{$info.location_name}"{/if}. -{else} -{/if} -
diff --git a/templates/kt3/notifications/subscriptions.generic.smarty b/templates/kt3/notifications/subscriptions.generic.smarty new file mode 100644 index 0000000..d1dc1c1 --- /dev/null +++ b/templates/kt3/notifications/subscriptions.generic.smarty @@ -0,0 +1,6 @@ +
{$info.title}
+
+ +