ActionsEduController.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. actionEdu.hydrate();
  41. ActionEduViewModel model = new ActionEduViewModel(actionEdu, db);
  42. return View(model);
  43. }
  44. // GET: ActionEduAxe/Create
  45. public ActionResult Create(int? annee_id)
  46. {
  47. if (annee_id == null)
  48. {
  49. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  50. }
  51. ActionEdu actionEdu = new ActionEdu();
  52. actionEdu.AnneeId = annee_id.Value;
  53. AnneeFactory fact = new AnneeFactory(db);
  54. actionEdu.Annee = fact.getById(annee_id);
  55. actionEdu.hydrate();
  56. actionEdu.TokenId = Guid.NewGuid();
  57. ActionEduViewModel model = new ActionEduViewModel(actionEdu, db, ModeAcces.Creation);
  58. return View(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. [ValidateAntiForgeryToken]
  65. public ActionResult Create(ActionEdu actionEdu)
  66. {
  67. if (ModelState.IsValid)
  68. {
  69. ActionEduFactory fact = new ActionEduFactory(db);
  70. fact.add(ref actionEdu);
  71. return RedirectToAction("Index", new { annee_id = actionEdu.AnneeId });
  72. }
  73. actionEdu.hydrate();
  74. ActionEduViewModel model = new ActionEduViewModel(actionEdu, db, ModeAcces.Creation);
  75. return View(model);
  76. }
  77. // GET: ActionEduAxe/Edit/5
  78. public ActionResult Edit(int? id)
  79. {
  80. if (id == null)
  81. {
  82. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  83. }
  84. ActionEduFactory fact = new ActionEduFactory(db);
  85. ActionEdu action = fact.getById(id.Value);
  86. if (action == null)
  87. {
  88. return HttpNotFound();
  89. }
  90. action.hydrate();
  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. [ValidateAntiForgeryToken]
  99. public ActionResult Edit(ActionEdu actionEdu)
  100. {
  101. if (ModelState.IsValid)
  102. {
  103. ActionEduFactory fact = new ActionEduFactory(db);
  104. fact.update(ref actionEdu);
  105. return RedirectToAction("Index", new { annee_id = actionEdu.AnneeId });
  106. }
  107. if (actionEdu.Annee == null)
  108. {
  109. db.ActionsEdu.Attach(actionEdu);
  110. db.Entry(actionEdu).Reference(i => i.Annee).Load();
  111. }
  112. actionEdu.hydrate();
  113. ActionEduViewModel model = new ActionEduViewModel(actionEdu, db, ModeAcces.Modification);
  114. return View(model);
  115. }
  116. // GET: ActionEduAxe/Delete/5
  117. public ActionResult Delete(int? id)
  118. {
  119. if (id == null)
  120. {
  121. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  122. }
  123. ActionEduFactory fact = new ActionEduFactory(db);
  124. ActionEdu actionEdu = fact.getById(id.Value);
  125. if (actionEdu == null)
  126. {
  127. return HttpNotFound();
  128. }
  129. actionEdu.hydrate();
  130. ActionEduViewModel model = new ActionEduViewModel(actionEdu, db);
  131. return View(model);
  132. }
  133. // POST: ActionEduAxe/Delete/5
  134. [HttpPost, ActionName("Delete")]
  135. [ValidateAntiForgeryToken]
  136. public ActionResult DeleteConfirmed(int id)
  137. {
  138. ActionEduCollegeFactory aec_fact = new ActionEduCollegeFactory(db);
  139. aec_fact.deleteMany(a => a.ActionEduId == id);
  140. ActionEduFactory fact = new ActionEduFactory(db);
  141. ActionEdu actionEdu = fact.getById(id);
  142. int annee_id = actionEdu.AnneeId;
  143. fact.delete(ref actionEdu);
  144. return RedirectToAction("Index", new { annee_id = annee_id });
  145. }
  146. public List<ActionEdu> actions_non_importees(int annee_id)
  147. {
  148. List<ActionEdu> non_importees = new List<ActionEdu>();
  149. AnneeFactory fact = new AnneeFactory(db);
  150. Annee annee_prec = fact.getById(annee_id - 1);
  151. foreach (ActionEdu actionEdu in annee_prec.ActionsEdu)
  152. {
  153. // 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
  154. if (!db.ActionsEdu.Any(a => a.TokenId == actionEdu.TokenId && a.AnneeId == annee_id))
  155. {
  156. non_importees.Add(actionEdu);
  157. }
  158. }
  159. return non_importees;
  160. }
  161. // GET: Colleges
  162. public ActionResult Import(int? annee_id)
  163. {
  164. if (annee_id == null)
  165. {
  166. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  167. }
  168. ImportActionEduViewModel model = new ImportActionEduViewModel();
  169. model.Annee_Id = annee_id.Value;
  170. model.Annee_Lib = db.Annees.Where(a => a.Id == annee_id.Value).First().Libelle;
  171. List<ActionEdu> non_importes = actions_non_importees(annee_id.Value);
  172. model.set_liste(non_importes);
  173. for (int i = 0; i < non_importes.Count; i++)
  174. {
  175. ActionEdu actionEdu = non_importes[i];
  176. foreach (ActionEduCollege aec in actionEdu.ActionsEduColleges)
  177. {
  178. if (!db.Colleges.Where(c=>c.TokenId==aec.College.TokenId && c.Annee_Id == annee_id.Value).Any())
  179. {
  180. model.actionsEdu_averts[i].Add(aec.College.Libelle);
  181. }
  182. }
  183. }
  184. return View(model);
  185. }
  186. [HttpPost]
  187. [ValidateAntiForgeryToken]
  188. public ActionResult Import(int Annee_Id, List<int> actionsEdu_ids, List<bool> selection)
  189. {
  190. ActionEduFactory fact = new ActionEduFactory(db);
  191. for (int i = 0; i < selection.Count; i++)
  192. {
  193. if (selection[i])
  194. {
  195. int actionEdu_id = actionsEdu_ids[i];
  196. ActionEdu actionEdu = fact.getById(actionEdu_id);
  197. fact.clone_to_year(actionEdu, Annee_Id);
  198. }
  199. }
  200. return RedirectToAction("Index", new { annee_id = Annee_Id });
  201. }
  202. public ActionResult Up(int? id)
  203. {
  204. if (id == null)
  205. {
  206. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  207. }
  208. ActionEduFactory fact = new ActionEduFactory(db);
  209. ActionEdu actionEdu = fact.getById(id.Value);
  210. fact.Up(ref actionEdu);
  211. return RedirectToAction("Index", new { annee_id = actionEdu.AnneeId });
  212. }
  213. public ActionResult Down(int? id)
  214. {
  215. if (id == null)
  216. {
  217. return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  218. }
  219. ActionEduFactory fact = new ActionEduFactory(db);
  220. ActionEdu actionEdu = fact.getById(id.Value);
  221. fact.Down(ref actionEdu);
  222. return RedirectToAction("Index", new { annee_id = actionEdu.AnneeId });
  223. }
  224. protected override void Dispose(bool disposing)
  225. {
  226. if (disposing)
  227. {
  228. db.Dispose();
  229. }
  230. base.Dispose(disposing);
  231. }
  232. }
  233. }