]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxAppointmentEditor.m
444e345bb6be2f76f42bfec0f6aa8b3b19edb5b7
[scalable-opengroupware.org] / UI / Scheduler / UIxAppointmentEditor.m
1 /* UIxAppointmentEditor.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 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 #include <math.h>
24
25 #import <NGObjWeb/SoObject.h>
26 #import <NGObjWeb/WORequest.h>
27 #import <NGObjWeb/NSException+HTTP.h>
28 #import <NGExtensions/NSCalendarDate+misc.h>
29
30 #import <NGCards/iCalCalendar.h>
31 #import <NGCards/iCalEvent.h>
32 #import <NGCards/iCalPerson.h>
33 #import <NGCards/iCalRecurrenceRule.h>
34
35 #import <SoObjects/SOGo/SOGoUser.h>
36 #import <SoObjects/SOGo/SOGoContentObject.h>
37 #import <SoObjects/Appointments/SOGoAppointmentFolder.h>
38 #import <SoObjects/Appointments/SOGoAppointmentObject.h>
39
40 #import "UIxComponentEditor.h"
41 #import "UIxAppointmentEditor.h"
42
43 @implementation UIxAppointmentEditor
44
45 - (id) init
46 {
47   if ((self = [super init]))
48     {
49       aptStartDate = nil;
50       aptEndDate = nil;
51       item = nil;
52       event = nil;
53       repeat = nil;
54       isAllDay = NO;
55     }
56
57   return self;
58 }
59
60 - (void) dealloc
61 {
62   [item release];
63   [repeat release];
64   [aptStartDate release];
65   [aptEndDate release];
66   [super dealloc];
67 }
68
69 /* template values */
70 - (iCalEvent *) event
71 {
72   return event;
73 }
74
75 - (NSString *) saveURL
76 {
77   return [NSString stringWithFormat: @"%@/saveAsAppointment",
78                    [[self clientObject] baseURL]];
79 }
80
81 /* icalendar values */
82 - (BOOL) isAllDay
83 {
84   NSString *hm;
85
86   hm = [self queryParameterForKey: @"hm"];
87
88   return (isAllDay
89           || (hm && [hm isEqualToString: @"allday"]));
90 }
91
92 - (void) setIsAllDay: (BOOL) newIsAllDay
93 {
94   isAllDay = newIsAllDay;
95 }
96
97 - (void) setAptStartDate: (NSCalendarDate *) newAptStartDate
98 {
99   ASSIGN (aptStartDate, newAptStartDate);
100 }
101
102 - (NSCalendarDate *) aptStartDate
103 {
104   return aptStartDate;
105 }
106
107 - (void) setAptEndDate: (NSCalendarDate *) newAptEndDate
108 {
109   ASSIGN (aptEndDate, newAptEndDate);
110 }
111
112 - (NSCalendarDate *) aptEndDate
113 {
114   return aptEndDate;
115 }
116
117 - (NSArray *) repeatList
118 {
119   static NSArray *repeatItems = nil;
120
121   if (!repeatItems)
122     {
123       repeatItems = [NSArray arrayWithObjects: @"DAILY",
124                              @"WEEKLY",
125                              @"BI-WEEKLY",
126                              @"EVERY WEEKDAY",
127                              @"MONTHLY",
128                              @"YEARLY",
129                              @"-",
130                              @"CUSTOM",
131                              nil];
132       [repeatItems retain];
133     }
134
135   return repeatItems;
136 }
137
138 - (NSString *) itemRepeatText
139 {
140   NSString *text;
141
142   if ([item isEqualToString: @"-"])
143     text = item;
144   else
145     text = [self labelForKey: [NSString stringWithFormat: @"repeat_%@", item]];
146
147   return text;
148 }
149
150 - (void) setItem: (NSString *) newItem
151 {
152   ASSIGN (item, newItem);
153 }
154
155 - (NSString *) item
156 {
157   return item;
158 }
159
160 - (NSArray *) reminderList
161 {
162   static NSArray *reminderItems = nil;
163
164   if (!reminderItems)
165     {
166       reminderItems = [NSArray arrayWithObjects: @"5_MINUTES_BEFORE",
167                                @"10_MINUTES_BEFORE",
168                                @"15_MINUTES_BEFORE",
169                                @"30_MINUTES_BEFORE",
170                                @"45_MINUTES_BEFORE",
171                                @"-",
172                                @"1_HOUR_BEFORE",
173                                @"2_HOURS_BEFORE",
174                                @"5_HOURS_BEFORE",
175                                @"15_HOURS_BEFORE",
176                                @"-",
177                                @"1_DAY_BEFORE",
178                                @"2_DAYS_BEFORE",
179                                @"1_WEEK_BEFORE",
180                                @"-",
181                                @"CUSTOM",
182                                nil];
183       [reminderItems retain];
184     }
185
186   return reminderItems;
187 }
188
189 // - (void) setReminder: (NSString *) reminder
190 // {
191 //   ASSIGN(reminder, _reminder);
192 // }
193
194 // - (NSString *) reminder
195 // {
196 //   return reminder;
197 // }
198
199 - (NSString *) reminder
200 {
201   return @"";
202 }
203
204 - (void) setReminder: (NSString *) newReminder
205 {
206 }
207
208 - (NSString *) itemReminderText
209 {
210   NSString *text;
211
212   if ([item isEqualToString: @"-"])
213     text = item;
214   else
215     text = [self labelForKey: [NSString stringWithFormat: @"reminder_%@", item]];
216
217   return text;
218 }
219
220 - (NSString *) repeat
221 {
222   return repeat;
223 }
224
225 - (void) setRepeat: (NSString *) newRepeat
226 {
227   ASSIGN (repeat, newRepeat);
228 }
229
230 /* actions */
231 - (NSCalendarDate *) newStartDate
232 {
233   NSCalendarDate *newStartDate, *now;
234   NSTimeZone *timeZone;
235   int hour;
236
237   newStartDate = [self selectedDate];
238   if ([[self queryParameterForKey: @"hm"] length] == 0)
239     {
240       now = [NSCalendarDate calendarDate];
241       timeZone = [[context activeUser] timeZone];
242       [now setTimeZone: timeZone];
243       if ([now isDateOnSameDay: newStartDate])
244         {
245           hour = [now hourOfDay];
246           if (hour < 8)
247             newStartDate = [now hour: 8 minute: 0];
248           else if (hour > 18)
249             newStartDate = [[now tomorrow] hour: 8 minute: 0];
250           else
251             newStartDate = now;
252         }
253       else
254         newStartDate = [newStartDate hour: 8 minute: 0];
255     }
256
257   return newStartDate;
258 }
259
260 - (id <WOActionResults>) defaultAction
261 {
262   NSCalendarDate *startDate, *endDate;
263   NSString *duration;
264   unsigned int minutes;
265   iCalRecurrenceRule *rule;
266
267   event = (iCalEvent *) [[self clientObject] component: NO];
268   if (event)
269     {
270       startDate = [event startDate];
271       isAllDay = [event isAllDay];
272       if (isAllDay)
273         endDate = [[event endDate] dateByAddingYears: 0 months: 0 days: -1];
274       else
275         endDate = [event endDate];
276     }
277   else
278     {
279       startDate = [self newStartDate];
280       duration = [self queryParameterForKey:@"dur"];
281       if ([duration length] > 0)
282         minutes = [duration intValue];
283       else
284         minutes = 60;
285       endDate
286         = [startDate dateByAddingYears: 0 months: 0 days: 0
287                      hours: 0 minutes: minutes seconds: 0];
288     }
289
290   ASSIGN (aptStartDate, startDate);
291   ASSIGN (aptEndDate, endDate);
292
293   // We initialize our repeat ivars
294   if ([event hasRecurrenceRules])
295     {
296       repeat = @"CUSTOM";
297
298       rule = [[event recurrenceRules] lastObject];
299
300       if ([rule frequency] == iCalRecurrenceFrequenceWeekly)
301         {
302           if ([rule repeatInterval] == 1)
303             repeat = @"WEEKLY";
304           else if ([rule repeatInterval] == 2)
305             repeat = @"BI-WEEKLY";
306         }
307       else if ([rule frequency] == iCalRecurrenceFrequenceDaily)
308         {
309           if ([rule byDayMask] == (iCalWeekDayMonday
310                                    | iCalWeekDayTuesday
311                                    | iCalWeekDayWednesday
312                                    | iCalWeekDayThursday
313                                    | iCalWeekDayFriday))
314             repeat = @"EVERY WEEKDAY";
315           else if (![rule byDayMask])
316             repeat = @"DAILY";
317         }
318       else if ([rule frequency] == iCalRecurrenceFrequenceMonthly
319                && [rule repeatInterval] == 1)
320         repeat = @"MONTHLY";
321       else if ([rule frequency] == iCalRecurrenceFrequenceYearly
322                && [rule repeatInterval] == 1)
323         repeat = @"YEARLY";
324     }
325   else
326     DESTROY(repeat);
327
328   return self;
329 }
330
331 - (id <WOActionResults>) newAction
332 {
333   NSString *objectId, *method, *uri;
334   id <WOActionResults> result;
335   SOGoAppointmentFolder *co;
336
337   co = [self clientObject];
338   objectId = [co globallyUniqueObjectId];
339   if ([objectId length] > 0)
340     {
341       method = [NSString stringWithFormat:@"%@/%@/editAsAppointment",
342                          [co soURL], objectId];
343       uri = [self completeHrefForMethod: method];
344       result = [self redirectToLocation: uri];
345     }
346   else
347     result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
348                           reason: @"could not create a unique ID"];
349
350   return result;
351 }
352
353 - (id <WOActionResults>) saveAction
354 {
355   SOGoAppointmentObject *clientObject;
356   NSString *iCalString;
357
358   clientObject = [self clientObject];
359   NSLog(@"saveAction, clientObject = %@", clientObject);
360
361   iCalString = [[clientObject calendar: NO] versitString];
362
363   NSLog(@"saveAction, iCalString = %@", iCalString);
364   [clientObject saveContentString: iCalString];
365
366   return [self jsCloseWithRefreshMethod: @"refreshEventsAndDisplay()"];
367 }
368
369 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
370                            inContext: (WOContext*) context
371 {
372   NSString *actionName;
373
374   actionName = [[request requestHandlerPath] lastPathComponent];
375
376   return ([[self clientObject] isKindOfClass: [SOGoAppointmentObject class]]
377           && [actionName hasPrefix: @"save"]);
378 }
379
380 - (void) takeValuesFromRequest: (WORequest *) _rq
381                      inContext: (WOContext *) _ctx
382 {
383   SOGoAppointmentObject *clientObject;
384   int nbrDays;
385   iCalRecurrenceRule *rule;
386
387   clientObject = [self clientObject];
388   event = (iCalEvent *) [clientObject component: YES];
389
390   [super takeValuesFromRequest: _rq inContext: _ctx];
391
392   if (isAllDay)
393     {
394       nbrDays = ((float) abs ([aptEndDate timeIntervalSinceDate: aptStartDate])
395                  / 86400) + 1;
396       [event setAllDayWithStartDate: aptStartDate
397              duration: nbrDays];
398     }
399   else
400     {
401       [event setStartDate: aptStartDate];
402       [event setEndDate: aptEndDate];
403     }
404   if ([clientObject isNew])
405     [event setTransparency: @"OPAQUE"];
406
407   // We remove any repeat rules
408   if (!repeat && [event hasRecurrenceRules])
409     [event removeAllRecurrenceRules];
410   else if (!([repeat caseInsensitiveCompare: @"-"] == NSOrderedSame
411              || [repeat caseInsensitiveCompare: @"CUSTOM"] == NSOrderedSame))
412     {
413       rule = [iCalRecurrenceRule new];
414
415       [rule setInterval: @"1"];
416       if ([repeat caseInsensitiveCompare: @"BI-WEEKLY"] == NSOrderedSame)
417         {
418           [rule setFrequency: iCalRecurrenceFrequenceWeekly];
419           [rule setInterval: @"2"];
420         }
421       else if ([repeat caseInsensitiveCompare: @"EVERY WEEKDAY"] == NSOrderedSame)
422         {
423           [rule setByDayMask: (iCalWeekDayMonday
424                                |iCalWeekDayTuesday
425                                |iCalWeekDayWednesday
426                                |iCalWeekDayThursday
427                                |iCalWeekDayFriday)];
428           [rule setFrequency: iCalRecurrenceFrequenceDaily];
429         }
430       else if ([repeat caseInsensitiveCompare: @"MONTHLY"] == NSOrderedSame)
431         {
432           [rule setNamedValue: @"bymonthday"
433                 to: [NSString stringWithFormat: @"%d", [aptStartDate dayOfMonth]]];
434           [rule setFrequency: iCalRecurrenceFrequenceMonthly];
435         }
436       else
437         [rule setFrequency:
438                 (iCalRecurrenceFrequency) [rule valueForFrequency: repeat]];
439       [event setRecurrenceRules: [NSArray arrayWithObject: rule]];
440       [rule release];
441     }
442 }
443
444 // TODO: add tentatively
445
446 - (id) acceptOrDeclineAction: (BOOL) accept
447 {
448   [[self clientObject] changeParticipationStatus: (accept
449                                                    ? @"ACCEPTED"
450                                                    : @"DECLINED")];
451
452   return self;
453 }
454
455 - (id) acceptAction
456 {
457   return [self acceptOrDeclineAction: YES];
458 }
459
460 - (id) declineAction
461 {
462   return [self acceptOrDeclineAction: NO];
463 }
464
465 @end