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
22 #include <NGObjWeb/WODynamicElement.h>
24 @interface WOKeyValueConditional : WODynamicElement
27 // WODynamicElement: extraAttributes
28 // WODynamicElement: otherTagString
35 WOAssociation *negate;
38 @end /* WOKeyValueConditional */
42 @implementation WOKeyValueConditional
44 - (id)initWithName:(NSString *)_name
45 associations:(NSDictionary *)_config
46 template:(WOElement *)_c
48 if ((self = [super initWithName:_name associations:_config template:_c])) {
49 self->key = WOExtGetProperty(_config, @"key");
50 self->value = WOExtGetProperty(_config, @"value");
51 self->negate = WOExtGetProperty(_config, @"negate");
52 self->template = [_c retain];
58 [self->template release];
59 [self->value release];
67 return self->template;
72 static inline BOOL _doShow(WOKeyValueConditional *self, WOContext *_ctx) {
73 WOComponent *c = [_ctx component];
75 BOOL doNegate = [self->negate boolValueInComponent:c];
79 k = [self->key stringValueInComponent:c];
80 v = [self->value valueInComponent:c];
81 kv = [c valueForKey:k];
83 doShow = [kv isEqual:v];
85 return doNegate ? !doShow : doShow;
88 /* handling requests */
90 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
91 if (_doShow(self, _ctx)) {
92 [_ctx appendElementIDComponent:@"1"];
93 [self->template takeValuesFromRequest:_rq inContext:_ctx];
94 [_ctx deleteLastElementIDComponent];
98 NSLog(@"didn't take value from request: %@\n doShow=%@\n doNegate=%@",
100 self->condition, self->negate);
105 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
108 state = [[_ctx currentElementID] stringValue];
111 [_ctx consumeElementID]; // consume state-id (on or off)
113 if ([state isEqualToString:@"1"]) {
116 [_ctx appendElementIDComponent:state];
117 result = [self->template invokeActionForRequest:_rq inContext:_ctx];
118 [_ctx deleteLastElementIDComponent];
126 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
127 if (_doShow(self, _ctx)) {
128 [_ctx appendElementIDComponent:@"1"];
129 [self->template appendToResponse:_response inContext:_ctx];
130 [_ctx deleteLastElementIDComponent];
136 - (NSString *)associationDescription {
137 NSMutableString *str;
139 str = [NSMutableString stringWithCapacity:64];
140 if (self->key) [str appendFormat:@" key=%@", self->key];
141 if (self->value) [str appendFormat:@" value=%@", self->value];
142 if (self->negate) [str appendFormat:@" negate=%@", self->negate];
143 if (self->template) [str appendFormat:@" template=%@", self->template];
147 @end /* WOKeyValueConditional */