Search for Users on UserSettings

For general questions and discussions specific to the AbleCommerce GOLD ASP.Net shopping cart software.
Post Reply
mbartens
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 107
Joined: Mon Mar 09, 2015 3:34 am

Search for Users on UserSettings

Post by mbartens » Wed Mar 25, 2015 4:55 am

I'd need to search for users based on their User Settings (ac_UserSettings)

Ideally it would search directly on that.

However since the settings are primarily Yes/No types I could modify my code so that when the user setting is saved it adds them to a group.
If I were going to do that though I would need to search multiple groups at one time.
So, would turning the Group dropdown into a checkbox list handle searching on multiple groups? Thanks!
May

nadeem
Captain (CAPT)
Captain (CAPT)
Posts: 258
Joined: Tue Jul 31, 2012 7:23 pm

Re: Search for Users on UserSettings

Post by nadeem » Thu Mar 26, 2015 1:46 am

I think you may try using a multiple-selection list box instead of check-box list. You can have a look at 'Change Assigned Groups' dialog on edit user page for example.

EDIT: Currently, the user search only supported against a single group. Therefore, you have to update back end logic to search against multiple groups to achieve the desired result.

mbartens
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 107
Joined: Mon Mar 09, 2015 3:34 am

Re: Search for Users on UserSettings

Post by mbartens » Fri Mar 27, 2015 4:38 am

Is there any example I can follow that can do a multi group search? Thanks so much.
May

nadeem
Captain (CAPT)
Captain (CAPT)
Posts: 258
Joined: Tue Jul 31, 2012 7:23 pm

Re: Search for Users on UserSettings

Post by nadeem » Fri Mar 27, 2015 4:55 am

Multi group search is not supported at the moment. You have to modify the code to work it against multi group.

nadeem
Captain (CAPT)
Captain (CAPT)
Posts: 258
Joined: Tue Jul 31, 2012 7:23 pm

Re: Search for Users on UserSettings

Post by nadeem » Fri Mar 27, 2015 6:15 am

Here is an example code for you. Open CommerceBuilder/Users/UserSearchCriteria.cs and made the following updates:

Include the following using statement at the top

Code: Select all

using System.Collections.Generic;
Add a new property to hold list of group ids

Code: Select all

/// <summary>
/// Gets or sets the groups of the user to search for
/// </summary>
public List<int> GroupIds { get; set; }
Add the GroupIds in the constructor to avoid null exception, something like this:

Code: Select all

/// <summary>
/// Initializes a new instance of the UserSearchCriteria class.
/// </summary>
public UserSearchCriteria()
{
   // DEFAULT VALUE
   this.ShowAssignedToGroup = true;
   this.GroupIds = new List<int>();
}
Now go to internal ICriteria GenerateCriteria(bool isCount, string sortExpression) method

Locate the following code:

Code: Select all

// BUILD THE WHERE CRITERIA
            if (this.GroupId > 0)
            {
                if (this.ShowAssignedToGroup)
                {
                    criteria.CreateCriteria("UserGroups", "UG", NHibernate.SqlCommand.JoinType.InnerJoin)
                    .Add(Restrictions.Eq("UG.GroupId", this.GroupId));
                }
                else
                {
                    DetachedCriteria subQuery = DetachedCriteria.For<UserGroup>("UG")
                        .Add(Restrictions.Eq("UG.GroupId", this.GroupId))
                        .SetProjection(Projections.Property("UG.UserId"));
                    criteria.Add(Subqueries.PropertyNotIn("U.Id", subQuery));
                }
            }
and replace with:

Code: Select all

// BUILD THE WHERE CRITERIA
            if (this.GroupId > 0)
            {
                if (this.ShowAssignedToGroup)
                {
                    criteria.CreateCriteria("UserGroups", "UG", NHibernate.SqlCommand.JoinType.InnerJoin)
                    .Add(Restrictions.Eq("UG.GroupId", this.GroupId));
                }
                else
                {
                    DetachedCriteria subQuery = DetachedCriteria.For<UserGroup>("UG")
                        .Add(Restrictions.Eq("UG.GroupId", this.GroupId))
                        .SetProjection(Projections.Property("UG.UserId"));
                    criteria.Add(Subqueries.PropertyNotIn("U.Id", subQuery));
                }
            }
            else if(GroupIds.Count > 0)
            {
                if (this.ShowAssignedToGroup)
                {
                    criteria.CreateCriteria("UserGroups", "UG", NHibernate.SqlCommand.JoinType.InnerJoin)
                    .Add(Restrictions.In("UG.GroupId", this.GroupIds.ToArray()));
                }
                else
                {
                    DetachedCriteria subQuery = DetachedCriteria.For<UserGroup>("UG")
                        .Add(Restrictions.In("UG.GroupId", this.GroupIds.ToArray()))
                        .SetProjection(Projections.Property("UG.UserId"));
                    criteria.Add(Subqueries.PropertyNotIn("U.Id", subQuery));
                }
            }
For testing I have set the groupIds from Admin/People/Users/Default.aspx.cs like this:

Code: Select all

criteria.GroupIds.AddRange(new List<int>() { 1, 2, 3, 4, 5 });
You have to take selected ids from multi select box.

Hope it helps.

mbartens
Lieutenant Commander (LCDR)
Lieutenant Commander (LCDR)
Posts: 107
Joined: Mon Mar 09, 2015 3:34 am

Re: Search for Users on UserSettings

Post by mbartens » Tue Mar 31, 2015 3:12 am

This is working great - thank you!
May

Post Reply