public ActionResult Create(BenevoleCreateViewModel vm) { Benevole b = new Benevole() { Nom = vm.Nom, Prenom = vm.Prenom, Permis = vm.Permis, Commentaire = vm.Commentaire, Email = vm.Email, Tel = vm.Tel }; db.Benevoles.Add(b); foreach (CreneauDef c in db.CreneauDefs) { Dispo d = new Dispo() { CreneauDef = c, EstDispo = vm.Jours.Any(j => j.IsSelected && c.JourEvenement.Id == j.Id), ModifManuel = false }; b.Dispoes.Add(d); } db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit(int id, [Bind(Include = "DisposByDays")] Benevole b) { Benevole benevole = db.Benevoles.Include("Dispoes.CreneauDef.JourEvenement").First(s => s.Id == id); if (ModelState.IsValid) { Regex r = new Regex(@"\[([0-9]+)\]"); foreach (var key in Request.Form.AllKeys.Where(k => k.Contains("DisposByDays"))) { var m = r.Match(key); int jourId = int.Parse(m.Groups[0].Captures[0].Value.Trim(new char[] { '[', ']' })); m = m.NextMatch(); int noCreneau = int.Parse(m.Groups[0].Captures[0].Value.Trim(new char[] { '[', ']' })); var val = Request.Form[key].ToString().ToLower(); bool isDispo = val.Contains("true"); Dispo d = benevole.Dispoes.First(s => s.CreneauDef.NoCreneau == noCreneau && s.CreneauDef.JourId == jourId); if (d.EstDispo != isDispo) { d.EstDispo = isDispo; d.ModifManuel = true; } } db.SaveChanges(); return(RedirectToAction("Index")); } benevole.FillDispoByDayFromDb(); var dico = new Dictionary <int, JourEvenement>(); var coms = new Dictionary <int, string>(); foreach (var j in db.JourEvenements) { var strDispo = benevole.CommentaireDispoes.FirstOrDefault(s => s.JourId == j.Id); dico.Add(j.Id, j); coms.Add(j.Id, strDispo == null ? "" : strDispo.Commentaire); } ViewBag.Jours = dico; ViewBag.Coms = coms; var nextBenevole = db.Benevoles.OrderBy(s => s.Id).First(s => s.Id > benevole.Id); if (nextBenevole == null) { nextBenevole = db.Benevoles.OrderBy(s => s.Id).First(); } ViewBag.NextBenId = nextBenevole.Id; return(View(benevole)); }
public ActionResult ImportBenevolePost() { if (ModelState.IsValid && Request.Files.Count == 1) { var list = new List <GoogleDriveResult>(); using (var workbook = new XLWorkbook(Request.Files[0].InputStream)) { var ws = workbook.Worksheet(1); int i = 2; var row = ws.Row(i); while (!row.IsEmpty()) { int j = 2; GoogleDriveResult g = new GoogleDriveResult(); g.Nom = row.Cell(j++).GetValue <string>(); g.Prenom = row.Cell(j++).GetValue <string>(); g.Telephone = row.Cell(j++).GetValue <string>(); g.Mail = row.Cell(j++).GetValue <string>().Trim().ToLower(); g.Permis = row.Cell(j++).GetValue <string>(); g.Majeur = row.Cell(j++).GetValue <string>(); g.DispoCampus = row.Cell(j++).GetValue <string>(); g.CommentaireCampus = row.Cell(j++).GetValue <string>(); g.DispoMercredi = row.Cell(j++).GetValue <string>(); g.CommentaireMercredi = row.Cell(j++).GetValue <string>(); g.DispoJeudi = row.Cell(j++).GetValue <string>(); g.CommentaireJeudi = row.Cell(j++).GetValue <string>(); g.DispoVendredi = row.Cell(j++).GetValue <string>(); g.CommentaireVendredi = row.Cell(j++).GetValue <string>(); g.DispoSamedi = row.Cell(j++).GetValue <string>(); g.CommentaireSamedi = row.Cell(j++).GetValue <string>(); g.DispoDimanche = row.Cell(j++).GetValue <string>(); g.CommentaireDimanche = row.Cell(j++).GetValue <string>(); g.DispoLundi = row.Cell(j++).GetValue <string>(); g.CommentaireLundi = row.Cell(j++).GetValue <string>(); j += 2; g.Preference = row.Cell(j++).GetValue <string>(); g.NonPreference = row.Cell(j++).GetValue <string>(); g.PrecisionGeneral = row.Cell(j++).GetValue <string>(); string valid = row.Cell(j++).GetValue <string>().Trim(); if (valid == "1") { list.Add(g); } i++; row = ws.Row(i); } } //Creation Bénévoles foreach (var g in list) { Benevole b = db.Benevoles.FirstOrDefault(s => s.Email.Trim().ToLower() == g.FormatedMail); if (b == null) { b = new Benevole(); db.Benevoles.Add(b); b.Email = g.FormatedMail; } b.Nom = UppercaseFirst(g.FormatedNom.Trim()); b.Prenom = UppercaseFirst(g.FormatedPrenom.Trim()); b.Tel = g.FormatedTel; b.Permis = g.APermis; b.Majeur = g.EstMajeur; b.Commentaire = g.PrecisionGeneral; } db.SaveChanges(); //Gestion des preferences foreach (var g in list) { Benevole b = db.Benevoles.Include("Preferences").First(s => s.Email.Trim().ToLower() == g.FormatedMail); foreach (var kv in g.Prefs) { Preference p = b.Preferences.FirstOrDefault(s => s.TypeTacheId == ((int)kv.Key) && s.BenevoleId == b.Id); if (p == null) { p = new Preference(); db.Preferences.Add(p); p.TypeTacheId = (int)kv.Key; p.BenevoleId = b.Id; } p.Valeur = kv.Value; db.SaveChanges(); } } db.SaveChanges(); //Gestion des dispo foreach (var g in list) { Benevole b = db.Benevoles.Include("Dispoes").First(s => s.Email.Trim().ToLower() == g.FormatedMail); foreach (var c in db.CreneauDefs.Include("JourEvenement")) { Dispo d = b.Dispoes.FirstOrDefault(s => s.CreneauDefId == c.Id && s.BenevoleId == b.Id); if (d == null) { d = new Dispo(); db.Dispoes.Add(d); d.CreneauDefId = c.Id; d.BenevoleId = b.Id; d.ModifManuel = false; } if (!d.ModifManuel) { string dispoTxt; switch (c.JourEvenement.Nom.Trim().ToLower()) { case "mercredi": dispoTxt = g.DispoMercredi; break; case "jeudi": dispoTxt = g.DispoJeudi; break; case "vendredi": dispoTxt = g.DispoVendredi; break; case "samedi": dispoTxt = g.DispoSamedi; break; case "dimanche": dispoTxt = g.DispoDimanche; break; default: throw new ArgumentException("c.JourEvenement.Nom"); } d.EstDispo = isDispo(dispoTxt, c); } } db.SaveChanges(); } db.SaveChanges(); //Gestion des commentaire dispo foreach (var g in list) { Benevole b = db.Benevoles.Include("Dispoes").First(s => s.Email.Trim().ToLower() == g.FormatedMail); foreach (var j in db.JourEvenements) { CommentaireDispo c = b.CommentaireDispoes.FirstOrDefault(s => s.JourId == j.Id && s.BenevoleId == b.Id); if (c == null) { c = new CommentaireDispo(); b.CommentaireDispoes.Add(c); c.JourId = j.Id; c.BenevoleId = b.Id; } string dispoTxt; switch (j.Nom.Trim().ToLower()) { case "mercredi": dispoTxt = g.CommentaireMercredi; break; case "jeudi": dispoTxt = g.CommentaireJeudi; break; case "vendredi": dispoTxt = g.CommentaireVendredi; break; case "samedi": dispoTxt = g.CommentaireSamedi; break; case "dimanche": dispoTxt = g.CommentaireDimanche; break; default: throw new ArgumentException(j.Nom); } c.Commentaire = dispoTxt.Trim(); } } db.SaveChanges(); } return(View()); }