diff --git a/PivotSubscriptions/EventLog.cls b/PivotSubscriptions/EventLog.cls index ed6b55b..185e028 100644 --- a/PivotSubscriptions/EventLog.cls +++ b/PivotSubscriptions/EventLog.cls @@ -2,11 +2,8 @@ Class PivotSubscriptions.EventLog Extends %Persistent { -/* - * Only possible values are creation, update, deletion, send, and tableError - */ -/// Event Type covers: creation, update, deletion and sent email subscriptions -Property EventType As %String(VALUELIST = ",create,update,delete,sendSubscription,sendError,tableError"); +/// Event Type covers: create, update, delete, sendSubscription, and tableError +Property EventType As %String(VALUELIST = ",create,update,delete,sendSubscription,tableError"); Property StartTime As %TimeStamp [ InitialExpression = {$zdt($h, 3, 1)} ]; @@ -31,7 +28,7 @@ Property Name As %String(MAXLEN = 100); Property Status As %Status; /// Takes in info about an Alert Task and logs the information to the TaskLog -ClassMethod LogEvent(pType, pUser, pPivot, pDayOfWeek, pHour, pMinute, pFormat, pEmails, pName, ByRef pEvent) As %Status +ClassMethod CreateEvent(pType, pUser, pPivot, pDayOfWeek, pHour, pMinute, pFormat, pEmails, pName, ByRef pEvent) As %Status { Set tSC=$$$OK @@ -48,18 +45,13 @@ ClassMethod LogEvent(pType, pUser, pPivot, pDayOfWeek, pHour, pMinute, pFormat, Set tEventLog.Format=pFormat Set tEventLog.Emails=pEmails - If pType="tableError" { - Set tEventLog.Status=$$$OK - Set tEventLog.EndTime=$zdt($h,3,1) - } - Set tSC=tEventLog.%Save() Set pEvent=tEventLog Quit tSC } -Method LogUpdate(pStatus) As %Status +Method UpdateStatus(pStatus) As %Status { Set tSC=$$$OK @@ -70,6 +62,22 @@ Method LogUpdate(pStatus) As %Status Quit tSC } +Method UpdateEvent(pDayOfWeek, pHour, pMinute, pFormat, pEmails, pName) As %Status +{ + Set tSC=$$$OK + + Set ..DayOfWeek=pDayOfWeek + Set ..Hour=pHour + Set ..Minute=pMinute + Set ..Format=pFormat + Set ..Emails=pEmails + Set ..Name=pName + + Set tSC=..%Save() + + Quit tSC +} + ClassMethod DayOfWeekDisplay(pDayOfWeek) As %String [ SqlProc ] { Set pDayOfWeek=$Replace(pDayOfWeek,0,"Sunday") diff --git a/PivotSubscriptions/Installer.cls b/PivotSubscriptions/Installer.cls index b4f2350..65ae448 100644 --- a/PivotSubscriptions/Installer.cls +++ b/PivotSubscriptions/Installer.cls @@ -1,53 +1,53 @@ -Class PivotSubscriptions.Installer -{ - -ClassMethod RunInstaller(pRootDir) -{ - Set tSC=$$$OK - - If pRootDir'="" { - Do $system.OBJ.LoadDir(pRootDir_"/PivotSubscriptions","ck",,1) - Set tSC=..ImportImage(pRootDir_"/Assets/PivotSubscriptions_BookCover.png") - If $$$ISERR(tSC) Quit - } - - Set tItem=##class(%DeepSee.UserLibrary.Link).%New() - Set tItem.fullName="Pivot Subscriptions" - Set tPage="PivotSubscriptions.UI.SubscriptionManager.zen" - Set tItem.href=$system.CSP.GetPortalApp($namespace,tPage)_tPage - Set tItem.title="Pivot Subscriptions" - Set tItem.bookCover="{background: {style:'background:white;',src:'covers/PivotSubscriptions_Cover.png'},header: {text:'$type',style:'display: none;'},title: {text:'$title',style:'display: none;'},image: {style:'display: none;',src:'deepsee/ds2_globe_44.png',imageStyle:'width:64px;height:64px;'},subtitle: {style:'display: none;'},footer: {text:'$owner',style:'display: none;'}}" - Set tSC=tItem.%Save() - - If $$$ISERR(tSC) Quit - - Set tSC=##class(PivotSubscriptions.Task).ConfigureTask() - - Quit tSC -} - -/// Import Cover, code taken from %DeepSee.UI.Dialog.ImageUpload:%OnSubmit -ClassMethod ImportImage(pDir) As %Status -{ - Set tSC=$$$OK - Set tStream=##class(%Stream.FileBinary).%New() - Set tSC=tStream.LinkToFile(pDir) - If $$$ISERR(tSC) Quit - Set tName="PivotSubscriptions_Cover.png" - - If $IsObject(tStream)&&(tName'="") { - Set tFileName=##class(%File).NormalizeDirectory(tName,##class(%DeepSee.UI.Dialog.ImageUpload).%GetImageDirectory()) - Set tLocalFile=##class(%File).%New(tFileName) - Set tSC=tLocalFile.Open("WSN") - If $$$ISERR(tSC) Quit - - Set tSC=tLocalFile.CopyFrom(tStream) - If $$$ISERR(tSC) Quit - - Do tLocalFile.Close() - } - - Quit tSC -} - -} \ No newline at end of file +Class PivotSubscriptions.Installer +{ + +ClassMethod RunInstaller(pRootDir) +{ + Set tSC=$$$OK + + If pRootDir'="" { + Do $system.OBJ.LoadDir(pRootDir_"/PivotSubscriptions","ck",,1) + Set tSC=..ImportImage(pRootDir_"/Assets/PivotSubscriptions_BookCover.png") + If $$$ISERR(tSC) Quit + } + + Set tItem=##class(%DeepSee.UserLibrary.Link).%New() + Set tItem.fullName="Pivot Subscriptions" + Set tPage="PivotSubscriptions.UI.SubscriptionManager.zen" + Set tItem.href=$system.CSP.GetPortalApp($namespace,tPage)_tPage + Set tItem.title="Pivot Subscriptions" + Set tItem.bookCover="{background: {style:'background:white;',src:'covers/PivotSubscriptions_Cover.png'},header: {text:'$type',style:'display: none;'},title: {text:'$title',style:'display: none;'},image: {style:'display: none;',src:'deepsee/ds2_globe_44.png',imageStyle:'width:64px;height:64px;'},subtitle: {style:'display: none;'},footer: {text:'$owner',style:'display: none;'}}" + Set tSC=tItem.%Save() + + If $$$ISERR(tSC) Quit + + Set tSC=##class(PivotSubscriptions.Task).ConfigureTask() + + Quit tSC +} + +/// Import Cover, code taken from %DeepSee.UI.Dialog.ImageUpload:%OnSubmit +ClassMethod ImportImage(pDir) As %Status +{ + Set tSC=$$$OK + Set tStream=##class(%Stream.FileBinary).%New() + Set tSC=tStream.LinkToFile(pDir) + If $$$ISERR(tSC) Quit + Set tName="PivotSubscriptions_Cover.png" + + If $IsObject(tStream)&&(tName'="") { + Set tFileName=##class(%File).NormalizeDirectory(tName,##class(%DeepSee.UI.Dialog.ImageUpload).%GetImageDirectory()) + Set tLocalFile=##class(%File).%New(tFileName) + Set tSC=tLocalFile.Open("WSN") + If $$$ISERR(tSC) Quit + + Set tSC=tLocalFile.CopyFrom(tStream) + If $$$ISERR(tSC) Quit + + Do tLocalFile.Close() + } + + Quit tSC +} + +} diff --git a/PivotSubscriptions/Subscription.cls b/PivotSubscriptions/Subscription.cls index 8ad03b6..e15cfb4 100644 --- a/PivotSubscriptions/Subscription.cls +++ b/PivotSubscriptions/Subscription.cls @@ -23,6 +23,8 @@ Property Name As %String(MAXLEN = 100); Property CustomFilters As array Of %String(MAXLEN = ""); +Property CustomEmailText As %String(MAXLEN = ""); + Storage Default { @@ -61,6 +63,9 @@ Storage Default Name + +CustomEmailText + ^PivotSubscriptions.SubD SubscriptionDefaultData diff --git a/PivotSubscriptions/Task.cls b/PivotSubscriptions/Task.cls index a250c4c..8d017d9 100644 --- a/PivotSubscriptions/Task.cls +++ b/PivotSubscriptions/Task.cls @@ -12,7 +12,7 @@ Method OnTask() As %Status Set tHour=$PIECE($ZTIME($PIECE($HOROLOG,",",2),2),":",1) Set tMinute=$PIECE($ZTIME($PIECE($HOROLOG,",",2),2),":",2) - If $G(^PivotSubscriptions.Settings("TestEnv")) { + If ##class(PivotSubscriptions.Utils).GetSettings("TestEnv",0) { // Ignore minute for testing purposes // This will allow the task to be run on demand and send emails immediately // Leaving DayOfWeek and Hour so the scheduled task does not spam emails @@ -41,9 +41,6 @@ Method OnTask() As %Status ClassMethod SendSubscription(pID, pPivot As %String, pEmails As %String, pFormat As %String, pDayOfWeek, pHour, pMinute, pName) As %Status { Set tSC=$$$OK - - Set tSC=##class(PivotSubscriptions.EventLog).LogEvent("sendSubscription", $username, pPivot, pDayOfWeek, pHour, pMinute, pFormat, pEmails, pName, .pEvent) - If $$$ISERR(tSC) Quit tSC If pName="" { Set pName=$E(pPivot,1,*-6) @@ -54,14 +51,6 @@ ClassMethod SendSubscription(pID, pPivot As %String, pEmails As %String, pFormat } Catch ex { Set tSC=ex.AsStatus() } - // Update send event with status and end time - Set tSC=pEvent.LogUpdate(tSC) - If $$$ISERR(tSC) Quit tSC - - If '(##class(PivotSubscriptions.Utils).CheckTable(pName)) { - Set tSC=##class(PivotSubscriptions.EventLog).LogEvent("tableError", $username, pPivot, pDayOfWeek, pHour, pMinute, pFormat, pEmails, pName) - If $$$ISERR(tSC) Quit tSC - } Quit tSC } @@ -72,14 +61,17 @@ ClassMethod SendEmail(pID, pPivot As %String, pEmails As %String, pFormat As %St Set tFilename="" Set mdx=##class(%DeepSee.Utils).%GetMDXFromPivot(pPivot,.tSC,0) + Set tSubscription=##class(PivotSubscriptions.Subscription).%OpenId(pID) // Iterate through all subscribers and send emails to each individually - For count=1:1:$l(pEmails,",") { - Set tEmail=$p(pEmails,",",count) + For tCount=1:1:$l(pEmails,",") { + Set tEmail=$p(pEmails,",",tCount) + + Set tSC=##class(PivotSubscriptions.EventLog).CreateEvent("sendSubscription",$username,pPivot,tSubscription.DayOfWeek,tSubscription.Hour,tSubscription.Minute,tSubscription.Format,tEmail,pName,.pEvent) + If $$$ISERR(tSC) Quit tSC Set tHasResults=0 - Set tSubscription=##class(PivotSubscriptions.Subscription).%OpenId(pID) Set tCustomFilter=tSubscription.CustomFilters.GetAt(tEmail) Set tSC=##class(PivotSubscriptions.Utils).ExecuteForSubscription(mdx,tCustomFilter,.tHasResults) @@ -101,7 +93,7 @@ ClassMethod SendEmail(pID, pPivot As %String, pEmails As %String, pFormat As %St Set validAdminEmail=##class(PivotSubscriptions.Utils).CheckConfigurationGlobal("AdminEmail") If validAdminEmail { - Set msg.ReplyTo=^PivotSubscriptions.Settings("AdminEmail") + Set msg.ReplyTo=##class(PivotSubscriptions.Utils).GetSettings("AdminEmail") } // Remove ".pivot" extension for subject @@ -110,9 +102,8 @@ ClassMethod SendEmail(pID, pPivot As %String, pEmails As %String, pFormat As %St Set msg.IsHTML=1 // Set IsHTML to 1 if email uses HTML If tHasResults { - // Add attachment - Do ##class(PivotSubscriptions.Utils).WriteEmail(pName, msg, tableStatus) + Do ##class(PivotSubscriptions.Utils).WriteEmail(tSubscription, msg, tableStatus) // if table is too large, then just attach Excel instead If ((pFormat = "PDF") && (tableStatus)) { @@ -120,7 +111,7 @@ ClassMethod SendEmail(pID, pPivot As %String, pEmails As %String, pFormat As %St } ElseIf 'tableStatus { // Check if both configuration settings for admin email is valid before sending error message If validAdminEmail { - Do ##class(PivotSubscriptions.Utils).SendErrorMessage(pPivot,^PivotSubscriptions.Settings("AdminEmail"),pName) + Do ##class(PivotSubscriptions.Utils).TableErrorMessage(tSubscription,tEmail,pName,"TooLarge") } } @@ -132,18 +123,22 @@ ClassMethod SendEmail(pID, pPivot As %String, pEmails As %String, pFormat As %St } } Else { Do ##class(PivotSubscriptions.Utils).WriteEmailHeader(msg) - Do msg.TextData.WriteLine($G(^PivotSubscriptions.Settings("NoDataMessage"),"No data for requested pivot.")) + Do msg.TextData.WriteLine(##class(PivotSubscriptions.Utils).GetSettings("NoDataMessage","No data for requested pivot.")) + Do ##class(PivotSubscriptions.Utils).WriteUnsubscribe(msg) Do ##class(PivotSubscriptions.Utils).WriteEmailFooter(msg) + + Do ##class(PivotSubscriptions.Utils).TableErrorMessage(tSubscription,tEmail,pName,"NoResults") } } Do msg.To.SetAt(tEmail,1) // Send email - If tHasResults||$G(^PivotSubscriptions.Settings("SendNoData"),1) { + If tHasResults||##class(PivotSubscriptions.Utils).GetSettings("SendNoData",1) { Set tSC=server.Send(msg) + Set tSC=pEvent.UpdateStatus(tSC) + If $$$ISERR(tSC) Quit tSC } - // TODO: Log individual send status? If tHasResults { // Delete file diff --git a/PivotSubscriptions/UI/ConfigurationPage.cls b/PivotSubscriptions/UI/ConfigurationPage.cls index 0720bbe..894ebbc 100644 --- a/PivotSubscriptions/UI/ConfigurationPage.cls +++ b/PivotSubscriptions/UI/ConfigurationPage.cls @@ -22,6 +22,9 @@ XData contentPane [ XMLNamespace = "http://www.intersystems.com/zen" ]