| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using System.Net;
- using System.Web.Mvc;
- using CD67.FicheCollege.Entity;
- using CD67.FicheCollege.Factory;
- using CD67.FicheCollege.MVC.Models;
- using System.Linq;
- using System.Collections.Generic;
- using System;
- namespace CD67.FicheCollege.MVC.Controllers
- {
- public class RestaurationParametresController : Controller
- {
- private Entities db = new Entities();
- // GET: RestaurationParametres/Edit/5
- public ActionResult Edit(int? annee_id)
- {
- if (annee_id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- AnneeFactory fact = new AnneeFactory(db);
- Annee annee = fact.getById(annee_id);
- RestaurationParametreFactory paramFactory = new RestaurationParametreFactory(db);
- RestaurationParametre param = paramFactory.getBy(x => x.Annee_Id == annee_id.Value);
- RestaurationParametreViewModel model;
- if (param == null)
- {
- param = new RestaurationParametre();
- param.Annee_Id = annee_id.Value;
- }
- RestaurationTypesRepaFactory typeRepasFactory = new RestaurationTypesRepaFactory(db);
- List<CheckBoxListItem> repasCBList = new List<CheckBoxListItem>();
- foreach (var item in typeRepasFactory.getAllValid().ToList())
- {
- repasCBList.Add(
- new CheckBoxListItem()
- {
- ID = item.Id.ToString(),
- Display = item.Libelle,
- IsChecked = param.RestaurationTypesRepas.Any(i => i.Id == item.Id) ? true : false
- }
- );
- }
- ViewBag.repasCBList = repasCBList;
- param.Annee = annee;
- if (param.Id == 0)
- model = new RestaurationParametreViewModel(param, ModeAcces.Creation);
- else
- model = new RestaurationParametreViewModel(param, ModeAcces.Modification);
- return View(model);
- }
- // POST: RestaurationParametres/Edit/5
- // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour
- // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598.
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Edit(Entity.RestaurationParametre param, List<CheckBoxListItem> RepasCBList)
- {
- ModeAcces mode = new ModeAcces();
- RestaurationTypesRepaFactory typeRepasFactory = new RestaurationTypesRepaFactory(db);
- if (ModelState.IsValid)
- {
- db.RestaurationParametres.Attach(param);
- //Récupération des types de repas cochés
- db.Entry(param).Collection(i => i.RestaurationTypesRepas).Load();
- for (int i = 0; i < RepasCBList.Count; i++)
- {
- Models.CheckBoxListItem item = RepasCBList[i];
- if (item.IsChecked)
- {
- if (!param.RestaurationTypesRepas.Any(repas => repas.Id == int.Parse(item.ID)))
- {
- param.RestaurationTypesRepas.Add(typeRepasFactory.getById(int.Parse(item.ID)));
- }
- }
- else
- {
- if (param.RestaurationTypesRepas.Any(repas => repas.Id == int.Parse(item.ID)))
- {
- param.RestaurationTypesRepas.Remove(typeRepasFactory.getById(int.Parse(item.ID)));
- }
- }
- }
- RestaurationParametreFactory paramFactory = new RestaurationParametreFactory(db);
- if (param.Id == 0)
- {
- paramFactory.add(ref param);
- mode = ModeAcces.Creation;
- }
- else
- {
- paramFactory.update(ref param);
- mode = ModeAcces.Modification;
- }
- return RedirectToAction("Index", "Restauration", new { annee_id = param.Annee_Id });
- }
- List<CheckBoxListItem> repasCBList = new List<CheckBoxListItem>();
- foreach (var item in typeRepasFactory.getAllValid().ToList())
- {
- repasCBList.Add(
- new CheckBoxListItem()
- {
- ID = item.Id.ToString(),
- Display = item.Libelle,
- IsChecked = param.RestaurationTypesRepas.Any(i => i.Id == item.Id) ? true : false
- }
- );
- }
- ViewBag.repasCBList = repasCBList;
- RestaurationParametreViewModel model = new RestaurationParametreViewModel(param, mode);
- return View(model);
- }
- // GET: RestaurationParametres/EditCampagne/5
- public ActionResult EditCampagne(int? annee_id, bool edit)
- {
- if (annee_id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- RestaurationParametreFactory paramFactory = new RestaurationParametreFactory(db);
- RestaurationParametre param = paramFactory.getBy(x => x.Annee_Id == annee_id.Value);
- if (param == null)
- {
- param = new RestaurationParametre();
- param.Annee_Id = annee_id.Value;
- param.Campagne = edit;
- paramFactory.add(ref param);
- }
- else
- {
- param.Campagne = edit;
- paramFactory.update(ref param);
- }
- return RedirectToAction("Index", "Restauration", new { annee_id = param.Annee_Id });
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- }
- }
|