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