1 package C4::Creators::Layout;
6 use autouse 'Data::Dumper' => qw(Dumper);
10 use C4::Creators::PDF;
13 use version; our $VERSION = qv('1.0.0_1');
16 # FIXME: Consider this style parameter verification instead...
20 # if (exists $default{$lc})
21 # { $default{$lc} = $param{$_};
24 # { print STDERR "Unknown parameter $_ , not used \n";
30 my @valtmpl_id_params = (
41 'layout_xml', # FIXME: all layouts should be stored in xml format to greatly simplify handling -chris_n
45 my %given_params = @_;
46 foreach my $key (keys %given_params) {
47 if (!(grep m/$key/, @valtmpl_id_params)) {
48 warn sprintf('(Multiple parameters) Unrecognized parameter type of "%s".', $key);
54 if (!(grep m/$_/, @valtmpl_id_params)) {
55 warn sprintf('(Single parameter) Unrecognized parameter type of "%s".', $_);
65 if (_check_params(@_) eq 1) {
68 my $type = ref($invocant) || $invocant;
69 if (grep {$_ eq 'Labels'} @_) {
71 barcode_type => 'CODE39',
72 printing_type => 'BAR',
73 layout_name => 'DEFAULT',
79 format_string => 'title, author, isbn, issn, itemtype, barcode, callnumber',
83 elsif (grep {$_ eq 'Patroncards'} @_) {
85 layout_xml => '<opt>Default Layout</opt>',
96 my $type = ref($invocant) || $invocant;
97 my $query = "SELECT * FROM creator_layouts WHERE layout_id = ? AND creator = ?";
98 my $sth = C4::Context->dbh->prepare($query);
99 $sth->execute($opts{'layout_id'}, $opts{'creator'});
101 warn sprintf('Database returned the following error: %s', $sth->errstr);
104 my $self = $sth->fetchrow_hashref;
105 bless ($self, $type);
115 $self = shift; # check to see if this is a method call
116 $call_type = 'C4::Labels::Layout->delete';
117 push @params, $self->{'layout_id'}, $self->{'creator'};
122 $call_type = $class . '::delete';
123 push @params, $opts{'layout_id'}, $opts{'creator'};
125 if (scalar(@params) < 2) { # If there is no layout id or creator type then we cannot delete it
126 warn sprintf('%s : Cannot delete layout as the profile id is invalid or non-existant.', $call_type) if !$params[0];
127 warn sprintf('%s : Cannot delete layout as the creator type is invalid or non-existant.', $call_type) if !$params[1];
130 my $query = "DELETE FROM creator_layouts WHERE layout_id = ? AND creator = ?";
131 my $sth = C4::Context->dbh->prepare($query);
132 $sth->execute(@params);
134 warn sprintf('Database returned the following error on attempted DELETE: %s', $sth->errstr);
141 if ($self->{'layout_id'}) { # if we have an id, the record exists and needs UPDATE
143 my $query = "UPDATE creator_layouts SET ";
144 foreach my $key (keys %{$self}) {
145 next if ($key eq 'layout_id') || ($key eq 'creator');
146 push (@params, $self->{$key});
147 $query .= "$key=?, ";
149 $query = substr($query, 0, (length($query)-2));
150 $query .= " WHERE layout_id=? AND creator = ?;";
151 push (@params, $self->{'layout_id'}, $self->{'creator'});
152 my $sth = C4::Context->dbh->prepare($query);
153 #local $sth->{TraceLevel} = "3"; # enable DBI trace and set level; outputs to STDERR
154 $sth->execute(@params);
156 warn sprintf('Database returned the following error: %s', $sth->errstr);
159 return $self->{'layout_id'};
161 else { # otherwise create a new record
163 my $query = "INSERT INTO creator_layouts (";
164 foreach my $key (keys %{$self}) {
165 push (@params, $self->{$key});
168 $query = substr($query, 0, (length($query)-2));
169 $query .= ") VALUES (";
170 for (my $i=1; $i<=(scalar keys %$self); $i++) {
173 $query = substr($query, 0, (length($query)-1));
175 my $sth = C4::Context->dbh->prepare($query);
176 $sth->execute(@params);
178 warn sprintf('Database returned the following error: %s', $sth->errstr);
181 my $sth1 = C4::Context->dbh->prepare("SELECT MAX(layout_id) FROM creator_layouts;");
183 my $id = $sth1->fetchrow_array;
184 $self->{'layout_id'} = $id;
191 if (_check_params(@_) eq 1) {
195 if (exists($self->{$attr})) {
196 return $self->{$attr};
206 if (_check_params(@_) eq 1) {
210 foreach my $attrib (keys(%attrs)) {
211 $self->{$attrib} = $attrs{$attrib};
216 sub get_text_wrap_cols {
222 my $textlimit = $params{'label_width'} - (( 3 * $params{'left_text_margin'} ) || 13.5 );
224 while ($strwidth < $textlimit) {
227 $strwidth = C4::Creators::PDF->StrWidth( $string, $self->{'font'}, $self->{'font_size'} );
237 C4::Labels::Layout -A class for creating and manipulating layout objects in Koha
241 This module provides methods for creating, retrieving, and otherwise manipulating label layout objects used by Koha to create and export labels.
247 Invoking the I<new> method constructs a new layout object containing the default values for a layout.
248 The following parameters are optionally accepted as key => value pairs:
250 C<barcode_type> Defines the barcode type to be used on labels. NOTE: At present only the following barcode types are supported in the label creator code:
258 CODE39MOD = Code 3 of 9 with modulo 43 checksum
261 CODE39MOD10 = Code 3 of 9 with modulo 10 checksum
264 COOP2OF5 = A varient of 2 of 5 barcode based on NEC's "Process 8000" code
267 INDUSTRIAL2OF5 = The standard 2 of 5 barcode (a binary level bar code developed by Identicon Corp. and Computer Identics Corp. in 1970)
271 C<printing_type> Defines the general layout to be used on labels. NOTE: At present there are only five printing types supported in the label creator code:
276 BIB = Only the bibliographic data is printed
279 BARBIB = Barcode proceeds bibliographic data
282 BIBBAR = Bibliographic data proceeds barcode
285 ALT = Barcode and bibliographic data are printed on alternating labels
288 BAR = Only the barcode is printed
292 C<layout_name> The descriptive name for this layout.
293 C<guidebox> Setting this to '1' will result in a guide box being drawn around the labels marking the edge of each label
294 C<font> Defines the type of font to be used on labels. NOTE: The following fonts are available by default on most systems:
308 TBI = Times Bold Italic
317 CO = Courier Oblique (Italic)
320 CBO = Courier Bold Oblique
329 HBO = Helvetical Bold Oblique
333 C<font_size> Defines the size of the font in postscript points to be used on labels
334 C<callnum_split> Setting this to '1' will enable call number splitting on labels
335 C<text_justify> Defines the text justification to be used on labels. NOTE: The following justification styles are currently supported by label creator code:
350 C<format_string> Defines what fields will be printed and in what order they will be printed on labels. These include any of the data fields that may be mapped
351 to your MARC frameworks. Specify MARC subfields as a 4-character tag-subfield string: ie. 254a Enclose a whitespace-separated list of fields
352 to concatenate on one line in double quotes. ie. "099a 099b" or "itemcallnumber barcode" Static text strings may be entered in single-quotes:
353 ie. 'Some static text here.'
356 C<my $layout = Layout->new(); # Creates and returns a new layout object>
358 C<my $layout = C4::Labels::Layout->new(barcode_type => 'CODE39', printing_type => 'BIBBAR', font => 'C', font_size => 6); # Creates and returns a new layout object using
359 the supplied values to override the defaults>
361 B<NOTE:> This layout is I<not> written to the database until save() is invoked. You have been warned!
363 =head2 retrieve(layout_id => layout_id)
365 Invoking the I<retrieve> method constructs a new layout object containing the current values for layout_id. The method returns a new object upon success and 1 upon failure.
366 Errors are logged to the Apache log.
369 C<my $layout = Layout->retrieve(layout_id => 1); # Retrieves layout record 1 and returns an object containing the record>
373 Invoking the delete method attempts to delete the layout from the database. The method returns 0 upon success and -1 upon failure. Errors are logged to the Apache log.
374 NOTE: This method may also be called as a function and passed a key/value pair simply deleteing that template from the database. See the example below.
377 C<my $exitstat = $layout->delete(); # to delete the record behind the $layout object>
378 C<my $exitstat = Layout->delete(layout_id => 1); # to delete layout record 1>
382 Invoking the I<save> method attempts to insert the layout into the database if the layout is new and update the existing layout record if the layout exists.
383 The method returns the new record id upon success and -1 upon failure (This avoids conflicting with a record id of 1). Errors are logged to the Apache log.
386 C<my $exitstat = $layout->save(); # to save the record behind the $layout object>
388 =head2 get_attr($attribute)
390 Invoking the I<get_attr> method will return the value of the requested attribute or -1 on errors.
393 C<my $value = $layout->get_attr($attribute);>
395 =head2 set_attr(attribute => value, attribute_2 => value)
397 Invoking the I<set_attr> method will set the value of the supplied attributes to the supplied values. The method accepts key/value pairs separated by
401 C<$layout->set_attr(attribute => value);>
403 =head2 get_text_wrap_cols()
405 Invoking the I<get_text_wrap_cols> method will return the number of columns that can be printed on the label before wrapping to the next line.
408 C<my $text_wrap_cols = $layout->get_text_wrap_cols();>
412 Chris Nighswonger <cnighswonger AT foundations DOT edu>
416 Copyright 2009 Foundations Bible College.
420 This file is part of Koha.
422 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
423 Foundation; either version 2 of the License, or (at your option) any later version.
425 You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
426 Fifth Floor, Boston, MA 02110-1301 USA.
428 =head1 DISCLAIMER OF WARRANTY
430 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
431 A PARTICULAR PURPOSE. See the GNU General Public License for more details.