| 1 |
# Copyright (C) 2005, 2006 Software Freedom Law Center, Inc. |
|---|
| 2 |
# Author: Bradley M. Kuhn <bkuhn@softwarefreedom.org> |
|---|
| 3 |
# |
|---|
| 4 |
# This software gives you freedom; it is licensed to you under version |
|---|
| 5 |
# 3 of the GNU Affero General Public License. |
|---|
| 6 |
# |
|---|
| 7 |
# This software is distributed WITHOUT ANY WARRANTY, without even the |
|---|
| 8 |
# implied warranties of MERCHANTABILITY and FITNESS FOR A PARTICULAR |
|---|
| 9 |
# PURPOSE. See the GNU Affero General Public License for further |
|---|
| 10 |
# details. |
|---|
| 11 |
# |
|---|
| 12 |
# You should have received a copy of the GNU Affero General Public |
|---|
| 13 |
# License, version 3 along with this software. If not, see |
|---|
| 14 |
# <http://www.gnu.org/licenses/>. |
|---|
| 15 |
package SFLC::TimeTracker::Output::Entry; |
|---|
| 16 |
|
|---|
| 17 |
use strict; |
|---|
| 18 |
use warnings; |
|---|
| 19 |
|
|---|
| 20 |
use base qw(SFLC::TimeTracker::Output); |
|---|
| 21 |
|
|---|
| 22 |
use Lingua::EN::Inflect qw ( PL); |
|---|
| 23 |
use Date::Manip; |
|---|
| 24 |
|
|---|
| 25 |
sub getDatabase { |
|---|
| 26 |
return $SFLC::TimeTracker::Input::DATABASE; |
|---|
| 27 |
} |
|---|
| 28 |
############################################################################### |
|---|
| 29 |
sub new { |
|---|
| 30 |
my($class) = shift; |
|---|
| 31 |
|
|---|
| 32 |
my %input = @_; |
|---|
| 33 |
if (defined $input{entry}) { |
|---|
| 34 |
$input{object} = { id => $input{entry}->get('id'), |
|---|
| 35 |
userHandle => $input{entry}->get('userHandle'), |
|---|
| 36 |
entryAsReceived => $input{entry} |
|---|
| 37 |
}; |
|---|
| 38 |
# Note that we keep around the entry as we received it. We may |
|---|
| 39 |
# be asked later to printout the entry after it is gone from the database. |
|---|
| 40 |
} |
|---|
| 41 |
my $self = SFLC::TimeTracker::Output::new($class, %input); |
|---|
| 42 |
return $self; |
|---|
| 43 |
} |
|---|
| 44 |
############################################################################### |
|---|
| 45 |
sub get { |
|---|
| 46 |
my($self, $field) = @_; |
|---|
| 47 |
if ($field =~ /^(?:entry)$/) { |
|---|
| 48 |
# Be smart here. First, see if the entry is in the database. If it is, |
|---|
| 49 |
# We want the freshest version of it we can. It could have been deleted |
|---|
| 50 |
# subsequent to the creation of this objec, however, so we have to |
|---|
| 51 |
# sometimes use the cached version. |
|---|
| 52 |
my $obj = $self->SUPER::get('object'); |
|---|
| 53 |
my $dbEntry = |
|---|
| 54 |
$self->getDatabase()->getEntryById($obj->{userHandle}, $obj->{id}); |
|---|
| 55 |
return (defined $dbEntry and ref $dbEntry) ? $dbEntry |
|---|
| 56 |
: $obj->{entryAsReceived}; |
|---|
| 57 |
} else { |
|---|
| 58 |
return $self->SUPER::get($field); |
|---|
| 59 |
} |
|---|
| 60 |
} |
|---|
| 61 |
############################################################################### |
|---|
| 62 |
sub action ($$) { |
|---|
| 63 |
my($self, $actionData) = @_; |
|---|
| 64 |
|
|---|
| 65 |
my $entry = $self->get('entry'); |
|---|
| 66 |
my $userHandle = $entry->get('userHandle'); |
|---|
| 67 |
|
|---|
| 68 |
print "in Entry action with edge $actionData->{edge}\n"; |
|---|
| 69 |
if ($actionData->{edge} eq "abort") { |
|---|
| 70 |
$entry->remove(); |
|---|
| 71 |
$self->set('status', 'ABORTED'); |
|---|
| 72 |
} else { |
|---|
| 73 |
foreach my $key (keys %{$actionData}) { |
|---|
| 74 |
if ($entry->needs($key)) { |
|---|
| 75 |
$entry->set($key, $actionData->{$key}); |
|---|
| 76 |
} |
|---|
| 77 |
} |
|---|
| 78 |
} |
|---|
| 79 |
# Always allow data to flow downward through chains of entry nodes. |
|---|
| 80 |
return $actionData; |
|---|
| 81 |
} |
|---|
| 82 |
############################################################################### |
|---|
| 83 |
sub _getTimesAndCat ($$) { |
|---|
| 84 |
my($self, $entry) = @_; |
|---|
| 85 |
|
|---|
| 86 |
my $amountTime = (defined $entry->{amountTime}) ? |
|---|
| 87 |
Delta_Format($entry->{amountTime}, 1, |
|---|
| 88 |
'%ht ' . PL("hour", $entry->{amountTime})) : ""; |
|---|
| 89 |
# If we are going to say "0.0 hours, just redo it in minutes |
|---|
| 90 |
if ($amountTime =~ /^\s*0.0+\s+hour/) { |
|---|
| 91 |
$amountTime = Delta_Format($entry->{amountTime}, 1, |
|---|
| 92 |
'%mt ' . PL("minute", $entry->{amountTime})); |
|---|
| 93 |
} |
|---|
| 94 |
# If we are going to say "0.0 minutes, just redo it in seconds |
|---|
| 95 |
if ($amountTime =~ /^\s*0.0+\s+minute/) { |
|---|
| 96 |
$amountTime = Delta_Format($entry->{amountTime}, 2, |
|---|
| 97 |
'%st ' . PL("second", $entry->{amountTime})); |
|---|
| 98 |
} |
|---|
| 99 |
$amountTime =~ s/s\s*$// if ($amountTime =~ /^\s*1.0\s+/); |
|---|
| 100 |
$amountTime = undef if $amountTime =~ /^\s*$/; |
|---|
| 101 |
|
|---|
| 102 |
my $category = $entry->get('category'); |
|---|
| 103 |
$category = (defined $category) ? $category->prettyPrint() : "UNKNOWN"; |
|---|
| 104 |
my $dateOccurred = UnixDate($entry->{dateOccurred}, '%b %E') |
|---|
| 105 |
if defined $entry->{dateOccurred}; |
|---|
| 106 |
|
|---|
| 107 |
# FIXME: User Config fo rthe way time looks |
|---|
| 108 |
my $startTime = UnixDate($entry->{startTime}, '%i:%M%p') |
|---|
| 109 |
if defined $entry->{startTime}; |
|---|
| 110 |
$startTime =~ s/^\s+// if defined $startTime; |
|---|
| 111 |
|
|---|
| 112 |
my $endTime = UnixDate($entry->{endTime}, '%i:%M%p') |
|---|
| 113 |
if defined $entry->{endTime}; |
|---|
| 114 |
$endTime =~ s/^\s+// if defined $endTime; |
|---|
| 115 |
|
|---|
| 116 |
my $note = $entry->get('note'); |
|---|
| 117 |
|
|---|
| 118 |
$category .= " (with note: " . $note . ")" |
|---|
| 119 |
if defined $note and $note !~ /^\s*$/; |
|---|
| 120 |
|
|---|
| 121 |
return ($startTime, $endTime, $amountTime, $dateOccurred, $category); |
|---|
| 122 |
} |
|---|
| 123 |
############################################################################### |
|---|
| 124 |
sub prettyPrintIRC ($$) { |
|---|
| 125 |
my($self, $now) = @_; |
|---|
| 126 |
|
|---|
| 127 |
my $today = UnixDate($now, '%b %E'); |
|---|
| 128 |
|
|---|
| 129 |
my($startTime, $endTime, $amountTime, $dateOccurred, $category) = |
|---|
| 130 |
$self->_getTimesAndCat($self->get('entry')); |
|---|
| 131 |
|
|---|
| 132 |
# FIXME: THese PL's don't actually work because amountTime is formatted |
|---|
| 133 |
# as a Delta |
|---|
| 134 |
|
|---|
| 135 |
if ($self->status eq "ABORTED") { |
|---|
| 136 |
my $retStr = "Aborted entry"; |
|---|
| 137 |
$retStr .= " that started at $startTime" if defined $startTime; |
|---|
| 138 |
$retStr .= " (it would have lasted for $amountTime)" if defined $amountTime; |
|---|
| 139 |
$retStr .= "."; |
|---|
| 140 |
return $retStr; |
|---|
| 141 |
} |
|---|
| 142 |
my $retStr = "Did " . $self->status . ". "; |
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
my $type = $self->type; my $status = $self->status; |
|---|
| 146 |
|
|---|
| 147 |
if ($type eq "completed" |
|---|
| 148 |
or ($type eq "new" and ($status =~ /^ADDED/ |
|---|
| 149 |
or $status eq "ADJUSTED"))) { |
|---|
| 150 |
$retStr .= "Got $amountTime"; |
|---|
| 151 |
$retStr .= " (on $dateOccurred)" if $dateOccurred ne $today; |
|---|
| 152 |
$retStr .= " in ${category}."; |
|---|
| 153 |
} elsif ($type eq "new" and $status eq "STARTED") { |
|---|
| 154 |
$retStr .= "You're in ${category} (as of ${startTime})."; |
|---|
| 155 |
} elsif ($type eq "modified") { |
|---|
| 156 |
$retStr .= "Work on ${category} now starts at $startTime"; |
|---|
| 157 |
$retStr .= " (on $dateOccurred)" if $dateOccurred ne $today; |
|---|
| 158 |
$retStr .= "."; |
|---|
| 159 |
} elsif ($type =~ /abandoned$/) { |
|---|
| 160 |
$retStr .= "Work in ${category}, started at $startTime"; |
|---|
| 161 |
$retStr .= " (on $dateOccurred)" if $dateOccurred ne $today; |
|---|
| 162 |
$retStr .= " has been abandoned."; |
|---|
| 163 |
} else { |
|---|
| 164 |
$retStr .= "REPORT THIS: output for $type not handled."; |
|---|
| 165 |
} |
|---|
| 166 |
return $retStr; |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
1; |
|---|
| 170 |
__END__ |
|---|
| 171 |
|
|---|
| 172 |
# |
|---|
| 173 |
# Local variables: |
|---|
| 174 |
# compile-command: "perl -I ../../../../Modules -c Entry.pm" |
|---|
| 175 |
# End: |
|---|