]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOSubmitButton.m
improved query string handling
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOSubmitButton.m
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 #include "WOInput.h"
23 #include <NGObjWeb/WOApplication.h>
24 #include "decommon.h"
25
26 @interface WOSubmitButton : WOInput
27 {
28   // WOInput: name
29   // WOInput: value
30   // WOInput: disabled
31 @protected
32   WOAssociation *action;
33   WOAssociation *pageName;
34
35   // new in WO4:
36   WOAssociation *queryDictionary;
37   NSDictionary  *queryParameters;  // associations beginning with ?
38   WOAssociation *actionClass;
39   WOAssociation *directActionName;
40   BOOL          sidInUrl;
41 }
42
43 @end /* WOSubmitButton */
44
45 @implementation WOSubmitButton
46
47 + (int)version {
48   return 2;
49 }
50
51 - (id)initWithName:(NSString *)_name
52   associations:(NSDictionary *)_config
53   template:(WOElement *)_t
54 {
55   if ((self = [super initWithName:_name associations:_config template:_t])) {
56     WOAssociation *sidInUrlAssoc;
57
58     sidInUrlAssoc  = OWGetProperty(_config, @"?wosid");
59     self->action   = OWGetProperty(_config, @"action");
60     self->pageName = OWGetProperty(_config, @"pageName");
61
62     self->queryDictionary    = OWGetProperty(_config, @"queryDictionary");
63     self->queryParameters    = OWExtractQueryParameters(_config);
64     self->actionClass        = OWGetProperty(_config, @"actionClass");
65     self->directActionName   = OWGetProperty(_config, @"directActionName");
66     
67     self->sidInUrl = (sidInUrlAssoc)
68       ? [sidInUrlAssoc boolValueInComponent:nil]
69       : YES;
70   }
71   return self;
72 }
73
74 - (void)dealloc {
75   [self->actionClass      release];
76   [self->directActionName release];
77   [self->queryDictionary  release];
78   [self->queryParameters  release];
79   [self->action           release];
80   [self->pageName         release];
81   [super dealloc];
82 }
83
84 /* handle request */
85
86 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
87   id formValue;
88   
89   if (self->disabled != nil) {
90     if ([self->disabled boolValueInComponent:[_ctx component]])
91       return;
92   }
93   
94   if ((formValue = [_rq formValueForKey:OWFormElementName(self, _ctx)])) {
95     //NSLog(@"%@: value=%@ ..", [self elementID], formValue);
96     
97     if ([self->value isValueSettable]) {
98       [self->value setStringValue:formValue
99            inComponent:[_ctx component]];
100     }
101     if ((self->action != nil) || (self->pageName != nil))
102       [_ctx addActiveFormElement:self];
103   }
104 }
105
106 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
107   if (self->disabled != nil) {
108     if ([self->disabled boolValueInComponent:[_ctx component]])
109       return nil;
110   }
111   
112   /* 
113      check whether this is the active form element (determined in take-values) 
114   */
115   if (![[_ctx elementID] isEqualToString:[_ctx senderID]]) {
116     NSLog(@"SUBMITBUTTON is not active (%@ vs %@) !",
117           [_ctx elementID], [_ctx senderID]);
118     return nil;
119   }
120   
121   if (self->action != nil)
122     return [self executeAction:self->action inContext:_ctx];
123
124   if (self->pageName) {
125     NSString    *pname = nil;
126     WOComponent *page = nil;
127
128     pname = [self->pageName stringValueInComponent:[_ctx component]];
129     page = [[_ctx application] pageWithName:pname inContext:_ctx];
130
131     if (page == nil) {
132       [[_ctx session] logWithFormat:
133                       @"%@[0x%08X]: did not find page with name %@ !",
134                       NSStringFromClass([self class]), self, pname];
135     }
136     [self logWithFormat:@"showing page %@", page];
137     return page;
138   }
139
140   return nil;
141 }
142
143 /* generate response */
144
145 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
146   NSString *v;
147   
148   v = [self->value stringValueInComponent:[_ctx component]];
149   WOResponse_AddCString(_response, "<input type=\"submit\" name=\"");
150   [_response appendContentHTMLAttributeValue:OWFormElementName(self, _ctx)];
151   WOResponse_AddCString(_response, "\" value=\"");
152   [_response appendContentHTMLAttributeValue:v];
153   WOResponse_AddChar(_response, '"');
154   
155   if ([self->disabled boolValueInComponent:[_ctx component]])
156     WOResponse_AddCString(_response, " disabled=\"disabled\"");
157   
158   [self appendExtraAttributesToResponse:_response inContext:_ctx];
159   if (self->otherTagString != nil) {
160     v = [self->otherTagString stringValueInComponent:[_ctx component]];
161     WOResponse_AddChar(_response, ' ');
162     WOResponse_AddString(_response, v);
163   }
164   WOResponse_AddEmptyCloseParens(_response, _ctx);
165 }
166
167 /* description */
168
169 - (NSString *)associationDescription {
170   NSMutableString *str;
171
172   str = [NSMutableString stringWithCapacity:128];
173   [str appendString:[super associationDescription]];
174   
175   if (self->action)   [str appendFormat:@" action=%@", self->action];
176   if (self->pageName) [str appendFormat:@" page=%@",   self->pageName];
177   
178   if (self->actionClass)
179     [str appendFormat:@" actionClass=%@", self->actionClass];
180   if (self->directActionName)
181     [str appendFormat:@" directAction=%@", self->directActionName];
182   return str;
183 }
184
185 @end /* WOSubmitButton */