RestaurationParametresController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System.Net;
  2. using System.Web.Mvc;
  3. using CD67.FicheCollege.Entity;
  4. using CD67.FicheCollege.Factory;
  5. using CD67.FicheCollege.MVC.Models;
  6. using System.Linq;
  7. using System.Collections.Generic;
  8. using System;
  9. namespace CD67.FicheCollege.MVC.Controllers
  10. {
  11. public class RestaurationParametresController : Controller
  12. {
  13. private Entities db = new Entities();
  14. // GET: RestaurationParametres/Edit/5
  15. public ActionResult Edit(int? annee_id)
  16. {
  17. if (annee_id == null)
  18. {
  19. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  20. }
  21. AnneeFactory fact = new AnneeFactory(db);
  22. Annee annee = fact.getById(annee_id);
  23. RestaurationParametreFactory paramFactory = new RestaurationParametreFactory(db);
  24. RestaurationParametre param = paramFactory.getBy(x => x.Annee_Id == annee_id.Value);
  25. RestaurationParametreViewModel model;
  26. if (param == null)
  27. {
  28. param = new RestaurationParametre();
  29. param.Annee_Id = annee_id.Value;
  30. }
  31. RestaurationTypeRepaFactory typeRepasFactory = new RestaurationTypeRepaFactory(db);
  32. List<CheckBoxListItem> repasCBList = new List<CheckBoxListItem>();
  33. foreach (var item in typeRepasFactory.getAllValid().ToList())
  34. {
  35. repasCBList.Add(
  36. new CheckBoxListItem()
  37. {
  38. ID = item.Id.ToString(),
  39. Display = item.Libelle,
  40. IsChecked = param.RestaurationTypeRepas.Any(i => i.Id == item.Id) ? true : false
  41. }
  42. );
  43. }
  44. ViewBag.repasCBList = repasCBList;
  45. param.Annee = annee;
  46. if (param.Id == 0)
  47. model = new RestaurationParametreViewModel(param, ModeAcces.Creation);
  48. else
  49. model = new RestaurationParametreViewModel(param, ModeAcces.Modification);
  50. return View(model);
  51. }
  52. // POST: RestaurationParametres/Edit/5
  53. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  54. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  55. [HttpPost]
  56. [ValidateAntiForgeryToken]
  57. public ActionResult Edit(Entity.RestaurationParametre param, List<CheckBoxListItem> RepasCBList)
  58. {
  59. ModeAcces mode = new ModeAcces();
  60. RestaurationTypeRepaFactory typeRepasFactory = new RestaurationTypeRepaFactory(db);
  61. if (ModelState.IsValid)
  62. {
  63. db.RestaurationParametres.Attach(param);
  64. //Récupération des types de repas cochés
  65. db.Entry(param).Collection(i => i.RestaurationTypeRepas).Load();
  66. for (int i = 0; i < RepasCBList.Count; i++)
  67. {
  68. Models.CheckBoxListItem item = RepasCBList[i];
  69. if (item.IsChecked)
  70. {
  71. if (!param.RestaurationTypeRepas.Any(repas => repas.Id == int.Parse(item.ID)))
  72. {
  73. param.RestaurationTypeRepas.Add(typeRepasFactory.getById(int.Parse(item.ID)));
  74. }
  75. }
  76. else
  77. {
  78. if (param.RestaurationTypeRepas.Any(repas => repas.Id == int.Parse(item.ID)))
  79. {
  80. param.RestaurationTypeRepas.Remove(typeRepasFactory.getById(int.Parse(item.ID)));
  81. }
  82. }
  83. }
  84. RestaurationParametreFactory paramFactory = new RestaurationParametreFactory(db);
  85. if (param.Id == 0)
  86. {
  87. paramFactory.add(ref param);
  88. mode = ModeAcces.Creation;
  89. }
  90. else
  91. {
  92. paramFactory.update(ref param);
  93. mode = ModeAcces.Modification;
  94. }
  95. return RedirectToAction("Index", "Restauration", new { annee_id = param.Annee_Id });
  96. }
  97. List<CheckBoxListItem> repasCBList = new List<CheckBoxListItem>();
  98. foreach (var item in typeRepasFactory.getAllValid().ToList())
  99. {
  100. repasCBList.Add(
  101. new CheckBoxListItem()
  102. {
  103. ID = item.Id.ToString(),
  104. Display = item.Libelle,
  105. IsChecked = param.RestaurationTypeRepas.Any(i => i.Id == item.Id) ? true : false
  106. }
  107. );
  108. }
  109. ViewBag.repasCBList = repasCBList;
  110. RestaurationParametreViewModel model = new RestaurationParametreViewModel(param, mode);
  111. return View(model);
  112. }
  113. // GET: RestaurationParametres/EditCampagne/5
  114. public ActionResult EditCampagne(int? annee_id, bool edit)
  115. {
  116. if (annee_id == null)
  117. {
  118. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  119. }
  120. RestaurationParametreFactory paramFactory = new RestaurationParametreFactory(db);
  121. RestaurationParametre param = paramFactory.getBy(x => x.Annee_Id == annee_id.Value);
  122. if (param == null)
  123. {
  124. param = new RestaurationParametre();
  125. param.Annee_Id = annee_id.Value;
  126. param.Campagne = edit;
  127. paramFactory.add(ref param);
  128. }
  129. else
  130. {
  131. param.Campagne = edit;
  132. paramFactory.update(ref param);
  133. }
  134. return RedirectToAction("Index", "Restauration", new { annee_id = param.Annee_Id });
  135. }
  136. protected override void Dispose(bool disposing)
  137. {
  138. if (disposing)
  139. {
  140. db.Dispose();
  141. }
  142. base.Dispose(disposing);
  143. }
  144. }
  145. }