예제 #1
0
        public static UnsubscribeUserReturnCode UnsubscribeUser(int mailingListID, string email)
        {
            ClearCache();

            // Find the Subscriber record by Email Address
            List <Subscriber> subs = Subscriber.SubscriberGetByEmail(email);

            if (subs.Count == 0)
            {
                return(UnsubscribeUserReturnCode.Never_Subscribed);
            }

            // Find the MailingList (validation purposes)
            MailingList ml = MailingList.GetByID(mailingListID);

            if (ml == null)
            {
                return(UnsubscribeUserReturnCode.Missing_MailingList);
            }

            // Find the MailingListSubscriber record (this could be better)
            List <MailingListSubscriber> mls
                = MailingListSubscriber.MailingListSubscriberGetBySubscriberID(subs[0].SubscriberID);

            if (mls.Count == 0)
            {
                return(UnsubscribeUserReturnCode.Never_Subscribed);
            }

            bool alreadyUnsubscribed = false;

            foreach (MailingListSubscriber t in mls)
            {
                if (t.MailingListID == mailingListID)
                {
                    alreadyUnsubscribed = true;
                    if (t.Active)
                    {
                        // Ok, the MailingListSubscriber is active, deactivate
                        t.Active = false;
                        t.Save();
                        return(UnsubscribeUserReturnCode.Success);
                    }
                }
            }

            if (alreadyUnsubscribed)
            {
                return(UnsubscribeUserReturnCode.Already_Unsubscribed);
            }

            return(UnsubscribeUserReturnCode.Already_Unsubscribed);
        }
예제 #2
0
        public static SubscribeUserReturnCode SubscribeUser(int mailingListID, string email, int newsletterFormatID, int?userID)
        {
            ClearCache();

            // Find the Subscriber record by Email Address
            List <Subscriber> subs = Subscriber.SubscriberGetByEmail(email);

            if (subs.Count == 0)
            {
                // Not yet a Subscriber, create new Subscriber
                Subscriber sub = new Subscriber();
                sub.Email = email;
                sub.DefaultNewsletterFormatID = newsletterFormatID;                 //this should be set to the user preference via SetSubscriberDefaultNewsletterFormatID
                sub.UserID = userID;
                sub.Save();
                subs.Add(sub);
            }

            // Find the MailingList (validation purposes)
            MailingList ml = MailingList.GetByID(mailingListID);

            if (ml == null)
            {
                return(SubscribeUserReturnCode.Missing_MailingList);
            }

            // Find the MailingListSubscriber record (this could be better)
            List <MailingListSubscriber> mls
                = MailingListSubscriber.MailingListSubscriberGetBySubscriberID(subs[0].SubscriberID);

            foreach (MailingListSubscriber t in mls)
            {
                if (t.MailingListID == mailingListID)
                {
                    //if subscriber isn't flagged as deleted...
                    if (t.Active)
                    {
                        // Ok, the MailingListSubscriber is active, return
                        t.NewsletterFormatID = newsletterFormatID;
                        t.Save();
                        return(SubscribeUserReturnCode.Already_Subscribed);
                    }
                    // MailingListSubscriber already exists but was inactive or was deleted from the subscriber list, reactivate and return success
                    subs[0].Deleted = false;
                    subs[0].Save();
                    t.Active             = true;
                    t.NewsletterFormatID = newsletterFormatID;
                    t.Save();
                    return(SubscribeUserReturnCode.Success);
                }
            }
            if (Settings.EnableMailingListLimitations && MailingListSubscriber.GetCountByMailingListIDActive(mailingListID, true) >= Settings.MaxNumberSubscribers)
            {
                return(SubscribeUserReturnCode.MailingList_Full);
            }
            //if MailingListSubscriber does not exist, create a new MailingListSubscriber
            MailingListSubscriber mlsNew = new MailingListSubscriber();

            mlsNew.MailingListID      = mailingListID;
            mlsNew.SubscriberID       = subs[0].SubscriberID;
            mlsNew.NewsletterFormatID = newsletterFormatID;
            mlsNew.Active             = true;
            mlsNew.EntityID           = Guid.NewGuid();
            mlsNew.Save();
            return(SubscribeUserReturnCode.Success);
        }