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