public async Task <ActionResult> GetArchivedScheduleVersionModal(int?id, int scheduleVersion = 1)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // load project from db, confirm that it's not null
            UserProjectsRepository repo      = new UserProjectsRepository();
            UserProjectViewModel   viewModel = await repo.GetUserProjectWithSchedule(id, scheduleVersion);

            if (viewModel == null)
            {
                return(HttpNotFound());
            }

            // authorize user
            ApplicationUser user = await ReturnCurrentUserAsync();

            if (viewModel.ApplicationUserID == user.Id)
            {
                return(PartialView("_ArchivedScheduleVersion.Modal.GanttContainer", viewModel));
            }

            Danger(unauthorizedAccessMessage, true);
            return(RedirectToAction("Index"));
        }
        // GET: UserProjects/Details/5
        public async Task <ActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // load project from db, confirm that it's not null
            UserProjectsRepository repo      = new UserProjectsRepository();
            UserProjectViewModel   viewModel = await repo.GetUserProjectWithSchedule(id, 1); // VersionNum = 1 for current schedule version

            if (viewModel == null)
            {
                return(HttpNotFound());
            }

            // authorize user
            ApplicationUser user = await ReturnCurrentUserAsync();

            if (viewModel.ApplicationUserID == user.Id)
            {
                return(View(viewModel));
            }

            Danger(unauthorizedAccessMessage, true);
            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> Create([Bind(Include = "ID,ApplicationUserID,Name,Description,Comments,ProjectStartDate,ProjectStatusID")] CreateUserProjectViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                UserProjectsRepository repo = new UserProjectsRepository();
                int newProjectID            = await repo.CreateNewProject(viewModel);

                Success("New Project Created Successfully", true);
                return(RedirectToAction("Details", new { id = newProjectID }));
            }
            ViewBag.ProjectStatusID = new SelectList(await db.ProjectStatuses.ToListAsync(), "ID", "Description");
            return(View(viewModel));
        }
        // GET: UserProjects/EditScheduleFS/5
        public async Task <ActionResult> EditScheduleFS(int?id, bool deleteAbandonedSchedule = false)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // load project from db, confirm that it's not null
            UserProjectsRepository repo      = new UserProjectsRepository();
            UserProjectViewModel   viewModel = await repo.GetUserProject(id);

            if (viewModel == null)
            {
                return(HttpNotFound());
            }

            // authorize user
            ApplicationUser user = await ReturnCurrentUserAsync();

            if (viewModel.ApplicationUserID == user.Id)
            {
                ProjectScheduleVersionsRepository    scheduleRepo = new ProjectScheduleVersionsRepository();
                Tuple <ProjectScheduleVersion, bool> newSchedule  = await scheduleRepo.CloneProjectScheduleVersionForEdit(viewModel.ID);

                if (deleteAbandonedSchedule)
                {
                    return(RedirectToAction("RemoveProjectScheduleVersion", new { id = newSchedule.Item1.ID }));
                }
                viewModel.ProjectScheduleVersion = newSchedule.Item1;
                viewModel.FullScreen             = true;
                viewModel.ReadOnly          = false;
                viewModel.AbandonedSchedule = newSchedule.Item2;
                return(View(viewModel));
            }
            else
            {
                Danger(unauthorizedAccessMessage, true);
                return(RedirectToAction("Index"));
            }
        }
        public async Task <ActionResult> RemoveProjectScheduleVersion(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // load schedule from db, confirm that it's not null
            ProjectScheduleVersionsRepository schedulesRepo = new ProjectScheduleVersionsRepository();
            var schedule = await schedulesRepo.FindProjectScheduleVersion(id.Value);

            if (schedule == null)
            {
                return(HttpNotFound());
            }

            // load project from db, confirm that it's not null
            UserProjectsRepository projectsRepo = new UserProjectsRepository();
            UserProjectViewModel   project      = await projectsRepo.GetUserProject(schedule.ProjectID);

            if (project == null)
            {
                return(HttpNotFound());
            }

            // authorize user
            ApplicationUser user = await ReturnCurrentUserAsync();

            if (project.ApplicationUserID == user.Id)
            {
                if (await schedulesRepo.RemoveProjectScheduleVersion(id.Value))
                {
                    return(RedirectToAction("EditScheduleFS", new { id = project.ID }));
                }
            }
            Danger("Error: Unable to abandon unsaved changes.");
            return(RedirectToAction("Details", new { id = project.ID }));
        }
        // GET: UserProjects
        public async Task <ActionResult> Index(string sortOrder, string currentFilter, string searchString, int?page, int pageSize = 10)
        {
            ViewBag.CurrentSort         = sortOrder;
            ViewBag.NameSortParm        = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DescriptionSortParm = sortOrder == "Description" ? "description_desc" : "Description";
            ViewBag.CreatedSortParm     = sortOrder == "Created" ? "created_desc" : "Created";
            ViewBag.ModifiedSortParm    = sortOrder == "Modified" ? "modified_desc" : "Modified";
            ViewBag.StatusSortParm      = sortOrder == "Status" ? "status_desc" : "Status";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;


            ApplicationUser user = await ReturnCurrentUserAsync();

            UserProjectsRepository repo = new UserProjectsRepository();
            var userProjects            = await repo.GetUserProjectList(user.Id);

            if (!String.IsNullOrEmpty(searchString))
            {
                userProjects = userProjects.Where(p => p.Name.ToUpper().Contains(searchString.Trim().ToUpper()) ||
                                                  p.Description.ToUpper().Contains(searchString.Trim().ToUpper()));
            }

            switch (sortOrder)
            {
            case "name_desc":
                userProjects = userProjects.OrderByDescending(p => p.Name);
                break;

            case "Description":
                userProjects = userProjects.OrderBy(p => p.Description);
                break;

            case "description_desc":
                userProjects = userProjects.OrderByDescending(p => p.Description);
                break;

            case "Created":
                userProjects = userProjects.OrderBy(p => p.CreatedAt);
                break;

            case "created_desc":
                userProjects = userProjects.OrderByDescending(p => p.CreatedAt);
                break;

            case "Modified":
                userProjects = userProjects.OrderBy(p => p.ModifiedAt);
                break;

            case "modified_desc":
                userProjects = userProjects.OrderByDescending(p => p.ModifiedAt);
                break;

            case "Status":
                userProjects = userProjects.OrderBy(p => p.ProjectStatus.Description);
                break;

            case "status_desc":
                userProjects = userProjects.OrderByDescending(p => p.ProjectStatus.Description);
                break;

            default:
                userProjects = userProjects.OrderBy(p => p.Name);
                break;
            }

            int pageNumber = (page ?? 1);

            return(View(userProjects.ToPagedList(pageNumber, pageSize)));
        }