using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data.Entity; using System.Globalization; using System.Linq; using System.Net; using System.Text; using System.Web.Mvc; using EnVisage.App_Start; using EnVisage.Code; using EnVisage.Code.BLL; using EnVisage.Models; using jQuery.DataTables.Mvc; using EnVisage.Code.HtmlHelpers; using Microsoft.AspNet.Identity; namespace EnVisage.Controllers { [Authorize] public class CalendarController : BaseController { /// /// GET: /Calendar/ /// /// Calendar view [HttpGet] [AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Read)] public ActionResult Index() { try { if (!HtmlHelpers.CheckSecurityObjectPermission(null, Areas.FiscalCalendar, AccessLevel.Read)) return Redirect("/"); var manager = new FiscalCalendarManager(DbContext); var model = manager.LoadFiscalCalendarSettings(); return View(model); } catch (BLLException blEx) { if (blEx.DisplayError) SetErrorScript(message: blEx.Message); else { LogException(blEx); SetErrorScript(); } } catch (Exception exception) { LogException(exception); SetErrorScript(); } return View(new FiscalCalendarModel()); } /// /// Returns JSON company list with filters and sort for jQuery DataTables /// [HttpPost] [AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Read)] public JsonResult Index(JQueryDataTablesModel jQueryDataTablesModel) { int totalRecordCount; int searchRecordCount; var clients = GetHolidays(startIndex: jQueryDataTablesModel.iDisplayStart, pageSize: jQueryDataTablesModel.iDisplayLength, sortedColumns: jQueryDataTablesModel.GetSortedColumns(), totalRecordCount: out totalRecordCount, searchRecordCount: out searchRecordCount, searchString: jQueryDataTablesModel.sSearch); return this.DataTablesJson(items: clients, totalRecords: totalRecordCount, totalDisplayRecords: searchRecordCount, sEcho: jQueryDataTablesModel.sEcho); // To-Do: how to output an error in the ajax datatable? } private IEnumerable GetHolidays(int startIndex, int pageSize, IEnumerable sortedColumns, out int totalRecordCount, out int searchRecordCount, string searchString) { var query = from c in DbContext.Holidays select new {c.Id, c.Name, c.OccurrenceType, c.OccurrenceMonth, c.OccurrenceMonthDay, c.OccurrenceWeekDay, c.NonWorkingWeek, c.WorkingDay}; //filter if (!string.IsNullOrWhiteSpace(searchString)) { query = query.Where(c => c.Name.ToLower().Contains(searchString.ToLower())); } //sort foreach (var sortedColumn in sortedColumns) { switch (sortedColumn.PropertyName) { case "Id": query = sortedColumn.Direction == SortingDirection.Ascending ? query.OrderBy(c => c.Id) : query.OrderByDescending(c => c.Id); break; default: query = sortedColumn.Direction == SortingDirection.Ascending ? query.OrderBy(c => c.Name) : query.OrderByDescending(c => c.Name); break; } } totalRecordCount = DbContext.Holidays.Count(); var list = query.Skip(startIndex).Take(pageSize).ToArray().Select(item => new { item.Id, item.Name, Date = HolidayModel.GetHolidayDate((HolidayModel.HolidayOccurrence)item.OccurrenceType, item.OccurrenceMonth, item.OccurrenceMonthDay, item.OccurrenceWeekDay).ToShortDateString(), item.NonWorkingWeek, item.WorkingDay, }).ToArray(); searchRecordCount = query.Count(); return list; } // GET: /Calendar/Edit [HttpGet] [AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Write)] public ActionResult Edit() { var model = new FiscalCalendarModel(); try { var manager = new FiscalCalendarManager(DbContext); model = manager.LoadFiscalCalendarSettings(); } catch (BLLException blEx) { if (blEx.DisplayError) SetErrorScript(message: blEx.Message); else { LogException(blEx); SetErrorScript(); } } catch (Exception exception) { LogException(exception); SetErrorScript(); } return View(model); } // POST: /Calendar/Edit [HttpPost] [ValidateAntiForgeryToken] [AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Write)] public ActionResult Edit(FiscalCalendarModel model) { if (ContentLocker.IsLock("SystemSettings", model.TypeSettingId.ToString(), User.Identity.Name) || ContentLocker.IsLock("SystemSettings", model.StartDateSettingId.ToString(), User.Identity.Name) || ContentLocker.IsLock("SystemSettings", model.WeekEndingTypeSettingId.ToString(), User.Identity.Name) || ContentLocker.IsLock("SystemSettings", model.WeekEndingSettingId.ToString(), User.Identity.Name)) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); model.TrimStringProperties(); if (ModelState.IsValid) { try { var manager = new FiscalCalendarManager(DbContext); manager.SaveFiscalCalendarSettings(model); DbContext.SaveChanges(); ContentLocker.RemoveLock("SystemSettings", model.TypeSettingId.ToString(), User.Identity.Name); ContentLocker.RemoveLock("SystemSettings", model.StartDateSettingId.ToString(), User.Identity.Name); ContentLocker.RemoveLock("SystemSettings", model.WeekEndingSettingId.ToString(), User.Identity.Name); ContentLocker.RemoveLock("SystemSettings", model.WeekEndingTypeSettingId.ToString(), User.Identity.Name); return RedirectToAction("Index"); } catch (BLLException blEx) // handle any system specific error { // display error message if required if (blEx.DisplayError) ModelState.AddModelError(string.Empty, blEx.Message); else // if display not requried then display modal form with general error message { LogException(blEx); SetErrorScript(); } } catch (Exception exception) // handle any unexpected error { LogException(exception); SetErrorScript(); } } // return empty model with validation message (if any) return View(model); } #region Holidays // GET: /Calendar/EditHoliday/5 [HttpGet] [AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Write)] public ActionResult EditHoliday(Guid? id) { var model = new HolidayModel(); try { if (id == null || id == Guid.Empty) return View(model); var manager = new FiscalCalendarManager(DbContext); model = (HolidayModel)manager.LoadHoliday(id.Value); if (model == null) return HttpNotFound(); } catch (BLLException blEx) { if (blEx.DisplayError) SetErrorScript(message: blEx.Message); else { LogException(blEx); SetErrorScript(); } } catch (Exception exception) { LogException(exception); SetErrorScript(); } return View(model); } // POST: /Calendar/EditHoliday/5 [HttpPost] [ValidateAntiForgeryToken] [AreaSecurityAttribute(area = Areas.FiscalCalendar, level = AccessLevel.Write)] public ActionResult EditHoliday(HolidayModel model) { if (ContentLocker.IsLock("Holiday", model.Id.ToString(), User.Identity.Name)) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); model.TrimStringProperties(); if (ModelState.IsValid) { try { var manager = new FiscalCalendarManager(DbContext); manager.SaveHoliday(model); DbContext.SaveChanges(); ContentLocker.RemoveLock("Holiday", model.Id.ToString(), User.Identity.Name); return RedirectToAction("Index"); } catch (BLLException blEx) { if (blEx.DisplayError) ModelState.AddModelError(string.Empty, blEx.Message); else { LogException(blEx); SetErrorScript(); } } catch (Exception exception) { LogException(exception); SetErrorScript(); } } return View(model); } #endregion } }