Commit 2562514ee08ca50e57c35b12ba0b941e4a3ecae6
1 parent
50982cc5
updated dependencies, fixed subscription firing bugs
git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@897 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
38 additions
and
40 deletions
lib/subscriptions/SubscriptionEngine.inc
| @@ -2,9 +2,10 @@ | @@ -2,9 +2,10 @@ | ||
| 2 | require_once("$default->owl_fs_root/lib/users/User.inc"); | 2 | require_once("$default->owl_fs_root/lib/users/User.inc"); |
| 3 | require_once("$default->owl_fs_root/lib/documentmanagement/Document.inc"); | 3 | require_once("$default->owl_fs_root/lib/documentmanagement/Document.inc"); |
| 4 | require_once("$default->owl_fs_root/lib/foldermanagement/Folder.inc"); | 4 | require_once("$default->owl_fs_root/lib/foldermanagement/Folder.inc"); |
| 5 | -require_once("$default->owl_fs_root/lib/subscriptions/DocumentSubscription.inc"); | ||
| 6 | -require_once("$default->owl_fs_root/lib/subscriptions/FolderSubscription.inc"); | ||
| 7 | -require_once("$default->owl_fs_root/lib/email/Email.inc"); | 5 | +require_once("$default->owl_fs_root/lib/subscriptions/Subscription.inc"); |
| 6 | +require_once("$default->owl_fs_root/lib/subscriptions/AlertContent.inc"); | ||
| 7 | +require_once("$default->owl_fs_root/lib/subscriptions/alert/EmailAlert.inc"); | ||
| 8 | +require_once("$default->owl_fs_root/lib/subscriptions/alert/SMSAlert.inc"); | ||
| 8 | 9 | ||
| 9 | /** | 10 | /** |
| 10 | * | 11 | * |
| @@ -21,31 +22,6 @@ require_once("$default->owl_fs_root/lib/email/Email.inc"); | @@ -21,31 +22,6 @@ require_once("$default->owl_fs_root/lib/email/Email.inc"); | ||
| 21 | */ | 22 | */ |
| 22 | class SubscriptionEngine { | 23 | class SubscriptionEngine { |
| 23 | 24 | ||
| 24 | - | ||
| 25 | - /* | ||
| 26 | - fireSubscription($iFolderID, SubscriptionConstants::subscriptionAlertType("AddFolder"), | ||
| 27 | - SubscriptionConstants::subscriptionType("FolderSubscription"), | ||
| 28 | - array( "newFolderName" =>, | ||
| 29 | - "parentFolderName" =>)); | ||
| 30 | - | ||
| 31 | - fireSubscription($iFolderID, SubscriptionConstants::subscriptionAlertType("RemoveFolder"), | ||
| 32 | - SubscriptionConstants::subscriptionType("FolderSubscription"), | ||
| 33 | - array( "removedFolderName" =>, | ||
| 34 | - "parentFolderName" =>)); | ||
| 35 | - fireSubscription($iFolderID, SubscriptionConstants::subscriptionAlertType("AddDocument"), | ||
| 36 | - SubscriptionConstants::subscriptionType("FolderSubscription"), | ||
| 37 | - array( "newDocumentName" =>, | ||
| 38 | - "folderName" =>)); | ||
| 39 | - | ||
| 40 | - fireSubscription($iFolderID, SubscriptionConstants::subscriptionAlertType("RemoveDocument"), | ||
| 41 | - SubscriptionConstants::subscriptionType("FolderSubscription"), | ||
| 42 | - array( "removedDocumentName" =>, | ||
| 43 | - "folderName" =>)); | ||
| 44 | - | ||
| 45 | - fireSubscription($iFolderID, SubscriptionConstants::subscriptionAlertType("ModifyDocument"), | ||
| 46 | - SubscriptionConstants::subscriptionType("DocumentSubscription"), | ||
| 47 | - array( "modifiedDocumentName" =>, )); | ||
| 48 | - */ | ||
| 49 | /** | 25 | /** |
| 50 | * Fires a subscription alert for this subscription content | 26 | * Fires a subscription alert for this subscription content |
| 51 | * | 27 | * |
| @@ -55,54 +31,75 @@ class SubscriptionEngine { | @@ -55,54 +31,75 @@ class SubscriptionEngine { | ||
| 55 | * @param array any dynamic values that should be sent with the alert (eg. document name, path to modified document) | 31 | * @param array any dynamic values that should be sent with the alert (eg. document name, path to modified document) |
| 56 | */ | 32 | */ |
| 57 | function fireSubscription($iExternalID, $iSubscriptionAlertType, $iSubscriptionType, $aValues) { | 33 | function fireSubscription($iExternalID, $iSubscriptionAlertType, $iSubscriptionType, $aValues) { |
| 34 | + global $default; | ||
| 35 | + | ||
| 58 | // get the list of subscriber addresses that we need to alert | 36 | // get the list of subscriber addresses that we need to alert |
| 59 | - $aSubscribers = retrieveSubscribers($iExternalID, $iSubscriptionType); | 37 | + $aSubscribers = SubscriptionEngine::retrieveSubscribers($iExternalID, $iSubscriptionType); |
| 38 | + | ||
| 39 | + // count the number of subscriptions we've sent | ||
| 40 | + $iSubscriptionsSent = 0; | ||
| 60 | 41 | ||
| 61 | - // if the subscription type is document, add the folder subscriber also | 42 | + // if the subscription type is document, fire the folder subscriptions also |
| 62 | if ($iSubscriptionType == SubscriptionConstants::subscriptionType("DocumentSubscription")) { | 43 | if ($iSubscriptionType == SubscriptionConstants::subscriptionType("DocumentSubscription")) { |
| 63 | - $aSubscribers = array_merge($aSubscribers, retrieveSubscribers($iExternalID, SubscriptionConstants::subscriptionType("FolderSubscription"))); | 44 | + $oDocument = & Document::get($iExternalID); |
| 45 | + $default->log->debug("document=$oDocument"); | ||
| 46 | + $iSubscriptionsSent = SubscriptionEngine::fireSubscription($oDocument->getFolderID(), | ||
| 47 | + $iSubscriptionAlertType, | ||
| 48 | + SubscriptionConstants::subscriptionType("FolderSubscription"), | ||
| 49 | + $aValues); | ||
| 50 | + $default->log->info("fired folder subscribers, count=$iSubscriptionsSent"); | ||
| 64 | } | 51 | } |
| 52 | + $default->log->info("subscribers=" . arrayToString($aSubscribers)); | ||
| 65 | 53 | ||
| 66 | // for each subscriber, construct an address based on their notification preferences | 54 | // for each subscriber, construct an address based on their notification preferences |
| 67 | for ($i=0; $i<count($aSubscribers); $i++) { | 55 | for ($i=0; $i<count($aSubscribers); $i++) { |
| 68 | - // lookup the subscription (just for the ID) | 56 | + $default->log->info("looking up subscription subID=" . $aSubscribers[$i]->getID() . " ID=$iExternalID; type=$iSubscriptionType"); |
| 57 | + // lookup the subscription | ||
| 69 | $oSubscription = & Subscription::getByIDs($aSubscribers[$i]->getID(), $iExternalID, $iSubscriptionType); | 58 | $oSubscription = & Subscription::getByIDs($aSubscribers[$i]->getID(), $iExternalID, $iSubscriptionType); |
| 70 | 59 | ||
| 71 | // update the alerted status | 60 | // update the alerted status |
| 72 | $oSubscription->setIsAlerted(true); | 61 | $oSubscription->setIsAlerted(true); |
| 73 | 62 | ||
| 63 | + // write it back to the db | ||
| 74 | if ($oSubscription->update()) { | 64 | if ($oSubscription->update()) { |
| 75 | 65 | ||
| 76 | - // create the link to this subscription | ||
| 77 | - $aValues["viewAlertLink"] = generateControllerUrl("viewAlert", "fSubscriptionID=" . $oSubscription->getID()); | ||
| 78 | - // supply the subscriber name | 66 | + // get the subscription id |
| 67 | + $aValues["subscriptionID"] = $oSubscription->getID(); | ||
| 68 | + // and subscriber name | ||
| 79 | $aValues["subscriberName"] = $aSubscribers[$i]->getName(); | 69 | $aValues["subscriberName"] = $aSubscribers[$i]->getName(); |
| 80 | 70 | ||
| 81 | // retrieve the appropriate content | 71 | // retrieve the appropriate content |
| 72 | + // using the values array to customise the notification message | ||
| 82 | $sAlertContent = AlertContent::get($iSubscriptionAlertType, $aValues); | 73 | $sAlertContent = AlertContent::get($iSubscriptionAlertType, $aValues); |
| 83 | 74 | ||
| 84 | // construct alerts | 75 | // construct alerts |
| 85 | if ($aSubscribers[$i]->getEmailNotification() && (strlen($aSubscribers[$i]->getEmail()) > 0)) { | 76 | if ($aSubscribers[$i]->getEmailNotification() && (strlen($aSubscribers[$i]->getEmail()) > 0)) { |
| 86 | 77 | ||
| 87 | - $oEmail = new EmailAlert($aSubscribers[$i]->getEmail(), $sAlertContent); | 78 | + $oEmail = new EmailAlert($aSubscribers[$i]->getEmail(), $sAlertContent["subject"], $sAlertContent["text"]); |
| 88 | if ($oEmail->send()) { | 79 | if ($oEmail->send()) { |
| 89 | - $default->log->debug("SubscriptionEngine::fireSubscription successfully sent email for folderID=$iFolderID, subscriber=" . $aSubscribers[$i]->getID() . "; text=$sNotificationText"); | 80 | + $iSubscriptionsSent++; |
| 81 | + $default->log->debug("SubscriptionEngine::fireSubscription successfully sent email alert to " . $aSubscribers[$i]->getEmail() . " for subscriptionID=" . $aSubscribers[$i]->getID()); | ||
| 90 | } else { | 82 | } else { |
| 91 | - $default->log->error("SubscriptionEngine::fireSubscription failed sending email for folderID=$iFolderID, subscriber=" . $aSubscribers[$i]->getID() . "; text=$sNotificationText"); | 83 | + $default->log->error("SubscriptionEngine::fireSubscription failed sending email alert to " . $aSubscribers[$i]->getEmail() . " for subscriptionID=" . $aSubscribers[$i]->getID() . "; text=$sAlertContent"); |
| 92 | } | 84 | } |
| 93 | } | 85 | } |
| 94 | 86 | ||
| 95 | // if sms notification is enabled, sms them | 87 | // if sms notification is enabled, sms them |
| 96 | if ($aSubscribers[$i]->getSmsNotification() && strlen($aSubscribers[$i]->getMobile()) > 0) { | 88 | if ($aSubscribers[$i]->getSmsNotification() && strlen($aSubscribers[$i]->getMobile()) > 0) { |
| 97 | - $oSms = new SMSAlert($aSubscribers[$i]->getMobile(), $sAlertContent); | 89 | + $oSms = new SMSAlert($aSubscribers[$i]->getMobile(), $sAlertContent["text"]); |
| 98 | if ($oSms->send()) { | 90 | if ($oSms->send()) { |
| 91 | + $iSubscriptionsSent++; | ||
| 99 | $default->log->debug("SubscriptionEngine::fireSubscription successfully sent sms for folderID=$iFolderID, subscriber=" . $aSubscribers[$i]->getID() . "; text=$sNotificationText"); | 92 | $default->log->debug("SubscriptionEngine::fireSubscription successfully sent sms for folderID=$iFolderID, subscriber=" . $aSubscribers[$i]->getID() . "; text=$sNotificationText"); |
| 100 | } else { | 93 | } else { |
| 101 | $default->log->error("SubscriptionEngine::fireSubscription failed sending sms for folderID=$iFolderID, subscriber=" . $aSubscribers[$i]->getID() . "; text=$sNotificationText"); | 94 | $default->log->error("SubscriptionEngine::fireSubscription failed sending sms for folderID=$iFolderID, subscriber=" . $aSubscribers[$i]->getID() . "; text=$sNotificationText"); |
| 102 | } | 95 | } |
| 103 | } | 96 | } |
| 97 | + } else { | ||
| 98 | + $default->log->error("SubscriptionEngine::fireSubscription could not update subscription- db error?"); | ||
| 104 | } | 99 | } |
| 105 | } | 100 | } |
| 101 | + // return the number of processed subscriptions | ||
| 102 | + return $iSubscriptionsSent; | ||
| 106 | } | 103 | } |
| 107 | 104 | ||
| 108 | /** | 105 | /** |
| @@ -117,7 +114,8 @@ class SubscriptionEngine { | @@ -117,7 +114,8 @@ class SubscriptionEngine { | ||
| 117 | 114 | ||
| 118 | $sql = $default->db; | 115 | $sql = $default->db; |
| 119 | $aUsers = array(); | 116 | $aUsers = array(); |
| 120 | - if ($sql->query("SELECT user_id from $default->owl_folder_subscriptions_table WHERE folder_id=$iFolderID")) { | 117 | + if ($sql->query("SELECT user_id FROM " . Subscription::getTableName($iSubscriptionType) . " " . |
| 118 | + "WHERE " . Subscription::getIdFieldName($iSubscriptionType) . " = $iExternalID")) { | ||
| 121 | while ($sql->next_record()) { | 119 | while ($sql->next_record()) { |
| 122 | $aUsers[] = & User::get($sql->f("user_id")); | 120 | $aUsers[] = & User::get($sql->f("user_id")); |
| 123 | } | 121 | } |