ActionsEduController.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using CD67.FicheCollege.Entity;
  2. using CD67.FicheCollege.Factory;
  3. using CD67.FicheCollege.MVC.Models;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Web;
  9. using System.Web.Mvc;
  10. namespace CD67.FicheCollege.MVC.Controllers
  11. {
  12. public class ActionsEduController : Controller
  13. {
  14. private Entities db = new Entities();
  15. // GET: Actions
  16. public ActionResult Index(int? annee_id)
  17. {
  18. if (annee_id == null)
  19. {
  20. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  21. }
  22. AnneeFactory fact = new AnneeFactory(db);
  23. Annee annee = fact.getById(annee_id);
  24. AnneeViewModel model = new AnneeViewModel(annee, db, ModeAcces.Lecture);
  25. return View(model);
  26. }
  27. // GET: ActionsEdu/Details/5
  28. public ActionResult Details(int? id)
  29. {
  30. if (id == null)
  31. {
  32. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  33. }
  34. ActionEduFactory fact = new ActionEduFactory(db);
  35. Entity.ActionEdu actionEdu = fact.getById(id);
  36. if (actionEdu == null)
  37. {
  38. return HttpNotFound();
  39. }
  40. ActionEduViewModel model = new ActionEduViewModel(actionEdu, db);
  41. return View(model);
  42. }
  43. // GET: ActionEduAxe/Create
  44. public ActionResult Create(int? annee_id)
  45. {
  46. if (annee_id == null)
  47. {
  48. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  49. }
  50. ActionEdu actionEdu = new ActionEdu();
  51. actionEdu.AnneeId = annee_id.Value;
  52. AnneeFactory fact = new AnneeFactory(db);
  53. actionEdu.Annee = fact.getById(annee_id);
  54. actionEdu.TokenId = Guid.NewGuid();
  55. ActionEduViewModel model = new ActionEduViewModel(actionEdu, db, ModeAcces.Creation);
  56. return View("Edit", model);
  57. }
  58. // POST: ActionEduAxe/Create
  59. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  60. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  61. [HttpPost]
  62. [ValidateAntiForgeryToken]
  63. public ActionResult Create(ActionEdu actionEdu)
  64. {
  65. if (ModelState.IsValid)
  66. {
  67. ActionEduFactory fact = new ActionEduFactory(db);
  68. fact.add(ref actionEdu);
  69. return RedirectToAction("Details", new { id = actionEdu.Id });
  70. }
  71. ActionEduViewModel model = new ActionEduViewModel(actionEdu, db, ModeAcces.Creation);
  72. return View("Edit", model);
  73. }
  74. // GET: ActionEduAxe/Edit/5
  75. public ActionResult Edit(int? id)
  76. {
  77. if (id == null)
  78. {
  79. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  80. }
  81. ActionEduFactory fact = new ActionEduFactory(db);
  82. ActionEdu action = fact.getById(id.Value);
  83. if (action == null)
  84. {
  85. return HttpNotFound();
  86. }
  87. ActionEduViewModel model = new ActionEduViewModel(action, db, ModeAcces.Modification);
  88. return View(model);
  89. }
  90. // POST: ActionEduAxe/Edit/5
  91. // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
  92. // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
  93. [HttpPost]
  94. [ValidateAntiForgeryToken]
  95. public ActionResult Edit(ActionEdu actionEdu)
  96. {
  97. if (ModelState.IsValid)
  98. {
  99. ActionEduFactory fact = new ActionEduFactory(db);
  100. fact.update(ref actionEdu);
  101. return RedirectToAction("Details", new { id = actionEdu.Id });
  102. }
  103. if (actionEdu.Annee == null)
  104. {
  105. db.ActionsEdu.Attach(actionEdu);
  106. db.Entry(actionEdu).Reference(i => i.Annee).Load();
  107. }
  108. ActionEduViewModel model = new ActionEduViewModel(actionEdu, db, ModeAcces.Modification);
  109. return View(model);
  110. }
  111. // GET: ActionEduAxe/Delete/5
  112. public ActionResult Delete(int? id)
  113. {
  114. if (id == null)
  115. {
  116. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  117. }
  118. ActionEduFactory fact = new ActionEduFactory(db);
  119. ActionEdu actionEdu = fact.getById(id.Value);
  120. if (actionEdu == null)
  121. {
  122. return HttpNotFound();
  123. }
  124. ActionEduViewModel model = new ActionEduViewModel(actionEdu, db, ModeAcces.Suppression);
  125. return View("Details", model);
  126. }
  127. // POST: ActionEduAxe/Delete/5
  128. [HttpPost, ActionName("Delete")]
  129. [ValidateAntiForgeryToken]
  130. public ActionResult DeleteConfirmed(int id)
  131. {
  132. ActionEduCollegeFactory aec_fact = new ActionEduCollegeFactory(db);
  133. aec_fact.deleteMany(a => a.ActionEduId == id);
  134. ActionEduFactory fact = new ActionEduFactory(db);
  135. ActionEdu actionEdu = fact.getById(id);
  136. int annee_id = actionEdu.AnneeId;
  137. fact.delete(ref actionEdu);
  138. return RedirectToAction("Index", new { annee_id = annee_id });
  139. }
  140. public List<ActionEdu> actions_non_importees(int annee_id)
  141. {
  142. List<ActionEdu> non_importees = new List<ActionEdu>();
  143. AnneeFactory fact = new AnneeFactory(db);
  144. Annee annee_prec = fact.getById(annee_id - 1);
  145. foreach (ActionEdu actionEdu in annee_prec.ActionsEdu)
  146. {
  147. // Pour chaque college de l'année précédente, on vérifie s'il existe dans l'année en cours en se basant sur le TokenId
  148. if (!db.ActionsEdu.Any(a => a.TokenId == actionEdu.TokenId && a.AnneeId == annee_id))
  149. {
  150. non_importees.Add(actionEdu);
  151. }
  152. }
  153. return non_importees;
  154. }
  155. // GET: Colleges
  156. public ActionResult Import(int? annee_id)
  157. {
  158. if (annee_id == null)
  159. {
  160. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  161. }
  162. ImportActionEduViewModel model = new ImportActionEduViewModel();
  163. model.Annee_Id = annee_id.Value;
  164. model.Annee_Lib = db.Annees.Where(a => a.Id == annee_id.Value).First().Libelle;
  165. List<ActionEdu> non_importes = actions_non_importees(annee_id.Value);
  166. model.set_liste(non_importes);
  167. for (int i = 0; i < non_importes.Count; i++)
  168. {
  169. ActionEdu actionEdu = non_importes[i];
  170. foreach (ActionEduCollege aec in actionEdu.ActionsEduColleges)
  171. {
  172. if (!db.Colleges.Where(c=>c.TokenId==aec.College.TokenId && c.Annee_Id == annee_id.Value).Any())
  173. {
  174. model.actionsEdu_averts[i].Add(aec.College.Libelle);
  175. }
  176. }
  177. }
  178. return View(model);
  179. }
  180. [HttpPost]
  181. [ValidateAntiForgeryToken]
  182. public ActionResult Import(int Annee_Id, List<int> actionsEdu_ids, List<bool> selection)
  183. {
  184. ActionEduFactory fact = new ActionEduFactory(db);
  185. for (int i = 0; i < selection.Count; i++)
  186. {
  187. if (selection[i])
  188. {
  189. int actionEdu_id = actionsEdu_ids[i];
  190. ActionEdu actionEdu = fact.getById(actionEdu_id);
  191. fact.clone_to_year(actionEdu, Annee_Id);
  192. }
  193. }
  194. return RedirectToAction("Index", new { annee_id = Annee_Id });
  195. }
  196. protected override void Dispose(bool disposing)
  197. {
  198. if (disposing)
  199. {
  200. db.Dispose();
  201. }
  202. base.Dispose(disposing);
  203. }
  204. }
  205. }