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