2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 WEQualifierConditional
25 IfPerson: WEQualifierConditional {
26 condition = "isPerson=YES AND isCategory like '*customer*'";
27 object = currentPerson;
29 bindings = nil; // optional qualifier bindings
30 requiresAllVariables = YES; // whether all bindings must be resolved
33 If no object is given, the qualifier is evaluated against the component.
36 #include <NGObjWeb/WODynamicElement.h>
40 @interface WEQualifierConditional : WODynamicElement
43 WOAssociation *condition;
44 WOAssociation *object;
45 WOAssociation *negate;
47 WOAssociation *bindings;
48 WOAssociation *requiresAllVariables;
54 // TODO: add support for qualifier arguments?
56 @implementation WEQualifierConditional
58 - (id)initWithName:(NSString *)_name
59 associations:(NSDictionary *)_config
60 template:(WOElement *)_t
62 if ((self = [super initWithName:_name associations:_config template:_t])) {
63 self->condition = WOExtGetProperty(_config, @"condition");
64 self->object = WOExtGetProperty(_config, @"object");
65 self->negate = WOExtGetProperty(_config, @"negate");
66 self->bindings = WOExtGetProperty(_config, @"bindings");
67 self->requiresAllVariables =
68 WOExtGetProperty(_config, @"requiresAllVariables");
69 self->template = [_t retain];
71 if (self->condition == nil) {
73 @"WARNING: missing 'condition' association in element: '%@'",
76 else if ([self->condition isValueConstant]) {
77 /* optimization, replace constant associations with a parsed qualifier */
80 if ((value = [self->condition stringValueInComponent:nil])) {
83 q = [EOQualifier qualifierWithQualifierFormat:value];
87 tmp = [[WOAssociation associationWithValue:q] retain];
88 [self->condition release];
89 self->condition = tmp;
98 [self->template release];
99 [self->condition release];
100 [self->object release];
101 [self->negate release];
102 [self->bindings release];
103 [self->requiresAllVariables release];
110 return self->template;
115 - (BOOL)_doShowInContext:(WOContext *)_ctx {
119 id qualifier, context;
120 BOOL doShow, doNegate, needAllVars;
122 cmp = [_ctx component];
123 doNegate = self->negate ? [self->negate boolValueInComponent:cmp] : NO;
125 vars = [self->bindings valueInComponent:cmp];
127 needAllVars = self->requiresAllVariables
128 ? [self->requiresAllVariables boolValueInComponent:cmp]
131 /* determine qualifier */
133 if ((qualifier = [self->condition valueInComponent:cmp])) {
134 if ([qualifier isKindOfClass:[NSString class]]) {
135 qualifier = [EOQualifier qualifierWithQualifierFormat:qualifier
140 /* apply qualifier bindings */
142 if (vars != nil && qualifier != nil) {
143 qualifier = [qualifier qualifierWithBindings:vars
144 requiresAllVariables:needAllVars];
147 /* find context object */
149 context = self->object
150 ? [self->object valueInComponent:cmp]
155 if (![qualifier respondsToSelector:@selector(evaluateWithObject:)]) {
157 @"ERROR: got a qualifier which does not respond to "
158 @"evaluateWithObject: %@", qualifier];
162 doShow = [qualifier evaluateWithObject:context];
164 return doNegate ? !doShow : doShow;
167 /* processing requests */
169 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
170 if (![self _doShowInContext:_ctx])
173 [_ctx appendElementIDComponent:@"1"];
174 [self->template takeValuesFromRequest:_rq inContext:_ctx];
175 [_ctx deleteLastElementIDComponent];
178 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
182 state = [[_ctx currentElementID] stringValue];
186 [_ctx consumeElementID]; // consume state-id (on or off)
188 if (![state isEqualToString:@"1"])
191 [_ctx appendElementIDComponent:state];
192 result = [self->template invokeActionForRequest:_rq inContext:_ctx];
193 [_ctx deleteLastElementIDComponent];
197 /* generating response */
199 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
200 if (![self _doShowInContext:_ctx])
203 [_ctx appendElementIDComponent:@"1"];
204 [self->template appendToResponse:_response inContext:_ctx];
205 [_ctx deleteLastElementIDComponent];
210 - (NSString *)associationDescription {
211 NSMutableString *str;
213 str = [NSMutableString stringWithCapacity:64];
214 if (self->condition) [str appendFormat:@" condition=%@", self->condition];
215 if (self->object) [str appendFormat:@" object=%@", self->object];
216 if (self->negate) [str appendFormat:@" negate=%@", self->negate];
217 if (self->template) [str appendFormat:@" template=%@", self->template];
221 @end /* WEQualifierConditional */