PageRenderTime 33ms CodeModel.GetById 23ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/CMSModules/Newsletters/Controls/MySubscriptions.ascx.cs

https://bitbucket.org/kudutest/kenticogit
C# | 494 lines | 366 code | 65 blank | 63 comment | 66 complexity | bbf61dbed45eaeb6da0374e4cb1409f4 MD5 | raw file
  1using System;
  2using System.Data;
  3using System.Web.UI.WebControls;
  4
  5using CMS.GlobalHelper;
  6using CMS.Newsletter;
  7using CMS.CMSHelper;
  8using CMS.PortalEngine;
  9using CMS.SiteProvider;
 10using CMS.UIControls;
 11using CMS.SettingsProvider;
 12using CMS.WebAnalytics;
 13
 14public partial class CMSModules_Newsletters_Controls_MySubscriptions : CMSAdminControl
 15{
 16    #region "Variables"
 17
 18    private Subscriber sb = null;
 19    private bool mExternalUse = false;
 20    private int mCacheMinutes = 0;
 21    private string subscriberEmail = string.Empty;
 22    private bool userIsIdentified = false;
 23    private int mUserId = 0;
 24    private int mSiteId = 0;
 25    private string currentValues = string.Empty;
 26    private bool mSendConfirmationEmail = true;
 27
 28    #endregion
 29
 30
 31    #region "Properties"
 32
 33    /// <summary>
 34    /// Gets or sets the value that indicates whether send confirmation emails.
 35    /// </summary>
 36    public bool SendConfirmationEmail
 37    {
 38        get
 39        {
 40            return mSendConfirmationEmail;
 41        }
 42        set
 43        {
 44            mSendConfirmationEmail = value;            
 45        }
 46    }
 47
 48
 49    /// <summary>
 50    /// Gets or sets the value that indicates whether this control is visible.
 51    /// </summary>
 52    public bool ForcedVisible
 53    {
 54        get
 55        {
 56            return plcMain.Visible;
 57        }
 58        set
 59        {
 60            plcMain.Visible = value;
 61            lblInfo.Visible = value;
 62        }
 63    }
 64
 65
 66    /// <summary>
 67    /// Gets or sets the value that indicates whether this control is used in other control.
 68    /// </summary>
 69    public bool ExternalUse
 70    {
 71        get
 72        {
 73            return mExternalUse;
 74        }
 75        set
 76        {
 77            mExternalUse = value;
 78        }
 79    }
 80
 81
 82    /// <summary>
 83    /// Gets or sets the WebPart cache minutes.
 84    /// </summary>
 85    public int CacheMinutes
 86    {
 87        get
 88        {
 89            return mCacheMinutes;
 90        }
 91        set
 92        {
 93            mCacheMinutes = value;
 94        }
 95    }
 96
 97
 98    /// <summary>
 99    /// Gets or sets current site ID.
100    /// </summary>
101    public int SiteID
102    {
103        get
104        {
105            return mSiteId;
106        }
107        set
108        {
109            mSiteId = value;
110        }
111    }
112
113
114    /// <summary>
115    /// Gets or sets current user ID.
116    /// </summary>
117    public int UserID
118    {
119        get
120        {
121            return mUserId;
122        }
123        set
124        {
125            mUserId = value;
126        }
127    }
128
129
130    /// <summary>
131    /// Indicatec if selector control is on live site.
132    /// </summary>
133    public override bool IsLiveSite
134    {
135        get
136        {
137            return ValidationHelper.GetBoolean(this.GetValue("IsLiveSite"), false);
138        }
139        set
140        {
141            this.SetValue("IsLiveSite", value);
142        }
143    }
144
145    #endregion
146
147
148    #region "Methods"
149
150    /// <summary>
151    /// PageLoad.
152    /// </summary>
153    protected void Page_Load(object sender, EventArgs e)
154    {
155        if (ExternalUse)
156        {
157            LoadData();
158        }
159    }
160
161
162    /// <summary>
163    /// Load data.
164    /// </summary>
165    public void LoadData()
166    {
167        if (this.StopProcessing)
168        {
169            // Hide control
170            this.Visible = false;
171        }
172        else
173        {
174            this.SetContext();
175
176            // Get specified user if used instead of current user
177            UserInfo ui = null;
178            if (this.UserID > 0)
179            {
180                ui = UserInfoProvider.GetUserInfo(this.UserID);
181            }
182            else
183            {
184                ui = CMSContext.CurrentUser;
185            }
186
187            // Get specified site ID instead of current site ID
188            int siteId = 0;
189            if (this.SiteID > 0)
190            {
191                siteId = this.SiteID;
192            }
193            else
194            {
195                siteId = CMSContext.CurrentSiteID;
196            }
197
198            usNewsletters.WhereCondition = "NewsletterSiteID = " + siteId;
199            usNewsletters.ButtonRemoveSelected.CssClass = "XLongButton";
200            usNewsletters.ButtonAddItems.CssClass = "XLongButton";
201            usNewsletters.OnSelectionChanged += new EventHandler(usNewsletters_OnSelectionChanged);
202            usNewsletters.IsLiveSite = this.IsLiveSite;
203
204            this.userIsIdentified = (ui != null) && (!ui.IsPublic()) && (ValidationHelper.IsEmail(ui.Email) || ValidationHelper.IsEmail(ui.UserName));
205            if (this.userIsIdentified)
206            {
207                usNewsletters.Visible = true;
208
209                // Try to get subsriber info with specified e-mail
210                sb = SubscriberProvider.GetSubscriber(ui.Email, siteId);
211                if (sb == null)
212                {
213                    // Try to get subscriber info according to user info
214                    sb = SubscriberProvider.GetSubscriber(SiteObjectType.USER, ui.UserID, siteId);
215                }
216
217                // Get user e-mail address
218                if (sb != null)
219                {
220                    subscriberEmail = sb.SubscriberEmail;
221
222                    // Get selected newsletters
223                    DataSet ds = SubscriberNewsletterInfoProvider.GetSubscriberNewsletters(sb.SubscriberID, null, -1, "NewsletterID");
224                    if (!DataHelper.DataSourceIsEmpty(ds))
225                    {
226                        currentValues = TextHelper.Join(";", SqlHelperClass.GetStringValues(ds.Tables[0], "NewsletterID"));
227                    }
228
229                    // Load selected newsletters
230                    if (!string.IsNullOrEmpty(currentValues))
231                    {
232                        usNewsletters.Value = currentValues;
233                    }
234                }
235
236                // Try to get email address from user data
237                if (string.IsNullOrEmpty(subscriberEmail))
238                {
239                    if (ValidationHelper.IsEmail(ui.Email))
240                    {
241                        subscriberEmail = ui.Email;
242                    }
243                    else if (ValidationHelper.IsEmail(ui.UserName))
244                    {
245                        subscriberEmail = ui.UserName;
246                    }
247                }
248            }
249            else
250            {
251                usNewsletters.Visible = false;
252
253                lblInfo.Visible = true;
254
255                if ((this.UserID > 0) && (CMSContext.CurrentUser.UserID == this.UserID))
256                {
257                    lblInfo.Text = GetString("MySubscriptions.CannotIdentify");
258                }
259                else
260                {
261                    lblInfo.Text = GetString("MySubscriptions.CannotIdentifyUser");
262                }
263            }
264
265            this.ReleaseContext();
266        }
267    }
268
269
270    /// <summary>
271    /// Logs activity for subscribing/unsubscribing
272    /// </summary>
273    /// <param name="ui">User</param>
274    /// <param name="newsletterId">Newsletter ID</param>
275    /// <param name="subscribe">Subscribing/unsubscribing flag</param>
276    /// <param name="siteId">Site ID</param>
277    private void LogActivity(UserInfo ui, int newsletterId, bool subscribe, int siteId)
278    {
279        if ((sb == null) || (ui == null) || (CMSContext.ViewMode != ViewModeEnum.LiveSite) ||
280            !ActivitySettingsHelper.ActivitiesEnabledAndModuleLoaded(siteId) || !ActivitySettingsHelper.ActivitiesEnabledForThisUser(ui))
281        {
282            return;
283        }
284
285        if (sb.SubscriberType == SiteObjectType.USER)
286        {
287            if (subscribe && ActivitySettingsHelper.NewsletterSubscribeEnabled(siteId) ||
288                !subscribe && ActivitySettingsHelper.NewsletterUnsubscribeEnabled(siteId))
289            {
290                Newsletter news = NewsletterProvider.GetNewsletter(newsletterId);
291                if ((news != null) && news.NewsletterLogActivity)
292                {
293                    var data = new ActivityData()
294                    {
295                        ContactID = ModuleCommands.OnlineMarketingGetCurrentContactID(),
296                        SiteID = sb.SubscriberSiteID,
297                        Type = PredefinedActivityType.NEWSLETTER_UNSUBSCRIBING,
298                        TitleData = news.NewsletterName,
299                        ItemID = newsletterId,
300                        URL = URLHelper.CurrentRelativePath,
301                        Campaign = CMSContext.Campaign
302                    };
303                    ActivityLogProvider.LogActivity(data);
304                }
305            }
306        }
307    }
308
309
310    private void usNewsletters_OnSelectionChanged(object sender, EventArgs e)
311    {
312        if (RaiseOnCheckPermissions("ManageSubscribers", this))
313        {
314            if (this.StopProcessing)
315            {
316                return;
317            }
318        }
319
320        // Get specified user if used instead of current user
321        UserInfo ui = null;
322        if (this.UserID > 0)
323        {
324            ui = UserInfoProvider.GetUserInfo(this.UserID);
325        }
326        else
327        {
328            ui = CMSContext.CurrentUser;
329        }
330
331        // Get specified site ID instead of current site ID
332        int siteId = 0;
333        if (this.SiteID > 0)
334        {
335            siteId = this.SiteID;
336        }
337        else
338        {
339            siteId = CMSContext.CurrentSiteID;
340        }
341
342        if ((sb == null) && (ui != null))
343        {
344            // Create new subsciber (bind to existing user account)
345            if ((!ui.IsPublic()) && (ValidationHelper.IsEmail(ui.Email) || ValidationHelper.IsEmail(ui.UserName)))
346            {
347                sb = new Subscriber();
348                if (ui != null)
349                {
350                    if (!string.IsNullOrEmpty(ui.FirstName) && !string.IsNullOrEmpty(ui.LastName))
351                    {
352                        sb.SubscriberFirstName = ui.FirstName;
353                        sb.SubscriberLastName = ui.LastName;
354                    }
355                    else
356                    {
357                        sb.SubscriberFirstName = ui.FullName;
358                    }
359                    // Full name consists of "user " and user full name
360                    sb.SubscriberFullName = "User '" + ui.FullName + "'";
361                }
362
363                sb.SubscriberSiteID = siteId;
364                sb.SubscriberType = SiteObjectType.USER;
365                sb.SubscriberRelatedID = ui.UserID;
366                // Save subscriber to DB
367                SubscriberProvider.SetSubscriber(sb);
368            }
369        }
370
371        if (sb == null)
372        {
373            return;
374        }
375
376        // Remove old items
377        int newsletterId = 0;
378        string newValues = ValidationHelper.GetString(usNewsletters.Value, null);
379        string items = DataHelper.GetNewItemsInList(newValues, currentValues);
380        if (!String.IsNullOrEmpty(items))
381        {
382            string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
383            if (newItems != null)
384            {
385                foreach (string item in newItems)
386                {
387                    newsletterId = ValidationHelper.GetInteger(item, 0);
388
389                    // If subscriber is subscribed, unsubscribe him
390                    if (SubscriberProvider.IsSubscribed(sb.SubscriberID, newsletterId))
391                    {
392                        SubscriberProvider.Unsubscribe(sb.SubscriberID, newsletterId, SendConfirmationEmail);
393                        // Log activity
394                        LogActivity(ui, newsletterId, false, siteId);
395                    }
396                }
397            }
398        }
399
400        // Add new items
401        items = DataHelper.GetNewItemsInList(currentValues, newValues);
402        if (!String.IsNullOrEmpty(items))
403        {
404            string[] newItems = items.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
405            if (newItems != null)
406            {
407                foreach (string item in newItems)
408                {
409                    newsletterId = ValidationHelper.GetInteger(item, 0);
410
411                    // If subscriber is not subscribed, subscribe him
412                    if (!SubscriberProvider.IsSubscribed(sb.SubscriberID, newsletterId))
413                    {
414                        try
415                        {
416                            SubscriberProvider.Subscribe(sb.SubscriberID, newsletterId, DateTime.Now, SendConfirmationEmail);
417                            // Log activity
418                            LogActivity(ui, newsletterId, true, siteId);
419                        }
420                        catch { }
421                    }
422                }
423            }
424        }
425
426        // Display information about successful (un)subscription
427        this.lblInfoMsg.Visible = true;
428    }
429
430
431    protected override void OnPreRender(EventArgs e)
432    {
433        base.OnPreRender(e);
434
435        // Display appropriate message
436        if (this.userIsIdentified)
437        {
438            // There are some newsletters to display
439            if (CMSContext.CurrentUser.UserID == this.UserID)
440            {
441                lblText.Text = GetString("MySubscriptions.MainText").Replace("##EMAIL##", HTMLHelper.HTMLEncode(subscriberEmail));
442            }
443            else
444            {
445                lblText.Text = GetString("MySubscriptions.MainTextUser").Replace("##EMAIL##", HTMLHelper.HTMLEncode(subscriberEmail));
446            }
447        }
448    }
449
450
451    /// <summary>
452    /// Overriden SetValue - because of MyAccount webpart.
453    /// </summary>
454    /// <param name="propertyName">Name of the property to set</param>
455    /// <param name="value">Value to set</param>
456    public override void SetValue(string propertyName, object value)
457    {
458        base.SetValue(propertyName, value);
459
460        switch (propertyName.ToLower())
461        {
462            case "forcedvisible":
463                this.ForcedVisible = ValidationHelper.GetBoolean(value, false);
464                break;
465
466            case "externaluse":
467                this.ExternalUse = ValidationHelper.GetBoolean(value, false);
468                break;
469
470            case "cacheminutes":
471                this.CacheMinutes = ValidationHelper.GetInteger(value, 0);
472                break;
473
474            case "reloaddata":
475                // Special property which enables to call LoadData from MyAccount webpart
476                LoadData();
477                break;
478
479            case "userid":
480                this.UserID = ValidationHelper.GetInteger(value, 0);
481                break;
482
483            case "siteid":
484                this.SiteID = ValidationHelper.GetInteger(value, 0);
485                break;
486
487            case "sendconfirmationemail":
488                mSendConfirmationEmail = ValidationHelper.GetBoolean (value,true);
489                break;
490        }
491    }
492
493    #endregion
494}