1 package C4::Creators::Layout;
6 use autouse 'Data::Dumper' => qw(Dumper);
11 # FIXME: Consider this style parameter verification instead...
15 # if (exists $default{$lc})
16 # { $default{$lc} = $param{$_};
19 # { print STDERR "Unknown parameter $_ , not used \n";
25 my @valtmpl_id_params = (
37 'layout_xml', # FIXME: all layouts should be stored in xml format to greatly simplify handling -chris_n
43 my %given_params = @_;
44 foreach my $key (keys %given_params) {
45 if (!(grep m/$key/, @valtmpl_id_params)) {
46 warn sprintf('(Multiple parameters) Unrecognized parameter type of "%s".', $key);
52 if (!(grep m/$_/, @valtmpl_id_params)) {
53 warn sprintf('(Single parameter) Unrecognized parameter type of "%s".', $_);
60 use constant PRESET_FIELDS => [qw(title author isbn issn itemtype barcode itemcallnumber)];
64 if (_check_params(@_) eq 1) {
67 my $type = ref($invocant) || $invocant;
68 if (grep {$_ eq 'Labels'} @_) {
73 barcode_type => 'CODE39',
74 printing_type => 'BAR',
75 layout_name => 'DEFAULT',
82 format_string => join(', ', @{ PRESET_FIELDS() }),
86 elsif (grep {$_ eq 'Patroncards'} @_) {
88 layout_xml => '<opt>Default Layout</opt>',
99 my $type = ref($invocant) || $invocant;
100 my $query = "SELECT * FROM creator_layouts WHERE layout_id = ? AND creator = ?";
101 my $sth = C4::Context->dbh->prepare($query);
102 $sth->execute($opts{'layout_id'}, $opts{'creator'});
104 warn sprintf('Database returned the following error: %s', $sth->errstr);
107 my $self = $sth->fetchrow_hashref;
108 bless ($self, $type);
118 $self = shift; # check to see if this is a method call
119 $call_type = 'C4::Labels::Layout->delete';
120 push @params, $self->{'layout_id'}, $self->{'creator'};
125 $call_type = $class . '::delete';
126 push @params, $opts{'layout_id'}, $opts{'creator'};
128 if (scalar(@params) < 2) { # If there is no layout id or creator type then we cannot delete it
129 warn sprintf('%s : Cannot delete layout as the profile id is invalid or non-existent.', $call_type) if !$params[0];
130 warn sprintf('%s : Cannot delete layout as the creator type is invalid or non-existent.', $call_type) if !$params[1];
133 my $query = "DELETE FROM creator_layouts WHERE layout_id = ? AND creator = ?";
134 my $sth = C4::Context->dbh->prepare($query);
135 $sth->execute(@params);
137 warn sprintf('Database returned the following error on attempted DELETE: %s', $sth->errstr);
144 if ($self->{'layout_id'}) { # if we have an id, the record exists and needs UPDATE
146 my $query = "UPDATE creator_layouts SET ";
147 foreach my $key (keys %{$self}) {
148 next if ($key eq 'layout_id') || ($key eq 'creator');
149 push (@params, $self->{$key});
150 $query .= "$key=?, ";
152 $query = substr($query, 0, (length($query)-2));
153 $query .= " WHERE layout_id=? AND creator = ?;";
154 push (@params, $self->{'layout_id'}, $self->{'creator'});
155 my $sth = C4::Context->dbh->prepare($query);
156 #local $sth->{TraceLevel} = "3"; # enable DBI trace and set level; outputs to STDERR
157 $sth->execute(@params);
159 warn sprintf('Database returned the following error: %s', $sth->errstr);
162 return $self->{'layout_id'};
164 else { # otherwise create a new record
166 delete $self->{layout_id}; # Could be an empty string
167 my $query = "INSERT INTO creator_layouts (";
168 foreach my $key (keys %{$self}) {
169 push (@params, $self->{$key});
172 $query = substr($query, 0, (length($query)-2));
173 $query .= ") VALUES (";
174 for (my $i=1; $i<=(scalar keys %$self); $i++) {
177 $query = substr($query, 0, (length($query)-1));
179 my $sth = C4::Context->dbh->prepare($query);
180 $sth->execute(@params);
182 warn sprintf('Database returned the following error: %s', $sth->errstr);
185 my $sth1 = C4::Context->dbh->prepare("SELECT MAX(layout_id) FROM creator_layouts;");
187 my $id = $sth1->fetchrow_array;
188 $self->{'layout_id'} = $id;
195 if (_check_params(@_) eq 1) {
199 if (exists($self->{$attr})) {
200 return $self->{$attr};
210 if (_check_params(@_) eq 1) {
214 foreach my $attrib (keys(%attrs)) {
215 $self->{$attrib} = $attrs{$attrib};
220 sub get_text_wrap_cols {
226 my $textlimit = $params{'label_width'} - (( 3 * $params{'left_text_margin'} ) || 13.5 );
228 while ($strwidth < $textlimit) {
229 $string .= '8'; # using '8' as filling char instead of '0'
231 $strwidth = C4::Creators::PDF->StrWidth( $string, $self->{'font'}, $self->{'font_size'} );
241 C4::Labels::Layout -A class for creating and manipulating layout objects in Koha
245 This module provides methods for creating, retrieving, and otherwise manipulating label layout objects used by Koha to create and export labels.
251 Invoking the I<new> method constructs a new layout object containing the default values for a layout.
252 The following parameters are optionally accepted as key => value pairs:
254 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:
262 CODE39MOD = Code 3 of 9 with modulo 43 checksum
265 CODE39MOD10 = Code 3 of 9 with modulo 10 checksum
268 COOP2OF5 = A variant of 2 of 5 barcode based on NEC's "Process 8000" code
271 INDUSTRIAL2OF5 = The standard 2 of 5 barcode (a binary level bar code developed by Identicon Corp. and Computer Identics Corp. in 1970)
275 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:
280 BIB = Only the bibliographic data is printed
283 BARBIB = Barcode proceeds bibliographic data
286 BIBBAR = Bibliographic data proceeds barcode
289 ALT = Barcode and bibliographic data are printed on alternating labels
292 BAR = Only the barcode is printed
296 C<layout_name> The descriptive name for this layout.
297 C<guidebox> Setting this to '1' will result in a guide box being drawn around the labels marking the edge of each label
298 C<font> Defines the type of font to be used on labels. NOTE: The following fonts are available by default on most systems:
312 TBI = Times Bold Italic
321 CO = Courier Oblique (Italic)
324 CBO = Courier Bold Oblique
333 HBO = Helvetical Bold Oblique
337 C<font_size> Defines the size of the font in postscript points to be used on labels
338 C<callnum_split> Setting this to '1' will enable call number splitting on labels
339 C<text_justify> Defines the text justification to be used on labels. NOTE: The following justification styles are currently supported by label creator code:
354 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
355 to your MARC frameworks. Specify MARC subfields as a 4-character tag-subfield string: ie. 254a Enclose a whitespace-separated list of fields
356 to concatenate on one line in double quotes. ie. "099a 099b" or "itemcallnumber barcode" Static text strings may be entered in single-quotes:
357 ie. 'Some static text here.'
360 C<my $layout = Layout->new(); # Creates and returns a new layout object>
362 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
363 the supplied values to override the defaults>
365 B<NOTE:> This layout is I<not> written to the database until save() is invoked. You have been warned!
367 =head2 retrieve(layout_id => layout_id)
369 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.
370 Errors are logged to the Apache log.
373 C<my $layout = Layout->retrieve(layout_id => 1); # Retrieves layout record 1 and returns an object containing the record>
377 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.
378 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.
381 C<my $exitstat = $layout->delete(); # to delete the record behind the $layout object>
382 C<my $exitstat = Layout->delete(layout_id => 1); # to delete layout record 1>
386 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.
387 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.
390 C<my $exitstat = $layout->save(); # to save the record behind the $layout object>
392 =head2 get_attr($attribute)
394 Invoking the I<get_attr> method will return the value of the requested attribute or -1 on errors.
397 C<my $value = $layout->get_attr($attribute);>
399 =head2 set_attr(attribute => value, attribute_2 => value)
401 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
405 C<$layout->set_attr(attribute => value);>
407 =head2 get_text_wrap_cols()
409 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.
412 C<my $text_wrap_cols = $layout->get_text_wrap_cols();>
416 Chris Nighswonger <cnighswonger AT foundations DOT edu>
420 Copyright 2009 Foundations Bible College.
424 This file is part of Koha.
426 Koha is free software; you can redistribute it and/or modify it
427 under the terms of the GNU General Public License as published by
428 the Free Software Foundation; either version 3 of the License, or
429 (at your option) any later version.
431 Koha is distributed in the hope that it will be useful, but
432 WITHOUT ANY WARRANTY; without even the implied warranty of
433 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
434 GNU General Public License for more details.
436 You should have received a copy of the GNU General Public License
437 along with Koha; if not, see <http://www.gnu.org/licenses>.
439 =head1 DISCLAIMER OF WARRANTY
441 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
442 A PARTICULAR PURPOSE. See the GNU General Public License for more details.