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