]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxCalDayTable.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1034 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Scheduler / UIxCalDayTable.m
1 /* UIxCalDayTable.m - this file is part of SOGo
2  *
3  * Copyright (C) 2006 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
7  * This file is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This file is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSCalendarDate.h>
25 #import <Foundation/NSDictionary.h>
26 #import <Foundation/NSKeyValueCoding.h>
27 #import <Foundation/NSString.h>
28
29 #import <EOControl/EOQualifier.h>
30
31 #import <NGExtensions/NSCalendarDate+misc.h>
32 #import <SOGoUI/SOGoDateFormatter.h>
33
34 #import "UIxCalDayTable.h"
35
36 @class SOGoAppointment;
37
38 @implementation UIxCalDayTable
39
40 - (id) init
41 {
42   if ((self = [super init]))
43     {
44       allAppointments = nil;
45       daysToDisplay = nil;
46       hoursToDisplay = nil;
47       numberOfDays = 1;
48       startDate = nil;
49       currentTableDay = nil;
50       currentTableHour = nil;
51       dateFormatter = [[SOGoDateFormatter alloc]
52                         initWithLocale: [self locale]];
53     }
54
55   return self;
56 }
57
58 - (void) dealloc
59 {
60   if (allAppointments)
61     [allAppointments release];
62   if (daysToDisplay)
63     [daysToDisplay release];
64   if (hoursToDisplay)
65     [hoursToDisplay release];
66   [dateFormatter release];
67   [super dealloc];
68 }
69
70 - (void) setCSSClass: (NSString *) aCssClass
71 {
72   cssClass = aCssClass;
73 }
74
75 - (NSString *) cssClass
76 {
77   return cssClass;
78 }
79
80 - (void) setCSSId: (NSString *) aCssId
81 {
82   cssId = aCssId;
83 }
84
85 - (NSString *) cssId
86 {
87   return cssId;
88 }
89
90 - (void) setNumberOfDays: (NSString *) aNumber
91 {
92   numberOfDays = [aNumber intValue];
93   if (daysToDisplay)
94     {
95       [daysToDisplay release];
96       daysToDisplay = nil;
97     }
98 }
99
100 - (NSString *) numberOfDays
101 {
102   return [NSString stringWithFormat: @"%d", numberOfDays];
103 }
104
105 - (void) setStartDate: (NSCalendarDate *) aStartDate
106 {
107   startDate = aStartDate;
108   if (daysToDisplay)
109     {
110       [daysToDisplay release];
111       daysToDisplay = nil;
112     }
113 }
114
115 - (NSCalendarDate *) startDate
116 {
117   if (!startDate)
118     startDate = [super startDate];
119
120   return [startDate beginOfDay];
121 }
122
123 - (NSCalendarDate *) endDate
124 {
125   NSCalendarDate *endDate;
126
127   endDate = [[self startDate] dateByAddingYears: 0
128                               months: 0
129                               days: numberOfDays - 1];
130
131   return [endDate endOfDay];
132 }
133
134 - (NSArray *) hoursToDisplay
135 {
136   unsigned int currentHour, lastHour;
137
138   if (!hoursToDisplay)
139     {
140       currentHour = [self dayStartHour];
141       lastHour = [self dayEndHour];
142       hoursToDisplay = [NSMutableArray new];
143
144       while (currentHour < lastHour)
145         {
146           [hoursToDisplay
147             addObject: [NSString stringWithFormat: @"%d", currentHour]];
148           currentHour++;
149         }
150       [hoursToDisplay
151         addObject: [NSString stringWithFormat: @"%d", currentHour]];
152     }
153
154   return hoursToDisplay;
155 }
156
157 - (NSArray *) daysToDisplay
158 {
159   NSCalendarDate *currentDate;
160   int count;
161
162   if (!daysToDisplay)
163     {
164       daysToDisplay = [NSMutableArray new];
165       currentDate = [[self startDate] hour: [self dayStartHour]
166                                       minute: 0];
167       [daysToDisplay addObject: currentDate];
168       for (count = 1; count < numberOfDays; count++)
169         [daysToDisplay addObject: [currentDate dateByAddingYears: 0
170                                                months: 0
171                                                days: count]];
172     }
173
174   return daysToDisplay;
175 }
176
177 - (void) setCurrentTableDay: (NSCalendarDate *) aTableDay
178 {
179   currentTableDay = aTableDay;
180 }
181
182 - (NSCalendarDate *) currentTableDay
183 {
184   return currentTableDay;
185 }
186
187 - (void) setCurrentTableHour: (NSString *) aTableHour
188 {
189   currentTableHour = aTableHour;
190 }
191
192 - (NSString *) currentTableHour
193 {
194   return currentTableHour;
195 }
196
197 - (NSString *) currentAppointmentHour
198 {
199   return [NSString stringWithFormat: @"%.2d00", [currentTableHour intValue]];
200 }
201
202 - (NSString *) labelForDay
203 {
204   return [NSString stringWithFormat: @"%@<br />%@",
205                    [dateFormatter shortDayOfWeek: [currentTableDay dayOfWeek]],
206                    [dateFormatter stringForObjectValue: currentTableDay]];
207 }
208
209 - (NSDictionary *) _adjustedAppointment: (NSDictionary *) anAppointment
210                                forStart: (NSCalendarDate *) start
211                                  andEnd: (NSCalendarDate *) end
212 {
213   NSMutableDictionary *newMutableAppointment;
214   NSDictionary *newAppointment;
215   BOOL startIsEarlier, endIsLater;
216
217   startIsEarlier
218     = ([[anAppointment objectForKey: @"startDate"] laterDate: start] == start);
219   endIsLater
220     = ([[anAppointment objectForKey: @"endDate"] earlierDate: end] == end);
221
222   if (startIsEarlier || endIsLater)
223     {
224       newMutableAppointment
225         = [NSMutableDictionary dictionaryWithDictionary: anAppointment];
226       
227       if (startIsEarlier)
228         [newMutableAppointment setObject: start
229                                forKey: @"startDate"];
230       if (endIsLater)
231         [newMutableAppointment setObject: end
232                                forKey: @"endDate"];
233
234       newAppointment = newMutableAppointment;
235     }
236   else
237     newAppointment = anAppointment;
238
239   return newAppointment;
240 }
241
242 - (NSArray *) appointmentsForCurrentDay
243 {
244   NSMutableArray *filteredAppointments;
245   NSEnumerator *aptsEnumerator;
246   NSDictionary *currentDayAppointment;
247   NSCalendarDate *start, *end;
248   int endHour;
249
250   if (!allAppointments)
251     {
252       allAppointments = [self fetchCoreAppointmentsInfos];
253       [allAppointments retain];
254     }
255
256   filteredAppointments = [NSMutableArray new];
257   [filteredAppointments autorelease];
258
259   start = [currentTableDay hour: [self dayStartHour] minute: 0];
260   endHour = [self dayEndHour];
261   if (endHour < 24)
262     end = [currentTableDay hour: [self dayEndHour] minute: 59];
263   else
264     end = [[currentTableDay tomorrow] hour: 0 minute: 0];
265
266   aptsEnumerator = [allAppointments objectEnumerator];
267   currentDayAppointment = [aptsEnumerator nextObject];
268   while (currentDayAppointment)
269     {
270       if (([end laterDate: [currentDayAppointment
271                              valueForKey: @"startDate"]] == end)
272           && ([start earlierDate: [currentDayAppointment
273                                     valueForKey: @"endDate"]] == start))
274         [filteredAppointments
275           addObject: [self _adjustedAppointment: currentDayAppointment
276                            forStart: start andEnd: end]];
277       currentDayAppointment = [aptsEnumerator nextObject];
278     }
279
280   return filteredAppointments;
281 }
282
283 - (void) setCurrentAppointment: (NSDictionary *) newCurrentAppointment
284 {
285   currentAppointment = newCurrentAppointment;
286 }
287
288 - (NSDictionary *) currentAppointment
289 {
290   return currentAppointment;
291 }
292
293 - (NSString *) appointmentsClasses
294 {
295   return [NSString stringWithFormat: @"appointments appointmentsFor%dDays",
296                    numberOfDays];
297 }
298
299 - (NSString *) daysViewClasses
300 {
301   return [NSString stringWithFormat: @"daysView daysViewFor%dDays", numberOfDays];
302 }
303
304 - (NSString *) dayClasses
305 {
306   NSMutableString *classes;
307   int dayOfWeek;
308
309   classes = [NSMutableString new];
310   [classes autorelease];
311   [classes appendFormat: @"day day%d", [currentTableDay dayOfWeek]];
312   if (numberOfDays > 1)
313     {
314       dayOfWeek = [currentTableDay dayOfWeek];
315       if (dayOfWeek == 0 || dayOfWeek == 6)
316         [classes appendString: @" weekEndDay"];
317       if ([currentTableDay isToday])
318         [classes appendString: @" dayOfToday"];
319       if ([[self selectedDate] isDateOnSameDay: currentTableDay])
320         [classes appendString: @" selectedDay"];
321     }
322
323   return classes;
324 }
325
326 - (NSString *) clickableHourCellClass
327 {
328   return [NSString stringWithFormat: @"clickableHourCell clickableHourCell%@", currentTableHour];
329 }
330
331 @end