]> err.no Git - scalable-opengroupware.org/blob - UI/Scheduler/UIxDatePicker.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1089 d1b88da0-ebda-0310...
[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   BOOL isDisabled;
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 - (id) init
47 {
48   if ((self = [super init]))
49     {
50       isDisabled = NO;
51     }
52
53   return self;
54 }
55
56 - (void)dealloc {
57   [self->dateID release];
58   [self->day    release];
59   [self->month  release];
60   [self->year   release];
61   [self->label  release];
62   [super dealloc];
63 }
64
65 /* Accessors */
66
67 - (void)setDateID:(NSString *)_dateID {
68   ASSIGNCOPY(self->dateID, _dateID);
69 }
70 - (NSString *)dateID {
71   return self->dateID;
72 }
73
74 - (void)setDay:(id)_day {
75   ASSIGN(self->day, _day);
76 }
77 - (id)day {
78     return self->day;
79 }
80 - (void)setMonth:(id)_month {
81   ASSIGN(self->month, _month);
82 }
83 - (id)month {
84     return self->month;
85 }
86 - (void)setYear:(id)_year {
87   ASSIGN(self->year, _year);
88 }
89 - (id)year {
90     return self->year;
91 }
92
93
94 - (void)setLabel:(NSString *)_label {
95   ASSIGNCOPY(self->label, _label);
96 }
97 - (NSString *)label {
98   return self->label;
99 }
100
101
102 /* formats */
103
104 - (BOOL)useISOFormats {
105   WOContext *ctx;
106   NSNumber  *useISOFormats;
107   
108   ctx           = [self context];
109   useISOFormats = [ctx valueForKey:@"useISOFormats"];
110   if (!useISOFormats) {
111       NSArray *languages = [ctx resourceLookupLanguages];
112       if (languages && [languages count] > 0) {
113         if ([[languages objectAtIndex:0] isEqualToString:@"French"]) {
114           useISOFormats = [NSNumber numberWithBool:NO];
115         }
116       }
117       if (!useISOFormats)
118         useISOFormats = [NSNumber numberWithBool:YES];
119       [ctx takeValue:useISOFormats forKey:@"useISOFormats"];
120  }
121   return [useISOFormats boolValue];
122 }
123
124 - (NSString *) formattedDateString
125 {
126   char buf[22];
127
128   if ([self useISOFormats]) {
129     sprintf(buf, "%04d-%02d-%02d",
130             [[self year]  intValue],
131             [[self month] intValue],
132             [[self day]  intValue]);
133   }
134   else {
135     sprintf(buf, "%02d/%02d/%04d",
136             [[self day] intValue],
137             [[self month] intValue],
138             [[self year] intValue]);
139   }
140   return [NSString stringWithCString:buf];
141 }
142
143 - (NSString *) dateFormat
144 {
145   return [self useISOFormats] ? @"%Y-%m-%d" : @"%d/%m/%Y";
146 }
147
148 - (NSString *) jsDateFormat
149 {
150   return [self useISOFormats] ? @"yyyy-mm-dd" : @"dd/mm/yyyy";
151 }
152
153 /* action */
154
155 - (void) takeValuesFromRequest: (WORequest *) _rq
156                      inContext: (WOContext *)_ctx
157 {
158   NSString       *dateString;
159   NSCalendarDate *d;
160
161   dateString = [_rq formValueForKey:[self dateID]];
162   if (dateString == nil) {
163     [self debugWithFormat:@"got no date string!"];
164     return;
165   }
166
167   d = [NSCalendarDate dateWithString:dateString
168                       calendarFormat:[self dateFormat]];
169   if (d == nil) {
170     [self warnWithFormat:@"Could not parse dateString: '%@'", 
171             dateString];
172   }
173
174   [self setDay:  [NSNumber numberWithInt:[d dayOfMonth]]];
175   [self setMonth:[NSNumber numberWithInt:[d monthOfYear]]];
176   [self setYear: [NSNumber numberWithInt:[d yearOfCommonEra]]];
177   
178   [super takeValuesFromRequest:_rq inContext:_ctx];
179 }
180
181 - (void) setDisabled: (BOOL) disabled
182 {
183   isDisabled = disabled;
184 }
185
186 - (BOOL) disabled
187 {
188   return isDisabled;
189 }
190
191 @end /* UIxDatePicker */