#region Copyright
//
// DotNetNukeŽ - http://www.dotnetnuke.com
// Copyright (c) 2002-2011
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endregion
#region Usings
using System;
using System.Collections;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
#endregion
namespace DotNetNuke.Modules.Admin.Security
{
/// -----------------------------------------------------------------------------
///
/// The MemberServices UserModuleBase is used to manage a User's services
///
///
///
///
/// [cnurse] 03/03/2006
///
/// -----------------------------------------------------------------------------
public partial class MemberServices : UserModuleBase
{
#region Delegates
public delegate void SubscriptionUpdatedEventHandler(object sender, SubscriptionUpdatedEventArgs e);
#endregion
#region "Events"
public event SubscriptionUpdatedEventHandler SubscriptionUpdated;
#endregion
#region "Private Methods"
/// -----------------------------------------------------------------------------
///
/// FormatPrice formats the Fee amount and filters out null-values
///
///
///
/// The price to format
/// The correctly formatted price
///
/// [cnurse] 9/13/2004 Updated to reflect design changes for Help, 508 support
/// and localisation
///
/// -----------------------------------------------------------------------------
private string FormatPrice(float price)
{
string formatPrice = Null.NullString;
try
{
if (price != Null.NullSingle)
{
formatPrice = price.ToString("##0.00");
}
else
{
formatPrice = "";
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
return formatPrice;
}
private ArrayList GetRoles(int portalId, int userId)
{
var objRoles = new RoleController();
return objRoles.GetUserRoles(portalId, userId, false);
}
private void Subscribe(int roleID, bool cancel)
{
var objRoles = new RoleController();
RoleInfo objRole = objRoles.GetRole(roleID, PortalSettings.PortalId);
if (objRole.IsPublic && objRole.ServiceFee == 0.0)
{
objRoles.UpdateUserRole(PortalId, UserInfo.UserID, roleID, cancel);
//Raise SubscriptionUpdated Event
OnSubscriptionUpdated(new SubscriptionUpdatedEventArgs(cancel, objRole.RoleName));
}
else
{
if (!cancel)
{
Response.Redirect("~/admin/Sales/PayPalSubscription.aspx?tabid=" + TabId + "&RoleID=" + roleID, true);
}
else
{
Response.Redirect("~/admin/Sales/PayPalSubscription.aspx?tabid=" + TabId + "&RoleID=" + roleID + "&cancel=1", true);
}
}
}
private void UseTrial(int roleID)
{
var objRoles = new RoleController();
RoleInfo objRole = objRoles.GetRole(roleID, PortalSettings.PortalId);
if (objRole.IsPublic && objRole.TrialFee == 0.0)
{
objRoles.UpdateUserRole(PortalId, UserInfo.UserID, roleID, false);
//Raise SubscriptionUpdated Event
OnSubscriptionUpdated(new SubscriptionUpdatedEventArgs(false, objRole.RoleName));
}
else
{
Response.Redirect("~/admin/Sales/PayPalSubscription.aspx?tabid=" + TabId + "&RoleID=" + roleID, true);
}
}
#endregion
#region "Protected Methods"
/// -----------------------------------------------------------------------------
///
/// FormatExpiryDate formats the expiry date and filters out null-values
///
///
///
/// The date to format
/// The correctly formatted date
///
/// [cnurse] 9/13/2004 Updated to reflect design changes for Help, 508 support
/// and localisation
///
/// -----------------------------------------------------------------------------
protected string FormatExpiryDate(DateTime expiryDate)
{
string formatExpiryDate = Null.NullString;
try
{
if (!Null.IsNull(expiryDate))
{
if (expiryDate > DateTime.Today)
{
formatExpiryDate = expiryDate.ToShortDateString();
}
else
{
formatExpiryDate = Localization.GetString("Expired", LocalResourceFile);
}
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
return formatExpiryDate;
}
/// -----------------------------------------------------------------------------
///
/// FormatPrice formats the Fee amount and filters out null-values
///
///
///
/// The price to format
/// The correctly formatted price
///
/// [cnurse] 01/18/2007 Created
///
/// -----------------------------------------------------------------------------
protected string FormatPrice(float price, int period, string frequency)
{
string formatPrice = Null.NullString;
try
{
switch (frequency)
{
case "N":
case "":
formatPrice = Localization.GetString("NoFee", LocalResourceFile);
break;
case "O":
formatPrice = FormatPrice(price);
break;
default:
formatPrice = string.Format(Localization.GetString("Fee", LocalResourceFile), FormatPrice(price), period, Localization.GetString("Frequency_" + frequency, LocalResourceFile));
break;
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
return formatPrice;
}
/// -----------------------------------------------------------------------------
///
/// FormatTrial formats the Trial Fee amount and filters out null-values
///
///
///
/// The price to format
/// The correctly formatted price
///
/// [cnurse] 03/28/2007 Created
///
/// -----------------------------------------------------------------------------
protected string FormatTrial(float price, int period, string frequency)
{
string formatTrial = Null.NullString;
try
{
switch (frequency)
{
case "N":
case "":
formatTrial = Localization.GetString("NoFee", LocalResourceFile);
break;
case "O":
formatTrial = FormatPrice(price);
break;
default:
formatTrial = string.Format(Localization.GetString("TrialFee", LocalResourceFile),
FormatPrice(price),
period,
Localization.GetString("Frequency_" + frequency, LocalResourceFile));
break;
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
return formatTrial;
}
/// -----------------------------------------------------------------------------
///
/// FormatURL correctly formats a URL
///
///
///
/// The correctly formatted url
///
/// [cnurse] 9/13/2004 Updated to reflect design changes for Help, 508 support
/// and localisation
///
/// -----------------------------------------------------------------------------
protected string FormatURL()
{
string formatURL = Null.NullString;
try
{
string serverPath = Request.ApplicationPath;
if (!serverPath.EndsWith("/"))
{
serverPath += "/";
}
formatURL = serverPath + "Register.aspx?tabid=" + TabId;
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
return formatURL;
}
/// -----------------------------------------------------------------------------
///
/// ServiceText gets the Service Text (Cancel or Subscribe)
///
///
///
/// The service state
/// The correctly formatted text
///
/// [cnurse] 9/13/2004 Updated to reflect design changes for Help, 508 support
/// and localisation
///
/// -----------------------------------------------------------------------------
protected string ServiceText(bool subscribed, DateTime expiryDate)
{
string serviceText = Null.NullString;
try
{
if (!subscribed)
{
serviceText = Localization.GetString("Subscribe", LocalResourceFile);
}
else
{
serviceText = Localization.GetString("Unsubscribe", LocalResourceFile);
if (!Null.IsNull(expiryDate))
{
if (expiryDate < DateTime.Today)
{
serviceText = Localization.GetString("Renew", LocalResourceFile);
}
}
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
return serviceText;
}
protected bool ShowSubscribe(int roleID)
{
var objRoles = new RoleController();
bool showSubscribe = Null.NullBoolean;
RoleInfo objRole = objRoles.GetRole(roleID, PortalSettings.PortalId);
if (objRole.IsPublic)
{
var objPortals = new PortalController();
PortalInfo objPortal = objPortals.GetPortal(PortalSettings.PortalId);
if (objRole.ServiceFee == 0.0)
{
showSubscribe = true;
}
else if (objPortal != null && !string.IsNullOrEmpty(objPortal.ProcessorUserId))
{
showSubscribe = true;
}
}
return showSubscribe;
}
protected bool ShowTrial(int roleID)
{
var objRoles = new RoleController();
bool showTrial = Null.NullBoolean;
RoleInfo objRole = objRoles.GetRole(roleID, PortalSettings.PortalId);
if (objRole.TrialFrequency == "N" || (objRole.IsPublic && objRole.ServiceFee == 0.0))
{
showTrial = Null.NullBoolean;
}
else if (objRole.IsPublic && objRole.TrialFee == 0.0)
{
//Use Trial?
UserRoleInfo objUserRole = objRoles.GetUserRole(PortalId, UserInfo.UserID, roleID);
if ((objUserRole == null) || (!objUserRole.IsTrialUsed))
{
showTrial = true;
}
}
return showTrial;
}
#endregion
#region "Public Methods"
/// -----------------------------------------------------------------------------
///
/// DataBind binds the data to the controls
///
///
/// [cnurse] 03/13/2006 Created
///
/// -----------------------------------------------------------------------------
public override void DataBind()
{
if (Request.IsAuthenticated)
{
grdServices.DataSource = GetRoles(PortalId, UserInfo.UserID);
grdServices.DataBind();
//if no service available then hide options
ServicesRow.Visible = (grdServices.Items.Count > 0);
}
}
#endregion
#region "Event Methods"
/// -----------------------------------------------------------------------------
///
/// Raises the SubscriptionUpdated Event
///
///
/// [cnurse] 01/17/2006 Created
///
/// -----------------------------------------------------------------------------
public void OnSubscriptionUpdated(SubscriptionUpdatedEventArgs e)
{
if (SubscriptionUpdated != null)
{
SubscriptionUpdated(this, e);
}
}
#endregion
#region "Event Handlers"
/// -----------------------------------------------------------------------------
///
/// Page_Load runs when the control is loaded
///
///
///
///
/// [cnurse] 03/13/2006
///
/// -----------------------------------------------------------------------------
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
cmdRSVP.Click += cmdRSVP_Click;
grdServices.ItemCommand += grdServices_ItemCommand;
try
{
lblRSVP.Text = "";
//If this is the first visit to the page, localize the datalist
if (Page.IsPostBack == false)
{
//Localize the Headers
Localization.LocalizeDataGrid(ref grdServices, LocalResourceFile);
}
}
catch (Exception exc) //Module failed to load
{
Exceptions.ProcessModuleLoadException(this, exc);
}
}
/// -----------------------------------------------------------------------------
///
/// cmdRSVP_Click runs when the Subscribe to RSVP Code Roles Button is clicked
///
///
///
///
/// [cnurse] 01/19/2006 created
///
/// -----------------------------------------------------------------------------
private void cmdRSVP_Click(object sender, EventArgs e)
{
//Get the RSVP code
string code = txtRSVPCode.Text;
bool rsvpCodeExists = false;
if (!String.IsNullOrEmpty(code))
{
//Get the roles from the Database
var objRoles = new RoleController();
ArrayList arrRoles = objRoles.GetPortalRoles(PortalSettings.PortalId);
//Parse the roles
foreach (RoleInfo objRole in arrRoles)
{
if (objRole.RSVPCode == code)
{
objRoles.UpdateUserRole(PortalId, UserInfo.UserID, objRole.RoleID);
rsvpCodeExists = true;
//Raise SubscriptionUpdated Event
OnSubscriptionUpdated(new SubscriptionUpdatedEventArgs(false, objRole.RoleName));
}
}
if (rsvpCodeExists)
{
lblRSVP.Text = Localization.GetString("RSVPSuccess", LocalResourceFile);
//Reset RSVP Code field
txtRSVPCode.Text = "";
}
else
{
lblRSVP.Text = Localization.GetString("RSVPFailure", LocalResourceFile);
}
}
DataBind();
}
protected void grdServices_ItemCommand(object source, DataGridCommandEventArgs e)
{
string commandName = e.CommandName;
int roleID = Convert.ToInt32(e.CommandArgument);
if (commandName == Localization.GetString("Subscribe", LocalResourceFile) || commandName == Localization.GetString("Renew", LocalResourceFile))
{
//Subscribe
Subscribe(roleID, false);
}
else if (commandName == Localization.GetString("Unsubscribe", LocalResourceFile))
{
//Unsubscribe
Subscribe(roleID, true);
}
else if (commandName == Localization.GetString("Unsubscribe", LocalResourceFile))
{
//Unsubscribe
Subscribe(roleID, true);
}
else if (commandName == "UseTrial")
{
//Use Trial
UseTrial(roleID);
}
//Rebind Grid
DataBind();
}
#endregion
#region Nested type: SubscriptionUpdatedEventArgs
/// -----------------------------------------------------------------------------
///
/// The SubscriptionUpdatedEventArgs class provides a customised EventArgs class for
/// the SubscriptionUpdated Event
///
///
/// [cnurse] 01/17/2006 created
///
/// -----------------------------------------------------------------------------
public class SubscriptionUpdatedEventArgs
{
/// -----------------------------------------------------------------------------
///
/// Constructs a new SubscriptionUpdatedEventArgs
///
/// Whether this is a subscription cancellation
///
/// [cnurse] 01/17/2006 created
///
/// -----------------------------------------------------------------------------
public SubscriptionUpdatedEventArgs(bool cancel, string roleName)
{
Cancel = cancel;
RoleName = roleName;
}
/// -----------------------------------------------------------------------------
///
/// Gets and sets whether this was a cancelation
///
///
/// [cnurse] 01/17/2006 created
///
/// -----------------------------------------------------------------------------
public bool Cancel { get; set; }
/// -----------------------------------------------------------------------------
///
/// Gets and sets the RoleName that was (un)subscribed to
///
///
/// [cnurse] 01/17/2006 created
///
/// -----------------------------------------------------------------------------
public string RoleName { get; set; }
}
#endregion
}
}