1 /* UIxTaskEditor.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.
23 #import <NGObjWeb/SoObject.h>
24 #import <NGObjWeb/WORequest.h>
25 #import <NGObjWeb/NSException+HTTP.h>
26 #import <NGExtensions/NSCalendarDate+misc.h>
28 #import <NGCards/iCalToDo.h>
29 #import <NGCards/iCalPerson.h>
31 #import <SoObjects/SOGo/SOGoUser.h>
32 #import <SoObjects/SOGo/SOGoContentObject.h>
33 #import <SoObjects/Appointments/SOGoAppointmentFolder.h>
34 #import <SoObjects/Appointments/SOGoTaskObject.h>
36 #import "UIxComponentEditor.h"
37 #import "UIxTaskEditor.h"
39 @implementation UIxTaskEditor
43 if ((self = [super init]))
61 [taskStartDate release];
62 [taskDueDate release];
65 [statusPercent release];
75 - (NSString *) saveURL
77 return [NSString stringWithFormat: @"%@/saveAsTask",
78 [[self clientObject] baseURL]];
81 /* icalendar values */
82 - (void) setTaskStartDate: (NSCalendarDate *) newTaskStartDate
84 ASSIGN (taskStartDate, newTaskStartDate);
87 - (NSCalendarDate *) taskStartDate
92 - (void) setHasStartDate: (BOOL) newHasStartDate
94 hasStartDate = newHasStartDate;
102 - (BOOL) startDateDisabled
104 return !hasStartDate;
107 - (void) setTaskDueDate: (NSCalendarDate *) newTaskDueDate
109 ASSIGN (taskDueDate, newTaskDueDate);
112 - (NSCalendarDate *) taskDueDate
117 - (void) setHasDueDate: (BOOL) newHasDueDate
119 hasDueDate = newHasDueDate;
127 - (BOOL) dueDateDisabled
132 - (NSArray *) repeatList
134 static NSArray *repeatItems = nil;
138 repeatItems = [NSArray arrayWithObjects: @"DAILY",
147 [repeatItems retain];
153 - (NSString *) itemRepeatText
157 if ([item isEqualToString: @"-"])
160 text = [self labelForKey: [NSString stringWithFormat: @"repeat_%@", item]];
165 - (NSArray *) statusList
167 static NSArray *statusItems = nil;
171 statusItems = [NSArray arrayWithObjects: @"NEEDS-ACTION",
176 [statusItems retain];
182 - (NSString *) itemStatusText
184 return [self labelForKey: [NSString stringWithFormat: @"status_%@", item]];
187 - (void) setItem: (NSString *) newItem
197 - (NSString *) repeat
202 - (void) setRepeat: (NSString *) newRepeat
206 - (NSString *) status
211 - (void) setStatus: (NSString *) newStatus
216 - (void) setStatusDate: (NSCalendarDate *) newStatusDate
218 ASSIGN (statusDate, newStatusDate);
221 - (NSCalendarDate *) statusDate
226 - (BOOL) statusDateDisabled
228 return ![status isEqualToString: @"COMPLETED"];
231 - (BOOL) statusPercentDisabled
233 return ([status length] == 0
234 || [status isEqualToString: @"CANCELLED"]);
237 - (void) setStatusPercent: (NSString *) newStatusPercent
239 ASSIGN (statusPercent, newStatusPercent);
242 - (NSString *) statusPercent
244 return statusPercent;
248 - (NSCalendarDate *) newStartDate
250 NSCalendarDate *newStartDate, *now;
252 NSTimeZone *timeZone;
254 timeZone = [[context activeUser] timeZone];
256 newStartDate = [self selectedDate];
257 if ([[self queryParameterForKey: @"hm"] length] == 0)
259 now = [NSCalendarDate calendarDate];
260 [now setTimeZone: timeZone];
261 if ([now isDateOnSameDay: newStartDate])
263 hour = [now hourOfDay];
265 newStartDate = [now hour: 8 minute: 0];
267 newStartDate = [[now tomorrow] hour: 8 minute: 0];
272 newStartDate = [newStartDate hour: 8 minute: 0];
278 - (id <WOActionResults>) defaultAction
280 NSCalendarDate *startDate, *dueDate;
282 unsigned int minutes;
284 todo = (iCalToDo *) [[self clientObject] component: NO];
287 startDate = [todo startDate];
288 dueDate = [todo due];
289 hasStartDate = (startDate != nil);
290 hasDueDate = (dueDate != nil);
291 ASSIGN (status, [todo status]);
292 if ([status isEqualToString: @"COMPLETED"])
293 ASSIGN (statusDate, [todo completed]);
295 ASSIGN (statusDate, [self newStartDate]);
296 ASSIGN (statusPercent, [todo percentComplete]);
300 startDate = [self newStartDate];
301 duration = [self queryParameterForKey:@"dur"];
302 if ([duration length] > 0)
303 minutes = [duration intValue];
306 dueDate = [startDate dateByAddingYears: 0 months: 0 days: 0
307 hours: 0 minutes: minutes seconds: 0];
310 ASSIGN (statusDate, [self newStartDate]);
311 ASSIGN (status, @"");
312 ASSIGN (statusPercent, @"");
315 ASSIGN (taskStartDate, startDate);
316 ASSIGN (taskDueDate, dueDate);
318 /* here comes the code for initializing repeat, reminder and isAllDay... */
323 - (id <WOActionResults>) newAction
325 NSString *objectId, *method, *uri;
326 id <WOActionResults> result;
329 clientKlazz = [[self clientObject] class];
330 objectId = [clientKlazz globallyUniqueObjectId];
331 if ([objectId length] > 0)
333 method = [NSString stringWithFormat:@"%@/Calendar/%@/editAsTask",
334 [self userFolderPath], objectId];
335 uri = [self completeHrefForMethod: method];
336 result = [self redirectToLocation: uri];
339 result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
340 reason: @"could not create a unique ID"];
345 - (id <WOActionResults>) saveAction
347 SOGoTaskObject *clientObject;
348 NSString *iCalString;
350 clientObject = [self clientObject];
351 iCalString = [[clientObject calendar: NO] versitString];
352 [clientObject saveContentString: iCalString];
354 return [self jsCloseWithRefreshMethod: @"refreshTasks()"];
357 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
358 inContext: (WOContext*) context
360 return ([[self clientObject] isKindOfClass: [SOGoTaskObject class]]
361 && [[request method] isEqualToString: @"POST"]);
364 - (void) takeValuesFromRequest: (WORequest *) _rq
365 inContext: (WOContext *) _ctx
367 SOGoTaskObject *clientObject;
369 clientObject = [self clientObject];
370 todo = (iCalToDo *) [clientObject component: YES];
372 [super takeValuesFromRequest: _rq inContext: _ctx];
375 [todo setStartDate: taskStartDate];
377 [todo setDue: taskDueDate];
378 if ([status isEqualToString: @"COMPLETED"])
379 [todo setCompleted: statusDate];
381 [todo setCompleted: nil];
382 if ([status length] > 0)
384 [todo setStatus: status];
385 [todo setPercentComplete: statusPercent];
389 [todo setStatus: @""];
390 [todo setPercentComplete: @""];
394 // TODO: add tentatively
396 - (id) acceptOrDeclineAction: (BOOL) _accept
398 [[self clientObject] changeParticipationStatus:
399 _accept ? @"ACCEPTED" : @"DECLINED"];
406 return [self acceptOrDeclineAction: YES];
411 return [self acceptOrDeclineAction: NO];
414 - (id) changeStatusAction
416 SOGoTaskObject *clientObject;
417 NSString *newStatus, *iCalString;
419 clientObject = [self clientObject];
420 todo = (iCalToDo *) [clientObject component: NO];
423 newStatus = [self queryParameterForKey: @"status"];
424 if ([newStatus intValue])
425 [todo setCompleted: [NSCalendarDate date]];
428 [todo setCompleted: nil];
429 [todo setPercentComplete: @"0"];
430 [todo setStatus: @"IN-PROCESS"];
433 iCalString = [[clientObject calendar: NO] versitString];
434 [clientObject saveContentString: iCalString];