]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxAppointmentEditor.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1127 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / Scheduler / UIxAppointmentEditor.m
1 /* UIxAppointmentEditor.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 #include <math.h>
24
25 #import <NGObjWeb/SoObject.h>
26 #import <NGObjWeb/WORequest.h>
27 #import <NGObjWeb/NSException+HTTP.h>
28 #import <NGExtensions/NSCalendarDate+misc.h>
29
30 #import <NGCards/iCalEvent.h>
31 #import <NGCards/iCalPerson.h>
32
33 #import <SoObjects/SOGo/SOGoUser.h>
34 #import <SoObjects/SOGo/SOGoContentObject.h>
35 #import <SoObjects/Appointments/SOGoAppointmentFolder.h>
36 #import <SoObjects/Appointments/SOGoAppointmentObject.h>
37
38 #import "UIxComponentEditor.h"
39 #import "UIxAppointmentEditor.h"
40
41 @implementation UIxAppointmentEditor
42
43 - (id) init
44 {
45   if ((self = [super init]))
46     {
47       aptStartDate = nil;
48       aptEndDate = nil;
49       item = nil;
50       event = nil;
51       isAllDay = NO;
52     }
53
54   return self;
55 }
56
57 /* template values */
58 - (iCalEvent *) event
59 {
60   return event;
61 }
62
63 - (NSString *) saveURL
64 {
65   return [NSString stringWithFormat: @"%@/saveAsAppointment",
66                    [[self clientObject] baseURL]];
67 }
68
69 /* icalendar values */
70 - (BOOL) isAllDay
71 {
72   NSString *hm;
73
74   hm = [self queryParameterForKey: @"hm"];
75
76   return (isAllDay
77           || (hm && [hm isEqualToString: @"allday"]));
78 }
79
80 - (void) setIsAllDay: (BOOL) newIsAllDay
81 {
82   isAllDay = newIsAllDay;
83 }
84
85 - (void) setAptStartDate: (NSCalendarDate *) newAptStartDate
86 {
87   ASSIGN (aptStartDate, newAptStartDate);
88 }
89
90 - (NSCalendarDate *) aptStartDate
91 {
92   return aptStartDate;
93 }
94
95 - (void) setAptEndDate: (NSCalendarDate *) newAptEndDate
96 {
97   ASSIGN (aptEndDate, newAptEndDate);
98 }
99
100 - (NSCalendarDate *) aptEndDate
101 {
102   return aptEndDate;
103 }
104
105 - (NSArray *) repeatList
106 {
107   static NSArray *repeatItems = nil;
108
109   if (!repeatItems)
110     {
111       repeatItems = [NSArray arrayWithObjects: @"DAILY",
112                              @"WEEKLY",
113                              @"BI-WEEKLY",
114                              @"EVERY WEEKDAY",
115                              @"MONTHLY",
116                              @"YEARLY",
117                              @"-",
118                              @"CUSTOM",
119                              nil];
120       [repeatItems retain];
121     }
122
123   return repeatItems;
124 }
125
126 - (NSString *) itemRepeatText
127 {
128   NSString *text;
129
130   if ([item isEqualToString: @"-"])
131     text = item;
132   else
133     text = [self labelForKey: [NSString stringWithFormat: @"repeat_%@", item]];
134
135   return text;
136 }
137
138 - (void) setItem: (NSString *) newItem
139 {
140   item = newItem;
141 }
142
143 - (NSString *) item
144 {
145   return item;
146 }
147
148 - (NSArray *) reminderList
149 {
150   static NSArray *reminderItems = nil;
151
152   if (!reminderItems)
153     {
154       reminderItems = [NSArray arrayWithObjects: @"5_MINUTES_BEFORE",
155                                @"10_MINUTES_BEFORE",
156                                @"15_MINUTES_BEFORE",
157                                @"30_MINUTES_BEFORE",
158                                @"45_MINUTES_BEFORE",
159                                @"-",
160                                @"1_HOUR_BEFORE",
161                                @"2_HOURS_BEFORE",
162                                @"5_HOURS_BEFORE",
163                                @"15_HOURS_BEFORE",
164                                @"-",
165                                @"1_DAY_BEFORE",
166                                @"2_DAYS_BEFORE",
167                                @"1_WEEK_BEFORE",
168                                @"-",
169                                @"CUSTOM",
170                                nil];
171       [reminderItems retain];
172     }
173
174   return reminderItems;
175 }
176
177 // - (void) setReminder: (NSString *) reminder
178 // {
179 //   ASSIGN(reminder, _reminder);
180 // }
181
182 // - (NSString *) reminder
183 // {
184 //   return reminder;
185 // }
186
187 - (NSString *) itemReminderText
188 {
189   NSString *text;
190
191   if ([item isEqualToString: @"-"])
192     text = item;
193   else
194     text = [self labelForKey: [NSString stringWithFormat: @"reminder_%@", item]];
195
196   return text;
197 }
198
199 - (NSString *) repeat
200 {
201   return @"";
202 }
203
204 - (void) setRepeat: (NSString *) newRepeat
205 {
206 }
207
208 - (NSString *) reminder
209 {
210   return @"";
211 }
212
213 - (void) setReminder: (NSString *) newReminder
214 {
215 }
216
217 /* actions */
218 - (NSCalendarDate *) newStartDate
219 {
220   NSCalendarDate *newStartDate, *now;
221   NSTimeZone *timeZone;
222   int hour;
223
224   newStartDate = [self selectedDate];
225   if ([[self queryParameterForKey: @"hm"] length] == 0)
226     {
227       now = [NSCalendarDate calendarDate];
228       timeZone = [[context activeUser] timeZone];
229       [now setTimeZone: timeZone];
230       if ([now isDateOnSameDay: newStartDate])
231         {
232           hour = [now hourOfDay];
233           if (hour < 8)
234             newStartDate = [now hour: 8 minute: 0];
235           else if (hour > 18)
236             newStartDate = [[now tomorrow] hour: 8 minute: 0];
237           else
238             newStartDate = now;
239         }
240       else
241         newStartDate = [newStartDate hour: 8 minute: 0];
242     }
243
244   return newStartDate;
245 }
246
247 - (id <WOActionResults>) defaultAction
248 {
249   NSCalendarDate *startDate, *endDate;
250   NSString *duration;
251   unsigned int minutes;
252
253   event = (iCalEvent *) [[self clientObject] component: NO];
254   if (event)
255     {
256       startDate = [event startDate];
257       isAllDay = [event isAllDay];
258       if (isAllDay)
259         endDate = [[event endDate] dateByAddingYears: 0 months: 0 days: -1];
260       else
261         endDate = [event endDate];
262     }
263   else
264     {
265       startDate = [self newStartDate];
266       duration = [self queryParameterForKey:@"dur"];
267       if ([duration length] > 0)
268         minutes = [duration intValue];
269       else
270         minutes = 60;
271       endDate
272         = [startDate dateByAddingYears: 0 months: 0 days: 0
273                      hours: 0 minutes: minutes seconds: 0];
274     }
275
276   ASSIGN (aptStartDate, startDate);
277   ASSIGN (aptEndDate, endDate);
278
279
280   /* here comes the code for initializing repeat, reminder and isAllDay... */
281
282   return self;
283 }
284
285 - (id <WOActionResults>) newAction
286 {
287   NSString *objectId, *method, *uri;
288   id <WOActionResults> result;
289   Class clientKlazz;
290
291   clientKlazz = [[self clientObject] class];
292   objectId = [clientKlazz globallyUniqueObjectId];
293   if ([objectId length] > 0)
294     {
295       method = [NSString stringWithFormat:@"%@/Calendar/%@/editAsAppointment",
296                          [self userFolderPath], objectId];
297       uri = [self completeHrefForMethod: method];
298       result = [self redirectToLocation: uri];
299     }
300   else
301     result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
302                           reason: @"could not create a unique ID"];
303
304   return result;
305 }
306
307 - (id <WOActionResults>) saveAction
308 {
309   SOGoAppointmentObject *clientObject;
310   NSString *iCalString;
311
312   clientObject = [self clientObject];
313   iCalString = [[clientObject calendar: NO] versitString];
314   [clientObject saveContentString: iCalString];
315
316   return [self jsCloseWithRefreshMethod: @"refreshEventsAndDisplay()"];
317 }
318
319 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
320                            inContext: (WOContext*) context
321 {
322   return ([[self clientObject] isKindOfClass: [SOGoAppointmentObject class]]
323           && [[request method] isEqualToString: @"POST"]);
324 }
325
326 - (void) takeValuesFromRequest: (WORequest *) _rq
327                      inContext: (WOContext *) _ctx
328 {
329   SOGoAppointmentObject *clientObject;
330   int nbrDays;
331
332   clientObject = [self clientObject];
333   event = (iCalEvent *) [clientObject component: YES];
334
335   [super takeValuesFromRequest: _rq inContext: _ctx];
336
337   if (isAllDay)
338     {
339       nbrDays = ((float) abs ([aptEndDate timeIntervalSinceDate: aptStartDate])
340                  / 86400) + 1;
341       [event setAllDayWithStartDate: aptStartDate
342              duration: nbrDays];
343     }
344   else
345     {
346       [event setStartDate: aptStartDate];
347       [event setEndDate: aptEndDate];
348     }
349   if ([clientObject isNew])
350     [event setTransparency: @"OPAQUE"];
351 }
352
353 // TODO: add tentatively
354
355 - (id) acceptOrDeclineAction: (BOOL) accept
356 {
357   [[self clientObject] changeParticipationStatus: (accept
358                                                    ? @"ACCEPTED"
359                                                    : @"DECLINED")];
360
361   return self;
362 }
363
364 - (id) acceptAction
365 {
366   return [self acceptOrDeclineAction: YES];
367 }
368
369 - (id) declineAction
370 {
371   return [self acceptOrDeclineAction: NO];
372 }
373
374 @end