PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/CmsWeb/Areas/Dialog/Models/SearchUsersModel.cs

https://github.com/vs06/bvcms
C# | 124 lines | 113 code | 5 blank | 6 comment | 21 complexity | a505042ef29ce8359d20c629a976df76 MD5 | raw file
  1. /* Author: David Carroll
  2. * Copyright (c) 2008, 2009 Bellevue Baptist Church
  3. * Licensed under the GNU General Public License (GPL v2)
  4. * you may not use this code except in compliance with the License.
  5. * You may obtain a copy of the License at http://bvcms.codeplex.com/license
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Data.Linq;
  11. using System.Web;
  12. using System.Web.Mvc;
  13. using CmsData;
  14. using UtilityExtensions;
  15. using System.Data.Linq.SqlClient;
  16. using System.Web.UI.WebControls;
  17. using System.Text.RegularExpressions;
  18. namespace CmsWeb.Models
  19. {
  20. public class SearchUsersModel
  21. {
  22. public string name {get; set;}
  23. public int maxitems { get; set; }
  24. public int count { get; set; }
  25. public int listcount { get; set; }
  26. public bool singlemode { get; set; }
  27. public bool ordered { get; set; }
  28. public int? topid { get; set; }
  29. public SearchUsersModel()
  30. {
  31. maxitems = 25;
  32. }
  33. private IQueryable<Person> people;
  34. public IQueryable<Person> FetchPeople()
  35. {
  36. if (people != null)
  37. return people;
  38. if (Util2.OrgMembersOnly)
  39. people = DbUtil.Db.OrgMembersOnlyTag2().People(DbUtil.Db);
  40. else if (Util2.OrgLeadersOnly)
  41. people = DbUtil.Db.OrgLeadersOnlyTag2().People(DbUtil.Db);
  42. else
  43. people = DbUtil.Db.People.AsQueryable();
  44. people = people.Where(p => p.Users.Any(uu => uu.UserRoles.Any(ur => ur.Role.RoleName == "Access")));
  45. if (name.HasValue())
  46. {
  47. string First, Last;
  48. Util.NameSplit(name, out First, out Last);
  49. if (First.HasValue())
  50. people = from p in people
  51. where (p.LastName.StartsWith(Last) || p.MaidenName.StartsWith(Last)
  52. || p.LastName.StartsWith(name) || p.MaidenName.StartsWith(name))
  53. && (p.FirstName.StartsWith(First) || p.NickName.StartsWith(First) || p.MiddleName.StartsWith(First))
  54. select p;
  55. else
  56. if (Last.AllDigits())
  57. people = from p in people
  58. where p.PeopleId == Last.ToInt()
  59. select p;
  60. else
  61. people = from p in people
  62. where p.LastName.StartsWith(Last) || p.MaidenName.StartsWith(Last)
  63. || p.LastName.StartsWith(name) || p.MaidenName.StartsWith(name)
  64. select p;
  65. }
  66. return people;
  67. }
  68. public List<UserInfo> PeopleList()
  69. {
  70. var people = FetchPeople();
  71. var list = new List<UserInfo>();
  72. var n = 0;
  73. if (!singlemode)
  74. {
  75. var t = DbUtil.Db.FetchOrCreateTag(Util.SessionId, Util.UserPeopleId, DbUtil.TagTypeId_AddSelected);
  76. n = t.People(DbUtil.Db).Count();
  77. list = CheckedPeopleList(
  78. from p in t.People(DbUtil.Db)
  79. orderby p.PeopleId == topid ? "0" : "1"
  80. select p).ToList();
  81. var ids = list.Select(p => p.PeopleId).ToArray();
  82. people = people.Where(p => !ids.Contains(p.PeopleId));
  83. }
  84. count = people.Count();
  85. people = people.OrderBy(p => p.LastName).ThenBy(p => p.Name);
  86. list.AddRange(PeopleList(people).Take(maxitems));
  87. maxitems += n;
  88. listcount = list.Count;
  89. return list;
  90. }
  91. private IEnumerable<UserInfo> CheckedPeopleList(IQueryable<Person> query)
  92. {
  93. return PeopleList(query, ck: true);
  94. }
  95. private IEnumerable<UserInfo> PeopleList(IQueryable<Person> query)
  96. {
  97. return PeopleList(query, ck: false);
  98. }
  99. private IEnumerable<UserInfo> PeopleList(IQueryable<Person> query, bool ck)
  100. {
  101. var q = from p in query
  102. select new UserInfo
  103. {
  104. PeopleId = p.PeopleId,
  105. Name = p.Name,
  106. EmailAddress = p.EmailAddress,
  107. HasTag = ck
  108. };
  109. return q;
  110. }
  111. public class UserInfo
  112. {
  113. public int PeopleId { get; set; }
  114. public string Name { get; set; }
  115. public bool HasTag { get; set; }
  116. public string EmailAddress { get; set; }
  117. }
  118. }
  119. }