]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxTaskEditor.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1043 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Scheduler / UIxTaskEditor.m
1 /* UIxTaskEditor.m - this file is part of SOGo
2  *
3  * Copyright (C) 2007 Inverse groupe conseil
4  *
5  * Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
23 #import <NGObjWeb/SoObject.h>
24 #import <NGObjWeb/WORequest.h>
25 #import <NGObjWeb/NSException+HTTP.h>
26 #import <NGExtensions/NSCalendarDate+misc.h>
27
28 #import <NGCards/iCalToDo.h>
29 #import <NGCards/iCalPerson.h>
30
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>
36
37 #import "UIxComponentEditor.h"
38 #import "UIxTaskEditor.h"
39
40 @implementation UIxTaskEditor
41
42 - (id) init
43 {
44   if ((self = [super init]))
45     {
46       taskStartDate = nil;
47       taskDueDate = nil;
48       statusDate = nil;
49       hasStartDate = NO;
50       hasDueDate = NO;
51       status = nil;
52       statusPercent = nil;
53       item = nil;
54       todo = nil;
55     }
56
57   return self;
58 }
59
60 - (void) dealloc
61 {
62   [taskStartDate release];
63   [taskDueDate release];
64   [statusDate release];
65   [status release];
66   [statusPercent release];
67   [super dealloc];
68 }
69
70 /* template values */
71 - (iCalToDo *) todo
72 {
73   return todo;
74 }
75
76 - (NSString *) saveURL
77 {
78   return [NSString stringWithFormat: @"%@/saveAsTask",
79                    [[self clientObject] baseURL]];
80 }
81
82 - (NSString *) _toolbarForCalObject
83 {
84   SOGoUser *currentUser;
85   SOGoTaskObject *clientObject;
86   NSString *filename, *email;
87   iCalPerson *person;
88   iCalPersonPartStat participationStatus;
89
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";
96   else
97     {
98       if ([clientObject isParticipant: email])
99         {
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";
106           else
107             filename = @"SOGoTaskObjectAcceptOrDecline.toolbar";
108         }
109       else
110         filename = @"SOGoComponentClose.toolbar";
111     }
112
113   return filename;
114 }
115
116 - (NSString *) toolbar
117 {
118   return ([self _toolbarForCalObject]);
119 }
120
121 /* icalendar values */
122 - (void) setTaskStartDate: (NSCalendarDate *) newTaskStartDate
123 {
124   ASSIGN (taskStartDate, newTaskStartDate);
125 }
126
127 - (NSCalendarDate *) taskStartDate
128 {
129   return taskStartDate;
130 }
131
132 - (void) setHasStartDate: (BOOL) newHasStartDate
133 {
134   hasStartDate = newHasStartDate;
135 }
136
137 - (BOOL) hasStartDate
138 {
139   return hasStartDate;
140 }
141
142 - (BOOL) startDateDisabled
143 {
144   return !hasStartDate;
145 }
146
147 - (void) setTaskDueDate: (NSCalendarDate *) newTaskDueDate
148 {
149   ASSIGN (taskDueDate, newTaskDueDate);
150 }
151
152 - (NSCalendarDate *) taskDueDate
153 {
154   return taskDueDate;
155 }
156
157 - (void) setHasDueDate: (BOOL) newHasDueDate
158 {
159   hasDueDate = newHasDueDate;
160 }
161
162 - (BOOL) hasDueDate
163 {
164   return hasDueDate;
165 }
166
167 - (BOOL) dueDateDisabled
168 {
169   return !hasDueDate;
170 }
171
172 - (NSArray *) repeatList
173 {
174   static NSArray *repeatItems = nil;
175
176   if (!repeatItems)
177     {
178       repeatItems = [NSArray arrayWithObjects: @"DAILY",
179                              @"WEEKLY",
180                              @"BI-WEEKLY",
181                              @"EVERY WEEKDAY",
182                              @"MONTHLY",
183                              @"YEARLY",
184                              @"-",
185                              @"CUSTOM",
186                              nil];
187       [repeatItems retain];
188     }
189
190   return repeatItems;
191 }
192
193 - (NSString *) itemRepeatText
194 {
195   NSString *text;
196
197   if ([item isEqualToString: @"-"])
198     text = item;
199   else
200     text = [self labelForKey: [NSString stringWithFormat: @"repeat_%@", item]];
201
202   return text;
203 }
204
205 - (NSArray *) statusList
206 {
207   static NSArray *statusItems = nil;
208
209   if (!statusItems)
210     {
211       statusItems = [NSArray arrayWithObjects: @"NEEDS-ACTION",
212                              @"IN-PROCESS",
213                              @"COMPLETED",
214                              @"CANCELLED",
215                              nil];
216       [statusItems retain];
217     }
218
219   return statusItems;
220 }
221
222 - (NSString *) itemStatusText
223 {
224   return [self labelForKey: [NSString stringWithFormat: @"status_%@", item]];
225 }
226
227 - (void) setItem: (NSString *) newItem
228 {
229   item = newItem;
230 }
231
232 - (NSString *) item
233 {
234   return item;
235 }
236
237 - (NSString *) repeat
238 {
239   return @"";
240 }
241
242 - (void) setRepeat: (NSString *) newRepeat
243 {
244 }
245
246 - (NSString *) status
247 {
248   return status;
249 }
250
251 - (void) setStatus: (NSString *) newStatus
252 {
253   status = newStatus;
254 }
255
256 - (void) setStatusDate: (NSCalendarDate *) newStatusDate
257 {
258   ASSIGN (statusDate, newStatusDate);
259 }
260
261 - (NSCalendarDate *) statusDate
262 {
263   return statusDate;
264 }
265
266 - (BOOL) statusDateDisabled
267 {
268   return ![status isEqualToString: @"COMPLETED"];
269 }
270
271 - (BOOL) statusPercentDisabled
272 {
273   NSLog (@"status: '%@'", status);
274   return ([status length] == 0
275           || [status isEqualToString: @"CANCELLED"]);
276 }
277
278 - (void) setStatusPercent: (NSString *) newStatusPercent
279 {
280   ASSIGN (statusPercent, newStatusPercent);
281 }
282
283 - (NSString *) statusPercent
284 {
285   return statusPercent;
286 }
287
288 /* actions */
289 - (NSCalendarDate *) newStartDate
290 {
291   NSCalendarDate *newStartDate, *now;
292   int hour;
293
294   newStartDate = [self selectedDate];
295   if ([[self queryParameterForKey: @"hm"] length] == 0)
296     {
297       now = [NSCalendarDate calendarDate];
298       [now setTimeZone: [[self clientObject] userTimeZone]];
299       if ([now isDateOnSameDay: newStartDate])
300         {
301           hour = [now hourOfDay];
302           if (hour < 8)
303             newStartDate = [now hour: 8 minute: 0];
304           else if (hour > 18)
305             newStartDate = [[now tomorrow] hour: 8 minute: 0];
306           else
307             newStartDate = now;
308         }
309       else
310         newStartDate = [newStartDate hour: 8 minute: 0];
311     }
312
313   return newStartDate;
314 }
315
316 - (id <WOActionResults>) defaultAction
317 {
318   NSCalendarDate *startDate, *dueDate;
319   NSString *duration;
320   unsigned int minutes;
321
322   todo = (iCalToDo *) [[self clientObject] component: NO];
323   if (todo)
324     {
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]);
332       else
333         ASSIGN (statusDate, [self newStartDate]);
334       ASSIGN (statusPercent, [todo percentComplete]);
335     }
336   else
337     {
338       startDate = [self newStartDate];
339       duration = [self queryParameterForKey:@"dur"];
340       if ([duration length] > 0)
341         minutes = [duration intValue];
342       else
343         minutes = 60;
344       dueDate = [startDate dateByAddingYears: 0 months: 0 days: 0
345                            hours: 0 minutes: minutes seconds: 0];
346       hasStartDate = NO;
347       hasDueDate = NO;
348       ASSIGN (statusDate, [self newStartDate]);
349       ASSIGN (status, @"");
350       ASSIGN (statusPercent, @"");
351     }
352
353   ASSIGN (taskStartDate, startDate);
354   ASSIGN (taskDueDate, dueDate);
355
356   /* here comes the code for initializing repeat, reminder and isAllDay... */
357
358   return self;
359 }
360
361 - (id <WOActionResults>) newAction
362 {
363   NSString *objectId, *method, *uri;
364   id <WOActionResults> result;
365   Class clientKlazz;
366
367   clientKlazz = [[self clientObject] class];
368   objectId = [clientKlazz globallyUniqueObjectId];
369   if ([objectId length] > 0)
370     {
371       method = [NSString stringWithFormat:@"%@/Calendar/%@/editAsTask",
372                          [self userFolderPath], objectId];
373       uri = [self completeHrefForMethod: method];
374       result = [self redirectToLocation: uri];
375     }
376   else
377     result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
378                           reason: @"could not create a unique ID"];
379
380   return result;
381 }
382
383 - (id <WOActionResults>) saveAction
384 {
385   SOGoTaskObject *clientObject;
386   NSString *iCalString;
387
388   clientObject = [self clientObject];
389   iCalString = [[clientObject calendar: NO] versitString];
390   [clientObject saveContentString: iCalString];
391
392   return [self jsCloseWithRefreshMethod: @"refreshTasks()"];
393 }
394
395 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
396                            inContext: (WOContext*) context
397 {
398   return ([[self clientObject] isKindOfClass: [SOGoTaskObject class]]
399           && [[request method] isEqualToString: @"POST"]);
400 }
401
402 - (void) takeValuesFromRequest: (WORequest *) _rq
403                      inContext: (WOContext *) _ctx
404 {
405   SOGoTaskObject *clientObject;
406
407   clientObject = [self clientObject];
408   todo = (iCalToDo *) [clientObject component: YES];
409
410   [super takeValuesFromRequest: _rq inContext: _ctx];
411
412   if (hasStartDate)
413     [todo setStartDate: taskStartDate];
414   if (hasDueDate)
415     [todo setDue: taskDueDate];
416   if ([status isEqualToString: @"COMPLETED"])
417     [todo setCompleted: statusDate];
418   else
419     [todo setCompleted: nil];
420   if ([status length] > 0)
421     {
422       [todo setStatus: status];
423       [todo setPercentComplete: statusPercent];
424     }
425   else
426     {
427       [todo setStatus: @""];
428       [todo setPercentComplete: @""];
429     }
430 }
431
432 // TODO: add tentatively
433
434 - (id) acceptOrDeclineAction: (BOOL) _accept
435 {
436   [[self clientObject] changeParticipationStatus:
437                          _accept ? @"ACCEPTED" : @"DECLINED"
438                        inContext: [self context]];
439
440   return self;
441 }
442
443 - (id) acceptAction
444 {
445   return [self acceptOrDeclineAction:YES];
446 }
447
448 - (id) declineAction
449 {
450   return [self acceptOrDeclineAction:NO];
451 }
452
453 - (id) changeStatusAction
454 {
455   SOGoTaskObject *clientObject;
456   NSString *newStatus, *iCalString;
457
458   clientObject = [self clientObject];
459   todo = (iCalToDo *) [clientObject component: NO];
460   if (todo)
461     {
462       newStatus = [self queryParameterForKey: @"status"];
463       if ([newStatus intValue])
464         [todo setCompleted: [NSCalendarDate date]];
465       else
466         {
467           [todo setCompleted: nil];
468           [todo setPercentComplete: @"0"];
469           [todo setStatus: @"IN-PROCESS"];
470         }
471
472       iCalString = [[clientObject calendar: NO] versitString];
473       [clientObject saveContentString: iCalString];
474     }
475
476   return self;
477 }
478
479 @end