1/*
2  Copyright (C) 2000-2005 SKYRIX Software AG
3
4  This file is part of SOPE.
5
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
9  later version.
10
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.
15
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
19  02111-1307, USA.
20*/
21
22#import "common.h"
23#import "WEContextConditional.h"
24
25@implementation WEContextConditional
26
27- (id)initWithName:(NSString *)_name
28  associations:(NSDictionary *)_config
29  template:(WOElement *)_c
30{
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");
35
36    self->template  = RETAIN(_c);
37  }
38  return self;
39}
40
41- (void)dealloc {
42  [self->template   release];
43  [self->negate     release];
44  [self->contextKey release];
45  [self->didMatch   release];
46  [super dealloc];
47}
48
49/* accessors */
50
51- (id)template {
52  return self->template;
53}
54
55- (NSString *)_contextKey {
56  return nil;
57}
58
59- (NSString *)_didMatchKey {
60  return nil;
61}
62
63/* state */
64
65static inline BOOL _doShow(WEContextConditional *self, WOContext *_ctx) {
66  BOOL doShow   = NO;
67  BOOL doNegate = [self->negate boolValueInComponent:[_ctx component]];
68
69  if ([self _contextKey])
70    doShow = ([_ctx objectForKey:[self _contextKey]] != nil);
71  else if (self->contextKey) {
72    id cKey = [self->contextKey valueInComponent:[_ctx component]];
73
74    doShow = ([_ctx objectForKey:cKey] != nil);
75  }
76  doShow = doNegate ? !doShow : doShow;
77
78  if (doShow && [self->didMatch isValueSettable])
79    [self->didMatch setBoolValue:YES inComponent:[_ctx component]];
80
81  if (doShow && [self _didMatchKey] != nil)
82    [_ctx setObject:@"YES" forKey:[self _didMatchKey]];
83
84  return doShow;
85}
86
87/* handling requests */
88
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];
94  }
95}
96
97- (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
98  NSString *state;
99  id result;
100
101  if ((state = [[_ctx currentElementID] stringValue]) == nil)
102    return nil;
103
104  [_ctx consumeElementID]; // consume state-id (on or off)
105
106  if (![state isEqualToString:@"1"])
107    return nil;
108
109  [_ctx appendElementIDComponent:state];
110  result = [self->template invokeActionForRequest:_rq inContext:_ctx];
111  [_ctx deleteLastElementIDComponent];
112  return result;
113}
114
115/* generate response */
116
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];
122  }
123}
124
125@end /* WEContextConditional */
126