Esempio n. 1
0
    /// <summary>
    /// Gets and bulk updates notification subscriptions. Called when the "Get and bulk update subscriptions" button is pressed.
    /// Expects the CreateNotificationSubscription method to be run first.
    /// </summary>
    private bool GetAndBulkUpdateNotificationSubscriptions()
    {
        // Get the gateway
        NotificationGatewayInfo gateway = NotificationGatewayInfoProvider.GetNotificationGatewayInfo("MyNewGateway");

        // Get the tamplate
        NotificationTemplateInfo template = NotificationTemplateInfoProvider.GetNotificationTemplateInfo("MyNewTemplate", CMSContext.CurrentSiteID);

        if ((gateway != null) && (template != null))
        {
            // Prepare the parameters
            string where = "SubscriptionGatewayID = " + gateway.GatewayID + " AND SubscriptionTemplateID = " + template.TemplateID;

            // Get the notification subscription
            DataSet subscriptions = NotificationSubscriptionInfoProvider.GetSubscriptions(where, null);
            if (!DataHelper.DataSourceIsEmpty(subscriptions))
            {
                foreach (DataRow subscriptionDr in subscriptions.Tables[0].Rows)
                {
                    // Create object from DataRow
                    NotificationSubscriptionInfo updateSubscription = new NotificationSubscriptionInfo(subscriptions.Tables[0].Rows[0]);

                    // Update the property
                    updateSubscription.SubscriptionEventDisplayName = updateSubscription.SubscriptionEventDisplayName.ToUpper();

                    // Update the notification subscription
                    NotificationSubscriptionInfoProvider.SetNotificationSubscriptionInfo(updateSubscription);

                    return(true);
                }
            }
        }

        return(false);
    }
Esempio n. 2
0
    /// <summary>
    /// Deletes notification subscription. Called when the "Delete subscription" button is pressed.
    /// Expects the CreateNotificationSubscription method to be run first.
    /// </summary>
    private bool DeleteNotificationSubscription()
    {
        // Get the gateway
        NotificationGatewayInfo gateway = NotificationGatewayInfoProvider.GetNotificationGatewayInfo("MyNewGateway");

        // Get the template
        NotificationTemplateInfo template = NotificationTemplateInfoProvider.GetNotificationTemplateInfo("MyNewTemplate", CMSContext.CurrentSiteID);

        if ((gateway != null) && (template != null))
        {
            // Prepare the parameters
            string where = "SubscriptionGatewayID = " + gateway.GatewayID + " AND SubscriptionTemplateID = " + template.TemplateID;

            // Get the notification subscription
            DataSet subscriptions = NotificationSubscriptionInfoProvider.GetSubscriptions(where, null);
            if (!DataHelper.DataSourceIsEmpty(subscriptions))
            {
                // Create object from DataRow
                NotificationSubscriptionInfo deleteSubscription = new NotificationSubscriptionInfo(subscriptions.Tables[0].Rows[0]);

                // Delete the notification subscription
                NotificationSubscriptionInfoProvider.DeleteNotificationSubscriptionInfo(deleteSubscription);

                return(deleteSubscription != null);
            }
        }

        return(false);
    }
Esempio n. 3
0
    /// <summary>
    /// Calls Validate() method, if validation fails returns error message,
    /// otherwise creates subscriptions and returns empty string.
    /// </summary>
    public override string Subscribe()
    {
        // Validate inputs
        string errorMessage = Validate();

        if (!String.IsNullOrEmpty(errorMessage))
        {
            return(errorMessage);
        }

        // Get correct user ID
        int userId = 0;

        if (EventUserID > 0)
        {
            userId = EventUserID;
        }
        else
        {
            if (MembershipContext.AuthenticatedUser != null)
            {
                userId = MembershipContext.AuthenticatedUser.UserID;
            }
        }

        // Parse the notification template site and name
        NotificationTemplateInfo nti = null;
        string templateName          = NotificationTemplateName;

        if (NotificationTemplateName != null)
        {
            string[] temp = NotificationTemplateName.Split(new char[] { '.' });
            if (temp.Length == 2)
            {
                SiteInfo tempSite = SiteInfoProvider.GetSiteInfo(temp[0]);
                if (tempSite != null)
                {
                    templateName = temp[1];
                    nti          = NotificationTemplateInfoProvider.GetNotificationTemplateInfo(templateName, tempSite.SiteID);
                }
            }
            else
            {
                nti = NotificationTemplateInfoProvider.GetNotificationTemplateInfo(templateName, 0);
            }
        }

        bool hasSubscription = false;

        // Inputs are valid now, create the subscriptions
        for (int i = 0; i < NotificationGateways.Count; i++)
        {
            CMSCheckBox            chk = controls[i, 0] as CMSCheckBox;
            Panel                  pnl = controls[i, 1] as Panel;
            CMSNotificationGateway g   = controls[i, 2] as CMSNotificationGateway;
            if ((pnl != null) && (chk != null) && (g != null) && (chk.Checked))
            {
                // Register the subscriptions
                if (g.NotificationGatewayObj != null && g.NotificationGatewayForm != null)
                {
                    // Stores NotificationSubscriptionInfo objects
                    List <NotificationSubscriptionInfo> infos = new List <NotificationSubscriptionInfo>();
                    bool uniquenessFailed = false;
                    bool templateFailed   = false;

                    foreach (NotificationSubscriptionInfo nsiTemplate in Subscriptions)
                    {
                        // Create new subscription and initialize it with default values
                        NotificationSubscriptionInfo nsi = new NotificationSubscriptionInfo();
                        nsi.SubscriptionEventDisplayName = EventDisplayName;
                        nsi.SubscriptionTarget           = Convert.ToString(g.NotificationGatewayForm.Value);
                        nsi.SubscriptionEventCode        = EventCode;
                        nsi.SubscriptionEventObjectID    = EventObjectID;
                        nsi.SubscriptionEventSource      = EventSource;
                        nsi.SubscriptionGatewayID        = g.NotificationGatewayObj.GatewayID;
                        nsi.SubscriptionTime             = DateTime.Now;
                        nsi.SubscriptionUserID           = userId;
                        nsi.SubscriptionEventData1       = EventData1;
                        nsi.SubscriptionEventData2       = EventData2;
                        nsi.SubscriptionUseHTML          = SubscriptionUseHTML;
                        nsi.SubscriptionSiteID           = SubscriptionSiteID;
                        if (nti != null)
                        {
                            nsi.SubscriptionTemplateID = nti.TemplateID;
                        }

                        // Overwrite default values if these are specified in template subscription
                        if (!String.IsNullOrEmpty(nsiTemplate.SubscriptionEventDisplayName))
                        {
                            nsi.SubscriptionEventDisplayName = nsiTemplate.SubscriptionEventDisplayName;
                        }
                        if (!String.IsNullOrEmpty(nsiTemplate.SubscriptionEventCode))
                        {
                            nsi.SubscriptionEventCode = nsiTemplate.SubscriptionEventCode;
                        }
                        if (!String.IsNullOrEmpty(nsiTemplate.SubscriptionEventSource))
                        {
                            nsi.SubscriptionEventSource = nsiTemplate.SubscriptionEventSource;
                        }
                        if (!String.IsNullOrEmpty(nsiTemplate.SubscriptionEventData1))
                        {
                            nsi.SubscriptionEventData1 = nsiTemplate.SubscriptionEventData1;
                        }
                        if (!String.IsNullOrEmpty(nsiTemplate.SubscriptionEventData2))
                        {
                            nsi.SubscriptionEventData2 = nsiTemplate.SubscriptionEventData2;
                        }
                        if (nsiTemplate.SubscriptionEventObjectID > 0)
                        {
                            nsi.SubscriptionEventObjectID = nsiTemplate.SubscriptionEventObjectID;
                        }
                        if (nsiTemplate.SubscriptionUserID > 0)
                        {
                            nsi.SubscriptionEventObjectID = nsiTemplate.SubscriptionUserID;
                        }
                        if (nsiTemplate.SubscriptionSiteID > 0)
                        {
                            nsi.SubscriptionSiteID = nsiTemplate.SubscriptionSiteID;
                        }
                        if (nsiTemplate.SubscriptionTemplateID > 0)
                        {
                            nsi.SubscriptionTemplateID = nsiTemplate.SubscriptionTemplateID;
                        }

                        // Check uniqueness (create only unique subscriptions)
                        string where = "SubscriptionEventSource = '" + SqlHelper.GetSafeQueryString(nsi.SubscriptionEventSource, false) + "' AND " +
                                       "SubscriptionEventCode = '" + SqlHelper.GetSafeQueryString(nsi.SubscriptionEventCode, false) + "' AND " +
                                       "SubscriptionTarget = '" + SqlHelper.GetSafeQueryString(nsi.SubscriptionTarget, false) + "' AND " +
                                       "SubscriptionEventObjectID = " + nsi.SubscriptionEventObjectID + " AND " +
                                       "SubscriptionGatewayID = " + nsi.SubscriptionGatewayID + " AND " +
                                       // Notification event data support wildcard parameters due to AliasPath value
                                       "SubscriptionEventData1 LIKE '" + SqlHelper.EscapeQuotes(nsi.SubscriptionEventData1) + "' AND " +
                                       "SubscriptionEventData2 LIKE '" + SqlHelper.EscapeQuotes(nsi.SubscriptionEventData2) + "' AND ";

                        if (nsi.SubscriptionSiteID > 0)
                        {
                            where += "SubscriptionSiteID = " + nsi.SubscriptionSiteID;
                        }
                        else
                        {
                            where += "SubscriptionSiteID IS NULL";
                        }

                        // Only if subscription is unique and if the template is set, put it into the list
                        if (!DataHelper.DataSourceIsEmpty(NotificationSubscriptionInfoProvider.GetSubscriptions(@where, null)))
                        {
                            uniquenessFailed = true;
                            break;
                        }
                        else if (nsi.SubscriptionTemplateID <= 0)
                        {
                            templateFailed = true;
                            break;
                        }
                        else
                        {
                            infos.Add(nsi);
                        }
                    }

                    if (uniquenessFailed)
                    {
                        return(GetString("notifications.subscription.notunique"));
                    }
                    else if (templateFailed)
                    {
                        return(GetString("notifications.subscription.templatemissing"));
                    }
                    else
                    {
                        // Save vaild subscriptions into the DB
                        foreach (NotificationSubscriptionInfo nsi in infos)
                        {
                            NotificationSubscriptionInfoProvider.SetNotificationSubscriptionInfo(nsi);
                        }
                    }

                    // Clear the form after successful registration
                    g.NotificationGatewayForm.ClearForm();

                    hasSubscription = true;
                }
            }
        }

        if (hasSubscription)
        {
            if (NotificationGateways.Count > 1)
            {
                // Reset the state of the gateways
                for (int i = 0; i < NotificationGateways.Count; i++)
                {
                    CMSCheckBox chk = controls[i, 0] as CMSCheckBox;
                    Panel       pnl = controls[i, 1] as Panel;
                    if ((chk != null) && (pnl != null))
                    {
                        chk.Checked = false;
                        pnl.Attributes.Add("style", "display: none;");
                    }
                }
            }

            return(String.Empty);
        }
        else
        {
            return(GetString("notifications.subscription.selectgateway"));
        }
    }