1 /* UIxAppointmentEditor.m - this file is part of SOGo
3 * Copyright (C) 2007 Inverse groupe conseil
5 * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
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)
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.
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.
25 #import <NGObjWeb/SoObject.h>
26 #import <NGObjWeb/WORequest.h>
27 #import <NGObjWeb/NSException+HTTP.h>
28 #import <NGExtensions/NSCalendarDate+misc.h>
30 #import <NGCards/iCalEvent.h>
31 #import <NGCards/iCalPerson.h>
32 #import <NGCards/iCalRecurrenceRule.h>
34 #import <SoObjects/SOGo/SOGoUser.h>
35 #import <SoObjects/SOGo/SOGoContentObject.h>
36 #import <SoObjects/Appointments/SOGoAppointmentFolder.h>
37 #import <SoObjects/Appointments/SOGoAppointmentObject.h>
39 #import "UIxComponentEditor.h"
40 #import "UIxAppointmentEditor.h"
42 @implementation UIxAppointmentEditor
46 if ((self = [super init]))
63 [aptStartDate release];
74 - (NSString *) saveURL
76 return [NSString stringWithFormat: @"%@/saveAsAppointment",
77 [[self clientObject] baseURL]];
80 /* icalendar values */
85 hm = [self queryParameterForKey: @"hm"];
88 || (hm && [hm isEqualToString: @"allday"]));
91 - (void) setIsAllDay: (BOOL) newIsAllDay
93 isAllDay = newIsAllDay;
96 - (void) setAptStartDate: (NSCalendarDate *) newAptStartDate
98 ASSIGN (aptStartDate, newAptStartDate);
101 - (NSCalendarDate *) aptStartDate
106 - (void) setAptEndDate: (NSCalendarDate *) newAptEndDate
108 ASSIGN (aptEndDate, newAptEndDate);
111 - (NSCalendarDate *) aptEndDate
116 - (NSArray *) repeatList
118 static NSArray *repeatItems = nil;
122 repeatItems = [NSArray arrayWithObjects: @"DAILY",
131 [repeatItems retain];
137 - (NSString *) itemRepeatText
141 if ([item isEqualToString: @"-"])
144 text = [self labelForKey: [NSString stringWithFormat: @"repeat_%@", item]];
149 - (void) setItem: (NSString *) newItem
151 ASSIGN (item, newItem);
159 - (NSArray *) reminderList
161 static NSArray *reminderItems = nil;
165 reminderItems = [NSArray arrayWithObjects: @"5_MINUTES_BEFORE",
166 @"10_MINUTES_BEFORE",
167 @"15_MINUTES_BEFORE",
168 @"30_MINUTES_BEFORE",
169 @"45_MINUTES_BEFORE",
182 [reminderItems retain];
185 return reminderItems;
188 // - (void) setReminder: (NSString *) reminder
190 // ASSIGN(reminder, _reminder);
193 // - (NSString *) reminder
198 - (NSString *) reminder
203 - (void) setReminder: (NSString *) newReminder
207 - (NSString *) itemReminderText
211 if ([item isEqualToString: @"-"])
214 text = [self labelForKey: [NSString stringWithFormat: @"reminder_%@", item]];
219 - (NSString *) repeat
224 - (void) setRepeat: (NSString *) newRepeat
226 ASSIGN (repeat, newRepeat);
230 - (NSCalendarDate *) newStartDate
232 NSCalendarDate *newStartDate, *now;
233 NSTimeZone *timeZone;
236 newStartDate = [self selectedDate];
237 if ([[self queryParameterForKey: @"hm"] length] == 0)
239 now = [NSCalendarDate calendarDate];
240 timeZone = [[context activeUser] timeZone];
241 [now setTimeZone: timeZone];
242 if ([now isDateOnSameDay: newStartDate])
244 hour = [now hourOfDay];
246 newStartDate = [now hour: 8 minute: 0];
248 newStartDate = [[now tomorrow] hour: 8 minute: 0];
253 newStartDate = [newStartDate hour: 8 minute: 0];
259 - (id <WOActionResults>) defaultAction
261 NSCalendarDate *startDate, *endDate;
263 unsigned int minutes;
264 iCalRecurrenceRule *rule;
266 event = (iCalEvent *) [[self clientObject] component: NO];
269 startDate = [event startDate];
270 isAllDay = [event isAllDay];
272 endDate = [[event endDate] dateByAddingYears: 0 months: 0 days: -1];
274 endDate = [event endDate];
278 startDate = [self newStartDate];
279 duration = [self queryParameterForKey:@"dur"];
280 if ([duration length] > 0)
281 minutes = [duration intValue];
285 = [startDate dateByAddingYears: 0 months: 0 days: 0
286 hours: 0 minutes: minutes seconds: 0];
289 ASSIGN (aptStartDate, startDate);
290 ASSIGN (aptEndDate, endDate);
292 // We initialize our repeat ivars
293 if ([event hasRecurrenceRules])
297 rule = [[event recurrenceRules] lastObject];
299 if ([rule frequency] == iCalRecurrenceFrequenceWeekly)
301 if ([rule repeatInterval] == 1)
303 else if ([rule repeatInterval] == 2)
304 repeat = @"BI-WEEKLY";
306 else if ([rule frequency] == iCalRecurrenceFrequenceDaily)
308 if ([rule byDayMask] == (iCalWeekDayMonday
310 | iCalWeekDayWednesday
311 | iCalWeekDayThursday
312 | iCalWeekDayFriday))
313 repeat = @"EVERY WEEKDAY";
314 else if (![rule byDayMask])
317 else if ([rule frequency] == iCalRecurrenceFrequenceMonthly
318 && [rule repeatInterval] == 1)
320 else if ([rule frequency] == iCalRecurrenceFrequenceYearly
321 && [rule repeatInterval] == 1)
330 - (id <WOActionResults>) newAction
332 NSString *objectId, *method, *uri;
333 id <WOActionResults> result;
334 SOGoAppointmentFolder *co;
336 co = [self clientObject];
337 objectId = [co globallyUniqueObjectId];
338 if ([objectId length] > 0)
340 method = [NSString stringWithFormat:@"%@/%@/editAsAppointment",
341 [co soURL], objectId];
342 uri = [self completeHrefForMethod: method];
343 result = [self redirectToLocation: uri];
346 result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
347 reason: @"could not create a unique ID"];
352 - (id <WOActionResults>) saveAction
354 SOGoAppointmentObject *clientObject;
355 NSString *iCalString;
357 clientObject = [self clientObject];
358 NSLog(@"saveAction, clientObject = %@", clientObject);
360 iCalString = [[clientObject calendar: NO] versitString];
362 NSLog(@"saveAction, iCalString = %@", iCalString);
363 [clientObject saveContentString: iCalString];
365 return [self jsCloseWithRefreshMethod: @"refreshEventsAndDisplay()"];
368 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
369 inContext: (WOContext*) context
371 NSString *actionName;
373 actionName = [[request requestHandlerPath] lastPathComponent];
375 return ([[self clientObject] isKindOfClass: [SOGoAppointmentObject class]]
376 && [actionName hasPrefix: @"save"]);
379 - (void) takeValuesFromRequest: (WORequest *) _rq
380 inContext: (WOContext *) _ctx
382 SOGoAppointmentObject *clientObject;
384 iCalRecurrenceRule *rule;
386 clientObject = [self clientObject];
387 event = (iCalEvent *) [clientObject component: YES];
389 [super takeValuesFromRequest: _rq inContext: _ctx];
393 nbrDays = ((float) abs ([aptEndDate timeIntervalSinceDate: aptStartDate])
395 [event setAllDayWithStartDate: aptStartDate
400 [event setStartDate: aptStartDate];
401 [event setEndDate: aptEndDate];
403 if ([clientObject isNew])
404 [event setTransparency: @"OPAQUE"];
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))
412 rule = [iCalRecurrenceRule new];
414 [rule setInterval: @"1"];
415 if ([repeat caseInsensitiveCompare: @"BI-WEEKLY"] == NSOrderedSame)
417 [rule setFrequency: iCalRecurrenceFrequenceWeekly];
418 [rule setInterval: @"2"];
420 else if ([repeat caseInsensitiveCompare: @"EVERY WEEKDAY"] == NSOrderedSame)
422 [rule setByDayMask: (iCalWeekDayMonday
424 |iCalWeekDayWednesday
426 |iCalWeekDayFriday)];
427 [rule setFrequency: iCalRecurrenceFrequenceDaily];
431 (iCalRecurrenceFrequency) [rule valueForFrequency: repeat]];
432 [event setRecurrenceRules: [NSArray arrayWithObject: rule]];
437 // TODO: add tentatively
439 - (id) acceptOrDeclineAction: (BOOL) accept
441 [[self clientObject] changeParticipationStatus: (accept
450 return [self acceptOrDeclineAction: YES];
455 return [self acceptOrDeclineAction: NO];