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