/* Copyright (C) 2000-2004 SKYRIX Software AG This file is part of OpenGroupware.org. OGo is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. OGo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with OGo; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ // $Id$ #include @interface WOConditional : WODynamicElement { @protected // WODynamicElement: extraAttributes // WODynamicElement: otherTagString WOAssociation *condition; WOAssociation *negate; WOElement *template; // non-WO WOAssociation *value; // compare the condition with value #if DEBUG NSString *condName; #endif } @end /* WOConditional */ #include "common.h" #include "WOElement+private.h" // TODO: make that a class cluster for improved performance @implementation WOConditional static int descriptiveIDs = -1; + (void)initialize { NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; descriptiveIDs = [ud boolForKey:@"WODescriptiveElementIDs"] ? 1 : 0; } - (id)initWithName:(NSString *)_name associations:(NSDictionary *)_config template:(WOElement *)_c { #if DEBUG self->condName = _name ? [_name copy] : @"condYES"; #endif if ((self = [super initWithName:_name associations:_config template:_c])) { self->condition = OWGetProperty(_config, @"condition"); self->negate = OWGetProperty(_config, @"negate"); self->value = OWGetProperty(_config, @"value"); self->template = [_c retain]; if (self->condition == nil) { [self logWithFormat: @"WARNING: missing 'condition' association in element: '%@'", _name]; } } return self; } - (void)dealloc { [self->template release]; [self->value release]; [self->condition release]; [self->negate release]; #if DEBUG [self->condName release]; #endif [super dealloc]; } /* accessors */ - (WOElement *)template { return self->template; } /* state */ static inline BOOL _doShow(WOConditional *self, WOContext *_ctx) { WOComponent *cmp = [_ctx component]; BOOL doShow = NO; BOOL doNegate = [self->negate boolValueInComponent:cmp]; if (self->value) { id v = [self->value valueInComponent:cmp]; id cv = [self->condition valueInComponent:cmp]; doShow = [cv isEqual:v]; } else doShow = [self->condition boolValueInComponent:cmp]; return doNegate ? !doShow : doShow; } /* processing requests */ - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx { if (!_doShow(self, _ctx)) { #if 0 NSLog(@"didn't take value from request: %@\n doShow=%@\n doNegate=%@", [self elementID], self->condition, self->negate); #endif return; } #if DEBUG [_ctx appendElementIDComponent:descriptiveIDs ? self->condName : @"1"]; #else [_ctx appendElementIDComponent:@"1"]; #endif [self->template takeValuesFromRequest:_rq inContext:_ctx]; [_ctx deleteLastElementIDComponent]; } - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx { NSString *state; NSString *key; id result; state = [[_ctx currentElementID] stringValue]; if (!state) return nil; [_ctx consumeElementID]; // consume state-id (on or off) #if DEBUG key = descriptiveIDs ? self->condName : @"1"; #else key = @"1"; #endif if (![state isEqualToString:key]) return nil; [_ctx appendElementIDComponent:state]; result = [self->template invokeActionForRequest:_rq inContext:_ctx]; [_ctx deleteLastElementIDComponent]; return result; } /* generating response */ - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx { if (!_doShow(self, _ctx)) return; #if DEBUG [_ctx appendElementIDComponent:descriptiveIDs ? self->condName : @"1"]; #else [_ctx appendElementIDComponent:@"1"]; #endif [self->template appendToResponse:_response inContext:_ctx]; [_ctx deleteLastElementIDComponent]; } /* description */ - (NSString *)associationDescription { NSMutableString *str; str = [NSMutableString stringWithCapacity:64]; if (self->condition) [str appendFormat:@" condition=%@", self->condition]; if (self->negate) [str appendFormat:@" negate=%@", self->negate]; if (self->template) [str appendFormat:@" template=%@", self->template]; return str; } @end /* WOConditional */