/*
  Copyright (C) 2000-2005 SKYRIX Software AG

  This file is part of SOPE.

  SOPE 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.

  SOPE 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 SOPE; see the file COPYING.  If not, write to the
  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
*/

#include "WOInput.h"
#include <NGObjWeb/WOApplication.h>
#include "common.h"

@interface WOSubmitButton : WOInput
{
  // WOInput: name
  // WOInput: value
  // WOInput: disabled
@protected
  WOAssociation *action;
  WOAssociation *pageName;

  // new in WO4:
  WOAssociation *queryDictionary;
  NSDictionary  *queryParameters;  // associations beginning with ?
  WOAssociation *actionClass;
  WOAssociation *directActionName;
  BOOL          sidInUrl;
}

@end /* WOSubmitButton */

@implementation WOSubmitButton

+ (int)version {
  return 2;
}

- (id)initWithName:(NSString *)_name
  associations:(NSDictionary *)_config
  template:(WOElement *)_t
{
  if ((self = [super initWithName:_name associations:_config template:_t])) {
    WOAssociation *sidInUrlAssoc;

    sidInUrlAssoc  = OWGetProperty(_config, @"?wosid");
    self->action   = OWGetProperty(_config, @"action");
    self->pageName = OWGetProperty(_config, @"pageName");

    self->queryDictionary    = OWGetProperty(_config, @"queryDictionary");
    self->queryParameters    = OWExtractQueryParameters(_config);
    self->actionClass        = OWGetProperty(_config, @"actionClass");
    self->directActionName   = OWGetProperty(_config, @"directActionName");
    
    self->sidInUrl = (sidInUrlAssoc)
      ? [sidInUrlAssoc boolValueInComponent:nil]
      : YES;
  }
  return self;
}

#if !LIB_FOUNDATION_BOEHM_GC
- (void)dealloc {
  RELEASE(self->actionClass);
  RELEASE(self->directActionName);
  RELEASE(self->queryDictionary);
  RELEASE(self->queryParameters);
  RELEASE(self->action);
  RELEASE(self->pageName);
  [super dealloc];
}
#endif

// responder

- (void)takeValuesFromRequest:(WORequest *)_request
  inContext:(WOContext *)_ctx
{
  id formValue;
  
  if (self->disabled) {
    if ([self->disabled boolValueInComponent:[_ctx component]])
      return;
  }
  
  if ((formValue = [_request formValueForKey:OWFormElementName(self, _ctx)])) {
    //NSLog(@"%@: value=%@ ..", [self elementID], formValue);
    
    if ([self->value isValueSettable]) {
      [self->value setStringValue:formValue
           inComponent:[_ctx component]];
    }
    if ((self->action != nil) || (self->pageName != nil))
      [_ctx addActiveFormElement:self];
  }
}

- (id)invokeActionForRequest:(WORequest *)_request inContext:(WOContext *)_ctx {
  if (self->disabled) {
    if ([self->disabled boolValueInComponent:[_ctx component]])
      return nil;
  }
  
  /* check whether this is the active form element (determined in take-values) */
  if (![[_ctx elementID] isEqualToString:[_ctx senderID]]) {
    NSLog(@"SUBMITBUTTON is not active (%@ vs %@) !",
          [_ctx elementID], [_ctx senderID]);
    return nil;
  }
  
  if (self->action)
    return [self executeAction:self->action inContext:_ctx];

  if (self->pageName) {
    NSString    *pname = nil;
    WOComponent *page = nil;

    pname = [self->pageName stringValueInComponent:[_ctx component]];
    page = [[_ctx application] pageWithName:pname inContext:_ctx];

    if (page == nil) {
      [[_ctx session] logWithFormat:
                      @"%@[0x%08X]: did not find page with name %@ !",
                      NSStringFromClass([self class]), self, pname];
    }
    NSLog(@"%@: showing page %@", self, page);
    return page;
  }

  return nil;
}

- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
  NSString *v = [self->value stringValueInComponent:[_ctx component]];

  WOResponse_AddCString(_response, "<input type=\"submit\" name=\"");
  [_response appendContentHTMLAttributeValue:OWFormElementName(self, _ctx)];
  WOResponse_AddCString(_response, "\" value=\"");
  [_response appendContentHTMLAttributeValue:v];
  WOResponse_AddChar(_response, '"');
  [self appendExtraAttributesToResponse:_response inContext:_ctx];
  if (self->otherTagString) {
    WOResponse_AddChar(_response, ' ');
    WOResponse_AddString(_response,
                         [self->otherTagString stringValueInComponent:
                              [_ctx component]]);
  }
  WOResponse_AddCString(_response, " />");
}

/* description */

- (NSString *)associationDescription {
  NSMutableString *str = [NSMutableString stringWithCapacity:128];
  [str appendString:[super associationDescription]];
  if (self->action)   [str appendFormat:@" action=%@", self->action];
  if (self->pageName) [str appendFormat:@" page=%@",   self->pageName];

  if (self->actionClass)
    [str appendFormat:@" actionClass=%@", self->actionClass];
  if (self->directActionName)
    [str appendFormat:@" directAction=%@", self->directActionName];
  return str;
}

@end /* WOSubmitButton */
