[Koha-patches] [PATCH] New C4 Modules
Daniel Sweeney
daniel.sweeney at liblime.com
Tue Feb 3 23:02:03 CET 2009
From: John Beppu <john.beppu at liblime.com>
* C4::Category
- patron categories as objects
- an all() method to return all the categories as a list
* C4::ItemType
- itemtypes as objects
- an all() method to return all the item types as a list
* C4::ItemCirculationAlertPreference
- wrapper around the item_circulation_alert_preference table
- create() and delete() methods for easy manipulation of the preferences
- I regret giving it such a long name.
- The item_type column should've been named itemtype to make it
more similar to pre-existing tables. Oh well.
(C4::Category and C4::ItemType were made so that I wouldn't have to
write SQL inside my CGI scripts.
Their main purpose is to provide an easy way to get a list of
patron categories and item types.
Do not be fooled by the amount of POD.
There's actually very little code in these modules.)
Signed-off-by: Daniel Sweeney <daniel.sweeney at liblime.com>
---
C4/Category.pm | 155 +++++++++++++++++
C4/ItemCirculationAlertPreference.pm | 317 ++++++++++++++++++++++++++++++++++
C4/ItemType.pm | 159 +++++++++++++++++
3 files changed, 631 insertions(+), 0 deletions(-)
create mode 100644 C4/Category.pm
create mode 100644 C4/ItemCirculationAlertPreference.pm
create mode 100644 C4/ItemType.pm
diff --git a/C4/Category.pm b/C4/Category.pm
new file mode 100644
index 0000000..760b396
--- /dev/null
+++ b/C4/Category.pm
@@ -0,0 +1,155 @@
+package C4::Category;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA 02111-1307 USA
+
+use strict;
+use warnings;
+use C4::Context;
+
+our $AUTOLOAD;
+
+
+
+
+=head1 NAME
+
+C4::Category - objects from the categories table
+
+=head1 SYNOPSIS
+
+ use C4::Category;
+ my @categories = C4::Category->all;
+ print join("\n", map { $_->description } @categories), "\n";
+
+=head1 DESCRIPTION
+
+Objects of this class represent a row in the C<categories> table.
+
+Currently, the bare minimum for using this as a read-only data source has
+been implemented. The API was designed to make it easy to transition to
+an ORM later on.
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 C4::Category->new(\%opts)
+
+Given a hashref, a new (in-memory) C4::Category object will be instantiated.
+The database is not touched.
+
+=cut
+
+sub new {
+ my ($class, $opts) = @_;
+ bless $opts => $class;
+}
+
+
+
+
+=head3 C4::Category->all
+
+This returns all the categories as objects. By default they're ordered by
+C<description>.
+
+=cut
+
+sub all {
+ my ($class) = @_;
+ my $dbh = C4::Context->dbh;
+ return map { $class->new($_) } @{$dbh->selectall_arrayref(
+ # The categories table is small enough for
+ # `SELECT *` to be harmless.
+ "SELECT * FROM categories ORDER BY description",
+ { Slice => {} },
+ )};
+}
+
+
+
+
+=head2 Object Methods
+
+These are read-only accessors for attributes of a C4::Category object.
+
+=head3 $category->categorycode
+
+=head3 $category->description
+
+=head3 $category->enrolmentperiod
+
+=head3 $category->upperagelimit
+
+=head3 $category->dateofbirthrequired
+
+=head3 $category->finetype
+
+=head3 $category->bulk
+
+=head3 $category->enrolmentfee
+
+=head3 $category->overduenoticerequired
+
+=head3 $category->issuelimit
+
+=head3 $category->reservefee
+
+=head3 $category->category_type
+
+=cut
+
+sub AUTOLOAD {
+ my $self = shift;
+ my $attr = $AUTOLOAD;
+ $attr =~ s/.*://;
+ if (exists $self->{$attr}) {
+ return $self->{$attr};
+ } else {
+ return undef;
+ }
+}
+
+
+
+
+=head1 SEE ALSO
+
+The following modules make reference to the C<categories> table.
+
+L<C4::Members>, L<C4::Overdues>, L<C4::Reserves>
+
+
+=head1 AUTHOR
+
+John Beppu <john.beppu at liblime.com>
+
+=cut
+
+1;
+
+# Local Variables: ***
+# mode: cperl ***
+# indent-tabs-mode: nil ***
+# cperl-close-paren-offset: -4 ***
+# cperl-continued-statement-offset: 4 ***
+# cperl-indent-level: 4 ***
+# cperl-indent-parens-as-block: t ***
+# cperl-tab-always-indent: nil ***
+# End: ***
+# vim:tabstop=8 softtabstop=4 shiftwidth=4 shiftround expandtab
diff --git a/C4/ItemCirculationAlertPreference.pm b/C4/ItemCirculationAlertPreference.pm
new file mode 100644
index 0000000..df231a4
--- /dev/null
+++ b/C4/ItemCirculationAlertPreference.pm
@@ -0,0 +1,317 @@
+package C4::ItemCirculationAlertPreference;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA 02111-1307 USA
+
+use strict;
+use warnings;
+use C4::Context;
+use Carp qw(carp croak);
+
+our $AUTOLOAD;
+
+# helper function for validating \%opts
+our $valid = sub {
+ my $opts = shift;
+ for (qw(branchcode categorycode item_type)) {
+ exists($opts->{$_}) || croak("'$_' is a required parameter.");
+ }
+};
+
+
+
+
+=head1 NAME
+
+C4::ItemCirculationAlertPreference - manage preferences for sending alerts
+
+=head1 SYNOPSIS
+
+Basics:
+
+ use C4::ItemCirculationAlertPreference;
+
+ # a short-cut to reduce typing the long package name over and over again
+ my $preferences = 'C4::ItemCirculationAlertPreference';
+
+Creating Rules:
+
+ my $pref = $preferences->create({
+ branchcode => 'CPL',
+ categorycode => 'YA',
+ item_type => 'BK',
+ });
+
+Deleting Rules:
+
+ $preferences->delete({
+ branchcode => 'CPL',
+ categorycode => 'YA',
+ item_type => 'BK',
+ });
+
+=head1 DESCRIPTION
+
+This class is used to manage the preferences for when an alert may be sent. By
+default, item circulation alerts are enabled for every B<branch>, B<patron
+category> and B<item type>.
+
+However, if you would like to prevent item circulation alerts from being sent
+for any combination of these 3 variables, a preference can be inserted into the
+C<item_circulation_alert_preferences> table to make that a policy.
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 C4::ItemCirculationAlertPreference->new(\%opts)
+
+This is a constructor for an in-memory C4::ItemCirculationAlertPreference
+object. The database is not affected by this method.
+
+=cut
+
+sub new {
+ my ($class, $opts) = @_;
+ bless $opts => $class;
+}
+
+
+
+
+=head3 C4::ItemCirculationAlertPreference->create(\%opts)
+
+This will find or create an item circulation alert preference. You must pass
+it a B<branchcode>, B<categorycode>, and B<item_type>.
+
+=cut
+
+sub create {
+ my ($class, $opts) = @_;
+ $valid->($opts);
+ my $dbh = C4::Context->dbh;
+ my $prefs = $dbh->selectall_arrayref(
+ "SELECT id, branchcode, categorycode, item_type
+ FROM item_circulation_alert_preferences
+ WHERE branchcode = ?
+ AND categorycode = ?
+ AND item_type = ?",
+ { Slice => {} },
+ $opts->{branchcode},
+ $opts->{categorycode},
+ $opts->{item_type},
+ );
+ if (@$prefs) {
+ return $class->new($prefs->[0]);
+ } else {
+ my $success = $dbh->do(
+ "INSERT INTO item_circulation_alert_preferences
+ (branchcode, categorycode, item_type) VALUES (?, ?, ?)",
+ {},
+ $opts->{branchcode},
+ $opts->{categorycode},
+ $opts->{item_type},
+ );
+ if ($success) {
+ my $self = {
+ id => $dbh->last_insert_id(undef, undef, undef, undef),
+ branchcode => $opts->{branchcode},
+ categorycode => $opts->{categorycode},
+ item_type => $opts->{item_type},
+ };
+ return $class->new($self);
+ } else {
+ carp $dbh->errstr;
+ return undef;
+ }
+ }
+}
+
+
+
+
+=head3 C4::ItemCirculationAlertPreference->delete(\%opts)
+
+Delete an item circulation alert preference. You can delete by either passing
+in an B<id> or passing in a B<branchcode>, B<categorycode>, B<item_type>
+triplet.
+
+=cut
+
+sub delete {
+ my ($class, $opts) = @_;
+ my $dbh = C4::Context->dbh;
+ if ($opts->{id}) {
+ $dbh->do(
+ "DELETE FROM item_circulation_alert_preferences WHERE id = ?",
+ {},
+ $opts->{id}
+ );
+ } else {
+ $valid->($opts);
+ $dbh->do(
+ "DELETE FROM item_circulation_alert_preferences
+ WHERE branchcode = ?
+ AND categorycode = ?
+ AND item_type = ?",
+ {},
+ $opts->{branchcode},
+ $opts->{categorycode},
+ $opts->{item_type}
+ );
+ }
+}
+
+
+
+
+=head3 C4::ItemCirculationAlertPreference->is_enabled_for(\%opts)
+
+Based on the existing preferences in the C<item_circulation_alert_preferences>
+table, can an alert be sent for the given B<branchcode>, B<categorycode>, and
+B<itemtype>?
+
+B<Example>:
+
+ my $alert = 'C4::ItemCirculationAlertPreference';
+ my $conditions = {
+ branchcode => 'CPL',
+ categorycode => 'IL',
+ item_type => 'MU',
+ };
+
+ if ($alert->is_enabled_for($conditions)) {
+ # ...
+ }
+
+=cut
+
+sub is_enabled_for {
+ my ($class, $opts) = @_;
+ $valid->($opts);
+ my $dbh = C4::Context->dbh;
+
+ # Does a preference exist to block this alert?
+ my $query = qq{
+ SELECT id
+ FROM item_circulation_alert_preferences
+ WHERE (branchcode = ? OR branchcode = '*')
+ AND (categorycode = ? OR categorycode = '*')
+ AND (item_type = ? OR item_type = '*')
+ };
+
+ my $preferences = $dbh->selectall_arrayref(
+ $query,
+ { },
+ $opts->{branchcode},
+ $opts->{categorycode},
+ $opts->{item_type},
+ );
+
+ # If any preferences showed up, we are NOT enabled.
+ if (@$preferences) {
+ return undef;
+ } else {
+ return 1;
+ }
+}
+
+
+
+
+=head3 C4::ItemCirculationAlertPreference->find({ branchcode => $bc })
+
+This method returns all the item circulation alert preferences for a given
+branch.
+
+B<Example>:
+
+ my @branch_prefs = C4::ItemCirculationAlertPreference->find({
+ branchcode => 'CPL',
+ });
+
+=cut
+
+sub find {
+ my ($class, $where) = @_;
+ my $dbh = C4::Context->dbh;
+ my $query = qq{
+ SELECT id, branchcode, categorycode, item_type
+ FROM item_circulation_alert_preferences
+ WHERE branchcode = ?
+ ORDER BY categorycode, item_type
+ };
+ return map { $class->new($_) } @{$dbh->selectall_arrayref(
+ $query,
+ { Slice => {} },
+ $where->{branchcode}
+ )};
+}
+
+
+
+
+=head2 Object Methods
+
+These are read-only accessors for the various attributes of a preference.
+
+=head3 $pref->id
+
+=head3 $pref->branchcode
+
+=head3 $pref->categorycode
+
+=head3 $pref->item_type
+
+=cut
+
+sub AUTOLOAD {
+ my $self = shift;
+ my $attr = $AUTOLOAD;
+ $attr =~ s/.*://;
+ if (exists $self->{$attr}) {
+ return $self->{$attr};
+ } else {
+ return undef;
+ }
+}
+
+
+
+
+=head1 SEE ALSO
+
+L<C4::Circulation>, C<admin/item_circulation_alerts.pl>
+
+=head1 AUTHOR
+
+John Beppu <john.beppu at liblime.com>
+
+=cut
+
+1;
+
+# Local Variables: ***
+# mode: cperl ***
+# indent-tabs-mode: nil ***
+# cperl-close-paren-offset: -4 ***
+# cperl-continued-statement-offset: 4 ***
+# cperl-indent-level: 4 ***
+# cperl-indent-parens-as-block: t ***
+# cperl-tab-always-indent: nil ***
+# End: ***
+# vim:tabstop=8 softtabstop=4 shiftwidth=4 shiftround expandtab
diff --git a/C4/ItemType.pm b/C4/ItemType.pm
new file mode 100644
index 0000000..5c9b663
--- /dev/null
+++ b/C4/ItemType.pm
@@ -0,0 +1,159 @@
+package C4::ItemType;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 2 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
+# Suite 330, Boston, MA 02111-1307 USA
+
+use strict;
+use warnings;
+use C4::Context;
+
+our $AUTOLOAD;
+
+
+
+
+=head1 NAME
+
+C4::ItemType - objects from the itemtypes table
+
+=head1 SYNOPSIS
+
+ use C4::ItemType;
+ my @itemtypes = C4::ItemType->all;
+ print join("\n", map { $_->description } @itemtypes), "\n";
+
+=head1 DESCRIPTION
+
+Objects of this class represent a row in the C<itemtypes> table.
+
+Currently, the bare minimum for using this as a read-only data source has
+been implemented. The API was designed to make it easy to transition to
+an ORM later on.
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+=head3 C4::ItemType->new(\%opts)
+
+Given a hashref, a new (in-memory) C4::ItemType object will be instantiated.
+The database is not touched.
+
+=cut
+
+sub new {
+ my ($class, $opts) = @_;
+ bless $opts => $class;
+}
+
+
+
+
+=head3 C4::ItemType->all
+
+This returns all the itemtypes as objects. By default they're ordered by
+C<description>.
+
+=cut
+
+sub all {
+ my ($class) = @_;
+ my $dbh = C4::Context->dbh;
+ return map { $class->new($_) } @{$dbh->selectall_arrayref(
+ # The itemtypes table is small enough for
+ # `SELECT *` to be harmless.
+ "SELECT * FROM itemtypes ORDER BY description",
+ { Slice => {} },
+ )};
+}
+
+
+
+
+=head2 Object Methods
+
+These are read-only accessors for attributes of a C4::ItemType object.
+
+=head3 $itemtype->itemtype
+
+=head3 $itemtype->description
+
+=head3 $itemtype->renewalsallowed
+
+=head3 $itemtype->rentalcharge
+
+=head3 $itemtype->notforloan
+
+=head3 $itemtype->imageurl
+
+=head3 $itemtype->summary
+
+=cut
+
+sub AUTOLOAD {
+ my $self = shift;
+ my $attr = $AUTOLOAD;
+ $attr =~ s/.*://;
+ if (exists $self->{$attr}) {
+ return $self->{$attr};
+ } else {
+ return undef;
+ }
+}
+
+
+
+
+# ack itemtypes | grep '\.pm' | awk '{ print $1 }' | sed 's/:.*$//' | sort | uniq | sed -e 's,/,::,g' -e 's/\.pm//' -e 's/^/L<C4::/' -e 's/$/>,/'
+
+=head1 SEE ALSO
+
+The following modules make reference to the C<itemtypes> table.
+
+L<C4::Biblio>,
+L<C4::Circulation>,
+L<C4::Context>,
+L<C4::Items>,
+L<C4::Koha>,
+L<C4::Labels>,
+L<C4::Overdues>,
+L<C4::Reserves>,
+L<C4::Search>,
+L<C4::VirtualShelves::Page>,
+L<C4::VirtualShelves>,
+L<C4::XSLT>
+
+
+
+=head1 AUTHOR
+
+John Beppu <john.beppu at liblime.com>
+
+=cut
+
+1;
+
+# Local Variables: ***
+# mode: cperl ***
+# indent-tabs-mode: nil ***
+# cperl-close-paren-offset: -4 ***
+# cperl-continued-statement-offset: 4 ***
+# cperl-indent-level: 4 ***
+# cperl-indent-parens-as-block: t ***
+# cperl-tab-always-indent: nil ***
+# End: ***
+# vim:tabstop=8 softtabstop=4 shiftwidth=4 shiftround expandtab
--
1.5.5.GIT
More information about the Koha-patches
mailing list