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 #import "WEContextConditional.h"
25 @implementation WEContextConditional
27 - (id)initWithName:(NSString *)_name
28 associations:(NSDictionary *)_config
29 template:(WOElement *)_c
31 if ((self = [super initWithName:_name associations:_config template:_c])) {
32 self->negate = WOExtGetProperty(_config, @"negate");
33 self->contextKey = WOExtGetProperty(_config, @"contextKey");
34 self->didMatch = WOExtGetProperty(_config, @"didMatch");
36 self->template = RETAIN(_c);
42 [self->template release];
43 [self->negate release];
44 [self->contextKey release];
45 [self->didMatch release];
52 return self->template;
55 - (NSString *)_contextKey {
59 - (NSString *)_didMatchKey {
65 static inline BOOL _doShow(WEContextConditional *self, WOContext *_ctx) {
67 BOOL doNegate = [self->negate boolValueInComponent:[_ctx component]];
69 if ([self _contextKey])
70 doShow = ([_ctx objectForKey:[self _contextKey]] != nil);
71 else if (self->contextKey) {
72 id cKey = [self->contextKey valueInComponent:[_ctx component]];
74 doShow = ([_ctx objectForKey:cKey] != nil);
76 doShow = doNegate ? !doShow : doShow;
78 if (doShow && [self->didMatch isValueSettable])
79 [self->didMatch setBoolValue:YES inComponent:[_ctx component]];
81 if (doShow && [self _didMatchKey] != nil)
82 [_ctx setObject:@"YES" forKey:[self _didMatchKey]];
87 /* handling requests */
89 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
90 if (_doShow(self, _ctx)) {
91 [_ctx appendElementIDComponent:@"1"];
92 [self->template takeValuesFromRequest:_rq inContext:_ctx];
93 [_ctx deleteLastElementIDComponent];
97 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
101 if ((state = [[_ctx currentElementID] stringValue]) == nil)
104 [_ctx consumeElementID]; // consume state-id (on or off)
106 if (![state isEqualToString:@"1"])
109 [_ctx appendElementIDComponent:state];
110 result = [self->template invokeActionForRequest:_rq inContext:_ctx];
111 [_ctx deleteLastElementIDComponent];
115 /* generate response */
117 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
118 if (_doShow(self, _ctx)) {
119 [_ctx appendElementIDComponent:@"1"];
120 [self->template appendToResponse:_response inContext:_ctx];
121 [_ctx deleteLastElementIDComponent];
125 @end /* WEContextConditional */