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 - (NSString *) saveURL
78 return [NSString stringWithFormat: @"%@/saveAsTask",
79 [[self clientObject] baseURL]];
82 /* icalendar values */
83 - (void) setTaskStartDate: (NSCalendarDate *) newTaskStartDate
85 ASSIGN (taskStartDate, newTaskStartDate);
88 - (NSCalendarDate *) taskStartDate
93 - (void) setHasStartDate: (BOOL) newHasStartDate
95 hasStartDate = newHasStartDate;
103 - (BOOL) startDateDisabled
105 return !hasStartDate;
108 - (void) setTaskDueDate: (NSCalendarDate *) newTaskDueDate
110 ASSIGN (taskDueDate, newTaskDueDate);
113 - (NSCalendarDate *) taskDueDate
118 - (void) setHasDueDate: (BOOL) newHasDueDate
120 hasDueDate = newHasDueDate;
128 - (BOOL) dueDateDisabled
133 - (NSArray *) repeatList
135 static NSArray *repeatItems = nil;
139 repeatItems = [NSArray arrayWithObjects: @"DAILY",
148 [repeatItems retain];
154 - (NSString *) itemRepeatText
158 if ([item isEqualToString: @"-"])
161 text = [self labelForKey: [NSString stringWithFormat: @"repeat_%@", item]];
166 - (NSArray *) statusList
168 static NSArray *statusItems = nil;
172 statusItems = [NSArray arrayWithObjects: @"NEEDS-ACTION",
177 [statusItems retain];
183 - (NSString *) itemStatusText
185 return [self labelForKey: [NSString stringWithFormat: @"status_%@", item]];
188 - (void) setItem: (NSString *) newItem
198 - (NSString *) repeat
203 - (void) setRepeat: (NSString *) newRepeat
207 - (NSString *) status
212 - (void) setStatus: (NSString *) newStatus
217 - (void) setStatusDate: (NSCalendarDate *) newStatusDate
219 ASSIGN (statusDate, newStatusDate);
222 - (NSCalendarDate *) statusDate
227 - (BOOL) statusDateDisabled
229 return ![status isEqualToString: @"COMPLETED"];
232 - (BOOL) statusPercentDisabled
234 return ([status length] == 0
235 || [status isEqualToString: @"CANCELLED"]);
238 - (void) setStatusPercent: (NSString *) newStatusPercent
240 ASSIGN (statusPercent, newStatusPercent);
243 - (NSString *) statusPercent
245 return statusPercent;
249 - (NSCalendarDate *) newStartDate
251 NSCalendarDate *newStartDate, *now;
253 NSTimeZone *timeZone;
255 timeZone = [[context activeUser] timeZone];
257 newStartDate = [self selectedDate];
258 if ([[self queryParameterForKey: @"hm"] length] == 0)
260 now = [NSCalendarDate calendarDate];
261 [now setTimeZone: timeZone];
262 if ([now isDateOnSameDay: newStartDate])
264 hour = [now hourOfDay];
266 newStartDate = [now hour: 8 minute: 0];
268 newStartDate = [[now tomorrow] hour: 8 minute: 0];
273 newStartDate = [newStartDate hour: 8 minute: 0];
279 - (id <WOActionResults>) defaultAction
281 NSCalendarDate *startDate, *dueDate;
283 unsigned int minutes;
285 todo = (iCalToDo *) [[self clientObject] component: NO secure: YES];
288 startDate = [todo startDate];
289 dueDate = [todo due];
290 hasStartDate = (startDate != nil);
291 hasDueDate = (dueDate != nil);
292 ASSIGN (status, [todo status]);
293 if ([status isEqualToString: @"COMPLETED"])
294 ASSIGN (statusDate, [todo completed]);
296 ASSIGN (statusDate, [self newStartDate]);
297 ASSIGN (statusPercent, [todo percentComplete]);
301 startDate = [self newStartDate];
302 duration = [self queryParameterForKey:@"dur"];
303 if ([duration length] > 0)
304 minutes = [duration intValue];
307 dueDate = [startDate dateByAddingYears: 0 months: 0 days: 0
308 hours: 0 minutes: minutes seconds: 0];
311 ASSIGN (statusDate, [self newStartDate]);
312 ASSIGN (status, @"");
313 ASSIGN (statusPercent, @"");
316 ASSIGN (taskStartDate, startDate);
317 ASSIGN (taskDueDate, dueDate);
319 /* here comes the code for initializing repeat, reminder and isAllDay... */
324 - (id <WOActionResults>) newAction
326 NSString *objectId, *method, *uri;
327 id <WOActionResults> result;
328 SOGoAppointmentFolder *co;
330 co = [self clientObject];
331 objectId = [co globallyUniqueObjectId];
332 if ([objectId length] > 0)
334 method = [NSString stringWithFormat:@"%@/%@/editAsTask",
335 [co soURL], objectId];
336 uri = [self completeHrefForMethod: method];
337 result = [self redirectToLocation: uri];
340 result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
341 reason: @"could not create a unique ID"];
346 - (id <WOActionResults>) saveAction
348 [[self clientObject] saveComponent: todo];
350 return [self jsCloseWithRefreshMethod: @"refreshTasks()"];
353 // - (id <WOActionResults>) saveAction
355 // SOGoTaskObject *clientObject;
356 // NSString *iCalString;
358 // clientObject = [self clientObject];
359 // iCalString = [[clientObject calendar: NO secure: NO] versitString];
360 // [clientObject saveContentString: iCalString];
362 // return [self jsCloseWithRefreshMethod: @"refreshTasks()"];
365 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
366 inContext: (WOContext*) context
368 NSString *actionName;
370 actionName = [[request requestHandlerPath] lastPathComponent];
372 return ([[self clientObject] isKindOfClass: [SOGoTaskObject class]]
373 && [actionName hasPrefix: @"save"]);
376 - (void) takeValuesFromRequest: (WORequest *) _rq
377 inContext: (WOContext *) _ctx
379 SOGoTaskObject *clientObject;
381 clientObject = [self clientObject];
382 todo = (iCalToDo *) [clientObject component: YES secure: NO];
384 [super takeValuesFromRequest: _rq inContext: _ctx];
387 [todo setStartDate: taskStartDate];
389 [todo setStartDate: nil];
392 [todo setDue: taskDueDate];
396 if ([status isEqualToString: @"COMPLETED"])
397 [todo setCompleted: statusDate];
399 [todo setCompleted: nil];
400 if ([status length] > 0)
402 [todo setStatus: status];
403 [todo setPercentComplete: statusPercent];
407 [todo setStatus: @""];
408 [todo setPercentComplete: @""];
412 // TODO: add tentatively
414 // - (id) acceptOrDeclineAction: (BOOL) _accept
416 // [[self clientObject] changeParticipationStatus:
417 // _accept ? @"ACCEPTED" : @"DECLINED"];
422 // - (id) acceptAction
424 // return [self acceptOrDeclineAction: YES];
427 // - (id) declineAction
429 // return [self acceptOrDeclineAction: NO];
432 - (id) changeStatusAction
434 SOGoTaskObject *clientObject;
435 NSString *newStatus, *iCalString;
437 clientObject = [self clientObject];
438 todo = (iCalToDo *) [clientObject component: NO secure: NO];
441 newStatus = [self queryParameterForKey: @"status"];
442 if ([newStatus intValue])
443 [todo setCompleted: [NSCalendarDate date]];
446 [todo setCompleted: nil];
447 [todo setPercentComplete: @"0"];
448 [todo setStatus: @"IN-PROCESS"];
451 iCalString = [[clientObject calendar: NO secure: NO] versitString];
452 [clientObject saveContentString: iCalString];