| 1 |
# Copyright (C) 2005 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, along with the |
|---|
| 6 |
# additional permission in the following paragraph. |
|---|
| 7 |
# |
|---|
| 8 |
# This software is distributed WITHOUT ANY WARRANTY, without even the |
|---|
| 9 |
# implied warranties of MERCHANTABILITY and FITNESS FOR A PARTICULAR |
|---|
| 10 |
# PURPOSE. See the GNU Affero General Public License for further |
|---|
| 11 |
# details. |
|---|
| 12 |
# |
|---|
| 13 |
# You should have received a copy of the GNU Affero General Public |
|---|
| 14 |
# License, version 3 along with this software. If not, see |
|---|
| 15 |
# <http://www.gnu.org/licenses/>. |
|---|
| 16 |
|
|---|
| 17 |
package SFLC::TimeTracker::Category; |
|---|
| 18 |
|
|---|
| 19 |
use strict; |
|---|
| 20 |
use warnings; |
|---|
| 21 |
|
|---|
| 22 |
require Exporter; |
|---|
| 23 |
#use AutoLoader qw(AUTOLOAD); |
|---|
| 24 |
|
|---|
| 25 |
our @ISA = qw(Exporter); |
|---|
| 26 |
|
|---|
| 27 |
our @EXPORT_OK = qw(Create Initialize); |
|---|
| 28 |
|
|---|
| 29 |
our @EXPORT = qw( ); |
|---|
| 30 |
|
|---|
| 31 |
our $VERSION = '0.01'; |
|---|
| 32 |
|
|---|
| 33 |
our $DATABASE; |
|---|
| 34 |
use Carp; |
|---|
| 35 |
|
|---|
| 36 |
use Date::Manip; |
|---|
| 37 |
|
|---|
| 38 |
use strict; |
|---|
| 39 |
use warnings; |
|---|
| 40 |
|
|---|
| 41 |
sub getDatabase { |
|---|
| 42 |
return $SFLC::TimeTracker::Input::DATABASE; |
|---|
| 43 |
} |
|---|
| 44 |
############################################################################### |
|---|
| 45 |
sub new { |
|---|
| 46 |
my $class = shift; |
|---|
| 47 |
my %args = @_; |
|---|
| 48 |
|
|---|
| 49 |
my $name = $args{name}; |
|---|
| 50 |
croak "Unable to create new category without a name" if not defined $name; |
|---|
| 51 |
|
|---|
| 52 |
croak "malformed category name, $name" unless $name =~ m%^/\S+(?:/\S+)*%; |
|---|
| 53 |
|
|---|
| 54 |
chomp $name; |
|---|
| 55 |
|
|---|
| 56 |
my $userH = $args{userHandle}; |
|---|
| 57 |
|
|---|
| 58 |
my $self = bless {}, $class; |
|---|
| 59 |
|
|---|
| 60 |
$self->{name} = "\L$name\E"; |
|---|
| 61 |
|
|---|
| 62 |
$self->{id} = $self->getDatabase()->categoryAliasLookup($userH, $name); |
|---|
| 63 |
$self->{id} = $self->getDatabase()->insertCategory($self) |
|---|
| 64 |
if (not defined $self->{id}); |
|---|
| 65 |
return $self; |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
sub prettyPrint { |
|---|
| 69 |
my $self = shift; |
|---|
| 70 |
return $self->get('name'); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
sub get { |
|---|
| 74 |
my($self, $field) = @_; |
|---|
| 75 |
if ($field !~ /^(?:name|id)$/) { |
|---|
| 76 |
croak "invalid field $field"; |
|---|
| 77 |
} |
|---|
| 78 |
return $self->{$field}; |
|---|
| 79 |
} |
|---|
| 80 |
1; |
|---|