using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web.Mvc; using DHTMLX.Common; using DHTMLX.Scheduler; using DHTMLX.Scheduler.Data; using Scheduler.Models; using Scheduler.Models.CustomLightbox; using Scheduler.Models.CustomViews; namespace Scheduler.Controllers { public class HomeController : Controller { DHXScheduler scheduler; SchedulerContext context; View view; Lightbox fields; DbSet resourceList; private string locale; public ActionResult Index() { this.scheduler = new DHXScheduler(this); this.context = new SchedulerContext(); this.resourceList = context.Resources; this.view = new View(scheduler); this.fields = new Lightbox(scheduler); AddExtensions(); SetLocale(); view.AddViews(resourceList); fields.AddLightboxes(resourceList); scheduler.LoadData = true; scheduler.Config.show_loading = true; scheduler.Config.all_timed = DHTMLX.Scheduler.Settings.SchedulerConfig.AllTimed.Multiday; scheduler.Config.first_hour = 6; scheduler.Config.last_hour = 18; scheduler.Config.mark_now = true; scheduler.EnableDynamicLoading(SchedulerDataLoader.DynamicalLoadingMode.Month); scheduler.EnableDataprocessor = true; scheduler.InitialDate = new DateTime(); scheduler.Highlighter.Enable("highlight_section", 60); return View(new LocaleData(scheduler, this.locale)); } //Displays the events into the scheduler. public ContentResult Data() { var dateFrom = DateTime.ParseExact(this.Request.QueryString["from"], "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); var dateTo = DateTime.ParseExact(this.Request.QueryString["to"], "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); return (new SchedulerAjaxData( new SchedulerContext().Events .Select(e => new { e.id, e.text, e.start_date, e.end_date, e.event_length, e.rec_type, e.event_pid, e.status, e.event_location, e.type }) .Where(e => e.start_date <= dateTo && e.end_date >= dateFrom) ) ); } //Update event changes and save it. public ContentResult Save(int? id, FormCollection actionValues) { var action = new DataAction(actionValues); var entities = new SchedulerContext(); try { var changedEvent = (Event)DHXEventsHelper.Bind(typeof(Event), actionValues); bool isFinished = DeleteRelated(action, changedEvent, entities); UnassignResources(action, entities); if (!isFinished) { switch (action.Type) { case DataActionTypes.Insert: entities.Events.Add(changedEvent); if (changedEvent.rec_type == "none") action.Type = DataActionTypes.Delete; break; case DataActionTypes.Delete: changedEvent = entities.Events.SingleOrDefault(ev => ev.id == action.SourceId); entities.Events.Remove(changedEvent); break; default:// "update" var target = entities.Events.SingleOrDefault(ev => ev.id == action.SourceId); DHXEventsHelper.Update(target, changedEvent, new List { "id" }); break; } } entities.SaveChanges(); action.TargetId = changedEvent.id; UpdateResources(action, changedEvent, entities); } catch (Exception a) { action.Type = DataActionTypes.Error; } return (new AjaxSaveResponse(action)); } //Delete sets of recurring events. private bool DeleteRelated(DataAction action, Event changedEvent, SchedulerContext entities) { bool finished = false; if ((action.Type == DataActionTypes.Delete || action.Type == DataActionTypes.Update) && !string.IsNullOrEmpty(changedEvent.rec_type)) { entities.Events.RemoveRange(from ev in entities.Events where ev.event_pid == changedEvent.id select ev); } if (action.Type == DataActionTypes.Delete && changedEvent.id != 0 && changedEvent != null) { Event changed = (from ev in entities.Events where ev.id == action.TargetId select ev).Single(); changed.rec_type = "none"; entities.Events.Remove(changed); finished = true; } return finished; } //Assigns resource to event private void AssignResourcesToEvent(Event changedEvent, SchedulerContext entities, string[] resources) { foreach (string resource in resources) { int resource_id = Convert.ToInt32(resource); ResourceEventSync sync = new ResourceEventSync(); sync.event_id = changedEvent.id; sync.resource_id = resource_id; entities.Sync.Add(sync); } } //Updates resources on event private void UpdateResources(DataAction action, Event changedEvent, SchedulerContext entities) { if (!string.IsNullOrEmpty(changedEvent.resource_id)) { string[] resources = GetResources(changedEvent); if (action.Type == DataActionTypes.Insert) { AssignResourcesToEvent(changedEvent, entities, resources); } if (action.Type == DataActionTypes.Update) { var synctarget = (from s in entities.Sync where s.event_id == changedEvent.id select s).ToList(); if (synctarget.Count == 0) AssignResourcesToEvent(changedEvent, entities, resources); else { var newResources = resources.Where(p => !synctarget.Any(l => Convert.ToInt32(p) == l.resource_id)); var removeResources = synctarget.Where(p => !resources.Any(l => p.resource_id == Convert.ToInt32(l))); Event changed = (from ev in entities.Events where ev.id == action.TargetId select ev).Single(); var remove = (from s in removeResources where s.event_id == changed.id select s); entities.Sync.RemoveRange(remove); AssignResourcesToEvent(changedEvent, entities, newResources.ToArray()); } } entities.SaveChanges(); } } //Removes resource from event private void UnassignResources(DataAction action, SchedulerContext entities) { if (action.Type == DataActionTypes.Delete) { Event changed = (from ev in entities.Events where ev.id == action.TargetId select ev).Single(); entities.Sync.RemoveRange(from sync in entities.Sync where sync.event_id == changed.id select sync); } } private string[] GetResources(Event changedEvent) { string input = changedEvent.resource_id; string[] resources = input.Split(','); return resources; } //Add new extensions from dhtmlx private void AddExtensions() { scheduler.Extensions.Add(SchedulerExtensions.Extension.LiveUpdates); scheduler.Extensions.Add(SchedulerExtensions.Extension.Recurring); scheduler.Extensions.Add(SchedulerExtensions.Extension.Tooltip); scheduler.Extensions.Add(SchedulerExtensions.Extension.OuterDrag); scheduler.Extensions.Add(SchedulerExtensions.Extension.Cookie); scheduler.Extensions.Add(SchedulerExtensions.Extension.Multisection); } //Set local data and also save it into strings so it can be sent to view. private void SetLocale() { var locale = SchedulerLocalization.Localizations.English; var skin = DHXScheduler.Skins.Terrace; var request_lang = this.Request.QueryString["language"]; var request_skin = this.Request.QueryString["skin"]; if (!string.IsNullOrEmpty(request_lang)) { locale = (SchedulerLocalization.Localizations)Enum.Parse(typeof(SchedulerLocalization.Localizations), request_lang); } if (!string.IsNullOrEmpty(request_skin)) { skin = (DHXScheduler.Skins)Enum.Parse(typeof(DHXScheduler.Skins), request_skin); } scheduler.Skin = skin; scheduler.Localization.Set(locale); scheduler.Localization.Directory = "locale"; this.locale = locale.ToString(); } } }