PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/App_Code/StaffBroker/StaffBrokerFunctions.cs

https://github.com/mailekah/AgapeConnect1
C# | 804 lines | 603 code | 188 blank | 13 comment | 165 complexity | 471161cf60648507d365c47484549a39 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using StaffBroker;
  5. using DotNetNuke;
  6. using System.Net;
  7. using System.Text.RegularExpressions;
  8. using DotNetNuke.Services.FileSystem;
  9. public static class MenuLinkType
  10. {
  11. public const int URL = 0;
  12. public const int Page = 1;
  13. public const int Document = 2;
  14. public const int Folder = 3;
  15. public const int Resource = 4;
  16. public const int Conference = 5;
  17. public const int Story = 6;
  18. public const int Blank = 20;
  19. static public string TypeName(int TypeNo)
  20. {
  21. switch (TypeNo)
  22. {
  23. case 0: return "URL";
  24. case 1: return "Page";
  25. case 2: return "Document";
  26. case 3: return "Folder";
  27. case 4: return "Resource";
  28. case 5: return "Coference";
  29. case 6: return "Story";
  30. case 20: return "Blank";
  31. default: return "URL";
  32. }
  33. }
  34. }
  35. public static class CostCentreType
  36. {
  37. public const int Department = 0;
  38. public const int Staff = 1;
  39. public const int Other = 2;
  40. static public string TypeName(int TypeNo)
  41. {
  42. switch (TypeNo)
  43. {
  44. case 0: return "Department";
  45. case 1: return "Staff";
  46. case 2: return "Other";
  47. default: return "Other";
  48. }
  49. }
  50. }
  51. public static class AccountType
  52. {
  53. public const int Normal = 0;
  54. public const int AccountsReceivable = 1;
  55. public const int AccountsPayable = 2;
  56. public const int Income = 3;
  57. public const int Exspnse = 4;
  58. public const int Other = 5;
  59. static public string TypeName(int TypeNo)
  60. {
  61. switch (TypeNo)
  62. {
  63. case 0: return "Not Used";
  64. case 1: return "AccountsReceivable";
  65. case 2: return "AccountsPayable";
  66. case 3: return "Income";
  67. case 4: return "Expense";
  68. case 5: return "Other";
  69. default: return "Normal";
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// Summary description for StaffBrokerFunctions
  75. /// </summary>
  76. ///
  77. public class StaffBrokerFunctions
  78. {
  79. public struct LeaderRelationship
  80. {
  81. public int UserId { get; set; }
  82. public string UserName { get; set; }
  83. public int LeaderId { get; set; }
  84. public string LeaderName { get; set; }
  85. public int DelegateId { get; set; }
  86. public string Delegatename { get; set; }
  87. }
  88. public struct LeaderInfo
  89. {
  90. public int UserId { get; set; }
  91. public string DisplayName { get; set; }
  92. public Boolean isDelegate;
  93. public Boolean hasDelegated;
  94. }
  95. public StaffBrokerFunctions()
  96. {
  97. }
  98. static public void EventLog(string title, string message, int userid)
  99. {
  100. DotNetNuke.Entities.Portals.PortalSettings PS = (DotNetNuke.Entities.Portals.PortalSettings)System.Web.HttpContext.Current.Items["PortalSettings"];
  101. DotNetNuke.Services.Log.EventLog.EventLogController objEventLog = new DotNetNuke.Services.Log.EventLog.EventLogController();
  102. objEventLog.AddLog(title, message, PS, userid, DotNetNuke.Services.Log.EventLog.EventLogController.EventLogType.ADMIN_ALERT);
  103. }
  104. #region Department functions
  105. static public Boolean IsDept(int PortalId, string costCenter)
  106. {
  107. StaffBrokerDataContext d = new StaffBrokerDataContext();
  108. var cc = from c in d.AP_StaffBroker_CostCenters where c.CostCentreCode == costCenter select c.Type;
  109. if (cc.Count() > 0)
  110. return cc.First() == CostCentreType.Department;
  111. else return false;
  112. }
  113. static public String GetDeptGiveToURL(int PortalId, int costCenter)
  114. {
  115. StaffBrokerDataContext d = new StaffBrokerDataContext();
  116. var cc = from c in d.AP_StaffBroker_Departments where c.CostCenterId == costCenter && c.PortalId == PortalId select c.GivingShortcut;
  117. if (cc.Count() > 0)
  118. return cc.First();
  119. else return "";
  120. }
  121. static public string GetDeptPhoto(int deptID)
  122. {
  123. StaffBrokerDataContext d = new StaffBrokerDataContext();
  124. var cc = from c in d.AP_StaffBroker_Departments where c.CostCenterId == deptID select c.PhotoId;
  125. String fileId = cc.First().ToString();
  126. if (fileId == null || fileId.Equals(""))
  127. {
  128. return "/images/no_avatar.gif";
  129. }
  130. else
  131. {
  132. DotNetNuke.Services.FileSystem.IFileInfo theFile = DotNetNuke.Services.FileSystem.FileManager.Instance.GetFile(Convert.ToInt32(fileId));
  133. return DotNetNuke.Services.FileSystem.FileManager.Instance.GetUrl(theFile);
  134. }
  135. }
  136. static public AP_StaffBroker_Department GetDeptByGivingShortcut(String GivingShortcut)
  137. {
  138. StaffBrokerDataContext d = new StaffBrokerDataContext();
  139. var depts = from c in d.AP_StaffBroker_Departments where c.GivingShortcut == GivingShortcut select c;
  140. if (depts.Count() > 0)
  141. {
  142. return depts.First();
  143. }
  144. else
  145. {
  146. return null;
  147. }
  148. }
  149. #endregion
  150. public static decimal CurrencyConvert(decimal amount, string fromCurrency, string toCurrency)
  151. {
  152. string mode = "Yahoo";
  153. WebClient web = new WebClient();
  154. if (mode == "Google")
  155. {
  156. // string url = string.Format("http://www.google.com/ig/calculator?hl=en&q={2}{0}%3D%3F{1}", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
  157. //string response = web.DownloadString(url);
  158. // decimal rate = System.Convert.ToDecimal(response);
  159. return 1;
  160. }
  161. else if (mode == "Yahoo")
  162. {
  163. string url = string.Format("http://download.finance.yahoo.com/d/quotes.csv?s={0}{1}=X&f=l1", fromCurrency.ToUpper(), toCurrency.ToUpper(), amount);
  164. string response = web.DownloadString(url);
  165. decimal rate = System.Convert.ToDecimal(response);
  166. return rate * amount;
  167. }
  168. return 1;
  169. }
  170. static public Boolean VerifyCostCenter(int PortalId, string costCenter)
  171. {
  172. StaffBrokerDataContext d = new StaffBrokerDataContext();
  173. var cc = from c in d.AP_StaffBroker_CostCenters where c.CostCentreCode == costCenter select c;
  174. return cc.Count() > 0;
  175. }
  176. static public Boolean VerifyAccountCode(int PortalId, string AccountCode)
  177. {
  178. StaffBrokerDataContext d = new StaffBrokerDataContext();
  179. var acc = from c in d.AP_StaffBroker_AccountCodes where c.AccountCode == AccountCode select c;
  180. return acc.Count() > 0;
  181. }
  182. static public AP_StaffBroker_Staff CreateStaffMember(int PortalId, DotNetNuke.Entities.Users.UserInfo User1in, DotNetNuke.Entities.Users.UserInfo User2in, short staffTypeIn)
  183. {
  184. //Create Married Staff
  185. DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();
  186. if (rc.GetRoleByName(PortalId, "Staff") == null)
  187. {
  188. DotNetNuke.Security.Roles.RoleInfo insert = new DotNetNuke.Security.Roles.RoleInfo();
  189. insert.Description = "Staff Members";
  190. insert.RoleName = "Staff";
  191. insert.AutoAssignment = false;
  192. insert.IsPublic = false;
  193. insert.RoleGroupID = -1;
  194. insert.PortalID = PortalId;
  195. rc.AddRole(insert);
  196. }
  197. rc.AddUserRole(PortalId, User1in.UserID, rc.GetRoleByName(PortalId, "Staff").RoleID, DateTime.MaxValue);
  198. rc.AddUserRole(PortalId, User2in.UserID, rc.GetRoleByName(PortalId, "Staff").RoleID, DateTime.MaxValue);
  199. StaffBrokerDataContext d = new StaffBrokerDataContext();
  200. var searchStaff = from c in d.AP_StaffBroker_Staffs where c.Active && (c.UserId1 == User1in.UserID || c.UserId2 == User1in.UserID || c.UserId1 == User2in.UserID || c.UserId2 == User2in.UserID) select c;
  201. if (searchStaff.Count() > 0)
  202. return searchStaff.First();
  203. AP_StaffBroker_Staff rtn = new AP_StaffBroker_Staff();
  204. rtn.UserId1 = User1in.UserID;
  205. rtn.UserId2 = User2in.UserID;
  206. rtn.PortalId = PortalId;
  207. rtn.Active = true;
  208. rtn.DisplayName = User1in.FirstName + " & " + User2in.FirstName + " " + User1in.LastName;
  209. rtn.StaffTypeId = staffTypeIn;
  210. rtn.CostCenter = "";
  211. d.AP_StaffBroker_Staffs.InsertOnSubmit(rtn);
  212. d.SubmitChanges();
  213. return rtn;
  214. }
  215. static public void ChangeUsername(string OldUsername, string NewUsername)
  216. {
  217. StaffBrokerDataContext d = new StaffBrokerDataContext();
  218. d.Agape_Main_AlterUserName(OldUsername, NewUsername);
  219. }
  220. static public String ValidateAccountCode(string AccountCode, int PortalId)
  221. {
  222. StaffBrokerDataContext d = new StaffBrokerDataContext();
  223. return (from c in d.AP_StaffBroker_AccountCodes where c.AccountCode == AccountCode && c.PortalId == PortalId select c).Count() > 0 ? AccountCode : "";
  224. }
  225. static public String ValidateCostCenter(string CostCenter, int PortalId)
  226. {
  227. StaffBrokerDataContext d = new StaffBrokerDataContext();
  228. return (from c in d.AP_StaffBroker_CostCenters where c.CostCentreCode == CostCenter && c.PortalId == PortalId select c).Count() > 0 ? CostCenter : "";
  229. }
  230. static public AP_StaffBroker_Staff CreateStaffMember(int PortalId, DotNetNuke.Entities.Users.UserInfo User1in, short staffTypeIn = 1)
  231. {
  232. DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();
  233. if (rc.GetRoleByName(PortalId, "Staff") == null)
  234. {
  235. DotNetNuke.Security.Roles.RoleInfo insert = new DotNetNuke.Security.Roles.RoleInfo();
  236. insert.Description = "Staff Members";
  237. insert.RoleName = "Staff";
  238. insert.AutoAssignment = false;
  239. insert.IsPublic = false;
  240. insert.RoleGroupID = -1;
  241. insert.PortalID = PortalId;
  242. rc.AddRole(insert);
  243. }
  244. rc.AddUserRole(PortalId, User1in.UserID, rc.GetRoleByName(PortalId, "Staff").RoleID, DateTime.MaxValue);
  245. StaffBrokerDataContext d = new StaffBrokerDataContext();
  246. var searchStaff = from c in d.AP_StaffBroker_Staffs where c.Active && (c.UserId1 == User1in.UserID || c.UserId2 == User1in.UserID) select c;
  247. if (searchStaff.Count() > 0)
  248. return searchStaff.First();
  249. //Create Single Staff
  250. AP_StaffBroker_Staff rtn = new AP_StaffBroker_Staff();
  251. rtn.UserId1 = User1in.UserID;
  252. rtn.UserId2 = -2;
  253. rtn.DisplayName = User1in.FirstName + " " + User1in.LastName;
  254. rtn.StaffTypeId = staffTypeIn;
  255. rtn.CostCenter = "";
  256. rtn.PortalId = PortalId;
  257. rtn.Active = true;
  258. d.AP_StaffBroker_Staffs.InsertOnSubmit(rtn);
  259. d.SubmitChanges();
  260. return rtn;
  261. }
  262. static public AP_StaffBroker_Staff CreateStaffMember(int PortalId, DotNetNuke.Entities.Users.UserInfo User1in, string SpouseName, DateTime SpouseDOB, short staffTypeIn = 1)
  263. {
  264. DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();
  265. if (rc.GetRoleByName(PortalId, "Staff") == null)
  266. {
  267. DotNetNuke.Security.Roles.RoleInfo insert = new DotNetNuke.Security.Roles.RoleInfo();
  268. insert.Description = "Staff Members";
  269. insert.RoleName = "Staff";
  270. insert.AutoAssignment = false;
  271. insert.IsPublic = false;
  272. insert.RoleGroupID = -1;
  273. insert.PortalID = PortalId;
  274. rc.AddRole(insert);
  275. }
  276. rc.AddUserRole(PortalId, User1in.UserID, rc.GetRoleByName(PortalId, "Staff").RoleID, DateTime.MaxValue);
  277. StaffBrokerDataContext d = new StaffBrokerDataContext();
  278. var searchStaff = from c in d.AP_StaffBroker_Staffs where c.Active && (c.UserId1 == User1in.UserID || c.UserId2 == User1in.UserID) select c;
  279. if (searchStaff.Count() > 0)
  280. return searchStaff.First();
  281. //Create Married to Non-Staff
  282. AP_StaffBroker_Staff rtn = new AP_StaffBroker_Staff();
  283. rtn.UserId1 = User1in.UserID;
  284. rtn.UserId2 = -1;
  285. rtn.DisplayName = User1in.FirstName + " " + User1in.LastName;
  286. rtn.StaffTypeId = staffTypeIn;
  287. rtn.CostCenter = "";
  288. rtn.PortalId = PortalId;
  289. rtn.Active = true;
  290. d.AP_StaffBroker_Staffs.InsertOnSubmit(rtn);
  291. d.SubmitChanges();
  292. //Now add Spouse data
  293. AddProfileValue(PortalId, rtn.StaffId, "SpouseDOB", SpouseDOB.ToShortDateString());
  294. AddProfileValue(PortalId, rtn.StaffId, "SpouseName", SpouseName);
  295. return rtn;
  296. }
  297. static public void AddProfileValue(int PortalId, int staffId, string propertyName, string propertyValue, int Type = 0)
  298. {
  299. StaffBrokerDataContext d = new StaffBrokerDataContext();
  300. AP_StaffBroker_StaffProfile insert = new AP_StaffBroker_StaffProfile();
  301. var definition = (from c in d.AP_StaffBroker_StaffPropertyDefinitions where c.PropertyName == propertyName && c.PortalId == PortalId select c.StaffPropertyDefinitionId);
  302. if (definition.Count() == 0)
  303. {
  304. AP_StaffBroker_StaffPropertyDefinition newDef = new AP_StaffBroker_StaffPropertyDefinition();
  305. newDef.PortalId = PortalId;
  306. newDef.PropertyName = propertyName;
  307. newDef.Display = false;
  308. newDef.ViewOrder = (short?)((from c in d.AP_StaffBroker_StaffPropertyDefinitions where c.PortalId == PortalId select c.ViewOrder).Max() + 1);
  309. newDef.Type = Convert.ToByte(Type);
  310. newDef.PropertyHelp = "";
  311. d.AP_StaffBroker_StaffPropertyDefinitions.InsertOnSubmit(newDef);
  312. d.SubmitChanges();
  313. insert.StaffPropertyDefinitionId = newDef.StaffPropertyDefinitionId;
  314. }
  315. else
  316. insert.StaffPropertyDefinitionId = definition.First();
  317. insert.StaffId = staffId;
  318. insert.PropertyValue = propertyValue;
  319. var existing = from c in d.AP_StaffBroker_StaffProfiles where c.StaffId == insert.StaffId && c.StaffPropertyDefinitionId == insert.StaffPropertyDefinitionId select c;
  320. if (existing.Count() > 0)
  321. existing.First().PropertyValue = propertyValue;
  322. else
  323. d.AP_StaffBroker_StaffProfiles.InsertOnSubmit(insert);
  324. d.SubmitChanges();
  325. }
  326. static public string GetStaffProfileProperty(int staffId, string propertyName)
  327. {
  328. StaffBrokerDataContext d = new StaffBrokerDataContext();
  329. var existing = from c in d.AP_StaffBroker_StaffProfiles where c.StaffId == staffId && c.AP_StaffBroker_StaffPropertyDefinition.PropertyName == propertyName select c.PropertyValue;
  330. if (existing.Count() == 0)
  331. return "";
  332. else
  333. return existing.First();
  334. }
  335. static public string GetStaffProfileProperty(ref AP_StaffBroker_Staff Staffin, string propertyName)
  336. {
  337. var existing = from c in Staffin.AP_StaffBroker_StaffProfiles where c.AP_StaffBroker_StaffPropertyDefinition.PropertyName == propertyName select c.PropertyValue;
  338. if (existing.Count() == 0)
  339. return "";
  340. else
  341. return existing.First();
  342. }
  343. static public string GetStaffJointPhoto(int staffID)
  344. {
  345. String fileId = GetStaffProfileProperty(staffID, "JointPhoto");
  346. if (fileId == null || fileId.Equals("") || (GetStaffProfileProperty(staffID, "UnNamedStaff") == "True"))
  347. {
  348. return "/images/no_avatar.gif";
  349. }
  350. else
  351. {
  352. DotNetNuke.Services.FileSystem.IFileInfo theFile = DotNetNuke.Services.FileSystem.FileManager.Instance.GetFile(Convert.ToInt32(fileId));
  353. return DotNetNuke.Services.FileSystem.FileManager.Instance.GetUrl(theFile);
  354. }
  355. }
  356. static public AP_StaffBroker_Staff GetStaffbyStaffId(int StaffId)
  357. {
  358. StaffBrokerDataContext d = new StaffBrokerDataContext();
  359. var q = from c in d.AP_StaffBroker_Staffs where c.StaffId == StaffId select c;
  360. if (q.Count() > 0)
  361. return q.First();
  362. else
  363. return null;
  364. }
  365. static public AP_StaffBroker_Staff GetStaffMember(int UserId)
  366. {
  367. StaffBrokerDataContext d = new StaffBrokerDataContext();
  368. var q = from c in d.AP_StaffBroker_Staffs where c.UserId1 == UserId || c.UserId2 == UserId select c;
  369. if (q.Count() > 0)
  370. return q.First();
  371. else
  372. return null;
  373. }
  374. static public DotNetNuke.Entities.Users.UserInfo CreateUser(int PortalId, String GCXUsername, String FirstName, String LastName)
  375. {
  376. DotNetNuke.Security.Membership.UserCreateStatus objUserCreateStatus;
  377. DotNetNuke.Entities.Users.UserInfo objUserInfo = new DotNetNuke.Entities.Users.UserInfo();
  378. objUserInfo.FirstName = FirstName;
  379. objUserInfo.LastName = LastName;
  380. objUserInfo.DisplayName = FirstName + " " + LastName;
  381. objUserInfo.Username = GCXUsername + PortalId.ToString();
  382. objUserInfo.PortalID = PortalId;
  383. objUserInfo.Membership.Password = DotNetNuke.Entities.Users.UserController.GeneratePassword(8);
  384. objUserInfo.Email = GCXUsername;
  385. objUserCreateStatus = DotNetNuke.Entities.Users.UserController.CreateUser(ref objUserInfo);
  386. if (objUserCreateStatus == DotNetNuke.Security.Membership.UserCreateStatus.Success || objUserCreateStatus == DotNetNuke.Security.Membership.UserCreateStatus.UsernameAlreadyExists || objUserCreateStatus == DotNetNuke.Security.Membership.UserCreateStatus.UserAlreadyRegistered)
  387. return objUserInfo;
  388. else
  389. return null;
  390. }
  391. static public IQueryable<StaffBroker.User> GetStaff(params int[] ExcludeIDs)
  392. {
  393. DotNetNuke.Entities.Portals.PortalSettings PS = (DotNetNuke.Entities.Portals.PortalSettings)System.Web.HttpContext.Current.Items["PortalSettings"];
  394. StaffBrokerDataContext d = new StaffBrokerDataContext();
  395. var q = from c in d.Users where c.AP_StaffBroker_Staffs.Active && c.AP_StaffBroker_Staffs.PortalId == PS.PortalId && !(ExcludeIDs.Contains(c.UserID)) select c;
  396. q = q.Union(from c in d.Users join b in d.AP_StaffBroker_Staffs on c.UserID equals b.UserId2 where b.Active && b.PortalId == PS.PortalId && !(ExcludeIDs.Contains(c.UserID)) select c);
  397. return q.OrderBy(c => c.LastName).ThenBy(c => c.FirstName);
  398. }
  399. static public IQueryable<StaffBroker.User> GetStaffExcl(int PortalId = 0, params string[] ExcludeTypes)
  400. {
  401. StaffBrokerDataContext d = new StaffBrokerDataContext();
  402. var q = from c in d.Users where c.AP_StaffBroker_Staffs.Active && c.AP_StaffBroker_Staffs.PortalId == PortalId && !(ExcludeTypes.Contains(c.AP_StaffBroker_Staffs.AP_StaffBroker_StaffType.Name)) select c;
  403. q = q.Union(from c in d.Users join b in d.AP_StaffBroker_Staffs on c.UserID equals b.UserId2 where b.Active && b.PortalId == PortalId && !(ExcludeTypes.Contains(b.AP_StaffBroker_StaffType.Name)) select c);
  404. return q.OrderBy(c => c.LastName).ThenBy(c => c.FirstName);
  405. }
  406. static public IQueryable<StaffBroker.User> GetStaffIncl(int PortalId = 0, params string[] IncludeTypes)
  407. {
  408. StaffBrokerDataContext d = new StaffBrokerDataContext();
  409. var q = from c in d.Users where c.AP_StaffBroker_Staffs.Active && c.AP_StaffBroker_Staffs.PortalId == PortalId && (IncludeTypes.Contains(c.AP_StaffBroker_Staffs.AP_StaffBroker_StaffType.Name)) select c;
  410. q = q.Union(from c in d.Users join b in d.AP_StaffBroker_Staffs on c.UserID equals b.UserId2 where b.Active && b.PortalId == PortalId && (IncludeTypes.Contains(b.AP_StaffBroker_StaffType.Name)) select c);
  411. return q.OrderBy(c => c.LastName).ThenBy(c => c.FirstName);
  412. }
  413. static public List<LeaderRelationship> GetReportsTo(int UserId)
  414. {
  415. StaffBrokerDataContext d = new StaffBrokerDataContext();
  416. var s = from c in d.AP_StaffBroker_LeaderMetas where c.UserId == UserId select c;
  417. List<LeaderRelationship> rtn = new List<LeaderRelationship>();
  418. foreach (StaffBroker.AP_StaffBroker_LeaderMeta row in s)
  419. {
  420. LeaderRelationship x = new LeaderRelationship();
  421. x.UserId = row.UserId;
  422. x.UserName = row.User.DisplayName;
  423. x.LeaderId = row.LeaderId;
  424. x.LeaderName = row.Leaders.DisplayName;
  425. x.DelegateId = -1;
  426. x.Delegatename = "";
  427. if (row.DelegateId != null)
  428. {
  429. x.DelegateId = (int)row.DelegateId;
  430. x.Delegatename = row.Delegate.DisplayName;
  431. }
  432. rtn.Add(x);
  433. }
  434. return rtn;
  435. }
  436. static public List<User> GetTeam(int LeaderId)
  437. {
  438. StaffBrokerDataContext d = new StaffBrokerDataContext();
  439. var s = from c in d.AP_StaffBroker_LeaderMetas where c.LeaderId == LeaderId || c.DelegateId == LeaderId select c.User;
  440. return s.ToList();
  441. }
  442. static public List<LeaderInfo> GetLeadersDetailed(int UserId, int PortalId)
  443. {
  444. StaffBrokerDataContext d = new StaffBrokerDataContext();
  445. var s = from c in d.AP_StaffBroker_LeaderMetas where c.UserId == UserId select new { UserId = c.LeaderId, hasDelegated = c.DelegateId != null, isDelegate = false };
  446. s = s.Union(from c in d.AP_StaffBroker_LeaderMetas where (c.UserId == UserId) && !(c.DelegateId == null) select new { UserId = (int)c.DelegateId, hasDelegated = false, isDelegate = true });
  447. List<LeaderInfo> rtn = new List<LeaderInfo>();
  448. foreach (var row in s)
  449. {
  450. LeaderInfo insert = new LeaderInfo();
  451. insert.UserId = row.UserId;
  452. insert.DisplayName = DotNetNuke.Entities.Users.UserController.GetUserById(PortalId, row.UserId).DisplayName;
  453. insert.isDelegate = row.isDelegate;
  454. insert.hasDelegated = row.hasDelegated;
  455. rtn.Add(insert);
  456. }
  457. return rtn;
  458. }
  459. static public List<int> GetLeaders(int UserId, Boolean includeDelegates = true)
  460. {
  461. StaffBrokerDataContext d = new StaffBrokerDataContext();
  462. var s = from c in d.AP_StaffBroker_LeaderMetas where c.UserId == UserId select c.LeaderId;
  463. if (includeDelegates)
  464. s = s.Union(from c in d.AP_StaffBroker_LeaderMetas where (c.UserId == UserId) && !(c.DelegateId == null) select (int)c.DelegateId);
  465. return s.ToList();
  466. }
  467. static public List<AP_StaffBroker_Department> GetDepartments(int UserId)
  468. {
  469. StaffBrokerDataContext d = new StaffBrokerDataContext();
  470. List<AP_StaffBroker_Department> q;
  471. q = (from c in d.AP_StaffBroker_Departments where c.CostCentreManager == UserId || c.CostCentreDelegate == UserId select c).ToList();
  472. return q;
  473. }
  474. static public string GetFormattedCurrency(int PortalId, string Value)
  475. {
  476. string cur = GetSetting("Currency", PortalId);
  477. if (GetSetting("CurrencyLocation", PortalId) == "Right")
  478. {
  479. return Value + " " + cur;
  480. }
  481. else
  482. {
  483. return cur + Value;
  484. }
  485. }
  486. static public int GetSpouseId(int UserId)
  487. {
  488. StaffBrokerDataContext d = new StaffBrokerDataContext();
  489. var q = from c in d.AP_StaffBroker_Staffs where c.UserId1 == UserId select c.UserId2;
  490. if (q.Count() > 0)
  491. return (int)(q.First());
  492. else
  493. {
  494. var r = from c in d.AP_StaffBroker_Staffs where c.UserId2 == UserId select c.UserId1;
  495. if (r.Count() > 0)
  496. return r.First();
  497. else
  498. return -1;
  499. }
  500. }
  501. static public string GetTemplate(string templateName, int PortalId)
  502. {
  503. TemplatesDataContext d = new TemplatesDataContext();
  504. var q = from c in d.AP_StaffBroker_Templates where c.PortalId == PortalId && c.TemplateName == templateName select c.TemplateHTML;
  505. if (q.Count() > 0)
  506. {
  507. var PS = DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings();
  508. string Ministry = PS.PortalName;
  509. string root = DotNetNuke.Common.Globals.NavigateURL(PS.HomeTabId);
  510. root = root.Substring(0, root.IndexOf("/"));
  511. string logo = "";
  512. if (PS.LogoFile.Contains("http"))
  513. logo = PS.LogoFile;
  514. else
  515. logo = "http://" + PS.PortalAlias.HTTPAlias + PS.HomeDirectory + PS.LogoFile;
  516. string loginURL = DotNetNuke.Common.Globals.NavigateURL(PS.LoginTabId);
  517. return q.First().Replace("[MINISTRY]", Ministry).Replace("[LOGOURL]", logo).Replace("[LOGINURL]", loginURL);
  518. }
  519. else
  520. return "";
  521. }
  522. static public void SetSetting(string SettingName, string value, int portalId)
  523. {
  524. StaffBrokerDataContext d = new StaffBrokerDataContext();
  525. var q = from c in d.AP_StaffBroker_Settings where c.SettingName == SettingName && c.PortalId == portalId select c;
  526. if (q.Count() == 0)
  527. {
  528. AP_StaffBroker_Setting insert = new AP_StaffBroker_Setting();
  529. insert.SettingName = SettingName;
  530. insert.SettingValue = value;
  531. insert.PortalId = portalId;
  532. d.AP_StaffBroker_Settings.InsertOnSubmit(insert);
  533. }
  534. else
  535. {
  536. q.First().SettingValue = value;
  537. }
  538. d.SubmitChanges();
  539. }
  540. static public string GetSetting(string SettingName, int portalId)
  541. {
  542. try
  543. {
  544. StaffBrokerDataContext d = new StaffBrokerDataContext();
  545. var q = from c in d.AP_StaffBroker_Settings where c.SettingName == SettingName && c.PortalId == portalId select c;
  546. if (q.Count() == 0)
  547. return "";
  548. else if (q.First().SettingValue == null)
  549. return "";
  550. else
  551. return q.First().SettingValue;
  552. }
  553. catch (Exception)
  554. {
  555. return "";
  556. }
  557. }
  558. static public void SetUserProfileProperty(int PortalId, int UserId, String PropertyName, String PropertyValue, int DataType = 349)
  559. {
  560. var pd = DotNetNuke.Entities.Profile.ProfileController.GetPropertyDefinitionByName(PortalId, PropertyName);
  561. if (pd == null)
  562. {
  563. DotNetNuke.Entities.Profile.ProfilePropertyDefinition insert = new DotNetNuke.Entities.Profile.ProfilePropertyDefinition(PortalId);
  564. insert.DefaultValue = "";
  565. insert.Deleted = false;
  566. insert.DataType = DataType;
  567. insert.PropertyCategory = "Other";
  568. insert.PropertyName = PropertyName;
  569. insert.Length = 50;
  570. insert.Required = false;
  571. insert.ViewOrder = 200;
  572. insert.Visible = true;
  573. DotNetNuke.Entities.Profile.ProfileController.AddPropertyDefinition(insert);
  574. }
  575. DotNetNuke.Entities.Users.UserInfo theUser = DotNetNuke.Entities.Users.UserController.GetUserById(PortalId, UserId);
  576. theUser.Profile.SetProfileProperty(PropertyName, PropertyValue);
  577. DotNetNuke.Entities.Users.UserController.UpdateUser(PortalId, theUser);
  578. }
  579. static public string CreateUniqueFileName(IFolderInfo theFolder, string ext)
  580. {
  581. string allChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
  582. string uniqueCode = "";
  583. System.Text.StringBuilder str = new System.Text.StringBuilder();
  584. int xx;
  585. Random r = new Random();
  586. while (uniqueCode == "" || FileManager.Instance.FileExists(theFolder, uniqueCode == "" ? "X" : uniqueCode))
  587. {
  588. for (Byte i = 1; i <= 10; i++)
  589. {
  590. xx = r.Next(0, allChars.Length);
  591. str.Append(allChars.Trim()[xx]);
  592. }
  593. uniqueCode = str.ToString() + "." + ext;
  594. }
  595. return uniqueCode;
  596. }
  597. static public string GetStaffJointPhotoByFileId(int staffID, int fileId)
  598. {
  599. if (GetStaffProfileProperty(staffID, "UnNamedStaff") != "True")
  600. {
  601. // fileId may be 0 if no photo defined, then theFile would be null
  602. DotNetNuke.Services.FileSystem.IFileInfo theFile = DotNetNuke.Services.FileSystem.FileManager.Instance.GetFile(fileId);
  603. if (theFile != null)
  604. return DotNetNuke.Services.FileSystem.FileManager.Instance.GetUrl(theFile);
  605. }
  606. return "/images/no_avatar.gif";
  607. }
  608. static public string GetDeptPhotoByFileId(int fileId)
  609. {
  610. // fileId may be 0 if no photo defined, then theFile would be null
  611. DotNetNuke.Services.FileSystem.IFileInfo theFile = DotNetNuke.Services.FileSystem.FileManager.Instance.GetFile((int)fileId);
  612. if (theFile != null)
  613. return DotNetNuke.Services.FileSystem.FileManager.Instance.GetUrl(theFile);
  614. return "/images/no_avatar.gif";
  615. }
  616. }