Software Freedom Law Center

root/trunk/antimatter/tim/Modules/SFLC/TimeTracker/Entry.pm

Revision 53, 7.2 kB (checked in by bkuhn, 9 months ago)
  • Added SFLC's internally developed tim bot released under AGPLv3
Line 
1 # Entry.pm                                                                 -*- Perl -*-
2 #  Basic Time Entry object
3 # Copyright (C) 2005, 2006   Software Freedom Law Center, Inc.
4 #  Author: Bradley M. Kuhn <bkuhn@softwarefreedom.org>
5 #
6 #  This software gives you freedom; it is licensed to you under version
7 #  3 of the GNU Affero General Public License.
8 #
9 #  This software is distributed WITHOUT ANY WARRANTY, without even the
10 #  implied warranties of MERCHANTABILITY and FITNESS FOR A PARTICULAR
11 #  PURPOSE.  See the GNU Affero General Public License for further
12 #  details.
13 #
14 # You should have received a copy of the GNU Affero General Public
15 # License, version 3 along with this software.  If not, see
16 # <http://www.gnu.org/licenses/>.
17
18 package SFLC::TimeTracker::Entry;
19
20 use strict;
21 use warnings;
22
23 require Exporter;
24 #use AutoLoader qw(AUTOLOAD);
25
26 our @ISA = qw(Exporter);
27
28 our @EXPORT_OK = qw(Create Initialize);
29
30 our @EXPORT = qw( );
31
32 our $VERSION = '0.01';
33
34 our $DATABASE;
35 use Carp;
36
37 use Date::Manip;
38
39 ###############################################################################
40
41 =head1 NAME
42
43 SFLC::TimeTrack::Entry - Basic entry object for time tracker
44
45 =head1 SYNOPSIS
46
47   use SFLC::TimeTrack::Entry;
48
49   my $database = new SFLC::TimeTrack::Entry($database)
50
51 =head1 DESCRIPTION
52
53
54 =head2 EXPORT
55
56 None.
57
58 =head1 SEE ALSO
59
60 SFLC::TimeTrack::Input
61
62
63 =head1 EXTERNAL FUNCTIONS
64
65 =over
66
67 =cut
68
69 ###############################################################################
70
71 =item new
72
73 usage: new($databse)
74
75 Instaniates a new object.
76
77 =head2 ARGUMENTS
78
79 =over
80
81 =item C<$method>
82
83 Storage method used.  Currently, the only valid C<$METHOD> is "MLDBM".  In
84 the future, I hope to implement a true database storage method.
85
86 =item C<$OPTION1>
87
88 For MLDBM, C<$option1> is the path to the file that will store the
89 completed time data.
90
91 =item C<$option2>
92
93 For MLDBM, C<$option2> is the path to the pending data (tasks started but
94 never closed).
95
96 =back
97
98 =cut
99
100 use strict;
101 use warnings;
102
103
104 sub new {
105   my($class) = @_;
106
107   my $self = {};  bless $self, $class;
108
109   $self->{needToComplete} = {};
110
111   return $self;
112 }
113
114 sub Initialize {
115   my($database) = shift;
116   $DATABASE = $database;
117 }
118 sub neededList {
119   my $self = shift;
120   return keys %{$self->{needToComplete}};
121 }
122
123 sub isComplete {
124   my $self = shift;
125   return ( (keys %{$self->{needToComplete}}) == 0);
126 }
127
128 sub needs {
129   my $self = shift;
130   my $what = shift;
131   if (not defined $what) {
132     wantarray ? return $self->neededList() : return $self->isComplete();
133  } else {
134    return defined $self->{needToComplete}{$what};
135  }
136 }
137 sub rebuildNeedComplete {
138   my $self = shift;
139   $self->{needToComplete} = {};
140   if (not defined $self->{category} or $self->{category} =~ /^\s*$/) {
141     $self->{needToComplete}{category} = 1;
142   }
143
144   if (defined $self->{startTime} and not defined $self->{endTime}) {
145     $self->{needToComplete}{endTime} = 1;
146   } elsif (defined $self->{amountTime} and not defined $self->{dateOccurred}) {
147     $self->{needToComplete}{dateOccurred} = 1;
148   } elsif (not defined $self->{amountTime} and defined $self->{dateOccurred}) {
149     $self->{needToComplete}{amountTime} = 1;
150   }
151   if (defined $self->{startTime} and defined $self->{endTime} and
152       not defined $self->{amountTime}) {
153     $self->{amountTime} = DateCalc($self->{startTime}, $self->{endTime});
154     croak "unable to calculate date detla between $self->{startTime} $self->{endTime}"
155       unless defined $self->{amountTime} and $self->{amountTime} !~ /^\s*$/;
156     delete $self->{needToComplete}{amountTime};
157   }
158   if (defined $self->{startTime} and not defined $self->{dateOccurred}) {
159     delete $self->{needToComplete}{dateOccurred};
160     $self->{dateOccurred} = UnixDate($self->{startTime}, "%Y-%m-%d");
161   }
162 }
163 sub set {
164   my($self, $field, $value) = @_;
165
166   unless ($field =~ /^endTime|startTime|dateOccurred|amountTime|category|lastReminded|note/) {
167     carp "Unable to set field $field";
168     return;
169   }
170   my $oldValue = $value;
171   $value = $self->getDatabase()->getCategoryHandleForUserHandle($oldValue,
172                                                      $self->{userHandle})
173     if ($field eq "category");
174
175   croak "$oldValue does not match to a category in the database"
176     if not defined $value;
177
178   $self->{$field} = $value;
179   $self->rebuildNeedComplete;
180   $self->{inDB} = $self->getDatabase()->updateEntry($self->{inDB}, $self);
181 }
182 sub get {
183   my($self, $field) = @_;
184
185   unless ($field =~ /^endTime|startTime|dateOccurred|amountTime|category|lastReminded|id|userHandle|note/) {
186     carp "Unable to get field $field";
187     return;
188   }
189   return ($field eq 'category' and defined $self->{'category'}) ?
190     ($self->getDatabase()->getCategory($self->{'category'})) :
191     $self->{$field};
192 }
193
194
195 sub remove {
196   my($self, $field, $value) = @_;
197
198   $self->{inDB} = $self->getDatabase()->removeEntry($self->{inDB}, $self);
199 }
200 sub getDatabase {
201   return $DATABASE;
202 }
203 ###############################################################################
204
205 =item Create
206
207 usage: Create($database, startTime => DATE_MANIP_VALUE,
208                          endTime => DATE_MANIP_VALUE,
209                          amountTime => DATE_MANIP_DELTA_VALUE,
210                          dateOccurred => DATE_MANIP_VALUE,
211                          userHandle => DB_USER_HANDLE,
212                          category => DB_CATEGORY_HANDLE,
213                          note => FREE_FORM_NOTE
214                       )
215
216 Creates a new time entry, using values given, and returning the object.
217 'username' is required.  'category' is not required, but if given,
218 DB_CATEGORY_HANDLE must be a valid category handle returned by
219 SFLC::TimeTracker::DB.
220
221 All dates must already be prepared into canonical Date::Manip formats.
222 Note that 'amountTime' is a DELTA format.
223
224 If the following items are true, it will be placed in the "main" database:
225
226 =over
227
228
229 =item Either both startTime and  endTime are given (and are valid), or
230              both amountTime and dateOccurred are given (and are valid).
231
232 =item username is valid.
233
234 =item category is given and is valid.
235
236 =back
237
238 Otherwise, then entry is inserted into the pending database.
239
240
241 The caller should request the array C<$object->needToComplete> for a list
242 of what is needed for completion.  If this list is empty, the entry is complete.
243
244
245 =cut
246
247 sub Create  {
248   my(%values) = @_;
249
250   my $self;
251   $self = new SFLC::TimeTracker::Entry;
252   $values{userHandle} = $self->getDatabase()->getUserHandle($values{username})
253     if (defined $values{username} and not defined $values{userHandle});
254
255   foreach my $req (qw/userHandle/) {
256     croak "missing field $req for Entry creation" if not defined $values{$req};
257   }
258
259   foreach my $field (qw/startTime endTime amountTime userHandle/) {
260     $self->{$field} = $values{$field} if defined $values{$field};
261   }
262
263   $self->{dateOccurred} = UnixDate($values{dateOccurred}, "%Y-%m-%d")
264     if defined $values{dateOccurred};
265
266   foreach my $val (qw/startTime endTime amountTime dateOccurred userHandle/) {
267     $self->{$val} = undef if defined $self->{$val} and $self->{$val} =~ /^\s*$/;
268   }
269
270   $self->{category} = $values{category} if (defined $values{category});
271
272   $self->rebuildNeedComplete;
273
274   $self->{inDB} =  $self->isComplete() ? "main" : "pending";
275   $self->{id} = $self->getDatabase()->insertEntry($self->{inDB}, $self);
276
277   return $self;
278 }
279
280 1;
281 __END__
282
283 #
284 # Local variables:
285 # compile-command: "perl -I ../../../Modules -c Entry.pm"
286 # End:
Note: See TracBrowser for help on using the browser.

SFLC Main Page

[frdm] Support SFLC