]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/WOInput.m
Add libxml2-dev to libsope-xml4.7-dev deps
[sope] / sope-appserver / NGObjWeb / DynamicElements / WOInput.m
1 /*
2   Copyright (C) 2000-2006 SKYRIX Software AG
3   Copyright (C) 2006      Helge Hess
4
5   This file is part of SOPE.
6
7   SOPE is free software; you can redistribute it and/or modify it under
8   the terms of the GNU Lesser General Public License as published by the
9   Free Software Foundation; either version 2, or (at your option) any
10   later version.
11
12   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
13   WARRANTY; without even the implied warranty of MERCHANTABILITY or
14   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15   License for more details.
16
17   You should have received a copy of the GNU Lesser General Public
18   License along with SOPE; see the file COPYING.  If not, write to the
19   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20   02111-1307, USA.
21 */
22
23 #include "WOInput.h"
24 #include "WOElement+private.h"
25 #include "decommon.h"
26
27 @implementation WOInput
28
29 static BOOL takeValueDebugOn = YES;
30
31 + (int)version {
32   return [super version] + 0 /* v2 */;
33 }
34
35 + (void)initialize {
36   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
37   
38   NSAssert2([super version] == 2,
39             @"invalid superclass (%@) version %i !",
40             NSStringFromClass([self superclass]), [super version]);
41
42   if ((takeValueDebugOn = [ud boolForKey:@"WODebugTakeValues"]))
43     NSLog(@"WOInput: WODebugTakeValues on.");
44 }
45
46 - (id)initWithName:(NSString *)_name
47   associations:(NSDictionary *)_associations
48   template:(WOElement *)_rootChild
49 {
50   self = [super initWithName:_name associations:_associations
51                 template:_rootChild];
52   if (self) {
53     self->containsForm = YES;
54     self->name     = OWGetProperty(_associations, @"name");
55     self->value    = OWGetProperty(_associations, @"value");
56     self->disabled = OWGetProperty(_associations, @"disabled");
57     
58     /* type is defined by the element itself ... */
59     [(NSMutableDictionary *)_associations removeObjectForKey:@"type"];
60     
61     if ([_associations objectForKey:@"NAME"]) {
62       [self warnWithFormat:@"found 'NAME' association in element %@, "
63             @"'name' is probably the right thing.", _name];
64     }
65   }
66   return self;
67 }
68
69 - (void)dealloc {
70   [self->name     release];
71   [self->value    release];
72   [self->disabled release];
73   [super dealloc];
74 }
75
76 /* form support */
77
78 NSString *OWFormElementName(WOInput *self, WOContext *_ctx) {
79   NSString *name;
80   
81   if (self->name == nil)
82     return [_ctx elementID];
83   
84   if ((name = [self->name stringValueInComponent:[_ctx component]]) != nil)
85     return name;
86   
87   [[_ctx component]
88          warnWithFormat:
89                @"in element %@, 'name' attribute configured (%@),"
90                @"but no name assigned (using elementID as name) !",
91                self, self->name];
92   return [_ctx elementID];
93 }
94
95 /* taking form values */
96
97 - (id)parseFormValue:(id)_value inContext:(WOContext *)_ctx {
98   /* redefined in subclasses */
99   return _value;
100 }
101
102 - (void)takeValuesFromRequest:(WORequest *)_req inContext:(WOContext *)_ctx {
103   NSString *formName;
104   id formValue = nil;
105   
106   if ([self->disabled boolValueInComponent:[_ctx component]])
107     return;
108   
109   formName = OWFormElementName(self, _ctx);
110   if ((formValue = [_req formValueForKey:formName]) == nil)
111     // TODO: is this correct?
112     return;
113   
114   if (takeValueDebugOn) {
115     [self logWithFormat:
116             @"%s(%@): form=%@ ctx=%@ value=%@ ..", __PRETTY_FUNCTION__,
117             [_ctx elementID], formName, [_ctx contextID], formValue];
118   }
119   
120   if ([self->value isValueSettable]) {
121     formValue = [self parseFormValue:formValue inContext:_ctx];
122     [self->value setStringValue:formValue inComponent:[_ctx component]];
123   }
124   else if (self->value != nil) {
125     [self logWithFormat:
126             @"%s: form value is not settable: %@", __PRETTY_FUNCTION__,
127             self->value];
128   }
129 }
130
131 /* description */
132
133 - (NSString *)associationDescription {
134   NSMutableString *str;
135   
136   str = [NSMutableString stringWithCapacity:128];
137   if (self->value != nil) [str appendFormat:@" value=%@",    self->value];
138   if (self->name  != nil) [str appendFormat:@" name=%@",     self->name];
139   
140   if (self->disabled) [str appendFormat:@" disabled=%@", self->disabled];
141   return str;
142 }
143
144 @end /* WOInput */