2 Copyright (C) 2000-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
24 WEQualifierConditional
26 IfPerson: WEQualifierConditional {
27 condition = "isPerson=YES AND isCategory like '*customer*'";
28 object = currentPerson;
30 bindings = nil; // optional qualifier bindings
31 requiresAllVariables = YES; // whether all bindings must be resolved
34 If no object is given, the qualifier is evaluated against the component.
37 #include <NGObjWeb/WODynamicElement.h>
41 @interface WEQualifierConditional : WODynamicElement
44 WOAssociation *condition;
45 WOAssociation *object;
46 WOAssociation *negate;
48 WOAssociation *bindings;
49 WOAssociation *requiresAllVariables;
55 // TODO: add support for qualifier arguments?
57 @implementation WEQualifierConditional
59 - (id)initWithName:(NSString *)_name
60 associations:(NSDictionary *)_config
61 template:(WOElement *)_t
63 if ((self = [super initWithName:_name associations:_config template:_t])) {
64 self->condition = WOExtGetProperty(_config, @"condition");
65 self->object = WOExtGetProperty(_config, @"object");
66 self->negate = WOExtGetProperty(_config, @"negate");
67 self->bindings = WOExtGetProperty(_config, @"bindings");
68 self->requiresAllVariables =
69 WOExtGetProperty(_config, @"requiresAllVariables");
70 self->template = [_t retain];
72 if (self->condition == nil) {
74 @"WARNING: missing 'condition' association in element: '%@'",
77 else if ([self->condition isValueConstant]) {
78 /* optimization, replace constant associations with a parsed qualifier */
81 if ((value = [self->condition stringValueInComponent:nil])) {
84 q = [EOQualifier qualifierWithQualifierFormat:value];
88 tmp = [[WOAssociation associationWithValue:q] retain];
89 [self->condition release];
90 self->condition = tmp;
99 [self->template release];
100 [self->condition release];
101 [self->object release];
102 [self->negate release];
103 [self->bindings release];
104 [self->requiresAllVariables release];
110 - (WOElement *)template {
111 return self->template;
116 - (BOOL)_doShowInContext:(WOContext *)_ctx {
120 id qualifier, context;
121 BOOL doShow, doNegate, needAllVars;
123 cmp = [_ctx component];
124 doNegate = self->negate ? [self->negate boolValueInComponent:cmp] : NO;
126 vars = [self->bindings valueInComponent:cmp];
128 needAllVars = self->requiresAllVariables
129 ? [self->requiresAllVariables boolValueInComponent:cmp]
132 /* determine qualifier */
134 if ((qualifier = [self->condition valueInComponent:cmp])) {
135 if ([qualifier isKindOfClass:[NSString class]]) {
136 qualifier = [EOQualifier qualifierWithQualifierFormat:qualifier
141 /* apply qualifier bindings */
143 if (vars != nil && qualifier != nil) {
144 qualifier = [qualifier qualifierWithBindings:vars
145 requiresAllVariables:needAllVars];
148 /* find context object */
150 context = self->object
151 ? [self->object valueInComponent:cmp]
156 if (![qualifier respondsToSelector:@selector(evaluateWithObject:)]) {
158 @"ERROR: got a qualifier which does not respond to "
159 @"evaluateWithObject: %@", qualifier];
163 doShow = [qualifier evaluateWithObject:context];
165 return doNegate ? !doShow : doShow;
168 /* processing requests */
170 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
171 if (![self _doShowInContext:_ctx])
174 [_ctx appendElementIDComponent:@"1"];
175 [self->template takeValuesFromRequest:_rq inContext:_ctx];
176 [_ctx deleteLastElementIDComponent];
179 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
183 state = [[_ctx currentElementID] stringValue];
187 [_ctx consumeElementID]; // consume state-id (on or off)
189 if (![state isEqualToString:@"1"])
192 [_ctx appendElementIDComponent:state];
193 result = [self->template invokeActionForRequest:_rq inContext:_ctx];
194 [_ctx deleteLastElementIDComponent];
198 /* generating response */
200 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
201 if (![self _doShowInContext:_ctx])
204 [_ctx appendElementIDComponent:@"1"];
205 [self->template appendToResponse:_response inContext:_ctx];
206 [_ctx deleteLastElementIDComponent];
211 - (NSString *)associationDescription {
212 NSMutableString *str;
214 str = [NSMutableString stringWithCapacity:64];
215 if (self->condition) [str appendFormat:@" condition=%@", self->condition];
216 if (self->object) [str appendFormat:@" object=%@", self->object];
217 if (self->negate) [str appendFormat:@" negate=%@", self->negate];
218 if (self->template) [str appendFormat:@" template=%@", self->template];
222 @end /* WEQualifierConditional */