static public List <LeaderRelationship> GetReportsTo(int UserId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var s = from c in d.AP_StaffBroker_LeaderMetas where c.UserId == UserId select c;
        List <LeaderRelationship> rtn = new List <LeaderRelationship>();

        foreach (StaffBroker.AP_StaffBroker_LeaderMeta row in s)
        {
            LeaderRelationship x = new LeaderRelationship();
            x.UserId       = row.UserId;
            x.UserName     = row.User.DisplayName;
            x.LeaderId     = row.LeaderId;
            x.LeaderName   = row.Leaders.DisplayName;
            x.DelegateId   = -1;
            x.Delegatename = "";
            if (row.DelegateId != null)
            {
                x.DelegateId   = (int)row.DelegateId;
                x.Delegatename = row.Delegate.DisplayName;
            }

            rtn.Add(x);
        }

        return(rtn);
    }
    static public Boolean VerifyAccountCode(int PortalId, string AccountCode)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var acc = from c in d.AP_StaffBroker_AccountCodes where c.AccountCode == AccountCode select c;

        return(acc.Count() > 0);
    }
    static public Boolean VerifyCostCenter(int PortalId, string costCenter)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var cc = from c in d.AP_StaffBroker_CostCenters where c.CostCentreCode == costCenter select c;

        return(cc.Count() > 0);
    }
    static public string GetSetting(string SettingName, int portalId)
    {
        try
        {
            StaffBrokerDataContext d = new StaffBrokerDataContext();

            var q = from c in d.AP_StaffBroker_Settings where c.SettingName == SettingName && c.PortalId == portalId select c;

            if (q.Count() == 0)
            {
                return("");
            }
            else if (q.First().SettingValue == null)
            {
                return("");
            }
            else
            {
                return(q.First().SettingValue);
            }
        }
        catch (Exception)
        {
            return("");
        }
    }
    static public IQueryable <StaffBroker.User> GetStaffIncl(int PortalId = 0, params string[] IncludeTypes)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        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;

        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);
        return(q.OrderBy(c => c.LastName).ThenBy(c => c.FirstName));
    }
    static public List <User> GetTeam(int LeaderId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var s = from c in d.AP_StaffBroker_LeaderMetas where c.LeaderId == LeaderId || c.DelegateId == LeaderId select c.User;


        return(s.ToList());
    }
    static public IQueryable <StaffBroker.User> GetStaff(params int[] ExcludeIDs)
    {
        DotNetNuke.Entities.Portals.PortalSettings PS = (DotNetNuke.Entities.Portals.PortalSettings)System.Web.HttpContext.Current.Items["PortalSettings"];

        StaffBrokerDataContext d = new StaffBrokerDataContext();
        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;

        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);
        return(q.OrderBy(c => c.LastName).ThenBy(c => c.FirstName));
    }
    static public List <AP_StaffBroker_Department> GetDepartments(int UserId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        List <AP_StaffBroker_Department> q;

        q = (from c in d.AP_StaffBroker_Departments where c.CostCentreManager == UserId || c.CostCentreDelegate == UserId select c).ToList();

        return(q);
    }
    static public List <int> GetLeaders(int UserId, Boolean includeDelegates = true)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var s = from c in d.AP_StaffBroker_LeaderMetas where c.UserId == UserId select c.LeaderId;

        if (includeDelegates)
        {
            s = s.Union(from c in d.AP_StaffBroker_LeaderMetas where (c.UserId == UserId) && !(c.DelegateId == null)select(int) c.DelegateId);
        }

        return(s.ToList());
    }
    static public AP_StaffBroker_Staff CreateStaffMember(int PortalId, DotNetNuke.Entities.Users.UserInfo User1in, DotNetNuke.Entities.Users.UserInfo User2in, short staffTypeIn)
    {
        //Create Married Staff


        DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();
        if (rc.GetRoleByName(PortalId, "Staff") == null)
        {
            DotNetNuke.Security.Roles.RoleInfo insert = new DotNetNuke.Security.Roles.RoleInfo();
            insert.Description    = "Staff Members";
            insert.RoleName       = "Staff";
            insert.AutoAssignment = false;
            insert.IsPublic       = false;
            insert.RoleGroupID    = -1;
            insert.PortalID       = PortalId;
            rc.AddRole(insert);
        }

        rc.AddUserRole(PortalId, User1in.UserID, rc.GetRoleByName(PortalId, "Staff").RoleID, DateTime.MaxValue);
        rc.AddUserRole(PortalId, User2in.UserID, rc.GetRoleByName(PortalId, "Staff").RoleID, DateTime.MaxValue);



        StaffBrokerDataContext d = new StaffBrokerDataContext();
        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;

        if (searchStaff.Count() > 0)
        {
            return(searchStaff.First());
        }



        AP_StaffBroker_Staff rtn = new AP_StaffBroker_Staff();

        rtn.UserId1     = User1in.UserID;
        rtn.UserId2     = User2in.UserID;
        rtn.PortalId    = PortalId;
        rtn.Active      = true;
        rtn.DisplayName = User1in.FirstName + " & " + User2in.FirstName + " " + User1in.LastName;

        rtn.StaffTypeId = staffTypeIn;
        rtn.CostCenter  = "";

        d.AP_StaffBroker_Staffs.InsertOnSubmit(rtn);
        d.SubmitChanges();



        return(rtn);
    }
    static public string GetStaffProfileProperty(int staffId, string propertyName)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var existing             = from c in d.AP_StaffBroker_StaffProfiles where c.StaffId == staffId && c.AP_StaffBroker_StaffPropertyDefinition.PropertyName == propertyName select c.PropertyValue;

        if (existing.Count() == 0)
        {
            return("");
        }
        else
        {
            return(existing.First());
        }
    }
    static public AP_StaffBroker_Staff GetStaffMember(int UserId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var q = from c in d.AP_StaffBroker_Staffs where c.UserId1 == UserId || c.UserId2 == UserId select c;

        if (q.Count() > 0)
        {
            return(q.First());
        }
        else
        {
            return(null);
        }
    }
    static public AP_StaffBroker_Staff GetStaffbyStaffId(int StaffId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var q = from c in d.AP_StaffBroker_Staffs where c.StaffId == StaffId select c;

        if (q.Count() > 0)
        {
            return(q.First());
        }
        else
        {
            return(null);
        }
    }
    static public AP_StaffBroker_Department GetDeptByGivingShortcut(String GivingShortcut)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var depts = from c in d.AP_StaffBroker_Departments where c.GivingShortcut == GivingShortcut select c;

        if (depts.Count() > 0)
        {
            return(depts.First());
        }
        else
        {
            return(null);
        }
    }
    static public Boolean IsDept(int PortalId, string costCenter)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var cc = from c in d.AP_StaffBroker_CostCenters where c.CostCentreCode == costCenter select c.Type;

        if (cc.Count() > 0)
        {
            return(cc.First() == CostCentreType.Department);
        }

        else
        {
            return(false);
        }
    }
    static public String GetDeptGiveToURL(int PortalId, int costCenter)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var cc = from c in d.AP_StaffBroker_Departments where c.CostCenterId == costCenter && c.PortalId == PortalId select c.GivingShortcut;

        if (cc.Count() > 0)
        {
            return(cc.First());
        }

        else
        {
            return("");
        }
    }
    static public string GetDeptPhoto(int deptID)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var    cc     = from c in d.AP_StaffBroker_Departments where c.CostCenterId == deptID select c.PhotoId;
        String fileId = cc.First().ToString();

        if (fileId == null || fileId.Equals(""))
        {
            return("/images/no_avatar.gif");
        }
        else
        {
            DotNetNuke.Services.FileSystem.IFileInfo theFile = DotNetNuke.Services.FileSystem.FileManager.Instance.GetFile(Convert.ToInt32(fileId));
            return(DotNetNuke.Services.FileSystem.FileManager.Instance.GetUrl(theFile));
        }
    }
Пример #18
0
    static public AP_StaffBroker_Department CreateDept(string Name, string RC, int manager, int?del)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        DotNetNuke.Entities.Portals.PortalSettings PS = (DotNetNuke.Entities.Portals.PortalSettings)System.Web.HttpContext.Current.Items["PortalSettings"];
        if (del == 0)
        {
            del = null;
        }
        var q = from c in d.AP_StaffBroker_Departments where c.CostCentre == RC && c.PortalId == PS.PortalId select c;

        if (q.Count() > 0)
        {
            q.First().Name = Name;
            q.First().CostCentreManager  = manager;
            q.First().CostCentreDelegate = del;
            d.SubmitChanges();
            return(q.First());
        }
        else
        {
            AP_StaffBroker_Department insert = new AP_StaffBroker_Department();
            insert.PortalId          = PS.PortalId;
            insert.Name              = Name;
            insert.CostCentre        = RC;
            insert.CostCentreManager = manager;
            insert.CanRmb            = true;
            insert.CanGiveTo         = false;

            insert.CanCharge      = false;
            insert.IsProject      = false;
            insert.Spare1         = "False";
            insert.GivingText     = "";
            insert.GivingShortcut = "";
            insert.PayType        = "";


            insert.CostCentreDelegate = del;

            d.AP_StaffBroker_Departments.InsertOnSubmit(insert);
            d.SubmitChanges();
            return(insert);
        }
    }
    static public void AddProfileValue(int PortalId, int staffId, string propertyName, string propertyValue, int Type = 0)
    {
        StaffBrokerDataContext      d      = new StaffBrokerDataContext();
        AP_StaffBroker_StaffProfile insert = new AP_StaffBroker_StaffProfile();
        var definition = (from c in d.AP_StaffBroker_StaffPropertyDefinitions where c.PropertyName == propertyName && c.PortalId == PortalId select c.StaffPropertyDefinitionId);

        if (definition.Count() == 0)
        {
            AP_StaffBroker_StaffPropertyDefinition newDef = new AP_StaffBroker_StaffPropertyDefinition();
            newDef.PortalId     = PortalId;
            newDef.PropertyName = propertyName;
            newDef.Display      = false;
            newDef.ViewOrder    = (short?)((from c in d.AP_StaffBroker_StaffPropertyDefinitions where c.PortalId == PortalId select c.ViewOrder).Max() + 1);
            newDef.Type         = Convert.ToByte(Type);


            newDef.PropertyHelp = "";
            d.AP_StaffBroker_StaffPropertyDefinitions.InsertOnSubmit(newDef);
            d.SubmitChanges();
            insert.StaffPropertyDefinitionId = newDef.StaffPropertyDefinitionId;
        }
        else
        {
            insert.StaffPropertyDefinitionId = definition.First();
        }

        insert.StaffId       = staffId;
        insert.PropertyValue = propertyValue;
        var existing = from c in d.AP_StaffBroker_StaffProfiles where c.StaffId == insert.StaffId && c.StaffPropertyDefinitionId == insert.StaffPropertyDefinitionId select c;

        if (existing.Count() > 0)
        {
            existing.First().PropertyValue = propertyValue;
        }
        else
        {
            d.AP_StaffBroker_StaffProfiles.InsertOnSubmit(insert);
        }

        d.SubmitChanges();
    }
    static public void SetSetting(string SettingName, string value, int portalId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        var q = from c in d.AP_StaffBroker_Settings where c.SettingName == SettingName && c.PortalId == portalId select c;

        if (q.Count() == 0)
        {
            AP_StaffBroker_Setting insert = new AP_StaffBroker_Setting();
            insert.SettingName  = SettingName;
            insert.SettingValue = value;
            insert.PortalId     = portalId;
            d.AP_StaffBroker_Settings.InsertOnSubmit(insert);
        }
        else
        {
            q.First().SettingValue = value;
        }

        d.SubmitChanges();
    }
    static public int GetSpouseId(int UserId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var q = from c in d.AP_StaffBroker_Staffs where c.UserId1 == UserId select c.UserId2;

        if (q.Count() > 0)
        {
            return((int)(q.First()));
        }
        else
        {
            var r = from c in d.AP_StaffBroker_Staffs where c.UserId2 == UserId select c.UserId1;
            if (r.Count() > 0)
            {
                return(r.First());
            }
            else
            {
                return(-1);
            }
        }
    }
Пример #22
0
    static public Boolean IsDept(int PortalId, string costCenter)
    {
        DotNetNuke.Entities.Portals.PortalSettings PS = (DotNetNuke.Entities.Portals.PortalSettings)System.Web.HttpContext.Current.Items["PortalSettings"];
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        if (GetSetting("NonDynamics", PS.PortalId) == "True")
        {
            return((from c in d.AP_StaffBroker_Departments where c.CostCentre.ToLower() == costCenter.ToLower() && c.PortalId == PS.PortalId select c.CostCenterId).Count() > 0);
        }
        else
        {
            var cc = from c in d.AP_StaffBroker_CostCenters where c.CostCentreCode == costCenter select c.Type;
            if (cc.Count() > 0)
            {
                return(cc.First() == CostCentreType.Department);
            }

            else
            {
                return(false);
            }
        }
    }
    static public List <LeaderInfo> GetLeadersDetailed(int UserId, int PortalId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();



        var s = from c in d.AP_StaffBroker_LeaderMetas where c.UserId == UserId select new { UserId = c.LeaderId, hasDelegated = c.DelegateId != null, isDelegate = false };

        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 });

        List <LeaderInfo> rtn = new List <LeaderInfo>();

        foreach (var row in s)
        {
            LeaderInfo insert = new LeaderInfo();
            insert.UserId       = row.UserId;
            insert.DisplayName  = DotNetNuke.Entities.Users.UserController.GetUserById(PortalId, row.UserId).DisplayName;
            insert.isDelegate   = row.isDelegate;
            insert.hasDelegated = row.hasDelegated;
            rtn.Add(insert);
        }
        return(rtn);
    }
    public static AP_StaffBroker_Staff CreateStaffMember(int PortalId, DotNetNuke.Entities.Users.UserInfo User1in, string SpouseName, DateTime SpouseDOB, short staffTypeIn = 1)
    {
        DotNetNuke.Security.Roles.RoleController rc = new DotNetNuke.Security.Roles.RoleController();
        if (rc.GetRoleByName(PortalId, "Staff") == null)
        {
            DotNetNuke.Security.Roles.RoleInfo insert = new DotNetNuke.Security.Roles.RoleInfo();
            insert.Description = "Staff Members";
            insert.RoleName = "Staff";
            insert.AutoAssignment = false;
            insert.IsPublic = false;
            insert.RoleGroupID = -1;
            insert.PortalID = PortalId;
            rc.AddRole(insert);
        }

        rc.AddUserRole(PortalId, User1in.UserID, rc.GetRoleByName(PortalId, "Staff").RoleID, DateTime.MaxValue);

        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var searchStaff = from c in d.AP_StaffBroker_Staffs where c.Active && (c.UserId1 == User1in.UserID || c.UserId2 == User1in.UserID) select c;
        if (searchStaff.Count() > 0)
            return searchStaff.First();
        //Create Married to Non-Staff
        AP_StaffBroker_Staff rtn = new AP_StaffBroker_Staff();
        rtn.UserId1 = User1in.UserID;
        rtn.UserId2 = -1;
        rtn.DisplayName = User1in.FirstName + " " + User1in.LastName;

        rtn.StaffTypeId = staffTypeIn;
        rtn.CostCenter = "";
        rtn.PortalId = PortalId;
        rtn.Active = true;
        d.AP_StaffBroker_Staffs.InsertOnSubmit(rtn);
        d.SubmitChanges();
        //Now add Spouse data
        AddProfileValue(PortalId, rtn.StaffId, "SpouseDOB", SpouseDOB.ToShortDateString());
        AddProfileValue(PortalId, rtn.StaffId, "SpouseName", SpouseName);

        return rtn;
    }
    public static void ChangeUsername(string OldUsername, string NewUsername)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        d.Agape_Main_AlterUserName(OldUsername, NewUsername);
    }
    static public String ValidateAccountCode(string AccountCode, int PortalId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        return((from c in d.AP_StaffBroker_AccountCodes where c.AccountCode == AccountCode && c.PortalId == PortalId select c).Count() > 0 ? AccountCode : "");
    }
 public static AP_StaffBroker_Department GetDeptByGivingShortcut(String GivingShortcut)
 {
     StaffBrokerDataContext d = new StaffBrokerDataContext();
     var depts = from c in d.AP_StaffBroker_Departments where c.GivingShortcut == GivingShortcut select c;
     if (depts.Count() > 0)
     {
         return depts.First();
     }
     else
     {
         return null;
     }
 }
 public static List<User> GetTeam(int LeaderId)
 {
     StaffBrokerDataContext d = new StaffBrokerDataContext();
     //--Modified to return subordinates in depth, and ignore delegations.
     //var s = from c in d.AP_StaffBroker_LeaderMetas where c.LeaderId == LeaderId || c.DelegateId == LeaderId select c.User;
     return GetTeamToDepthOf(LeaderId, 2);
 }
    public static String GetDeptGiveToURL(int PortalId, int costCenter)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var cc = from c in d.AP_StaffBroker_Departments where c.CostCenterId == costCenter && c.PortalId == PortalId select c.GivingShortcut;
        if (cc.Count() > 0)
            return cc.First();

        else return "";
    }
    public static Boolean IsDept(int PortalId, string costCenter)
    {
        DotNetNuke.Entities.Portals.PortalSettings PS = (DotNetNuke.Entities.Portals.PortalSettings)System.Web.HttpContext.Current.Items["PortalSettings"];
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        if (GetSetting("NonDynamics", PS.PortalId) == "True")
        {
            return (from c in d.AP_StaffBroker_Departments where c.CostCentre.ToLower() == costCenter.ToLower() && c.PortalId == PS.PortalId select c.CostCenterId).Count() > 0;

        }
        else
        {

            var cc = from c in d.AP_StaffBroker_CostCenters where c.CostCentreCode == costCenter select c.Type;
            if (cc.Count() > 0)
                return cc.First() == CostCentreType.Department;

            else return false;
        }
    }
    public static IQueryable<StaffBroker.User> GetStaff(params int[] ExcludeIDs)
    {
        DotNetNuke.Entities.Portals.PortalSettings PS = (DotNetNuke.Entities.Portals.PortalSettings)System.Web.HttpContext.Current.Items["PortalSettings"];

        StaffBrokerDataContext d = new StaffBrokerDataContext();
        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;
        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);
        return q.OrderBy(c => c.LastName).ThenBy(c => c.FirstName);
    }
    public static List<User> GetTeam(int LeaderId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var s = from c in d.AP_StaffBroker_LeaderMetas where c.LeaderId == LeaderId || c.DelegateId == LeaderId select c.User;

        return s.ToList();
    }
    public static string GetStaffProfileProperty(int staffId, string propertyName)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var existing = from c in d.AP_StaffBroker_StaffProfiles where c.StaffId == staffId && c.AP_StaffBroker_StaffPropertyDefinition.PropertyName == propertyName select c.PropertyValue;

        if (existing.Count() == 0)
            return "";
        else
            return existing.First();
    }
    public static AP_StaffBroker_Staff GetStaffMember(int UserId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var q = from c in d.AP_StaffBroker_Staffs where c.UserId1 == UserId || c.UserId2 == UserId select c;

        if (q.Count() > 0)
            return q.First();
        else
            return null;
    }
 public static Boolean IsDept(int PortalId, string costCenter)
 {
     if (costCenter.Length == 6)
     {
         StaffBrokerDataContext d = new StaffBrokerDataContext();
         var cc = from c in d.AP_StaffBroker_CostCenters where c.CostCentreCode == costCenter select c.Type;
         if (cc.Count() > 0)
             return cc.First() == CostCentreType.Department;
     }
     return false;
 }
 public static String ValidateCostCenter(string CostCenter, int PortalId)
 {
     StaffBrokerDataContext d = new StaffBrokerDataContext();
     return (from c in d.AP_StaffBroker_CostCenters where c.CostCentreCode == CostCenter && c.PortalId == PortalId select c).Count() > 0 ? CostCenter : "";
 }
 public static IQueryable<StaffBroker.User> GetStaffIncl(int PortalId = 0, params string[] IncludeTypes)
 {
     StaffBrokerDataContext d = new StaffBrokerDataContext();
     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;
     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);
     return q.OrderBy(c => c.LastName).ThenBy(c => c.FirstName);
 }
 public static Boolean VerifyAccountCode(int PortalId, string AccountCode)
 {
     StaffBrokerDataContext d = new StaffBrokerDataContext();
     var acc = from c in d.AP_StaffBroker_AccountCodes where c.AccountCode == AccountCode select c;
     return acc.Count() > 0;
 }
    static public void ChangeUsername(string OldUsername, string NewUsername)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        d.Agape_Main_AlterUserName(OldUsername, NewUsername);
    }
 public static Boolean VerifyCostCenter(int PortalId, string costCenter)
 {
     StaffBrokerDataContext d = new StaffBrokerDataContext();
     var cc = from c in d.AP_StaffBroker_CostCenters where c.CostCentreCode == costCenter select c;
     return cc.Count() > 0;
 }
    static public String ValidateCostCenter(string CostCenter, int PortalId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        return((from c in d.AP_StaffBroker_CostCenters where c.CostCentreCode == CostCenter && c.PortalId == PortalId select c).Count() > 0 ? CostCenter : "");
    }
    public static int GetSpouseId(int UserId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var q = from c in d.AP_StaffBroker_Staffs where c.UserId1 == UserId select c.UserId2;
        if (q.Count() > 0)
            return (int)(q.First());
        else
        {
            var r = from c in d.AP_StaffBroker_Staffs where c.UserId2 == UserId select c.UserId1;
            if (r.Count() > 0)
                return r.First();
            else
                return -1;

        }
    }
    public static List<AP_StaffBroker_Department> GetDepartments(int UserId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        List<AP_StaffBroker_Department> q;
        q = (from c in d.AP_StaffBroker_Departments where c.CostCentreManager == UserId || c.CostCentreDelegate == UserId select c).ToList();

        return q;
    }
    public static string GetSetting(string SettingName, int portalId)
    {
        try
        {
            StaffBrokerDataContext d = new StaffBrokerDataContext();

            var q = from c in d.AP_StaffBroker_Settings where c.SettingName == SettingName && c.PortalId == portalId select c;

            if (q.Count() == 0)
                return "";
            else if (q.First().SettingValue == null)
                return "";
            else
                return q.First().SettingValue;
        }
        catch (Exception)
        {

            return "";
        }
    }
    public static AP_StaffBroker_Department CreateDept(string Name, string RC, int manager, int? del)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        DotNetNuke.Entities.Portals.PortalSettings PS = (DotNetNuke.Entities.Portals.PortalSettings)System.Web.HttpContext.Current.Items["PortalSettings"];
        if (del == 0) del = null;
        var q = from c in d.AP_StaffBroker_Departments where c.CostCentre == RC && c.PortalId == PS.PortalId select c;
        if (q.Count() > 0)
        {
            q.First().Name = Name;
            q.First().CostCentreManager = manager;
            q.First().CostCentreDelegate = del;
            d.SubmitChanges();
            return q.First();

        }
        else
        {
            AP_StaffBroker_Department insert = new AP_StaffBroker_Department();
            insert.PortalId = PS.PortalId;
            insert.Name = Name;
            insert.CostCentre = RC;
            insert.CostCentreManager = manager;
            insert.CanRmb = true;
            insert.CanGiveTo = false;

            insert.CanCharge = false;
            insert.IsProject = false;
            insert.Spare1 = "False";
            insert.GivingText = "";
            insert.GivingShortcut = "";
            insert.PayType = "";

            insert.CostCentreDelegate = del;

            d.AP_StaffBroker_Departments.InsertOnSubmit(insert);
            d.SubmitChanges();
            return insert;
        }
    }
    public static List<LeaderRelationship> GetReportsTo(int UserId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var s = from c in d.AP_StaffBroker_LeaderMetas where c.UserId == UserId select c;
        List<LeaderRelationship> rtn = new List<LeaderRelationship>();
        foreach (StaffBroker.AP_StaffBroker_LeaderMeta row in s)
        {
            LeaderRelationship x = new LeaderRelationship();
            x.UserId = row.UserId;
            x.UserName = row.User.DisplayName;
            x.LeaderId = row.LeaderId;
            try
            {
                x.LeaderName = row.Leaders.DisplayName;
            }
            catch (Exception)
            {
                x.LeaderName = "Unknown";

            }

            x.DelegateId = -1;
            x.Delegatename = "";
            if (row.DelegateId != null)
            {
                x.DelegateId = (int)row.DelegateId;
                x.Delegatename = row.Delegate.DisplayName;
            }

            rtn.Add(x);
        }

        return rtn;
    }
    public static void AddProfileValue(int PortalId, int staffId, string propertyName, string propertyValue, int Type = 0)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        AP_StaffBroker_StaffProfile insert = new AP_StaffBroker_StaffProfile();
        var definition = (from c in d.AP_StaffBroker_StaffPropertyDefinitions where c.PropertyName == propertyName && c.PortalId == PortalId select c.StaffPropertyDefinitionId);
        if (definition.Count() == 0)
        {
            AP_StaffBroker_StaffPropertyDefinition newDef = new AP_StaffBroker_StaffPropertyDefinition();
            newDef.PortalId = PortalId;
            newDef.PropertyName = propertyName;
            newDef.Display = false;
            newDef.ViewOrder = (short?)((from c in d.AP_StaffBroker_StaffPropertyDefinitions where c.PortalId == PortalId select c.ViewOrder).Max() + 1);
            newDef.Type = Convert.ToByte(Type);

            newDef.PropertyHelp = "";
            d.AP_StaffBroker_StaffPropertyDefinitions.InsertOnSubmit(newDef);
            d.SubmitChanges();
            insert.StaffPropertyDefinitionId = newDef.StaffPropertyDefinitionId;
        }
        else
            insert.StaffPropertyDefinitionId = definition.First();

        insert.StaffId = staffId;
        insert.PropertyValue = propertyValue;
        var existing = from c in d.AP_StaffBroker_StaffProfiles where c.StaffId == insert.StaffId && c.StaffPropertyDefinitionId == insert.StaffPropertyDefinitionId select c;

        if (existing.Count() > 0)
            existing.First().PropertyValue = propertyValue;
        else
            d.AP_StaffBroker_StaffProfiles.InsertOnSubmit(insert);

        d.SubmitChanges();
    }
    public static List<LeaderInfo> GetLeadersDetailed(int UserId, int PortalId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        var s = from c in d.AP_StaffBroker_LeaderMetas where c.UserId == UserId select new { UserId = c.LeaderId, hasDelegated = c.DelegateId != null, isDelegate = false };

        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 });

        List<LeaderInfo> rtn = new List<LeaderInfo>();

        foreach (var row in s)
        {
            LeaderInfo insert = new LeaderInfo();
            insert.UserId = row.UserId;
            insert.DisplayName = DotNetNuke.Entities.Users.UserController.GetUserById(PortalId, row.UserId).DisplayName;
            insert.isDelegate = row.isDelegate;
            insert.hasDelegated = row.hasDelegated;
            rtn.Add(insert);
        }
        return rtn;
    }
 private static List<User> GetTeamToDepthOf(int LeaderId, int Depth)
 {
     StaffBrokerDataContext d = new StaffBrokerDataContext();
     var s = from c in d.AP_StaffBroker_LeaderMetas where c.LeaderId == LeaderId select c.User;
     List<User> result = s.ToList<User>();
     if (Depth > 0)
     {
         foreach (User u in from c in d.AP_StaffBroker_LeaderMetas where c.LeaderId == LeaderId select c.User)
             result.AddRange(GetTeamToDepthOf(u.UserID, Depth - 1));
     }
     return result.OrderBy(o => o.LastName).ToList<User>();
 }
    public static List<int> GetLeaders(int UserId, Boolean includeDelegates = true)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var s = from c in d.AP_StaffBroker_LeaderMetas where c.UserId == UserId select c.LeaderId;
        if (includeDelegates)
            s = s.Union(from c in d.AP_StaffBroker_LeaderMetas where (c.UserId == UserId) && !(c.DelegateId == null) select (int)c.DelegateId);

        return s.ToList();
    }
 public static Boolean MinistryRequiresExtraApproval(string CostCenter)
 {
     StaffBrokerDataContext d = new StaffBrokerDataContext();
     try
     {
         int MinistryId = Int32.Parse((from c in d.AP_StaffBroker_Departments where c.CostCentre == CostCenter select c.Spare1).Single());
         return (from c in d.AP_StaffBroker_Ministries where c.MinistryId == MinistryId select c.FinancialOversightFlag).First();
     }
     catch
     {
         return false;
     }
 }
    public static string GetDeptPhoto(int deptID)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();
        var cc = from c in d.AP_StaffBroker_Departments where c.CostCenterId == deptID select c.PhotoId;
        String fileId = cc.First().ToString();

        if (fileId == null || fileId.Equals(""))
        {
            return "/images/no_avatar.gif";
        }
        else
        {
            DotNetNuke.Services.FileSystem.IFileInfo theFile = DotNetNuke.Services.FileSystem.FileManager.Instance.GetFile(Convert.ToInt32(fileId));
            return DotNetNuke.Services.FileSystem.FileManager.Instance.GetUrl(theFile);
        }
    }
    public static void SetSetting(string SettingName, string value, int portalId)
    {
        StaffBrokerDataContext d = new StaffBrokerDataContext();

        var q = from c in d.AP_StaffBroker_Settings where c.SettingName == SettingName && c.PortalId == portalId select c;

        if (q.Count() == 0)
        {
            AP_StaffBroker_Setting insert = new AP_StaffBroker_Setting();
            insert.SettingName = SettingName;
            insert.SettingValue = value;
            insert.PortalId = portalId;
            d.AP_StaffBroker_Settings.InsertOnSubmit(insert);

        }
        else
        {
            q.First().SettingValue = value;
        }

        d.SubmitChanges();
    }
 public static AP_StaffBroker_Staff GetStaffbyStaffId(int StaffId)
 {
     StaffBrokerDataContext d = new StaffBrokerDataContext();
     var q = from c in d.AP_StaffBroker_Staffs where c.StaffId == StaffId select c;
     if (q.Count() > 0)
         return q.First();
     else
         return null;
 }
 public static String ValidateAccountCode(string AccountCode, int PortalId)
 {
     StaffBrokerDataContext d = new StaffBrokerDataContext();
     return (from c in d.AP_StaffBroker_AccountCodes where c.AccountCode == AccountCode && c.PortalId == PortalId select c).Count() > 0 ? AccountCode : "";
 }
 public static int getDirectorFor(string CostCenter, int defaultDirector)
 {
     StaffBrokerDataContext d = new StaffBrokerDataContext();
     try
     {
         int MinistryId = Int32.Parse((from c in d.AP_StaffBroker_Departments where c.CostCentre == CostCenter select c.Spare1).Single());
         return (from c in d.AP_StaffBroker_Ministries where c.MinistryId == MinistryId select c.MinistryDirectorID).First();
     }
     catch
     {
         return defaultDirector;
     }
 }