]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/Scheduler/UIxDatePicker.m
Fixed SOGo Bug #1136
[scalable-opengroupware.org] / SOGo / UI / Scheduler / UIxDatePicker.m
1 /*
2   Copyright (C) 2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
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 <NGObjWeb/WOComponent.h>
24
25 @class NSString, NSCalendarDate;
26
27 @interface UIxDatePicker : WOComponent
28 {
29   NSString       *dateID;
30   id             day;
31   id             month;
32   id             year;
33   NSString       *label;
34 }
35
36 - (NSString *)dateID;
37 - (NSString *)dateFormat;
38 - (NSString *)jsDateFormat;
39 - (BOOL)useISOFormats;
40 @end
41
42 #include "common.h"
43
44 @implementation UIxDatePicker
45
46 - (void)dealloc {
47   [self->dateID release];
48   [self->day    release];
49   [self->month  release];
50   [self->year   release];
51   [self->label  release];
52   [super dealloc];
53 }
54
55 /* Accessors */
56
57 - (void)setDateID:(NSString *)_dateID {
58   ASSIGNCOPY(self->dateID, _dateID);
59 }
60 - (NSString *)dateID {
61   return self->dateID;
62 }
63
64 - (void)setDay:(id)_day {
65     ASSIGN(self->day, _day);
66 }
67 - (id)day {
68     return self->day;
69 }
70 - (void)setMonth:(id)_month {
71     ASSIGN(self->month, _month);
72 }
73 - (id)month {
74     return self->month;
75 }
76 - (void)setYear:(id)_year {
77     ASSIGN(self->year, _year);
78 }
79 - (id)year {
80     return self->year;
81 }
82
83
84 - (void)setLabel:(NSString *)_label {
85   ASSIGNCOPY(self->label, _label);
86 }
87 - (NSString *)label {
88   return self->label;
89 }
90
91
92 /* formats */
93
94 - (BOOL)useISOFormats {
95   WOContext *ctx;
96   NSNumber  *useISOFormats;
97   
98   ctx           = [self context];
99   useISOFormats = [ctx valueForKey:@"useISOFormats"];
100   if (!useISOFormats) {
101       NSArray *languages = [ctx resourceLookupLanguages];
102       if (languages && [languages count] > 0) {
103         if ([[languages objectAtIndex:0] isEqualToString:@"French"]) {
104           useISOFormats = [NSNumber numberWithBool:NO];
105         }
106       }
107       if (!useISOFormats)
108         useISOFormats = [NSNumber numberWithBool:YES];
109       [ctx takeValue:useISOFormats forKey:@"useISOFormats"];
110  }
111   return [useISOFormats boolValue];
112 }
113 - (NSString *)formattedDateString {
114   if ([self useISOFormats]) {
115     return [NSString stringWithFormat:@"%d-%02d-%02d",
116                                         [[self year] intValue],
117                                         [[self month] intValue],
118                                         [[self day] intValue]];
119   }
120   else {
121     return [NSString stringWithFormat:@"%02d/%02d/%04d",
122                                         [[self day] intValue],
123                                         [[self month] intValue],
124                                         [[self year] intValue]];
125   }
126 }
127
128 - (NSString *)dateFormat {
129   if ([self useISOFormats]) {
130     return @"%Y-%m-%d";
131   }
132   else {
133     return @"%d/%m/%Y";
134   }
135 }
136
137 - (NSString *)jsDateFormat {
138   if ([self useISOFormats]) {
139     return @"yyyy-mm-dd";
140   }
141   else {
142     return @"dd/mm/yyyy";
143   }
144 }
145
146
147 /* URLs */
148
149 - (NSString *)calendarPageURL {
150   WOResourceManager *rm;
151   WOContext *ctx;
152   NSArray   *languages;
153
154   if ((rm = [self resourceManager]) == nil)
155     rm = [[WOApplication application] resourceManager];
156   if (rm == nil)
157     [self warnWithFormat:@"missing resource manager!"];
158
159   ctx       = [self context];
160 #if 0
161   languages = [ctx resourceLookupLanguages];
162 #else
163 #warning !! FIX SoProduct to enable localizable resource, then disable this!
164   languages = nil;
165 #endif
166     
167   return [rm urlForResourceNamed:@"skycalendar.html"
168              inFramework:nil
169              languages:languages
170              request:[ctx request]];
171 }
172
173 /* JavaScript */
174
175 - (NSString *)jsPopup {
176   return [NSString stringWithFormat:@"javascript:calendar_%@.popup()",
177         [self dateID]];
178 }
179
180 - (NSString *)jsCode {
181   static NSString *code = \
182     @"var calendar_%@ = new skycalendar(document.getElementById('%@'));\n"
183     @"calendar_%@.setCalendarPage('%@');\n"
184     @"calendar_%@.setDateFormat('%@');\n";
185
186   return [NSString stringWithFormat:code,
187         self->dateID,
188         self->dateID,
189         self->dateID,
190         [self calendarPageURL],
191         self->dateID,
192         [self jsDateFormat]];
193 }
194
195 /* action */
196
197 - (void)takeValuesFromRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
198   NSString       *dateString;
199   NSCalendarDate *d;
200
201   dateString = [_rq formValueForKey:[self dateID]];
202   if (dateString == nil) {
203     [self debugWithFormat:@"got no date string!"];
204     return;
205   }
206
207   d = [NSCalendarDate dateWithString:dateString
208                       calendarFormat:[self dateFormat]];
209   if (d == nil)
210     [self warnWithFormat:@"Could not parse dateString: '%@'", 
211             dateString];
212   [self setDay:[NSNumber numberWithInt:[d dayOfMonth]]];
213   [self setMonth:[NSNumber numberWithInt:[d monthOfYear]]];
214   [self setYear:[NSNumber numberWithInt:[d yearOfCommonEra]]];
215   
216   [super takeValuesFromRequest:_rq inContext:_ctx];
217 }
218
219 @end /* UIxDatePicker */