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/AgenorUserManager.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 - (NSString *) _toolbarForCalObject
84 SOGoUser *currentUser;
85 SOGoTaskObject *clientObject;
86 NSString *filename, *email;
88 iCalPersonPartStat participationStatus;
90 clientObject = [self clientObject];
91 currentUser = [[self context] activeUser];
92 email = [currentUser email];
93 if ([clientObject isOrganizer: email
94 orOwner: [currentUser login]])
95 filename = @"SOGoTaskObject.toolbar";
98 if ([clientObject isParticipant: email])
100 person = [[clientObject component: NO] findParticipantWithEmail: email];
101 participationStatus = [person participationStatus];
102 if (participationStatus == iCalPersonPartStatAccepted)
103 filename = @"SOGoTaskObjectDecline.toolbar";
104 else if (participationStatus == iCalPersonPartStatDeclined)
105 filename = @"SOGoTaskObjectAccept.toolbar";
107 filename = @"SOGoTaskObjectAcceptOrDecline.toolbar";
110 filename = @"SOGoComponentClose.toolbar";
116 - (NSString *) toolbar
118 return ([self _toolbarForCalObject]);
121 /* icalendar values */
122 - (void) setTaskStartDate: (NSCalendarDate *) newTaskStartDate
124 ASSIGN (taskStartDate, newTaskStartDate);
127 - (NSCalendarDate *) taskStartDate
129 return taskStartDate;
132 - (void) setHasStartDate: (BOOL) newHasStartDate
134 hasStartDate = newHasStartDate;
137 - (BOOL) hasStartDate
142 - (BOOL) startDateDisabled
144 return !hasStartDate;
147 - (void) setTaskDueDate: (NSCalendarDate *) newTaskDueDate
149 ASSIGN (taskDueDate, newTaskDueDate);
152 - (NSCalendarDate *) taskDueDate
157 - (void) setHasDueDate: (BOOL) newHasDueDate
159 hasDueDate = newHasDueDate;
167 - (BOOL) dueDateDisabled
172 - (NSArray *) repeatList
174 static NSArray *repeatItems = nil;
178 repeatItems = [NSArray arrayWithObjects: @"DAILY",
187 [repeatItems retain];
193 - (NSString *) itemRepeatText
197 if ([item isEqualToString: @"-"])
200 text = [self labelForKey: [NSString stringWithFormat: @"repeat_%@", item]];
205 - (NSArray *) statusList
207 static NSArray *statusItems = nil;
211 statusItems = [NSArray arrayWithObjects: @"NEEDS-ACTION",
216 [statusItems retain];
222 - (NSString *) itemStatusText
224 return [self labelForKey: [NSString stringWithFormat: @"status_%@", item]];
227 - (void) setItem: (NSString *) newItem
237 - (NSString *) repeat
242 - (void) setRepeat: (NSString *) newRepeat
246 - (NSString *) status
251 - (void) setStatus: (NSString *) newStatus
256 - (void) setStatusDate: (NSCalendarDate *) newStatusDate
258 ASSIGN (statusDate, newStatusDate);
261 - (NSCalendarDate *) statusDate
266 - (BOOL) statusDateDisabled
268 return ![status isEqualToString: @"COMPLETED"];
271 - (BOOL) statusPercentDisabled
273 NSLog (@"status: '%@'", status);
274 return ([status length] == 0
275 || [status isEqualToString: @"CANCELLED"]);
278 - (void) setStatusPercent: (NSString *) newStatusPercent
280 ASSIGN (statusPercent, newStatusPercent);
283 - (NSString *) statusPercent
285 return statusPercent;
289 - (NSCalendarDate *) newStartDate
291 NSCalendarDate *newStartDate, *now;
294 newStartDate = [self selectedDate];
295 if ([[self queryParameterForKey: @"hm"] length] == 0)
297 now = [NSCalendarDate calendarDate];
298 [now setTimeZone: [[self clientObject] userTimeZone]];
299 if ([now isDateOnSameDay: newStartDate])
301 hour = [now hourOfDay];
303 newStartDate = [now hour: 8 minute: 0];
305 newStartDate = [[now tomorrow] hour: 8 minute: 0];
310 newStartDate = [newStartDate hour: 8 minute: 0];
316 - (id <WOActionResults>) defaultAction
318 NSCalendarDate *startDate, *dueDate;
320 unsigned int minutes;
322 todo = (iCalToDo *) [[self clientObject] component: NO];
325 startDate = [todo startDate];
326 dueDate = [todo due];
327 hasStartDate = (startDate != nil);
328 hasDueDate = (dueDate != nil);
329 ASSIGN (status, [todo status]);
330 if ([status isEqualToString: @"COMPLETED"])
331 ASSIGN (statusDate, [todo completed]);
333 ASSIGN (statusDate, [self newStartDate]);
334 ASSIGN (statusPercent, [todo percentComplete]);
338 startDate = [self newStartDate];
339 duration = [self queryParameterForKey:@"dur"];
340 if ([duration length] > 0)
341 minutes = [duration intValue];
344 dueDate = [startDate dateByAddingYears: 0 months: 0 days: 0
345 hours: 0 minutes: minutes seconds: 0];
348 ASSIGN (statusDate, [self newStartDate]);
349 ASSIGN (status, @"");
350 ASSIGN (statusPercent, @"");
353 ASSIGN (taskStartDate, startDate);
354 ASSIGN (taskDueDate, dueDate);
356 /* here comes the code for initializing repeat, reminder and isAllDay... */
361 - (id <WOActionResults>) newAction
363 NSString *objectId, *method, *uri;
364 id <WOActionResults> result;
367 clientKlazz = [[self clientObject] class];
368 objectId = [clientKlazz globallyUniqueObjectId];
369 if ([objectId length] > 0)
371 method = [NSString stringWithFormat:@"%@/Calendar/%@/editAsTask",
372 [self userFolderPath], objectId];
373 uri = [self completeHrefForMethod: method];
374 result = [self redirectToLocation: uri];
377 result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
378 reason: @"could not create a unique ID"];
383 - (id <WOActionResults>) saveAction
385 SOGoTaskObject *clientObject;
386 NSString *iCalString;
388 clientObject = [self clientObject];
389 iCalString = [[clientObject calendar: NO] versitString];
390 [clientObject saveContentString: iCalString];
392 return [self jsCloseWithRefreshMethod: @"refreshTasks()"];
395 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
396 inContext: (WOContext*) context
398 return ([[self clientObject] isKindOfClass: [SOGoTaskObject class]]
399 && [[request method] isEqualToString: @"POST"]);
402 - (void) takeValuesFromRequest: (WORequest *) _rq
403 inContext: (WOContext *) _ctx
405 SOGoTaskObject *clientObject;
407 clientObject = [self clientObject];
408 todo = (iCalToDo *) [clientObject component: YES];
410 [super takeValuesFromRequest: _rq inContext: _ctx];
413 [todo setStartDate: taskStartDate];
415 [todo setDue: taskDueDate];
416 if ([status isEqualToString: @"COMPLETED"])
417 [todo setCompleted: statusDate];
419 [todo setCompleted: nil];
420 if ([status length] > 0)
422 [todo setStatus: status];
423 [todo setPercentComplete: statusPercent];
427 [todo setStatus: @""];
428 [todo setPercentComplete: @""];
432 // TODO: add tentatively
434 - (id) acceptOrDeclineAction: (BOOL) _accept
436 [[self clientObject] changeParticipationStatus:
437 _accept ? @"ACCEPTED" : @"DECLINED"
438 inContext: [self context]];
445 return [self acceptOrDeclineAction:YES];
450 return [self acceptOrDeclineAction:NO];
453 - (id) changeStatusAction
455 SOGoTaskObject *clientObject;
456 NSString *newStatus, *iCalString;
458 clientObject = [self clientObject];
459 todo = (iCalToDo *) [clientObject component: NO];
462 newStatus = [self queryParameterForKey: @"status"];
463 if ([newStatus intValue])
464 [todo setCompleted: [NSCalendarDate date]];
467 [todo setCompleted: nil];
468 [todo setPercentComplete: @"0"];
469 [todo setStatus: @"IN-PROCESS"];
472 iCalString = [[clientObject calendar: NO] versitString];
473 [clientObject saveContentString: iCalString];