]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WORadioButton.m
added some WebDrive WebDAV properties
[sope] / sope-appserver / NGObjWeb / DynamicElements / WORadioButton.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 "decommon.h"
24
25 @interface WORadioButton : WOInput
26 {
27   // WODynamicElement: extraAttributes
28   // WODynamicElement: otherTagString
29   // WOInput:    name
30   // WOInput:    value
31   // WOInput:    disabled
32 @protected
33   WOAssociation *selection;
34   WOAssociation *checked;
35 }
36
37 @end /* WORadioButton */
38
39 @implementation WORadioButton
40
41 - (id)initWithName:(NSString *)_name
42   associations:(NSDictionary *)_config
43   template:(WOElement *)_t 
44 {
45   if ((self = [super initWithName:_name associations:_config template:_t])) {
46     self->selection = OWGetProperty(_config, @"selection");
47     self->checked   = OWGetProperty(_config, @"checked");
48     
49     if ((self->checked != nil) && (self->value != nil)) {
50       NSLog(@"WARNING: specified both, 'checked' and 'value', "
51             @"associations for radio button!");
52     }
53   }
54   return self;
55 }
56
57 - (void)dealloc {
58   [self->selection release];
59   [self->checked   release];
60   [super dealloc];
61 }
62
63 /* handling requests */
64
65 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
66   WOComponent *sComponent;
67   id formValue;
68   
69   sComponent = [_ctx component];
70   if ([self->disabled boolValueInComponent:sComponent])
71     return;
72   
73   // TODO: this seems to have issues
74
75   // TODO: when a page is called with GET, this overwrites the selection!
76   //       - so we should probably only push the value for POST?
77   
78   formValue = [_req formValueForKey:OWFormElementName(self, _ctx)];
79   
80 #warning FIXME, radio button form handling
81   
82   if (self->checked != nil) {
83     // TODO: check needs element-ids?
84     if ([self->checked isValueSettable]) {
85       // TODO: this only checks element-IDs! In case a value is assigned, it
86       //       must check the value
87       [self->checked setBoolValue:[formValue isEqual:[_ctx elementID]]
88                      inComponent:sComponent];
89     }
90   }
91   
92   /*
93     TODO
94     
95     This needs to check the value and compare it with the value of the form,
96     otherwise all radio elements will push the value, multiple times because
97     radio buttons always have the same name!
98
99     Note that the actual result is usually OK, because all elements push the
100     _same_ value, the one from the form, not their own.
101   */
102   if ([self->selection isValueSettable])
103     [self->selection setValue:formValue inComponent:sComponent];
104 }
105
106 /* generating response */
107
108 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
109   WOComponent *sComponent;
110   NSString *lvalue;
111   
112   sComponent = [_ctx component];
113   lvalue = self->checked
114     ? [_ctx elementID]
115     : [self->value stringValueInComponent:sComponent];
116   
117   WOResponse_AddCString(_response, "<input type=\"radio\" name=\"");
118   [_response appendContentHTMLAttributeValue:OWFormElementName(self, _ctx)];
119   WOResponse_AddCString(_response, "\" value=\"");
120   [_response appendContentHTMLAttributeValue:lvalue];
121   WOResponse_AddCString(_response, "\"");
122   
123   if (self->checked != nil) {
124     if ([self->checked boolValueInComponent:sComponent]) {
125       WOResponse_AddCString(_response, 
126                             (_ctx->wcFlags.allowEmptyAttributes 
127                              ? " checked" : " checked=\"checked\""));
128     }
129   }
130   else {
131     id v, sel;
132     
133     v   = [self->value     valueInComponent:sComponent];
134     sel = [self->selection valueInComponent:sComponent];
135     if ((v == sel) || [v isEqual:sel]) {
136       WOResponse_AddCString(_response, 
137                             (_ctx->wcFlags.allowEmptyAttributes 
138                              ? " checked" : " checked=\"checked\""));
139     }
140   }
141   
142   if ([self->disabled boolValueInComponent:sComponent]) {
143     WOResponse_AddCString(_response, 
144                           (_ctx->wcFlags.allowEmptyAttributes
145                            ? " disabled" : " disabled=\"disabled\""));
146   }
147   
148   [self appendExtraAttributesToResponse:_response inContext:_ctx];
149   if (self->otherTagString != nil) {
150     lvalue = [self->otherTagString stringValueInComponent:sComponent];
151     WOResponse_AddChar(_response, ' ');
152     WOResponse_AddString(_response, lvalue);
153   }
154   
155   WOResponse_AddEmptyCloseParens(_response, _ctx);
156 }
157
158 /* description */
159
160 - (NSString *)associationDescription {
161   NSMutableString *str = nil;
162   
163   str = [[[super associationDescription] mutableCopy] autorelease];
164   if (self->selection) [str appendFormat:@" selection=%@", self->selection];
165   if (self->checked)   [str appendFormat:@" checked=%@", self->checked];
166   return str;
167 }
168
169 @end /* WORadioButton */