2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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 #import "WEContextConditional.h"
26 @implementation WEContextConditional
28 - (id)initWithName:(NSString *)_name
29 associations:(NSDictionary *)_config
30 template:(WOElement *)_c
32 if ((self = [super initWithName:_name associations:_config template:_c])) {
33 self->negate = WOExtGetProperty(_config, @"negate");
34 self->contextKey = WOExtGetProperty(_config, @"contextKey");
35 self->didMatch = WOExtGetProperty(_config, @"didMatch");
37 self->template = RETAIN(_c);
42 #if !LIB_FOUNDATION_BOEHM_GC
44 RELEASE(self->template);
45 RELEASE(self->negate);
46 RELEASE(self->contextKey);
47 RELEASE(self->didMatch);
54 - (WOElement *)template {
55 return self->template;
58 - (NSString *)_contextKey {
62 - (NSString *)_didMatchKey {
68 static inline BOOL _doShow(WEContextConditional *self, WOContext *_ctx) {
70 BOOL doNegate = [self->negate boolValueInComponent:[_ctx component]];
72 if ([self _contextKey])
73 doShow = ([_ctx objectForKey:[self _contextKey]] != nil);
74 else if (self->contextKey) {
75 id cKey = [self->contextKey valueInComponent:[_ctx component]];
77 doShow = ([_ctx objectForKey:cKey] != nil);
79 doShow = doNegate ? !doShow : doShow;
81 if (doShow && [self->didMatch isValueSettable])
82 [self->didMatch setBoolValue:YES inComponent:[_ctx component]];
84 if (doShow && [self _didMatchKey] != nil)
85 [_ctx setObject:@"YES" forKey:[self _didMatchKey]];
90 // ******************** responder ********************
92 - (void)takeValuesFromRequest:(WORequest *)_request
93 inContext:(WOContext *)_ctx
95 if (_doShow(self, _ctx)) {
96 [_ctx appendElementIDComponent:@"1"];
97 [self->template takeValuesFromRequest:_request inContext:_ctx];
98 [_ctx deleteLastElementIDComponent];
102 - (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
105 state = [[_ctx currentElementID] stringValue];
108 [_ctx consumeElementID]; // consume state-id (on or off)
110 if ([state isEqualToString:@"1"]) {
113 [_ctx appendElementIDComponent:state];
114 result = [self->template invokeActionForRequest:_request inContext:_ctx];
115 [_ctx deleteLastElementIDComponent];
123 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
124 if (_doShow(self, _ctx)) {
125 [_ctx appendElementIDComponent:@"1"];
126 [self->template appendToResponse:_response inContext:_ctx];
127 [_ctx deleteLastElementIDComponent];
131 @end /* WEContextConditional */