1 package C4::Labels::Patroncard;
3 # Copyright 2009 Foundations Bible College.
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
26 use C4::Labels::Layout 1.000000; # use version 1.0.0 or better
27 use C4::Labels::Template 1.000000;
31 use version; our $VERSION = qv('1.0.0_1');
35 my $given_params = {};
37 my @valid_template_params = (
44 foreach my $key (keys %{$given_params}) {
45 if (!(grep m/$key/, @valid_template_params)) {
46 warn sprintf('Unrecognized parameter type of "%s".', $key);
52 if (!(grep m/$_/, @valid_template_params)) {
53 warn sprintf('Unrecognized parameter type of "%s".', $_);
62 C4::Labels::Batch - A class for creating and manipulating batch objects in Koha
68 =head2 C4::Labels::Batch->new(layout_id => layout_id, tmpl_id => template_id, prof_id => prof_id)
70 Invoking the I<new> method constructs a new batch object with no items.
73 my $batch = C4::Labels::Batch->new(layout_id => layout_id, tmpl_id => template_id, prof_id => prof_id);
74 # Creates and returns a new batch object
76 B<NOTE:> This batch is I<not> written to the database untill $batch->save() is invoked. You have been warned!
81 my ($invocant, %params) = @_;
82 my $type = ref($invocant) || $invocant;
85 layout_id => $params{layout_id},
86 tmpl_id => $params{tmpl_id},
87 prof_id => $params{prof_id},
89 batch_stat => 0, # False if any data has changed and the db has not been updated
95 =head2 $batch->add_item($item_number)
97 Invoking the I<add_item> method will add the supplied item to the batch object.
100 $batch->add_item($item_number);
106 my $item_num = shift;
107 push (@{$self->{items}}, $item_num);
108 $self->{batch_stat} = 0;
111 =head2 $batch->get_attr()
113 Invoking the I<get_attr> method will return the requested attribute.
116 my @items = $batch->get_attr($attr);
122 return $self->{$_[0]};
125 =head2 $batch->delete_item()
127 Invoking the I<delete_item> method will delete the supplied item from the batch object.
130 $batch->delete_item();
136 my $item_num = shift;
138 ++$index until $$self->{items}[$index] == $item_num or $item_num > $#$self->{items};
139 delete ($$self->{items}[$index]);
140 $self->{batch_stat} = 0;
143 =head2 $batch->save()
145 Invoking the I<save> method attempts to insert the batch into the database if the batch is new and
146 update the existing batch record if the batch exists. The method returns the new record batch_id upon
147 success and -1 upon failure (This avoids conflicting with a record batch_id of 1). Errors are
148 logged to the Apache log.
151 my $exitstat = $batch->save(); # to save the record behind the $batch object
157 if ($self->{batch_id} > 0) {
158 foreach my $item_number (@$self->{items}) {
159 my $query = "UPDATE labels_batches SET item_number=?, layout_id=?, tmpl_id=?, prof_id=? WHERE batch_id=?;";
160 warn "DEBUG: Updating: $query\n" if $debug;
161 my $sth->C4::Context->dbh->prepare($query);
162 $sth->execute($item_number, $self->{layout_id}, $self->{tmpl_id}, $self->{prof_id}, $self->{batch_id});
164 warn sprintf('Database returned the following error: %s', $sth->errstr);
170 foreach my $item_number (@$self->{items}) {
171 my $query = "INSERT INTO labels_batches (item_number, layout_id, tmpl_id, prof_id) VALUES (?,?,?,?);";
172 warn "DEBUG: Inserting: $query\n" if $debug;
173 my $sth->C4::Context->dbh->prepare($query);
174 $sth->execute($item_number, $self->{layout_id}, $self->{tmpl_id}, $self->{prof_id});
176 warn sprintf('Database returned the following error: %s', $sth->errstr);
179 my $sth1 = C4::Context->dbh->prepare("SELECT MAX(batch_id) FROM labels_batches;");
181 my $batch_id = $sth1->fetchrow_array;
182 $self->{batch_id} = $batch_id;
186 $self->{batch_stat} = 1;
189 =head2 C4::Labels::Template->retrieve(template_id)
191 Invoking the I<retrieve> method constructs a new template object containing the current values for template_id. The method returns
192 a new object upon success and 1 upon failure. Errors are logged to the Apache log. Two further options may be accessed. See the example
193 below for further description.
197 my $template = C4::Labels::Template->retrieve(template_id => 1); # Retrieves template record 1 and returns an object containing the record
199 my $template = C4::Labels::Template->retrieve(template_id => 1, convert => 1); # Retrieves template record 1, converts the units to points,
200 and returns an object containing the record
202 my $template = C4::Labels::Template->retrieve(template_id => 1, prof_id => prof_id); # Retrieves template record 1, converts the units
203 to points, applies the given profile id, and returns an object containing the record
208 my $invocant = shift;
210 my $type = ref($invocant) || $invocant;
211 my $query = "SELECT * FROM labels_batches WHERE batch_id = ? ORDER BY label_id";
212 my $sth = C4::Context->dbh->prepare($query);
213 $sth->execute($opts{batch_id});
215 warn sprintf('Database returned the following error: %s', $sth->errstr);
221 while (my $record = $sth->fetchrow_hashref) {
222 $self->{batch_id} = $record->{batch_id}; # FIXME: seems a bit wasteful to re-initialize these every trip: is there a better way?
223 $self->{layout_id} = $record->{layout_id};
224 $self->{tmpl_id} = $record->{tmpl_id};
225 $self->{prof_id} = $record->{prof_id};
226 push (@{$self->{items}}, $record->{item_number});
228 $self->{batch_stat} = 1;
229 bless ($self, $type);
233 =head2 C4::Labels::Batch->delete(batch_id => batch_id) | $batch->delete()
235 Invoking the delete method attempts to delete the batch from the database. The method returns 0 upon success
236 and 1 upon failure. Errors are logged to the Apache log.
239 my $exitstat = $batch->delete(); # to delete the record behind the $batch object
240 my $exitstat = C4::Labels::Batch->delete(batch_id => 1); # to delete batch record 1
247 if ((ref $self) && !$self->{'batch_id'}) { # If there is no batch batch_id then we cannot delete it from the db
248 warn 'Cannot delete batch: Batch has not been saved.';
251 elsif (!$opts{batch_id}) {
252 warn 'Cannot delete batch: Missing batch_id.';
255 my $query = "DELETE FROM labels_batches WHERE batch_id = ?";
256 my $sth = C4::Context->dbh->prepare($query);
257 $sth->execute($self->{'batch_id'});
267 Chris Nighswonger <cnighswonger AT foundations DOT edu>
273 # elsif ( $label_type eq 'PATCRD' ) {
274 # my $patron_data = $item;
276 # #FIXME: This needs to be paramatized and passed in from the user...
277 # #Each element of this hash is a separate line on the patron card. Keys are the text to print and the associated data is the point size.
279 # $patron_data->{'description'} => $template->get_attr('font_size'),
280 # $patron_data->{'branchname'} => ($template->get_attr('font_size') + 3),
283 # $debug and warn "Generating patron card for cardnumber $patron_data->{'cardnumber'}";
285 # my $barcode_height = $label_height / 2.75; #FIXME: Scaling barcode height; this needs to be a user parameter.
286 # $label->barcode( $llx, $lly, $barcode_height, $label_width, $patron_data->{'cardnumber'},
288 # DrawPatronCardText( $llx, $lly, $label_height, $label_width, $template->get_attr('font'), $template->get_attr('font_size'),
289 # $left_text_margin, $text_wrap_cols, $text, $label_type );
290 # _calc_next_label_pos();