]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOSubmitButton.m
added some WebDrive WebDAV properties
[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 static BOOL WOSubmitButtonEnableValueSync = NO;
48
49 + (int)version {
50   return 2;
51 }
52 + (void)initialize {
53   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
54   
55   WOSubmitButtonEnableValueSync = 
56     [ud boolForKey:@"WOSubmitButtonEnableValueSync"];
57 }
58
59 - (id)initWithName:(NSString *)_name
60   associations:(NSDictionary *)_config
61   template:(WOElement *)_t
62 {
63   if ((self = [super initWithName:_name associations:_config template:_t])) {
64     WOAssociation *sidInUrlAssoc;
65
66     sidInUrlAssoc  = OWGetProperty(_config, @"?wosid");
67     self->action   = OWGetProperty(_config, @"action");
68     self->pageName = OWGetProperty(_config, @"pageName");
69
70     self->queryDictionary    = OWGetProperty(_config, @"queryDictionary");
71     self->queryParameters    = OWExtractQueryParameters(_config);
72     self->actionClass        = OWGetProperty(_config, @"actionClass");
73     self->directActionName   = OWGetProperty(_config, @"directActionName");
74     
75     self->sidInUrl = (sidInUrlAssoc)
76       ? [sidInUrlAssoc boolValueInComponent:nil]
77       : YES;
78   }
79   return self;
80 }
81
82 - (void)dealloc {
83   [self->actionClass      release];
84   [self->directActionName release];
85   [self->queryDictionary  release];
86   [self->queryParameters  release];
87   [self->action           release];
88   [self->pageName         release];
89   [super dealloc];
90 }
91
92 /* handle request */
93
94 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
95   id formValue;
96   
97   if (self->disabled != nil) {
98     if ([self->disabled boolValueInComponent:[_ctx component]])
99       return;
100   }
101   
102   if ((formValue = [_rq formValueForKey:OWFormElementName(self, _ctx)])!=nil) {
103     // [self debugWithFormat:@"%@: value=%@ ..", [self elementID], formValue];
104     
105     if (WOSubmitButtonEnableValueSync) {
106       /*
107         We need this because some associations (eg
108         WOKeyPathAssociationSystemKVC) report "isValueSettable" as YES,
109         but raise an exception afterwards.
110
111         This section is disabled per default since its usually not required.
112         
113         See OGo bug #1568 for details.
114       */
115       if ([self->value isValueSettable])
116         [self->value setStringValue:formValue inComponent:[_ctx component]];
117     }
118     
119     if ((self->action != nil) || (self->pageName != nil))
120       [_ctx addActiveFormElement:self];
121   }
122 }
123
124 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
125   if (self->disabled != nil) {
126     if ([self->disabled boolValueInComponent:[_ctx component]])
127       return nil;
128   }
129   
130   /* 
131      check whether this is the active form element (determined in take-values) 
132   */
133   if (![[_ctx elementID] isEqualToString:[_ctx senderID]]) {
134     NSLog(@"SUBMITBUTTON is not active (%@ vs %@) !",
135           [_ctx elementID], [_ctx senderID]);
136     return nil;
137   }
138   
139   if (self->action != nil)
140     return [self executeAction:self->action inContext:_ctx];
141
142   if (self->pageName) {
143     NSString    *pname = nil;
144     WOComponent *page = nil;
145
146     pname = [self->pageName stringValueInComponent:[_ctx component]];
147     page = [[_ctx application] pageWithName:pname inContext:_ctx];
148
149     if (page == nil) {
150       [[_ctx session] logWithFormat:
151                       @"%@[0x%08X]: did not find page with name %@ !",
152                       NSStringFromClass([self class]), self, pname];
153     }
154     [self logWithFormat:@"showing page %@", page];
155     return page;
156   }
157
158   return nil;
159 }
160
161 /* generate response */
162
163 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
164   NSString *v;
165   
166   v = [self->value stringValueInComponent:[_ctx component]];
167   WOResponse_AddCString(_response, "<input type=\"submit\" name=\"");
168   [_response appendContentHTMLAttributeValue:OWFormElementName(self, _ctx)];
169   WOResponse_AddCString(_response, "\" value=\"");
170   [_response appendContentHTMLAttributeValue:v];
171   WOResponse_AddChar(_response, '"');
172   
173   if ([self->disabled boolValueInComponent:[_ctx component]])
174     WOResponse_AddCString(_response, " disabled=\"disabled\"");
175   
176   [self appendExtraAttributesToResponse:_response inContext:_ctx];
177   if (self->otherTagString != nil) {
178     v = [self->otherTagString stringValueInComponent:[_ctx component]];
179     WOResponse_AddChar(_response, ' ');
180     WOResponse_AddString(_response, v);
181   }
182   WOResponse_AddEmptyCloseParens(_response, _ctx);
183 }
184
185 /* description */
186
187 - (NSString *)associationDescription {
188   NSMutableString *str;
189
190   str = [NSMutableString stringWithCapacity:128];
191   [str appendString:[super associationDescription]];
192   
193   if (self->action)   [str appendFormat:@" action=%@", self->action];
194   if (self->pageName) [str appendFormat:@" page=%@",   self->pageName];
195   
196   if (self->actionClass)
197     [str appendFormat:@" actionClass=%@", self->actionClass];
198   if (self->directActionName)
199     [str appendFormat:@" directAction=%@", self->directActionName];
200   return str;
201 }
202
203 @end /* WOSubmitButton */