Esempio n. 1
0
        /// <summary>
        /// Gets the message template.
        /// </summary>
        /// <param name="dateType">Type of the date.</param>
        /// <param name="objecTypeId">The objec type id.</param>
        /// <param name="languageId">The language id.</param>
        /// <returns></returns>
        public static ReminderTemplate GetMessageTemplate(DateTypes dateType, int languageId)
        {
            ReminderTemplate retVal = null;

            retVal = new ReminderTemplate(dateType, languageId);
            retVal.Load();

            return(retVal);
        }
Esempio n. 2
0
        public static Alerts2.Message GetMessage(ReminderTemplate template, int?objectId, Guid?objectUid, ObjectTypes objectType, int userId, VariableInfo[] vars)
        {
            StringBuilder sbSubject = new StringBuilder();
            StringBuilder sbBody    = new StringBuilder();

            IFormatProvider prov = new CultureInfo(template.Locale, true);

            sbSubject.Append(template.Subject);
            sbBody.Append(template.Body);

            bool IsExternalUser = User.IsExternal(userId);

            string gateGuid = "";

            if (IsExternalUser && objectId.HasValue)
            {
                if (userId > 0)
                {
                    gateGuid = DBCommon.GetGateGuid((int)objectType, objectId.Value, userId);
                }
                //				else
                //					gateGuid = DBCommon.GetGateGuid(eInfo.ObjectTypeId, eInfo.ObjectId, User);
            }

            // Replace variables with values
            foreach (VariableInfo var in vars)
            {
                string nameBegin = string.Format("[={0}=]", var.name);
                string nameEnd   = string.Format("[=/{0}=]", var.name);

                string linkStartValue = string.Empty;
                string linkEndValue   = string.Empty;

                if (var.IsLink)                 // Modify link
                {
                    if (IsExternalUser && !var.External)
                    {
                        var.value = string.Empty;
                    }

                    if (var.value.Length == 0)
                    {
                        linkStartValue = linkEndValue = string.Empty;
                    }
                    else
                    {
                        if (IsExternalUser)
                        {
                            if (userId > 0)
                            {
                                var.value = Alerts2.MakeExternalLink(var.value, User.GetUserLogin(userId), gateGuid);
                            }
                            //							else
                            //								var.value = Alert2.MakeClientLink(var.value, gateGuid);
                        }

                        // Add NoMenu parameter to the end of link
                        if (!var.DisableNoMenu && !User.GetMenuInAlerts(userId) && var.name != "PortalLink" && var.name != "ServerLink")
                        {
                            if (var.value.IndexOf('?') != -1)
                            {
                                var.value += '&';
                            }
                            else
                            {
                                var.value += '?';
                            }
                            var.value += "nomenu=1";
                        }

                        linkStartValue = string.Format("<a href='{0}'>", var.value);
                        linkEndValue   = "</a>";
                    }
                }

                if (var.type == VariableType.Date.ToString())
                {
                    var.value = Alerts2.DateReformat(var.value, prov);
                }
                else if (var.type == VariableType.DateTime.ToString())
                {
                    var.value = Alerts2.DateTimeReformat(var.value, prov, User.GetUserTimeZoneId(userId));
                }

                sbSubject.Replace(nameBegin, var.value);

                if (var.IsLink)
                {
                    sbBody.Replace(nameBegin, linkStartValue);
                    sbBody.Replace(nameEnd, linkEndValue);
                }
                else
                {
                    var.value = HttpUtility.HtmlEncode(var.value).Replace("\r\n", "<BR>");
                    sbBody.Replace(nameBegin, var.value);
                }
            }

            return(new Alerts2.Message(sbSubject.ToString(), sbBody.ToString()));
        }
Esempio n. 3
0
        private static void SendReminder(DateTypes dateType, ObjectTypes objecType, int?objectId, Guid?objectUid, List <int> users)
        {
            // TODO: Step 0. Check Security (Not implemented yet)

            // Step 1. Calculate variables
            foreach (int userId in users)
            {
                int reminderType = 0;

                using (IDataReader reader = User.GetUserPreferences(userId))
                {
                    if (reader.Read())
                    {
                        reminderType = (int)reader["ReminderType"];
                    }
                }

                if (reminderType != 0)
                {
                    // Step 2. Get Message Template
                    ReminderTemplate tmpl = Reminder.GetMessageTemplate(dateType, User.GetUserLanguage(userId));

                    // Step 2.1. Calculate Variables
                    ArrayList vars = new ArrayList();

                    Alerts2.GetObjectVariables(objecType, objectId, objectUid, false, Reminder.GetVariables(dateType), vars);

                    // Step 3. Replace variables and GetMessage
                    Alerts2.Message msg = Reminder.GetMessage(tmpl, objectId, objectUid, objecType, userId, (VariableInfo[])vars.ToArray(typeof(VariableInfo)));

                    // Step 4. Save to log
                    using (DbTransaction tran = DbTransaction.Begin())
                    {
                        int logId = DbAlert2.MessageLogAdd(msg.Subject, msg.Body);
                        DBSystemEvents.RecipientUpdateSend(userId, (reminderType & 1) != 0, PortalConfig.UseIM && (reminderType & 2) != 0, logId);                         // IsNotifiedByEmail, IsNotifiedByIBN

                        tran.Commit();
                    }

                    #region -- Send via Email --
                    // Step 5. Send via Email
                    try
                    {
                        if ((reminderType & 1) != 0)                        //IsNotifiedByEmail
                        {
                            string body = "<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\" /></head><body>" + msg.Body + "</body></html>";

                            using (DbTransaction tran = DbTransaction.Begin())
                            {
                                Alerts2.SendMessage(DeliveryType.Email, Reminder.GetAddress(DeliveryType.Email, userId), body, msg.Subject);

                                DBSystemEvents.RecipientUpdateSent(userId, (int)DeliveryType.Email, true);

                                tran.Commit();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteError(ex.ToString());
                    }
                    #endregion

                    #region -- Send via IM --
                    // Step 6. Send via IM
                    try
                    {
                        if ((reminderType & 2) != 0)                        //uInfo != null && uInfo.IsNotifiedByIBN)
                        {
                            using (DbTransaction tran = DbTransaction.Begin())
                            {
                                Alerts2.SendMessage(DeliveryType.IBN, Reminder.GetAddress(DeliveryType.IBN, userId), msg.Body, msg.Subject);

                                DBSystemEvents.RecipientUpdateSent(userId, (int)DeliveryType.IBN, true);

                                tran.Commit();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.WriteError(ex.ToString());
                    }
                    #endregion
                }
            }
        }