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/iCalCalendar.h>
31 #import <NGCards/iCalEvent.h>
32 #import <NGCards/iCalPerson.h>
33 #import <NGCards/iCalRecurrenceRule.h>
35 #import <SoObjects/SOGo/SOGoUser.h>
36 #import <SoObjects/SOGo/SOGoContentObject.h>
37 #import <SoObjects/Appointments/SOGoAppointmentFolder.h>
38 #import <SoObjects/Appointments/SOGoAppointmentObject.h>
40 #import "UIxComponentEditor.h"
41 #import "UIxAppointmentEditor.h"
43 @implementation UIxAppointmentEditor
47 if ((self = [super init]))
64 [aptStartDate release];
75 - (NSString *) saveURL
77 return [NSString stringWithFormat: @"%@/saveAsAppointment",
78 [[self clientObject] baseURL]];
81 /* icalendar values */
86 hm = [self queryParameterForKey: @"hm"];
89 || (hm && [hm isEqualToString: @"allday"]));
92 - (void) setIsAllDay: (BOOL) newIsAllDay
94 isAllDay = newIsAllDay;
97 - (void) setAptStartDate: (NSCalendarDate *) newAptStartDate
99 ASSIGN (aptStartDate, newAptStartDate);
102 - (NSCalendarDate *) aptStartDate
107 - (void) setAptEndDate: (NSCalendarDate *) newAptEndDate
109 ASSIGN (aptEndDate, newAptEndDate);
112 - (NSCalendarDate *) aptEndDate
117 - (NSArray *) repeatList
119 static NSArray *repeatItems = nil;
123 repeatItems = [NSArray arrayWithObjects: @"DAILY",
132 [repeatItems retain];
138 - (NSString *) itemRepeatText
142 if ([item isEqualToString: @"-"])
145 text = [self labelForKey: [NSString stringWithFormat: @"repeat_%@", item]];
150 - (void) setItem: (NSString *) newItem
152 ASSIGN (item, newItem);
160 - (NSArray *) reminderList
162 static NSArray *reminderItems = nil;
166 reminderItems = [NSArray arrayWithObjects: @"5_MINUTES_BEFORE",
167 @"10_MINUTES_BEFORE",
168 @"15_MINUTES_BEFORE",
169 @"30_MINUTES_BEFORE",
170 @"45_MINUTES_BEFORE",
183 [reminderItems retain];
186 return reminderItems;
189 // - (void) setReminder: (NSString *) reminder
191 // ASSIGN(reminder, _reminder);
194 // - (NSString *) reminder
199 - (NSString *) reminder
204 - (void) setReminder: (NSString *) newReminder
208 - (NSString *) itemReminderText
212 if ([item isEqualToString: @"-"])
215 text = [self labelForKey: [NSString stringWithFormat: @"reminder_%@", item]];
220 - (NSString *) repeat
225 - (void) setRepeat: (NSString *) newRepeat
227 ASSIGN (repeat, newRepeat);
231 - (NSCalendarDate *) newStartDate
233 NSCalendarDate *newStartDate, *now;
234 NSTimeZone *timeZone;
237 newStartDate = [self selectedDate];
238 if ([[self queryParameterForKey: @"hm"] length] == 0)
240 now = [NSCalendarDate calendarDate];
241 timeZone = [[context activeUser] timeZone];
242 [now setTimeZone: timeZone];
243 if ([now isDateOnSameDay: newStartDate])
245 hour = [now hourOfDay];
247 newStartDate = [now hour: 8 minute: 0];
249 newStartDate = [[now tomorrow] hour: 8 minute: 0];
254 newStartDate = [newStartDate hour: 8 minute: 0];
260 - (id <WOActionResults>) defaultAction
262 NSCalendarDate *startDate, *endDate;
264 unsigned int minutes;
265 iCalRecurrenceRule *rule;
267 event = (iCalEvent *) [[self clientObject] component: NO];
270 startDate = [event startDate];
271 isAllDay = [event isAllDay];
273 endDate = [[event endDate] dateByAddingYears: 0 months: 0 days: -1];
275 endDate = [event endDate];
279 startDate = [self newStartDate];
280 duration = [self queryParameterForKey:@"dur"];
281 if ([duration length] > 0)
282 minutes = [duration intValue];
286 = [startDate dateByAddingYears: 0 months: 0 days: 0
287 hours: 0 minutes: minutes seconds: 0];
290 ASSIGN (aptStartDate, startDate);
291 ASSIGN (aptEndDate, endDate);
293 // We initialize our repeat ivars
294 if ([event hasRecurrenceRules])
298 rule = [[event recurrenceRules] lastObject];
300 if ([rule frequency] == iCalRecurrenceFrequenceWeekly)
302 if ([rule repeatInterval] == 1)
304 else if ([rule repeatInterval] == 2)
305 repeat = @"BI-WEEKLY";
307 else if ([rule frequency] == iCalRecurrenceFrequenceDaily)
309 if ([rule byDayMask] == (iCalWeekDayMonday
311 | iCalWeekDayWednesday
312 | iCalWeekDayThursday
313 | iCalWeekDayFriday))
314 repeat = @"EVERY WEEKDAY";
315 else if (![rule byDayMask])
318 else if ([rule frequency] == iCalRecurrenceFrequenceMonthly
319 && [rule repeatInterval] == 1)
321 else if ([rule frequency] == iCalRecurrenceFrequenceYearly
322 && [rule repeatInterval] == 1)
331 - (id <WOActionResults>) newAction
333 NSString *objectId, *method, *uri;
334 id <WOActionResults> result;
335 SOGoAppointmentFolder *co;
337 co = [self clientObject];
338 objectId = [co globallyUniqueObjectId];
339 if ([objectId length] > 0)
341 method = [NSString stringWithFormat:@"%@/%@/editAsAppointment",
342 [co soURL], objectId];
343 uri = [self completeHrefForMethod: method];
344 result = [self redirectToLocation: uri];
347 result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
348 reason: @"could not create a unique ID"];
353 - (id <WOActionResults>) saveAction
355 SOGoAppointmentObject *clientObject;
356 NSString *iCalString;
358 clientObject = [self clientObject];
359 NSLog(@"saveAction, clientObject = %@", clientObject);
361 iCalString = [[clientObject calendar: NO] versitString];
363 NSLog(@"saveAction, iCalString = %@", iCalString);
364 [clientObject saveContentString: iCalString];
366 return [self jsCloseWithRefreshMethod: @"refreshEventsAndDisplay()"];
369 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
370 inContext: (WOContext*) context
372 NSString *actionName;
374 actionName = [[request requestHandlerPath] lastPathComponent];
376 return ([[self clientObject] isKindOfClass: [SOGoAppointmentObject class]]
377 && [actionName hasPrefix: @"save"]);
380 - (void) takeValuesFromRequest: (WORequest *) _rq
381 inContext: (WOContext *) _ctx
383 SOGoAppointmentObject *clientObject;
385 iCalRecurrenceRule *rule;
387 clientObject = [self clientObject];
388 event = (iCalEvent *) [clientObject component: YES];
390 [super takeValuesFromRequest: _rq inContext: _ctx];
394 nbrDays = ((float) abs ([aptEndDate timeIntervalSinceDate: aptStartDate])
396 [event setAllDayWithStartDate: aptStartDate
401 [event setStartDate: aptStartDate];
402 [event setEndDate: aptEndDate];
404 if ([clientObject isNew])
405 [event setTransparency: @"OPAQUE"];
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))
413 rule = [iCalRecurrenceRule new];
415 [rule setInterval: @"1"];
416 if ([repeat caseInsensitiveCompare: @"BI-WEEKLY"] == NSOrderedSame)
418 [rule setFrequency: iCalRecurrenceFrequenceWeekly];
419 [rule setInterval: @"2"];
421 else if ([repeat caseInsensitiveCompare: @"EVERY WEEKDAY"] == NSOrderedSame)
423 [rule setByDayMask: (iCalWeekDayMonday
425 |iCalWeekDayWednesday
427 |iCalWeekDayFriday)];
428 [rule setFrequency: iCalRecurrenceFrequenceDaily];
430 else if ([repeat caseInsensitiveCompare: @"MONTHLY"] == NSOrderedSame)
432 [rule setNamedValue: @"bymonthday"
433 to: [NSString stringWithFormat: @"%d", [aptStartDate dayOfMonth]]];
434 [rule setFrequency: iCalRecurrenceFrequenceMonthly];
438 (iCalRecurrenceFrequency) [rule valueForFrequency: repeat]];
439 [event setRecurrenceRules: [NSArray arrayWithObject: rule]];
444 // TODO: add tentatively
446 - (id) acceptOrDeclineAction: (BOOL) accept
448 [[self clientObject] changeParticipationStatus: (accept
457 return [self acceptOrDeclineAction: YES];
462 return [self acceptOrDeclineAction: NO];