]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxAppointmentEditor.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1045 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   NSTimeZone *timeZone;
253   int hour;
254
255   newStartDate = [self selectedDate];
256   if ([[self queryParameterForKey: @"hm"] length] == 0)
257     {
258       now = [NSCalendarDate calendarDate];
259       timeZone = [[context activeUser] timeZone];
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, *endDate;
281   NSString *duration;
282   unsigned int minutes;
283
284   event = (iCalEvent *) [[self clientObject] component: NO];
285   if (event)
286     {
287       startDate = [event startDate];
288       endDate = [event endDate];
289     }
290   else
291     {
292       startDate = [self newStartDate];
293       duration = [self queryParameterForKey:@"dur"];
294       if ([duration length] > 0)
295         minutes = [duration intValue];
296       else
297         minutes = 60;
298       endDate
299         = [startDate dateByAddingYears: 0 months: 0 days: 0
300                      hours: 0 minutes: minutes seconds: 0];
301     }
302
303   ASSIGN (aptStartDate, startDate);
304   ASSIGN (aptEndDate, endDate);
305
306   /* here comes the code for initializing repeat, reminder and isAllDay... */
307
308   return self;
309 }
310
311 - (id <WOActionResults>) newAction
312 {
313   NSString *objectId, *method, *uri;
314   id <WOActionResults> result;
315   Class clientKlazz;
316
317   clientKlazz = [[self clientObject] class];
318   objectId = [clientKlazz globallyUniqueObjectId];
319   if ([objectId length] > 0)
320     {
321       method = [NSString stringWithFormat:@"%@/Calendar/%@/editAsAppointment",
322                          [self userFolderPath], objectId];
323       uri = [self completeHrefForMethod: method];
324       result = [self redirectToLocation: uri];
325     }
326   else
327     result = [NSException exceptionWithHTTPStatus: 500 /* Internal Error */
328                           reason: @"could not create a unique ID"];
329
330   return result;
331 }
332
333 - (id <WOActionResults>) saveAction
334 {
335   SOGoAppointmentObject *clientObject;
336   NSString *iCalString;
337
338   clientObject = [self clientObject];
339   iCalString = [[clientObject calendar: NO] versitString];
340   [clientObject saveContentString: iCalString];
341
342   return [self jsCloseWithRefreshMethod: @"refreshAppointmentsAndDisplay()"];
343 }
344
345 - (BOOL) shouldTakeValuesFromRequest: (WORequest *) request
346                            inContext: (WOContext*) context
347 {
348   return ([[self clientObject] isKindOfClass: [SOGoAppointmentObject class]]
349           && [[request method] isEqualToString: @"POST"]);
350 }
351
352 - (void) takeValuesFromRequest: (WORequest *) _rq
353                      inContext: (WOContext *) _ctx
354 {
355   SOGoAppointmentObject *clientObject;
356
357   clientObject = [self clientObject];
358   event = (iCalEvent *) [clientObject component: YES];
359
360   [super takeValuesFromRequest: _rq inContext: _ctx];
361
362   [event setStartDate: aptStartDate];
363   [event setEndDate: aptEndDate];
364   if ([clientObject isNew])
365     [event setTransparency: @"OPAQUE"];
366 }
367
368 // TODO: add tentatively
369
370 - (id) acceptOrDeclineAction: (BOOL) accept
371 {
372   [[self clientObject] changeParticipationStatus: (accept
373                                                    ? @"ACCEPTED"
374                                                    : @"DECLINED")];
375
376   return self;
377 }
378
379 - (id) acceptAction
380 {
381   return [self acceptOrDeclineAction: YES];
382 }
383
384 - (id) declineAction
385 {
386   return [self acceptOrDeclineAction: NO];
387 }
388
389 @end