]> err.no Git - sope/blob - sope-appserver/NGObjDOM/Dynamic.subproj/ODR_bind_datefield.m
2f832cb86aabbf4d6610cab50ba45aeee9fc7f9a
[sope] / sope-appserver / NGObjDOM / Dynamic.subproj / ODR_bind_datefield.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22
23 #include <NGObjDOM/ODNodeRenderer.h>
24
25 /*
26   Attributes:
27
28     day    - int
29     month  - int
30     year   - int
31     value  - string in format %Y-%m-%d
32   
33   Usage:
34     <var:datefield value="fromDate"/><br/>
35     <var:datefield value="toDate"/><br/>
36 */
37
38 @interface ODR_bind_datefield : ODNodeRenderer
39 @end
40
41 #include "common.h"
42
43 @implementation ODR_bind_datefield
44
45 - (BOOL)requiresFormForNode:(id)_domNode inContext:(WOContext *)_ctx {
46   return YES;
47 }
48
49 - (void)takeValuesForNode:(id)_node
50   fromRequest:(WORequest *)_req
51   inContext:(WOContext *)_ctx
52 {
53   int d, m, y;
54   NSString *s;
55   
56   [_ctx appendElementIDComponent:@"d"];
57   d = [[_req formValueForKey:[_ctx elementID]] intValue];
58   [_ctx deleteLastElementIDComponent];
59   
60   [_ctx appendElementIDComponent:@"m"];
61   m = [[_req formValueForKey:[_ctx elementID]] intValue];
62   [_ctx deleteLastElementIDComponent];
63   
64   [_ctx appendElementIDComponent:@"y"];
65   y = [[_req formValueForKey:[_ctx elementID]] intValue];
66   [_ctx deleteLastElementIDComponent];
67   
68   if (y < 100) y += 2000;
69   
70   if ([self isSettable:@"value" node:_node ctx:_ctx]) {
71     s = [NSString stringWithFormat:@"%i-%02i-%02i", y, m, d];
72     [self setString:s for:@"value" node:_node ctx:_ctx];
73   }
74   
75   if ([self isSettable:@"day"   node:_node ctx:_ctx])
76     [self setInt:d for:@"day"   node:_node ctx:_ctx];
77   if ([self isSettable:@"month" node:_node ctx:_ctx])
78     [self setInt:m for:@"month" node:_node ctx:_ctx];
79   if ([self isSettable:@"year"  node:_node ctx:_ctx])
80     [self setInt:y for:@"year"  node:_node ctx:_ctx];
81 }
82
83 - (void)appendNode:(id)_node
84   toResponse:(WOResponse *)_response
85   inContext:(WOContext *)_ctx
86 {
87   unsigned i;
88   NSCalendarDate *date;
89   NSString *s;
90   NSString *d,*m,*y;
91   
92   d = [self stringFor:@"day"   node:_node ctx:_ctx];
93   m = [self stringFor:@"month" node:_node ctx:_ctx];
94   y = [self stringFor:@"year"  node:_node ctx:_ctx];
95   
96   date = [self valueFor:@"value" node:_node ctx:_ctx];
97   
98   if ([date isKindOfClass:[NSCalendarDate class]]) {
99     s = [date descriptionWithCalendarFormat:@"%Y-%m-%s"];
100     
101     d = [NSString stringWithFormat:@"%i", [date dayOfMonth]];
102     m = [NSString stringWithFormat:@"%i", [date monthOfYear]];
103     y = [NSString stringWithFormat:@"%i", [date yearOfCommonEra]];
104   }
105   else {
106     s = [date stringValue];
107     date = nil;
108     
109     if ([s length] > 0) {
110       NSArray  *comps;
111       unsigned count;
112     
113       comps = [s componentsSeparatedByString:@"-"];
114       count = [comps count];
115
116       if (count > 0) y = [comps objectAtIndex:0];
117       if (count > 1) m = [comps objectAtIndex:1];
118       if (count > 2) d = [comps objectAtIndex:2];
119     }
120   }
121   
122   [_ctx appendElementIDComponent:@"d"];
123   {
124     [_response appendContentString:@"<select name=\""];
125     [_response appendContentHTMLAttributeValue:[_ctx elementID]];
126     [_response appendContentString:@"\" value=\""];
127     [_response appendContentHTMLAttributeValue:d];
128     [_response appendContentString:@"\">\n"];
129     
130     [_response appendContentString:@"<option value=\"\">-"];
131     
132     for (i = 1; i <= 31; i++) {
133       s = [NSString stringWithFormat:@"%d", i];
134       [_response appendContentString:@"<option value=\""];
135       [_response appendContentString:s];
136       if ((int)i == [d intValue])
137         [_response appendContentString:@"\" selected=\"selected\" />"];
138       else
139         [_response appendContentString:@"\" />"];
140       [_response appendContentString:s];
141       
142       /* XHTML */
143     }
144   
145     [_response appendContentString:@"</select>"];
146   }
147   [_ctx deleteLastElementIDComponent];
148   
149   [_ctx appendElementIDComponent:@"m"];
150   {
151     static NSString *months[12] = {
152       @"Jan", @"Feb", @"Mar", @"Apr", @"May", @"Jun",
153       @"Jul", @"Aug", @"Sep", @"Oct", @"Nov", @"Dec"
154     };
155     [_response appendContentString:@"<select name=\""];
156     [_response appendContentHTMLAttributeValue:[_ctx elementID]];
157     [_response appendContentString:@"\" value=\""];
158     [_response appendContentHTMLAttributeValue:m];
159     [_response appendContentString:@"\">\n"];
160
161     [_response appendContentString:@"<option value=\"\">-"];
162     
163     for (i = 1; i <= 12; i++) {
164       s = [NSString stringWithFormat:@"%d", i];
165       [_response appendContentString:@"<option value=\""];
166       [_response appendContentString:s];
167       if ((int)i == [m intValue])
168         [_response appendContentString:@"\" selected=\"selected\" />"];
169       else
170         [_response appendContentString:@"\" />"];
171       [_response appendContentString:months[i - 1]];
172       
173       /* XHTML */
174     }
175   
176     [_response appendContentString:@"</select>"];
177   }
178   [_ctx deleteLastElementIDComponent];
179   
180   [_ctx appendElementIDComponent:@"y"];
181   {
182     [_response appendContentString:@"<select name=\""];
183     [_response appendContentHTMLAttributeValue:[_ctx elementID]];
184     [_response appendContentString:@"\" value=\""];
185     [_response appendContentHTMLAttributeValue:y];
186     [_response appendContentString:@"\">\n"];
187     
188     [_response appendContentString:@"<option value=\"\">-"];
189     
190     for (i = 2001; i <= 2010; i++) {
191       s = [NSString stringWithFormat:@"%d", i];
192       [_response appendContentString:@"<option value=\""];
193       [_response appendContentString:s];
194       if ((int)i == [y intValue])
195         [_response appendContentString:@"\" selected=\"selected\" />"];
196       else
197         [_response appendContentString:@"\" />"];
198       [_response appendContentString:s];
199       
200       /* XHTML */
201     }
202     
203     [_response appendContentString:@"</select>"];
204   }
205   [_ctx deleteLastElementIDComponent];
206 }
207
208 @end /* ODR_bind_datefield */