]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOCheckBox.m
started TAL element builder
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOCheckBox.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 WOCheckBox : WOInput
26 {
27   // WODynamicElement: extraAttributes
28   // WODynamicElement: otherTagString
29   // inherited: name
30   // inherited: value
31   // inherited: disabled
32 @protected
33   WOAssociation *selection;
34   WOAssociation *checked;
35 }
36
37 @end /* WOCheckBox */
38
39 @implementation WOCheckBox
40
41 - (id)initWithName:(NSString *)_name
42   associations:(NSDictionary *)_config
43   template:(WOElement *)_c
44 {
45   if ((self = [super initWithName:_name associations:_config template:_c])) {
46     self->selection = OWGetProperty(_config, @"selection");
47     self->checked   = OWGetProperty(_config, @"checked");
48   }
49   return self;
50 }
51
52 - (void)dealloc {
53   [self->selection release];
54   [self->checked   release];
55   [super dealloc];
56 }
57
58 /* handling requests */
59
60 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
61   /*
62     Checkboxes are special in their form-value handling. If the form is
63     submitted and the checkbox is checked, a 'YES' value is transferred in the
64     request. If the checkbox is not-checked, no value is transferred at all !
65   */
66   id formValue;
67   
68   if ([self->disabled boolValueInComponent:[_ctx component]])
69     return;
70
71   formValue = [_rq formValueForKey:OWFormElementName(self, _ctx)];
72     
73   if ([self->checked isValueSettable]) {
74       [self->checked setBoolValue:formValue ? YES : NO
75                      inComponent:[_ctx component]];
76   }
77   
78   if ([self->value isValueSettable] && (formValue != nil))
79     [self->value setStringValue:formValue inComponent:[_ctx component]];
80 }
81
82 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
83   NSString *v;
84   BOOL     isChecked;
85   
86   if ([[_ctx request] isFromClientComponent])
87     return;
88
89   v         = [self->value   stringValueInComponent:[_ctx component]];
90   isChecked = [self->checked boolValueInComponent:[_ctx component]];
91     
92   WOResponse_AddCString(_response, "<input type=\"checkbox\" name=\"");
93   [_response appendContentHTMLAttributeValue:
94                OWFormElementName(self, _ctx)];
95   WOResponse_AddCString(_response, "\" value=\"");
96   [_response appendContentHTMLAttributeValue:([v length] > 0) ? v : @"1"];
97   WOResponse_AddCString(_response, "\"");
98   
99   if ([self->disabled boolValueInComponent:[_ctx component]]) {
100     WOResponse_AddCString(_response,
101                           (_ctx->wcFlags.allowEmptyAttributes
102                            ? " disabled" : " disabled=\"disabled\""));
103   }
104   
105   if (isChecked) {
106     WOResponse_AddCString(_response,
107                           (_ctx->wcFlags.allowEmptyAttributes 
108                            ? " checked" : " checked=\"checked\""));
109   }
110   
111   [self appendExtraAttributesToResponse:_response inContext:_ctx];
112   
113   if (self->otherTagString) {
114     WOResponse_AddChar(_response, ' ');
115     WOResponse_AddString(_response,
116                          [self->otherTagString stringValueInComponent:
117                                 [_ctx component]]);
118   }
119   
120   if (_ctx->wcFlags.xmlStyleEmptyElements) {
121     WOResponse_AddCString(_response, " />");
122   }
123   else {
124     WOResponse_AddCString(_response, ">");
125   }
126 }
127
128 /* description */
129
130 - (NSString *)associationDescription {
131   NSMutableString *str = nil;
132   str = [[NSMutableString alloc]
133                           initWithString:[super associationDescription]];
134
135   if (self->selection) [str appendFormat:@" selection=%@", self->selection];
136   if (self->checked)   [str appendFormat:@" checked=%@", self->checked];
137
138   return [str autorelease];
139 }
140
141 @end /* WOCheckBox */