Step 4.1. Initializing & configuring the scheduler

Step 4.1. Initializing & configuring the scheduler

Skip and go to the next step

We will devote this chapter to 'writing' the Manager() action in SystemController.cs.

All the code in the action is divided into 5 regions plus a part implementing initialization. On the base of this, this chapter we divided into 6 steps, each of which presents an appropriate part of the code.

Step 4.1.1. Scheduler initialization

First, we will initialize the scheduler.

On the Step 3 we created LINQ to SQl classes to retrieve data from the database. And next what we need to do in the action is to apply this classes in order to populate the select control (intended for setting the status of a task) with values from the Statuses database table.

Your actions: modify the Manager action so it looks like the action below:

public ActionResult Manager()
            var scheduler = new DHXScheduler(this);
            var context = new TasksDataContext();
            return View(scheduler );

The TasksDataContext class (LINQ to SQL DataContext) was generated by the Visual Studio Object Relational Designer.

Step 4.1.2. Checking user rights

Then, we will check one more time whether a user has manager rights. This protects our app from the unauthorized access in case a user skips the login page and loads your server-side login script URL directly in the browser.

Your action: add the region below to the Manager() action.

#region check rights
       if (!RoleIs("Manager"))// checks the role
            return RedirectToAction("Index", "System");//in case the role is not manager, redirects to the login page

RoleIs() is the function that we defined on the step 3 (sub-step 3.2.3).

Step 4.1.3. Scheduler configuration

On this step we will configure our scheduler.

Your action: add the region below to the Manager() action.

#region configuration
       scheduler.Config.first_hour = 8;//sets the minimum value for the hour scale (Y-Axis)
       scheduler.Config.last_hour = 17;//sets the maximum value for the hour scale (Y-Axis)
       scheduler.Config.time_step = 30;//sets the scale interval for the time selector in the lightbox. 
       scheduler.Config.full_day = true;// blocks entry fields in the 'Time period' section of the lightbox and sets time period to a full day from 00.00 the current cell date untill 00.00 next day. 

Back to top

Step 4.1.4. Configuration of scheduler views

On this step we will configure views of the scheduler. What will we do? First of all, we will keep in the scheduler just 2 views: units and week. For this purpose, we delete all the default views and add just the desired ones.

Then, we will populate the X-Axis of the unit view with data and make the units view selected initially. We will use names of employees as axis items(var staff).

Your action: add the region below to the Manager() action.

#region views configuration
       scheduler.Views.Clear();//removes all views from the scheduler
       scheduler.Views.Add(new WeekView());// adds a tab with the week view
       var units = new UnitsView("staff", "owner_id");// initializes the units view
       units.Label = "Staff";// sets the label of the units tab 
       var staff = from usr in context.aspnet_Users // the variable contains names of employees from the database
                  join link in context.aspnet_UsersInRoles on usr.UserId equals link.UserId
                  join role in context.aspnet_Roles on link.RoleId equals role.RoleId
                  where role.RoleName == "Employee"
                  select new { key = usr.UserId, label = usr.UserName };
       units.AddOptions(staff);// sets X-Axis items to names of employees           
       scheduler.Views.Add(units);//adds a tab with the units view
       scheduler.InitialView = units.Name;// makes the units view selected initially

Back to top

Step 4.1.5. Lightbox configuration

Move to the lightbox. Our lightbox will contain 4 elements:

  • a text input Task - the text of a task;
  • a text input Details - the description of a task;
  • a dropdown list Status - the list with possible values of the task status;
  • a control area containing 2 date inputs for setting start and end times;

Note, as soon as you add some custom control, the default controls will be removed.

Your action: add the region below to the Manager() action.

#region lightbox configuration
       var text = new LightboxText("text", "Task");// initializes a text input with the label 'Task'
       text.Height = 20;// sets the height of the control
       text.Focus = true;// set focus to the control
       scheduler.Lightbox.Add(text);// adds the control to the lightbox
       var description = new LightboxText("details", "Details");// initializes a text input with the label 'Task'
       description.Height = 80;
       var status = new LightboxSelect("status_id", "Status");// initializes a dropdown list with the label 'Status'
       status.AddOptions(context.Statuses);// populates the list with values from the 'Statuses' table
       scheduler.Lightbox.Add(new LightboxTime("time"));// initializes and adds a control area for setting start and end times of a task

Back to top

Step 4.1.6. Common data managing. Enabling dataProcessor

Here we'll enable dataProcessor to be able save data changes while creating, deleting, editing tasks.

Your action: add the region below to the Manager() action.

#region data  
       scheduler.EnableDataprocessor = true;// enables dataprocessor
       scheduler.LoadData = true;//'says' to send data request after scheduler initialization 
       scheduler.Data.Loader.PreventCache();// adds the current ticks value to url to prevent caching of the request 
       scheduler.Data.DataProcessor.UpdateFieldsAfterSave = true;// Tracks after server responses for modified event fields 
       scheduler.InitialDate = new DateTime(2011, 10, 7);// sets the initial date of the scheduler

See the full code of the Manager() action here.

comments powered by Disqus