public ActionResult ProjectCreateEdit(VMEditingProject model)
        {
            bool isValidInput = true;

            List<ProjectMedium> projectMedia = new List<ProjectMedium>();
            if (!AllProjectMediaVerified(out projectMedia))
            {
                isValidInput = false;
            }

            List<Contribution> contributions = new List<Contribution>();
            if (!AllContributionsVerified(out contributions))
            {
                isValidInput = false;
            }

            Contribution personalContribution = new Contribution();
            if(!PersonalContributionVerified(out personalContribution, model._currentUserId))
            {
                isValidInput = false;
            }

            if (isValidInput && ModelState.IsValid)
            {
                int currentUserId = WebSecurity.CurrentUserId;
                string selectedTemplate = Request.Form.GetValues("Template").FirstOrDefault();
                int template = (int)((ProjectTemplate)Enum.Parse(typeof(ProjectTemplate), selectedTemplate));

                string selectedContributionSetting = Request.Form.GetValues("ContributionSetting").FirstOrDefault();
                int setting = (int)((ContributionSetting)Enum.Parse(typeof(ContributionSetting), selectedContributionSetting));

                int projectId = int.Parse(Request.Form.GetValues("Id").FirstOrDefault());
                Project p = db.retrieveProject(projectId);
                bool newProject = true;
                #region Creating new project properties
                if (p == null)//Creating
                {
                    p = new Project();
                    personalContribution.UserId = currentUserId;
                    p.Title = model.Title;
                    p.Description = model.Description;
                    p.Template = template;
                    p.CollaborationSetting = setting;
                    foreach (Contribution c in contributions)
                    {
                        c.UserId = db.retrieveUserByEmail(c.User.Email).Id;
                        c.User = null;
                    }
                    p.Contributions = contributions;
                    personalContribution.User = null;
                    if (!string.IsNullOrEmpty(personalContribution.Title))
                    {
                        p.Contributions.Add(personalContribution);
                    }
                    else
                    {
                        p.Contributions.Add(new Contribution(){ UserId = currentUserId});
                    }
                    p.ProjectMedia = projectMedia;
                    p.ProjectPermissions.Add(new ProjectPermission() { UserId = currentUserId, IsProjectEditor = true, IsProjectMaster = true });
                }
                #endregion
                #region Editing existing project properties
                else //editing
                {
                    newProject = false;
                    p.Title = model.Title;
                    p.Description = model.Description;
                    p.Template = template;
                    p.CollaborationSetting = setting;
                    db.updateProject(p);
                    foreach (Contribution c in contributions)
                    {
                        if (c.ProjectId != projectId)//new contribution entry
                        {
                            c.UserId = db.retrieveUserByEmail(c.User.Email).Id;
                            c.User = null;
                            c.ProjectId = projectId;
                            new NotificationController().CollaborationAddition(WebSecurity.CurrentUserId, c.UserId, projectId);
                            db.addContribution(c, projectId);
                        }
                    }
                    foreach (ProjectMedium m in projectMedia)
                    {
                        ProjectMedium dm = db.retrieveProjectMedium(m.Id);
                        if (dm == null)//medium doesn't exist in project
                        {
                            db.addProjectMedium(m, projectId);
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(m.Caption) && string.IsNullOrEmpty(m.Link))//existed, wants to remove
                            {
                                db.removeProjectMedium(dm);
                            }
                            else
                            {
                                dm.Caption = m.Caption;
                                dm.Link = m.Link;
                                dm.MediumType = m.MediumType;
                                db.updateProjectMedium(dm);
                            }
                        }
                    }
                    #region personal Contribution
                    personalContribution.ProjectId = projectId;
                    personalContribution.User = null;
                    if (!p.Contributions.Any(c=> c.UserId == currentUserId))//didn't have personal contribution
                    {
                        if (string.IsNullOrEmpty(personalContribution.Title))
                        {
                            db.addContribution(new Contribution() { UserId = currentUserId }, projectId);
                        }
                        else
                        {
                            db.addContribution(personalContribution, projectId);
                        }
                    }
                    else //had personal contribution
                    {
                        Contribution existing = db.retrieveContribution(currentUserId, projectId);
                        existing.Title = personalContribution.Title;
                        existing.Description = personalContribution.Description;
                        foreach (ContributionMedium m in personalContribution.ContributionMedia)
                        {
                            if (existing.ContributionMedia.Any(me => me.Id == m.Id))//Contribution has that medium
                            {
                                if (string.IsNullOrEmpty(m.Link))//blanked out medium
                                {
                                    existing.ContributionMedia.Remove(existing.ContributionMedia.Where(me => me.Id == m.Id).First());
                                    db.removeContributionMedium(m);
                                }
                                else
                                {
                                    ContributionMedium med = existing.ContributionMedia.Where(me => me.Id == m.Id).First();
                                    med.Caption = m.Caption;
                                    med.Link = m.Link;
                                    med.MediumType = m.MediumType;
                                    db.updateContributionMedium(med);
                                }
                            }
                            else
                            {
                                db.addContributionMedium(m, projectId, currentUserId);
                            }
                        }
                        db.updateContribution(existing, projectId);
                    }
                    #endregion
                }
                #endregion

                #region Adding quick contact collaborators
                List<string> keys = Request.Form.AllKeys.Where(k => k.Contains("AddBox")).ToList();
                if (keys != null && keys.Count > 0)
                {
                    foreach (string key in keys)
                    {
                        int quickContactId = int.Parse(key.Substring(0, key.IndexOf("AddBox")));
                        bool addAsCollab = Request.Form.GetValues(key).FirstOrDefault().Equals("true");
                        if (addAsCollab)//Box was checked
                        {
                            if (!p.Contributions.Any(c => c.UserId == quickContactId))//That contact isn't already a collaborator
                            {
                                p.Contributions.Add(new Contribution() { UserId = quickContactId });
                                if (!newProject)
                                {
                                    db.addContribution(new Contribution() { UserId = quickContactId }, projectId);
                                    new NotificationController().CollaborationAddition(WebSecurity.CurrentUserId, quickContactId, projectId);
                                }
                            }
                        }
                    }
                }
                #endregion

                if (newProject)
                {
                    db.addProject(p);
                    foreach (Contribution c in p.Contributions.Where(c=>c.UserId != WebSecurity.CurrentUserId))
                    {
                        new NotificationController().CollaborationAddition(WebSecurity.CurrentUserId, c.UserId, p.Id);
                    }
                }
                return RedirectToAction("ProjectCatalog", new { id = currentUserId });
            }
            int projId = int.Parse(Request.Form.GetValues("Id").FirstOrDefault());
            Project proj = new Project();
            proj.Id = projId;
            proj.Title = model.Title;
            proj.ProjectMedia = projectMedia;
            proj.Contributions = contributions;

            VMEditingProject vmproj = new VMEditingProject(proj, null, model._currentUserId) { EditingCollaborators = model.EditingCollaborators,PersonalContribution = new VMContribution(personalContribution)};

            return View(new VMEditingProject());
        }
 public ActionResult ProjectCreateEdit(int id = -1)
 {
     if (RouteData.Values["id"] != null)
     {
         if (int.TryParse(RouteData.Values["id"].ToString(), out id)) { }
     }
     Project project;
     if (id != -1)
     {
         project = db.retrieveProject(id);
         if (project == null)
         {
             string error1 = "The Project you tried to edit either does not exist or could not be found.";
             string error2 = "Project Id: " + id;
             TempData["ErrorMessages"] = new string[] { error1, error2 };
             return RedirectToAction("Http404", "Error");
         }
     }
     else
     {
         project = new Project();
     }
     int currentUserId = WebSecurity.CurrentUserId;
     List<PortfolioUnleashed.User> quickContacts = new List<PortfolioUnleashed.User>();
     IEnumerable<int> quickContactIds = db.retrieveUser(currentUserId).QuickReferences.Select(c => c.QuickReferenceId);
     if (quickContactIds != null && quickContactIds.Count() > 0)
     {
         foreach (int Id in quickContactIds)
         {
             quickContacts.Add(db.retrieveUser(Id));
         }
     }
     if (quickContacts == null)
     {
         quickContacts = new List<User>();
     }
     VMEditingProject vmEdit = new VMEditingProject(project, quickContacts, currentUserId);
     if (vmEdit.ProjectEditorIds != null && vmEdit.ProjectEditorIds.Count > 0)
     {
         if (!vmEdit.ProjectEditorIds.Any(editorId => editorId == WebSecurity.CurrentUserId) && !User.IsInRole("Admin"))//If user trying to edit project is not a project editor or admin
         {
             string error1 = "You do not have permission to edit this project.";
             string error2 = "To edit your contribution to the project, please do so from your project catalog via the edit button.";
             string error3 = "If you are not registered as a contributor to this project, either add/request to add yourself as one, or contact a project editor for that project.";
             string error4 = "If you are the original creator of this project or otherwise believe that you have reached this page in error, please see the Support page to submit a report.";
             string error5 = "For mor information on Projects, Project Permissions, Project Editors, and Project Collaborators, please see the FAQ or Glossary page.";
             TempData["ErrorMessages"] = new string[] { error1, error2, error3, error4, error5 };
             return RedirectToAction("General", "Error");
         }
     }
     return View(vmEdit);
 }