How-to: Calendar
The examples use rooms and their bookings:
CLASS Room 'Room';
name 'Name' = DATA ISTRING[50] (Room) IN id;
color 'Color' = DATA COLOR (Room);
CLASS Booking 'Booking';
name 'Name' = DATA ISTRING[100] (Booking) IN id;
room 'Room' = DATA Room (Booking);
dateTimeFrom 'Start' = DATA DATETIME (Booking);
dateTimeTo 'End' = DATA DATETIME (Booking);
Example 1
Task
We need a form that shows the bookings on a calendar, each in the color of its room, and lets the user move a booking to another time.
Solution
FORM bookings 'Bookings'
OBJECTS b = Booking CALENDAR BACKGROUND color(room(b))
PROPERTIES(b) name READONLY, dateTimeFrom, dateTimeTo
;
NAVIGATOR {
NEW bookings;
}
The CALENDAR option of the object block displays the bookings in the web client as events of a calendar. The calendar finds the properties of an event by their export/import names, here the names of the properties themselves: dateTimeFrom and dateTimeTo give the start and the end of a booking, and name gives its text. The BACKGROUND option of the object group colors each booking in the color of its room. Since dateTimeFrom and dateTimeTo are of the DATETIME class, the calendar has a day view with a time scale besides the month and the week.
The user moves a booking by dragging it to another day. In the day view the user can also move it to another time and stretch it by dragging its end. The calendar writes the new start and end into dateTimeFrom and dateTimeTo, so they are left changeable, while name, which is only displayed, is marked READONLY. The changes are saved together with the other changes of the form.
Example 2
Task
Similar to Example 1, we need to forbid two bookings of one room that overlap in time.
Solution
CONSTRAINT room(Booking a) = room(Booking b) AND a != b
AND dateTimeFrom(a) < dateTimeTo(b) AND dateTimeFrom(b) < dateTimeTo(a)
MESSAGE 'The room is already booked for this time';
Two bookings overlap when each of them starts before the other ends, so two bookings where one ends when the other starts do not overlap. The condition a != b keeps a booking from being compared with itself. If the user drags a booking to a time when its room is already booked and saves the form, the constraint cancels the save with this message and the list of the overlapping bookings. The booking stays where the user dragged it: the user can drag it again or cancel the changes.