1 package C4::Labels::Template;
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
22 use Sys::Syslog qw(syslog);
28 use C4::Labels::Profile 1.000000;
29 use C4::Labels::PDF 1.000000;
32 use version; our $VERSION = qv('1.0.0_1');
43 my $given_params = {};
45 my @valid_template_params = (
66 foreach my $key (keys %{$given_params}) {
67 if (!(grep m/$key/, @valid_template_params)) {
68 syslog("LOG_ERR", "C4::Labels::Template : Unrecognized parameter type of \"%s\".", $key);
74 if (!(grep m/$_/, @valid_template_params)) {
75 syslog("LOG_ERR", "C4::Labels::Template : Unrecognized parameter type of \"%s\".", $_);
84 $self->{page_width} = $self->{page_width} * $unit_values->{$self->{units}};
85 $self->{page_height} = $self->{page_height} * $unit_values->{$self->{units}};
86 $self->{label_width} = $self->{label_width} * $unit_values->{$self->{units}};
87 $self->{label_height} = $self->{label_height} * $unit_values->{$self->{units}};
88 $self->{top_text_margin} = $self->{top_text_margin} * $unit_values->{$self->{units}};
89 $self->{left_text_margin} = $self->{left_text_margin} * $unit_values->{$self->{units}};
90 $self->{top_margin} = $self->{top_margin} * $unit_values->{$self->{units}};
91 $self->{left_margin} = $self->{left_margin} * $unit_values->{$self->{units}};
92 $self->{col_gap} = $self->{col_gap} * $unit_values->{$self->{units}};
93 $self->{row_gap} = $self->{row_gap} * $unit_values->{$self->{units}};
99 my $profile_id = shift;
100 my $profile = C4::Labels::Profile->retrieve(profile_id => $profile_id, convert => 1);
101 $self->{top_margin} = $self->{top_margin} + $profile->get_attr('offset_vert'); # controls vertical offset
102 $self->{left_margin} = $self->{left_margin} + $profile->get_attr('offset_horz'); # controls horizontal offset
103 $self->{label_height} = $self->{label_height} + $profile->get_attr('creep_vert'); # controls vertical creep
104 $self->{label_width} = $self->{label_width} + $profile->get_attr('creep_horz'); # controls horizontal creep
110 C4::Labels::Template - A class for creating and manipulating template objects in Koha
116 =head2 C4::Labels::Template->new()
118 Invoking the I<new> method constructs a new template object containing the default values for a template.
121 my $template = Template->new(); # Creates and returns a new template object
123 B<NOTE:> This template is I<not> written to the database untill $template->save() is invoked. You have been warned!
128 my $invocant = shift;
129 if (_check_params(@_) eq 1) {
132 my $type = ref($invocant) || $invocant;
140 top_text_margin => 0,
141 left_text_margin => 0,
151 tmpl_stat => 0, # false if any data has changed and the db has not been updated
154 bless ($self, $type);
158 =head2 C4::Labels::Template->retrieve(template_id)
160 Invoking the I<retrieve> method constructs a new template object containing the current values for template_id. The method returns
161 a new object upon success and 1 upon failure. Errors are logged to the syslog. Two further options may be accessed. See the example
162 below for further description.
166 my $template = C4::Labels::Template->retrieve(template_id => 1); # Retrieves template record 1 and returns an object containing the record
168 my $template = C4::Labels::Template->retrieve(template_id => 1, convert => 1); # Retrieves template record 1, converts the units to points,
169 and returns an object containing the record
171 my $template = C4::Labels::Template->retrieve(template_id => 1, profile_id => profile_id); # Retrieves template record 1, converts the units
172 to points, applies the given profile id, and returns an object containing the record
177 my $invocant = shift;
179 my $type = ref($invocant) || $invocant;
180 my $query = "SELECT * FROM labels_templates WHERE tmpl_id = ?";
181 my $sth = C4::Context->dbh->prepare($query);
182 $sth->execute($opts{template_id});
184 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
187 my $self = $sth->fetchrow_hashref;
188 $self = _conv_points($self) if (($opts{convert} && $opts{convert} == 1) || $opts{profile_id});
189 $self = _apply_profile($self, $opts{profile_id}) if $opts{profile_id};
190 $self->{tmpl_stat} = 1;
191 bless ($self, $type);
195 =head2 C4::Labels::Template->delete(tmpl_id => template_id) | $template->delete()
197 Invoking the delete method attempts to delete the template from the database. The method returns 0 upon success
198 and 1 upon failure. Errors are logged to the syslog.
201 my $exitstat = $template->delete(); # to delete the record behind the $template object
202 my $exitstat = C4::Labels::Template->delete(tmpl_id => 1); # to delete template record 1
208 if (!$self->{tmpl_id}) { # If there is no template tmpl_id then we cannot delete it
209 syslog("LOG_ERR", "Cannot delete template as it has not been saved.");
212 my $query = "DELETE FROM labels_templates WHERE tmpl_id = ?";
213 my $sth = C4::Context->dbh->prepare($query);
214 $sth->execute($self->{tmpl_id});
215 $self->{tmpl_stat} = 0;
219 =head2 $template->save()
221 Invoking the I<save> method attempts to insert the template into the database if the template is new and
222 update the existing template record if the template exists. The method returns the new record tmpl_id upon
223 success and -1 upon failure (This avotmpl_ids conflicting with a record tmpl_id of 1). Errors are logged to the syslog.
226 my $exitstat = $template->save(); # to save the record behind the $template object
232 if ($self->{'tmpl_id'}) { # if we have an tmpl_id, the record exists and needs UPDATE
234 my $query = "UPDATE labels_templates SET ";
235 foreach my $key (keys %{$self}) {
236 next if ($key eq 'tmpl_id') || ($key eq 'tmpl_stat');
237 push (@params, $self->{$key});
238 $query .= "$key=?, ";
240 $query = substr($query, 0, (length($query)-2));
241 push (@params, $self->{'tmpl_id'});
242 $query .= " WHERE tmpl_id=?;";
243 warn "DEBUG: Updating: $query\n" if $debug;
244 my $sth = C4::Context->dbh->prepare($query);
245 $sth->execute(@params);
247 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
250 $self->{tmpl_stat} = 1;
251 return $self->{'tmpl_id'};
253 else { # otherwise create a new record
255 my $query = "INSERT INTO labels_templates (";
256 foreach my $key (keys %{$self}) {
257 next if $key eq 'tmpl_stat';
258 push (@params, $self->{$key});
261 $query = substr($query, 0, (length($query)-2));
262 $query .= ") VALUES (";
263 for (my $i=1; $i<=((scalar keys %$self) - 1); $i++) { # key count less keys not db related...
266 $query = substr($query, 0, (length($query)-1));
268 warn "DEBUG: Saving: $query\n" if $debug;
269 my $sth = C4::Context->dbh->prepare($query);
270 $sth->execute(@params);
272 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
275 my $sth1 = C4::Context->dbh->prepare("SELECT MAX(tmpl_id) FROM labels_templates;");
277 my $tmpl_id = $sth1->fetchrow_array;
278 $self->{tmpl_id} = $tmpl_id;
279 $self->{tmpl_stat} = 1;
284 =head2 $template->get_attr("attr")
286 Invoking the I<get_attr> method will return the value of the requested attribute or 1 on errors.
289 my $value = $template->get_attr("attr");
295 if (_check_params(@_) eq 1) {
299 if (exists($self->{$attr})) {
300 return $self->{$attr};
307 =head2 $template->set_attr(attr, value)
309 Invoking the I<set_attr> method will set the value of the supplied attribute to the supplied value.
312 $template->set_attr(attr => value);
318 if (_check_params(@_) eq 1) {
321 my ($attr, $value) = @_;
322 $self->{$attr} = $value;
325 =head2 $template->get_text_wrap_cols()
327 Invoking the I<get_text_wrap_cols> method will return the number of columns that can be printed on the
328 label before wrapping to the next line.
331 my $text_wrap_cols = $template->get_text_wrap_cols();
335 sub get_text_wrap_cols {
340 my $textlimit = $self->{label_width} - ( 3 * $self->{left_text_margin});
342 while ($strwidth < $textlimit) {
345 $strwidth = C4::Labels::PDF->StrWidth( $string, $self->{font}, $self->{font_size} );
350 =head2 $template->get_label_position($start_label)
352 Invoking the I<get_label_position> method will return the row, column coordinates on the starting page
353 and the lower left x,y coordinates on the starting label for the template object.
356 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
360 sub get_label_position {
361 my ($self, $start_label) = @_;
362 my ($row_count, $col_count, $llx, $lly) = 0,0,0,0;
363 if ($start_label eq 1) {
366 $llx = $self->{left_margin};
367 $lly = ($self->{page_height} - $self->{top_margin} - $self->{label_height});
368 return ($row_count, $col_count, $llx, $lly);
371 $row_count = ceil($start_label / $self->{cols});
372 $col_count = ($start_label - (($row_count - 1) * $self->{cols}));
373 $llx = $self->{left_margin} + ($self->{label_width} * ($col_count - 1)) + ($self->{col_gap} * ($col_count - 1));
374 $lly = $self->{page_height} - $self->{top_margin} - ($self->{label_height} * $row_count) - ($self->{row_gap} * ($row_count - 1));
375 return ($row_count, $col_count, $llx, $lly);
384 Chris Nighswonger <cnighswonger AT foundations DOT edu>