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/iCalCalendar.h>
29 #import <NGCards/iCalToDo.h>
30 #import <NGCards/iCalPerson.h>
32 #import <SoObjects/SOGo/SOGoUser.h>
33 #import <SoObjects/SOGo/SOGoContentObject.h>
34 #import <SoObjects/Appointments/SOGoAppointmentFolder.h>
35 #import <SoObjects/Appointments/SOGoTaskObject.h>
37 #import "UIxComponentEditor.h"
38 #import "UIxTaskEditor.h"
40 @implementation UIxTaskEditor
44 if ((self = [super init]))
62 [taskStartDate release];
63 [taskDueDate release];
66 [statusPercent release];
76 todo = (iCalToDo *) [[self clientObject] component: NO secure: NO];
83 - (NSString *) saveURL
85 return [NSString stringWithFormat: @"%@/saveAsTask",
86 [[self clientObject] baseURL]];
89 /* icalendar values */
90 - (void) setTaskStartDate: (NSCalendarDate *) newTaskStartDate
92 ASSIGN (taskStartDate, newTaskStartDate);
95 - (NSCalendarDate *) taskStartDate
100 - (void) setHasStartDate: (BOOL) newHasStartDate
102 hasStartDate = newHasStartDate;
105 - (BOOL) hasStartDate
110 - (BOOL) startDateDisabled
112 return !hasStartDate;
115 - (void) setTaskDueDate: (NSCalendarDate *) newTaskDueDate
117 ASSIGN (taskDueDate, newTaskDueDate);
120 - (NSCalendarDate *) taskDueDate
125 - (void) setHasDueDate: (BOOL) newHasDueDate
127 hasDueDate = newHasDueDate;
135 - (BOOL) dueDateDisabled
140 - (NSArray *) repeatList
142 static NSArray *repeatItems = nil;
146 repeatItems = [NSArray arrayWithObjects: @"DAILY",
155 [repeatItems retain];
161 - (NSString *) itemRepeatText
165 if ([item isEqualToString: @"-"])
168 text = [self labelForKey: [NSString stringWithFormat: @"repeat_%@", item]];
173 - (NSArray *) statusList
175 static NSArray *statusItems = nil;
179 statusItems = [NSArray arrayWithObjects: @"NEEDS-ACTION",
184 [statusItems retain];
190 - (NSString *) itemStatusText
192 return [self labelForKey: [NSString stringWithFormat: @"status_%@", item]];
195 - (void) setItem: (NSString *) newItem
205 - (NSString *) repeat
210 - (void) setRepeat: (NSString *) newRepeat
214 - (NSString *) status
219 - (void) setStatus: (NSString *) newStatus
224 - (void) setStatusDate: (NSCalendarDate *) newStatusDate
226 ASSIGN (statusDate, newStatusDate);
229 - (NSCalendarDate *) statusDate
234 - (BOOL) statusDateDisabled
236 return ![status isEqualToString: @"COMPLETED"];
239 - (BOOL) statusPercentDisabled
241 return ([status length] == 0
242 || [status isEqualToString: @"CANCELLED"]);
245 - (void) setStatusPercent: (NSString *) newStatusPercent
247 ASSIGN (statusPercent, newStatusPercent);
250 - (NSString *) statusPercent
252 return statusPercent;
256 - (NSCalendarDate *) newStartDate
258 NSCalendarDate *newStartDate, *now;
260 NSTimeZone *timeZone;
262 timeZone = [[context activeUser] timeZone];
264 newStartDate = [self selectedDate];
265 if ([[self queryParameterForKey: @"hm"] length] == 0)
267 now = [NSCalendarDate calendarDate];
268 [now setTimeZone: timeZone];
269 if ([now isDateOnSameDay: newStartDate])
271 hour = [now hourOfDay];
273 newStartDate = [now hour: 8 minute: 0];
275 newStartDate = [[now tomorrow] hour: 8 minute: 0];
280 newStartDate = [newStartDate hour: 8 minute: 0];
286 - (id <WOActionResults>) defaultAction
288 NSCalendarDate *startDate, *dueDate;
290 unsigned int minutes;
292 todo = (iCalToDo *) [[self clientObject] component: NO secure: YES];
295 startDate = [todo startDate];
296 dueDate = [todo due];
297 hasStartDate = (startDate != nil);
298 hasDueDate = (dueDate != nil);
299 ASSIGN (status, [todo status]);
300 if ([status isEqualToString: @"COMPLETED"])
301 ASSIGN (statusDate, [todo completed]);
303 ASSIGN (statusDate, [self newStartDate]);
304 ASSIGN (statusPercent, [todo percentComplete]);
308 startDate = [self newStartDate];
309 duration = [self queryParameterForKey:@"dur"];
310 if ([duration length] > 0)
311 minutes = [duration intValue];
314 dueDate = [startDate dateByAddingYears: 0 months: 0 days: 0
315 hours: 0 minutes: minutes seconds: 0];
318 ASSIGN (statusDate, [self newStartDate]);
319 ASSIGN (status, @"");
320 ASSIGN (statusPercent, @"");
323 ASSIGN (taskStartDate, startDate);
324 ASSIGN (taskDueDate, dueDate);
326 /* here comes the code for initializing repeat, reminder and isAllDay... */
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:@"%@/%@/editAsTask",
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 [[self clientObject] saveComponent: todo];
357 return [self jsCloseWithRefreshMethod: @"refreshTasks()"];
360 // - (id <WOActionResults>) saveAction
362 // SOGoTaskObject *clientObject;
363 // NSString *iCalString;
365 // clientObject = [self clientObject];
366 // iCalString = [[clientObject calendar: NO secure: NO] versitString];
367 // [clientObject saveContentString: iCalString];
369 // return [self jsCloseWithRefreshMethod: @"refreshTasks()"];
372 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
373 inContext: (WOContext*) context
375 NSString *actionName;
377 actionName = [[request requestHandlerPath] lastPathComponent];
379 return ([[self clientObject] isKindOfClass: [SOGoTaskObject class]]
380 && [actionName hasPrefix: @"save"]);
383 - (void) takeValuesFromRequest: (WORequest *) _rq
384 inContext: (WOContext *) _ctx
386 SOGoTaskObject *clientObject;
388 clientObject = [self clientObject];
389 todo = (iCalToDo *) [clientObject component: YES secure: NO];
391 [super takeValuesFromRequest: _rq inContext: _ctx];
394 [todo setStartDate: taskStartDate];
396 [todo setStartDate: nil];
399 [todo setDue: taskDueDate];
403 if ([status isEqualToString: @"COMPLETED"])
404 [todo setCompleted: statusDate];
406 [todo setCompleted: nil];
407 if ([status length] > 0)
409 [todo setStatus: status];
410 [todo setPercentComplete: statusPercent];
414 [todo setStatus: @""];
415 [todo setPercentComplete: @""];
419 // TODO: add tentatively
421 // - (id) acceptOrDeclineAction: (BOOL) _accept
423 // [[self clientObject] changeParticipationStatus:
424 // _accept ? @"ACCEPTED" : @"DECLINED"];
429 // - (id) acceptAction
431 // return [self acceptOrDeclineAction: YES];
434 // - (id) declineAction
436 // return [self acceptOrDeclineAction: NO];
439 - (id) changeStatusAction
441 SOGoTaskObject *clientObject;
444 clientObject = [self clientObject];
445 todo = (iCalToDo *) [clientObject component: NO secure: NO];
449 newStatus = [self queryParameterForKey: @"status"];
450 if ([newStatus intValue])
451 [todo setCompleted: [NSCalendarDate date]];
454 [todo setCompleted: nil];
455 [todo setPercentComplete: @"0"];
456 [todo setStatus: @"IN-PROCESS"];
458 [clientObject saveComponent: todo];
461 return [self responseWith204];