]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOInput.m
renamed common.h file in DynamicElements
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOInput.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 "WOElement+private.h"
24 #include "decommon.h"
25
26 @implementation WOInput
27
28 static BOOL takeValueDebugOn = NO;
29
30 + (int)version {
31   return [super version] + 0 /* v2 */;
32 }
33
34 + (void)initialize {
35   NSAssert2([super version] == 2,
36             @"invalid superclass (%@) version %i !",
37             NSStringFromClass([self superclass]), [super version]);
38 }
39
40 - (id)initWithName:(NSString *)_name
41   associations:(NSDictionary *)_associations
42   template:(WOElement *)_rootChild
43 {
44   self = [super initWithName:_name associations:_associations
45                 template:_rootChild];
46   if (self) {
47     self->containsForm = YES;
48     self->name     = OWGetProperty(_associations, @"name");
49     self->value    = OWGetProperty(_associations, @"value");
50     self->disabled = OWGetProperty(_associations, @"disabled");
51     
52     /* type is defined by the element itself ... */
53     [(NSMutableDictionary *)_associations removeObjectForKey:@"type"];
54     
55     if ([_associations objectForKey:@"NAME"]) {
56       NSLog(@"WARNING: found 'NAME' association in element %@, "
57             @"'name' is probably the right thing ..",
58             self);
59     }
60   }
61   return self;
62 }
63
64 - (void)dealloc {
65   [self->name     release];
66   [self->value    release];
67   [self->disabled release];
68   [super dealloc];
69 }
70
71 /* form support */
72
73 NSString *OWFormElementName(WOInput *self, WOContext *_ctx) {
74   NSString *name;
75   
76   if (self->name == nil)
77     return [_ctx elementID];
78   
79   if ((name = [self->name stringValueInComponent:[_ctx component]]) != nil)
80     return name;
81   
82   [[_ctx component]
83          logWithFormat:
84                @"WARNING: in element %@, 'name' attribute configured (%@),"
85                @"but no name assigned (using elementID as name) !",
86                self, self->name];
87   return [_ctx elementID];
88 }
89
90 /* taking form values */
91
92 - (id)parseFormValue:(id)_value inContext:(WOContext *)_ctx {
93   /* redefined in subclasses */
94   return _value;
95 }
96
97 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
98   NSString *formName;
99   id formValue = nil;
100   
101   if ([self->disabled boolValueInComponent:[_ctx component]])
102     return;
103   
104   formName = OWFormElementName(self, _ctx);
105   if ((formValue = [_req formValueForKey:formName]) == nil)
106     // TODO: is this correct?
107     return;
108   
109   if (takeValueDebugOn) {
110     [self logWithFormat:
111             @"%s(%@): form=%@ ctx=%@ value=%@ ..", __PRETTY_FUNCTION__,
112             [_ctx elementID], formName, [_ctx contextID], formValue];
113   }
114   
115   if ([self->value isValueSettable]) {
116     formValue = [self parseFormValue:formValue inContext:_ctx];
117     [self->value setStringValue:formValue inComponent:[_ctx component]];
118   }
119   else if (self->value != nil) {
120     [self logWithFormat:
121             @"%s: form value is not settable: %@", __PRETTY_FUNCTION__,
122             self->value];
123   }
124 }
125
126 /* description */
127
128 - (NSString *)associationDescription {
129   NSMutableString *str;
130   
131   str = [NSMutableString stringWithCapacity:128];
132   if (self->value)    [str appendFormat:@" value=%@",    self->value];
133   if (self->name)     [str appendFormat:@" name=%@",     self->name];
134   if (self->disabled) [str appendFormat:@" disabled=%@", self->disabled];
135   return str;
136 }
137
138 @end /* WOInput */