/*
  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 "WOInput.h"
#include "WOElement+private.h"
#include "common.h"

@implementation WOInput

+ (int)version {
  return [super version] + 0 /* v2 */;
}

+ (void)initialize {
  NSAssert2([super version] == 2,
            @"invalid superclass (%@) version %i !",
            NSStringFromClass([self superclass]), [super version]);
}

- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_associations
  template:(WOElement *)_rootChild
{
  self = [super initWithName:_name associations:_associations
                template:_rootChild];
  if (self) {
    self->containsForm = YES;
    self->name     = OWGetProperty(_associations, @"name");
    self->value    = OWGetProperty(_associations, @"value");
    self->disabled = OWGetProperty(_associations, @"disabled");
    
    /* type is defined by the element itself ... */
    [(NSMutableDictionary *)_associations removeObjectForKey:@"type"];
    
    if ([_associations objectForKey:@"NAME"]) {
      NSLog(@"WARNING: found 'NAME' association in element %@, "
            @"'name' is probably the right thing ..",
            self);
    }
  }
  return self;
}

- (void)dealloc {
  [self->name     release];
  [self->value    release];
  [self->disabled release];
  [super dealloc];
}

/* form support */

NSString *OWFormElementName(WOInput *self, WOContext *_ctx) {
  NSString *name;
  
  if (self->name == nil)
    return [_ctx elementID];
  
  if ((name = [self->name stringValueInComponent:[_ctx component]]))
    return name;
  
  [[_ctx component]
         logWithFormat:
               @"WARNING: in element %@, 'name' attribute configured (%@),"
               @"but no name assigned (using elementID as name) !",
               self, self->name];
  return [_ctx elementID];
}

// ******************** responder ********************

- (id)parseFormValue:(id)_value inContext:(WOContext *)_ctx {
  return _value;
}

- (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
  NSString *formName;
  id formValue = nil;
  
  if ([self->disabled boolValueInComponent:[_ctx component]])
    return;
  
  formName = OWFormElementName(self, _ctx);
  
  if ((formValue = [_req formValueForKey:formName])) {
#if DEBUG && 0
    NSLog(@"%s(%@): form=%@ ctx=%@ value=%@ ..", __PRETTY_FUNCTION__,
	  [_ctx elementID], formName, [_ctx contextID], formValue);
#endif
    
    if ([self->value isValueSettable]) {
      formValue = [self parseFormValue:formValue inContext:_ctx];
      [self->value setStringValue:formValue inComponent:[_ctx component]];
    }
#if DEBUG
    else {
      NSLog(@"%s: form value is not settable: %@", __PRETTY_FUNCTION__,
            self->value);
    }
#endif
  }
}

/* description */

- (NSString *)associationDescription {
  NSMutableString *str = [NSMutableString stringWithCapacity:128];

  if (self->value)    [str appendFormat:@" value=%@",    self->value];
  if (self->name)     [str appendFormat:@" name=%@",     self->name];
  if (self->disabled) [str appendFormat:@" disabled=%@", self->disabled];

  return str;
}

@end /* WOInput */
