using GreenTree.Nachtragsmanagement.Core.Authentication; using GreenTree.Nachtragsmanagement.Services.Appendix; using GreenTree.Nachtragsmanagement.Services.Deviation; using GreenTree.Nachtragsmanagement.Services.Site; using GreenTree.Nachtragsmanagement.Services.User; using GreenTree.Nachtragsmanagement.Web.Models.Global; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace GreenTree.Nachtragsmanagement.Web.Controllers { public class GlobalController : Controller { private readonly IUserHelper _userHelper; private readonly IUserService _userService; private readonly IAppendixService _appendixService; private readonly IDeviationService _deviationService; private readonly ISiteService _siteService; public GlobalController( IUserHelper userHelper, IUserService userService, IAppendixService appendixService, IDeviationService deviationService, ISiteService siteService) { _userHelper = userHelper; _userService = userService; _appendixService = appendixService; _deviationService = deviationService; _siteService = siteService; } /// /// Represents the global Footer /// public ActionResult Footer() { var cookieUser = _userHelper.FromCookies(); if (cookieUser == null) return View("~/Views/Shared/_Footer.cshtml", null); var dbUser = _userService.GetUserById(cookieUser.Id); if (dbUser == null) return View("~/Views/Shared/_Footer.cshtml", null); var footerModel = new FooterModel { CustomNumber = dbUser.CustomNumber, Forename = dbUser.Forename, Lastname = dbUser.Lastname, RoleDescription = dbUser.CurrentRole.Description }; ViewData["Roles"] = dbUser.Roles .Select(r => new { r.Id, r.Description }); return View("~/Views/Shared/_Footer.cshtml", footerModel); } /// /// Sets the current role of the logged in user and redirects to the home page /// /// The id of the new role. public ActionResult SetRole(int roleId) { var user = _userHelper.FromCookies(); var role = _userService.GetRoleById(roleId); user.CurrentRole = role; _userHelper.ToCookies(user); return RedirectToAction("Index", "Home"); } /// /// Shows an unauthorized message /// public ActionResult NotAuthorized() { return View("~/Views/Global/NotAuthorized.cshtml"); } /// /// Gets a full comment of a given entity /// /// The entity type. /// The entity id. public ActionResult GetEntityComment(string entityType, int id) { var result = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = String.Empty }; if (String.IsNullOrEmpty(entityType)) return result; switch (entityType) { case "appendix": var appendix = _appendixService.GetAppendixById(id); result.Data = appendix.Comment; break; case "deviation": var deviation = _deviationService.GetDeviationById(id); result.Data = deviation.Comment; break; case "site": var site = _siteService.GetSiteById(id); result.Data = site.Comment; break; default: return result; } return result; } } }