2 Copyright (C) 2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 // $Id: AnaisSelector.m 184 2004-08-11 17:46:10Z znek $
23 #include <SOGoUI/UIxComponent.h>
28 Select a set of attendees using Anais.
31 attendees - array of iCalPerson objects
32 selectorID - string to be used as the identifier for form/JS elements
33 withCN - show CN of person (eg disabled for resources)
34 withAddressBook - allow selection from private address book (default:NO)
35 division - Anais division
36 emailForUser - default EMail
37 cnForUser - default CN
38 role - role for submitted attendees
41 <var:component className="AnaisAttendeeSelector"
42 selectorID="participant"
50 @interface AnaisAttendeeSelector : UIxComponent
56 NSString *emailForUser;
61 int withAddressBook:1;
68 #include <NGiCal/NGiCal.h>
69 #include <SOGoLogic/AgenorUserManager.h>
70 #include <Scheduler/iCalPerson+UIx.h>
71 #include <NGObjWeb/WOContext.h>
72 #include <NGObjWeb/WORequest.h>
75 @implementation AnaisAttendeeSelector
77 static BOOL debugOn = NO;
80 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
82 debugOn = [ud boolForKey:@"AnaisDebugEnabled"];
87 [self->emailForUser release];
88 [self->cnForUser release];
89 [self->division release];
90 [self->selectorID release];
91 [self->attendee release];
92 [self->attendees release];
99 [self->attendee release]; self->attendee = nil;
105 - (void)setAttendees:(NSArray *)_attendees {
106 ASSIGN(self->attendees, _attendees);
108 - (NSArray *)attendees {
109 return self->attendees;
111 - (BOOL)hasAttendees {
112 return [[self attendees] count] > 0 ? YES : NO;
114 - (BOOL)hasNoAttendees {
115 return [[self attendees] count] == 0 ? YES : NO;
118 - (void)setWithCN:(BOOL)_flag {
119 self->flags.withCN = _flag ? 1 : 0;
122 return self->flags.withCN ? YES : NO;
125 - (void)setWithAddressBook:(BOOL)_flag {
126 self->flags.withAddressBook = _flag ? 1 : 0;
128 - (BOOL)withAddressBook {
129 return self->flags.withAddressBook ? YES : NO;
132 - (void)setAttendee:(iCalPerson *)_attendee {
133 ASSIGN(self->attendee, _attendee);
135 - (iCalPerson *)attendee {
136 return self->attendee;
139 - (void)setDivision:(NSString *)_value {
140 ASSIGNCOPY(self->division, _value);
142 - (NSString *)division {
143 return self->division;
146 - (void)setRole:(NSString *)_value {
147 ASSIGNCOPY(self->role, _value);
153 - (void)setEmailForUser:(NSString *)_value {
154 ASSIGNCOPY(self->emailForUser, _value);
156 - (NSString *)emailForUser {
157 return self->emailForUser;
160 - (void)setCnForUser:(NSString *)_value {
161 ASSIGNCOPY(self->cnForUser, _value);
163 - (NSString *)cnForUser {
164 return self->cnForUser;
167 - (BOOL)showDefaultAttendees {
168 return [self hasNoAttendees] && [[self emailForUser] length] > 0 ? YES : NO;
173 - (NSString *)combinedCNAndEmailForUser {
176 e = [self emailForUser];
177 c = [self cnForUser];
178 return [[e stringByAppendingString:@";"] stringByAppendingString:c];
181 - (NSString *)combinedCNAndEmail {
184 e = [[self attendee] rfc822Email];
185 c = [[self attendee] cnForDisplay];
186 return [[e stringByAppendingString:@";"] stringByAppendingString:c];
191 - (void)setSelectorID:(NSString *)_value {
192 ASSIGNCOPY(self->selectorID, _value);
194 - (NSString *)selectorID {
195 return self->selectorID;
197 - (NSString *)capitalizedSelectorID {
198 return [[self selectorID] capitalizedString];
201 - (NSString *)windowId {
202 /* eg: 'Resources' */
203 return [[self capitalizedSelectorID] stringByAppendingString:@"s"];
205 - (NSString *)callbackName {
206 /* eg: 'addResource' */
207 return [@"add" stringByAppendingString:[self capitalizedSelectorID]];
209 - (NSString *)tableId {
210 /* eg: 'resources' */
211 return [[self selectorID] stringByAppendingString:@"s"];
213 - (NSString *)checkboxId {
214 /* eg: 'resources' */
215 return [self tableId]; /* TODO: znek, is this ok? */
218 /* handling requests */
220 - (NSArray *)getICalPersonsFromFormValues:(NSArray *)_values {
222 NSMutableArray *result;
224 count = [_values count];
225 result = [NSMutableArray arrayWithCapacity:count];
226 for (i = 0; i < count; i++) {
227 NSString *pString, *email, *cn;
231 pString = [_values objectAtIndex:i];
232 if ([pString length] == 0)
235 /* delimiter between email and cn */
236 r = [pString rangeOfString:@";"];
238 /* sample: 'test.et.di.cete-lyon@equipement.gouv.fr;test' */
239 email = [pString substringToIndex:r.location];
240 cn = [pString substringFromIndex:NSMaxRange(r)];
241 if ([cn length] == 0) cn = nil;
249 AgenorUserManager *um = [AgenorUserManager sharedUserManager];
250 cn = [um getCNForUID:[um getUIDForEmail:email]];
253 p = [[iCalPerson alloc] init];
254 [p setEmail:[@"mailto:" stringByAppendingString:email]];
255 if ([cn isNotNull]) [p setCn:cn];
257 /* see RFC2445, sect. 4.2.16 for details */
258 [p setRole:[self role]];
259 [result addObject:p];
264 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
265 /* OK, we have a special form value processor */
268 [self debugWithFormat:@"Note: will take values ..."];
270 tmp = [self getICalPersonsFromFormValues:
271 [_rq formValuesForKey:[self checkboxId]]];
272 [self debugWithFormat:@" got %i attendees: %@", [tmp count], tmp];
273 [self setAttendees:tmp];
276 /* response generation */
278 - (NSString *)jsCode {
283 ms = [NSMutableString stringWithCapacity:128];
285 ctx = [self context];
286 if (![[ctx valueForKey:@"HasAddTableAnaisAttendeeSelector"] boolValue]) {
287 static NSString *script = \
288 @"function addToTable(tableId, type, cn, dn, email, uid, sn) {\n"
289 @" var test = document.getElementById(email);"
293 @" var table = document.getElementById(tableId);"
294 @" var tr = document.createElement('tr');"
295 @" var td, checkbox, text;"
297 @" td = document.createElement('td');"
298 @" checkbox = document.createElement('input');"
299 @" checkbox.setAttribute('type', 'checkbox');"
300 @" checkbox.setAttribute('checked', 'checked');"
301 @" checkbox.setAttribute('value', email + ';' + cn);"
302 @" checkbox.setAttribute('id', email);"
303 @" checkbox.setAttribute('name', tableId);"
304 @" td.appendChild(checkbox);"
305 @" tr.appendChild(td);"
306 @" td = document.createElement('td');"
307 @" text = document.createTextNode(cn);"
308 @" td.appendChild(text);"
309 @" tr.appendChild(td);"
310 @" table.appendChild(tr);"
312 [ms appendString:script];
314 [ctx takeValue:[NSNumber numberWithBool:YES]
315 forKey:@"HasAddTableAnaisAttendeeSelector"];
319 @"function %@(type, cn, dn, email, uid, sn) {\n"
320 @" addToTable('%@', type, cn, dn, email, uid, sn);\n"
322 [ms appendFormat:s, [self callbackName], [self tableId]];
328 - (BOOL)isDebuggingEnabled {
332 @end /* AnaisAttendeeSelector */