Page 1 of 1

Manually adding order from admin needs "Create User&quo

Posted: Fri Feb 15, 2008 9:56 am
by AbleMods
When using the Add Order button to create a new customer order from the Admin side, the user record is not created so order history cannot be maintained for the customer.

A "Create User record" checkbox with a password field would be immensely helpful. Right now I have to make the user record first, then create the order and assign the order to the user before saving the order.

Posted: Mon Mar 24, 2008 2:31 am
by m_plugables
This feature will be quite handy. Admin/People/Users/AddUserDialog.ascx can help you as a good reference.

Posted: Mon Mar 24, 2008 4:04 am
by m_plugables
edit Admin/Orders/Default.aspx
and find

Code: Select all

<asp:Button ID="PlaceOrderButton" runat="server" Text="New Order" OnClick="PlaceOrderButton_Click" CausesValidation="true" />
and put this line

Code: Select all

<asp:CheckBox ID="CreateUser" runat="server" Text="Create User" OnCheckedChanged="CreateUser_CheckedChanged"
                                        AutoPostBack="True" />
below it. Now add a new row with the following code just below the current row in which you placed the checkbox.

Code: Select all

<tr>
                                <td colspan="6" style="width: 100%;">
                                    <asp:Panel ID="CreateUserPanel" runat="server" Visible="false">
                                        <table cellpadding="0" cellspacing="0" width="100%">
                                            <tr>
                                                <td>
                                                    <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email" Text="Email:"
                                                        ToolTip="Email address of the user to add."></asp:Label>
                                                    <asp:TextBox ID="Email" runat="server" Columns="30"></asp:TextBox>
                                                    
                                                    <asp:RequiredFieldValidator ID="EmailRequiredFieldValidator" runat="server" ControlToValidate="Email" 
                                                     Text="*" ErrorMessage="Valid Email Required" />
                                                    <asp:RegularExpressionValidator ID="EmailValidator" runat="server" Display="Static"
                                                        ControlToValidate="Email" ValidationExpression="\S+@\S+\.\S+" ErrorMessage="The email address is not properly formatted."
                                                        Text="*"></asp:RegularExpressionValidator>
                                                    
                                                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" Text="Password:"
                                                        ToolTip="New password for the user to add."></asp:Label>
                                                    <asp:TextBox ID="Password" runat="server" TextMode="Password" Columns="20"></asp:TextBox>
                                                    
                                                    <asp:RequiredFieldValidator ID="PasswordRequiredFieldValidator" runat="server" ControlToValidate="Password" 
                                                     Text="*" ErrorMessage="Password Required" />
                                                     <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="Password"
                                                        ValidationGroup="AddUser" OnServerValidate="UserValidation" Text="*" ErrorMessage="Password does not match the Password Policy."></asp:CustomValidator>
                                               
                                                    <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword"
                                                        Text="Retype:" ToolTip="Retype the new password of the user to add."></asp:Label>
                                                    <asp:TextBox ID="ConfirmPassword" runat="server" TextMode="Password" Columns="20"></asp:TextBox>
                                                    
                                                    <asp:RequiredFieldValidator ID="ConfirmRequiredFieldValidator" runat="server" ControlToValidate="ConfirmPassword" 
                                                     Text="*" ErrorMessage="Password Required" />
                                                    <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password"
                                                        ControlToValidate="ConfirmPassword" Display="Static" ErrorMessage="You did not retype the password correctly."
                                                        Text="*" ValidationGroup="AddUser"></asp:CompareValidator>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="validation" align="center">
                                                    <asp:Label ID="ErrorMessage" runat="server" SkinID="ErrorCondition" EnableViewState="false"
                                                        Visible="false"></asp:Label>
                                                    <asp:Label ID="AddedMessage" runat="server" SkinID="GoodCondition" EnableViewState="false"
                                                        Visible="false"></asp:Label>
                                                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="AddUser" />
                                                </td>
                                            </tr>
                                        </table>
                                    </asp:Panel>
                                </td>
                            </tr>
Now edit the Admin/Orders/Default.ascx.cs and following functions

Code: Select all

protected void UserValidation(object source, ServerValidateEventArgs args)
    {
        int groupId = 0;
        CommerceBuilder.Users.Group group = GroupDataSource.Load(groupId);

        PasswordPolicy policy;
        string password = args.Value.ToString();

        args.IsValid = true;
        CustomValidator1.ErrorMessage = "";
        policy = new CustomerPasswordPolicy();

        if (password.Length < policy.MinLength)
        {
            AddPwdValidationError("Password length must be at least " + policy.MinLength.ToString() + " characters.");
            args.IsValid = false;
        }

        if ((policy.RequireUpper) && (!Regex.IsMatch(password, "[A-Z]")))
        {
            AddPwdValidationError("At least one character should be in upper case. ");
            args.IsValid = false;
        }

        if ((policy.RequireLower) && (!Regex.IsMatch(password, "[a-z]")))
        {
            AddPwdValidationError("At least one character should be in lower case. ");
            args.IsValid = false;
        }
        if ((policy.RequireNumber) && (!Regex.IsMatch(password, "[0-9]")))
        {
            AddPwdValidationError("At least one digit is required in the password. ");
            args.IsValid = false;
        }

        if ((policy.RequireSymbol) && (!Regex.IsMatch(password, "[^0-9a-zA-Z]")))
        {
            AddPwdValidationError("At least one symbol is required in the password. ");
            args.IsValid = false;
        }

        if ((policy.RequireNonAlpha) && (!Regex.IsMatch(password, "[^a-zA-Z]")))
        {
            AddPwdValidationError("At least one non-alphabet character is required in password. ");
            args.IsValid = false;
        }
    }

    private void AddPwdValidationError(string message)
    {
        if (CustomValidator1.ErrorMessage.Length > 0)
        {
            CustomValidator1.ErrorMessage = CustomValidator1.ErrorMessage + "<li> " + message + "</li>";
        }
        else
        {
            CustomValidator1.ErrorMessage = message;
        }
    }

    protected int CreateNewUser()
    {
        int userId = 0;
        MembershipCreateStatus status;
        User newUser = UserDataSource.CreateUser(Email.Text, Password.Text, string.Empty, string.Empty, true, 0, out status);
        if (status == MembershipCreateStatus.Success)
        {
            // GET GROUP FOR NEW USER
            int groupId = 0;
            CommerceBuilder.Users.Group group = GroupDataSource.Load(groupId);
            if (group != null) newUser.UserGroups.Add(new UserGroup(newUser.UserId, groupId));
            // SAVE USER AND DISPLAY CONFIRMATION
            newUser.Save();
            userId = newUser.UserId;
        }
        else
        {
            switch (status)
            {
                case MembershipCreateStatus.DuplicateEmail:
                case MembershipCreateStatus.DuplicateUserName:
                    ErrorMessage.Text = "The email address is already registered.";
                    ErrorMessage.Visible = true;
                    break;
                case MembershipCreateStatus.InvalidEmail:
                case MembershipCreateStatus.InvalidUserName:
                    ErrorMessage.Text = "The email address provided is invalid.";
                    ErrorMessage.Visible = true;
                    break;
                case MembershipCreateStatus.InvalidPassword:
                    ErrorMessage.Text = "The password provided is invalid.";
                    ErrorMessage.Visible = true;
                    break;
                default:
                    ErrorMessage.Text = "Unexpected error: " + status.ToString();
                    ErrorMessage.Visible = true;
                    break;
            }
        }
        return userId;
    }
protected void CreateUser_CheckedChanged(object sender, EventArgs e)
    {
        CreateUserPanel.Visible = CreateUser.Checked;
    }
and now modify the
protected void PlaceOrderButton_Click(object sender, EventArgs e)
to look like

Code: Select all

protected void PlaceOrderButton_Click(object sender, EventArgs e)
    {
        Session.Remove("PlaceOrderUserId");
        if (CreateUser.Checked)
        {
            int userId = CreateNewUser();
            if (userId > 0)
            {
                Response.Redirect(String.Format("PlaceOrder1.aspx?UserId={0}", userId));
            }
            else
                return;
        }
        else
            Response.Redirect("PlaceOrder1.aspx");
    }

Re: Manually adding order from admin needs "Create User&quo

Posted: Mon Mar 31, 2008 3:57 pm
by jmestep
Should there be a change in this code for RC3? I'm getting the following error when I try to implement it:
Line 616: {
Line 617: int groupId = 0;
Line 618: CommerceBuilder.Users.Group group = GroupDataSource.Load(groupId);
Line 619:
Line 620: PasswordPolicy policy;

I've tried twice, but I could still be doing something wrong.

Re: Manually adding order from admin needs "Create User&quo

Posted: Tue Apr 01, 2008 1:18 am
by m_plugables
sorry i missed some information in the post.

Re: Manually adding order from admin needs "Create User&quo

Posted: Tue Apr 01, 2008 1:22 am
by m_plugables
This problem is because i forgot to include the name spaces
Edit the file Admin/Orders/Default.aspx.cs and in the top section of page where you will find the using statements add the following statements as well

Code: Select all

using CommerceBuilder.Users;
using System.Text.RegularExpressions;

Re: Manually adding order from admin needs "Create User&quo

Posted: Tue Apr 01, 2008 5:44 am
by jmestep
Thanks, that fixed it.