ActionsEduController.cs 8.4 KB

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