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;
30 use C4::Labels::Lib 1.000000 qw(get_unit_values);
33 use version; our $VERSION = qv('1.0.0_1');
37 my $given_params = {};
39 my @valid_template_params = (
59 foreach my $key (keys %{$given_params}) {
60 if (!(grep m/$key/, @valid_template_params)) {
61 syslog("LOG_ERR", "C4::Labels::Template : Unrecognized parameter type of \"%s\".", $key);
67 if (!(grep m/$_/, @valid_template_params)) {
68 syslog("LOG_ERR", "C4::Labels::Template : Unrecognized parameter type of \"%s\".", $_);
77 my @unit_value = grep {$_->{'type'} eq $self->{'units'}} @{get_unit_values()};
78 $self->{'page_width'} = $self->{'page_width'} * $unit_value[0]->{'value'};
79 $self->{'page_height'} = $self->{'page_height'} * $unit_value[0]->{'value'};
80 $self->{'label_width'} = $self->{'label_width'} * $unit_value[0]->{'value'};
81 $self->{'label_height'} = $self->{'label_height'} * $unit_value[0]->{'value'};
82 $self->{'top_text_margin'} = $self->{'top_text_margin'} * $unit_value[0]->{'value'};
83 $self->{'left_text_margin'} = $self->{'left_text_margin'} * $unit_value[0]->{'value'};
84 $self->{'top_margin'} = $self->{'top_margin'} * $unit_value[0]->{'value'};
85 $self->{'left_margin'} = $self->{'left_margin'} * $unit_value[0]->{'value'};
86 $self->{'col_gap'} = $self->{'col_gap'} * $unit_value[0]->{'value'};
87 $self->{'row_gap'} = $self->{'row_gap'} * $unit_value[0]->{'value'};
93 my $profile = C4::Labels::Profile->retrieve(profile_id => $self->{'profile_id'}, convert => 1);
94 $self->{'top_margin'} = $self->{'top_margin'} + $profile->get_attr('offset_vert'); # controls vertical offset
95 $self->{'left_margin'} = $self->{'left_margin'} + $profile->get_attr('offset_horz'); # controls horizontal offset
96 $self->{'label_height'} = $self->{'label_height'} + $profile->get_attr('creep_vert'); # controls vertical creep
97 $self->{'label_width'} = $self->{'label_width'} + $profile->get_attr('creep_horz'); # controls horizontal creep
103 C4::Labels::Template - A class for creating and manipulating template objects in Koha
109 =head2 C4::Labels::Template->new()
111 Invoking the I<new> method constructs a new template object containing the default values for a template.
114 my $template = Template->new(); # Creates and returns a new template object
116 B<NOTE:> This template is I<not> written to the database untill $template->save() is invoked. You have been warned!
121 my $invocant = shift;
122 if (_check_params(@_) eq 1) {
125 my $type = ref($invocant) || $invocant;
128 template_code => 'DEFAULT TEMPLATE',
129 template_desc => 'Default description',
134 top_text_margin => 0,
135 left_text_margin => 0,
143 tmpl_stat => 0, # false if any data has changed and the db has not been updated
146 bless ($self, $type);
150 =head2 C4::Labels::Template->retrieve(template_id)
152 Invoking the I<retrieve> method constructs a new template object containing the current values for template_id. The method returns
153 a new object upon success and 1 upon failure. Errors are logged to the syslog. Two further options may be accessed. See the example
154 below for further description.
158 my $template = C4::Labels::Template->retrieve(template_id => 1); # Retrieves template record 1 and returns an object containing the record
160 my $template = C4::Labels::Template->retrieve(template_id => 1, convert => 1); # Retrieves template record 1, converts the units to points,
161 and returns an object containing the record
163 my $template = C4::Labels::Template->retrieve(template_id => 1, profile_id => profile_id); # Retrieves template record 1, converts the units
164 to points, applies the given profile id, and returns an object containing the record
169 my $invocant = shift;
171 my $type = ref($invocant) || $invocant;
172 my $query = "SELECT * FROM labels_templates WHERE template_id = ?";
173 my $sth = C4::Context->dbh->prepare($query);
174 $sth->execute($opts{template_id});
176 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
179 my $self = $sth->fetchrow_hashref;
180 $self = _conv_points($self) if (($opts{convert} && $opts{convert} == 1) || $opts{profile_id});
181 $self = _apply_profile($self) if $opts{profile_id};
182 $self->{'tmpl_stat'} = 1;
183 bless ($self, $type);
187 =head2 C4::Labels::Template::delete(template_id => template_id) | $template->delete()
189 Invoking the delete method attempts to delete the template from the database. The method returns 0 upon success
190 and 1 upon failure. Errors are logged to the syslog.
193 my $exitstat = $template->delete(); # to delete the record behind the $template object
194 my $exitstat = C4::Labels::Template::delete(template_id => 1); # to delete template record 1
202 my $query_param = '';
204 $self = shift; # check to see if this is a method call
205 $call_type = 'C4::Labels::Template->delete';
206 $query_param = $self->{'template_id'};
210 $call_type = 'C4::Labels::Template::delete';
211 $query_param = $opts{'template_id'};
213 if ($query_param eq '') { # If there is no template id then we cannot delete it
214 syslog("LOG_ERR", "%s : Cannot delete layout as the template id is invalid or non-existant.", $call_type);
217 my $query = "DELETE FROM labels_templates WHERE template_id = ?";
218 my $sth = C4::Context->dbh->prepare($query);
219 $sth->execute($query_param);
220 $self->{'tmpl_stat'} = 0;
224 =head2 $template->save()
226 Invoking the I<save> method attempts to insert the template into the database if the template is new and
227 update the existing template record if the template exists. The method returns the new record template_id upon
228 success and -1 upon failure (This avotemplate_ids conflicting with a record template_id of 1). Errors are logged to the syslog.
231 my $exitstat = $template->save(); # to save the record behind the $template object
237 if ($self->{'template_id'}) { # if we have an template_id, the record exists and needs UPDATE
239 my $query = "UPDATE labels_templates SET ";
240 foreach my $key (keys %{$self}) {
241 next if ($key eq 'template_id') || ($key eq 'tmpl_stat');
242 push (@params, $self->{$key});
243 $query .= "$key=?, ";
245 $query = substr($query, 0, (length($query)-2));
246 push (@params, $self->{'template_id'});
247 $query .= " WHERE template_id=?;";
248 my $sth = C4::Context->dbh->prepare($query);
249 $sth->execute(@params);
251 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
254 $self->{'tmpl_stat'} = 1;
255 return $self->{'template_id'};
257 else { # otherwise create a new record
259 my $query = "INSERT INTO labels_templates (";
260 foreach my $key (keys %{$self}) {
261 next if $key eq 'tmpl_stat';
262 push (@params, $self->{$key});
265 $query = substr($query, 0, (length($query)-2));
266 $query .= ") VALUES (";
267 for (my $i=1; $i<=((scalar keys %$self) - 1); $i++) { # key count less keys not db related...
270 $query = substr($query, 0, (length($query)-1));
272 my $sth = C4::Context->dbh->prepare($query);
273 $sth->execute(@params);
275 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
278 my $sth1 = C4::Context->dbh->prepare("SELECT MAX(template_id) FROM labels_templates;");
280 my $template_id = $sth1->fetchrow_array;
281 $self->{'template_id'} = $template_id;
282 $self->{'tmpl_stat'} = 1;
287 =head2 $template->get_attr("attr")
289 Invoking the I<get_attr> method will return the value of the requested attribute or 1 on errors.
292 my $value = $template->get_attr("attr");
298 if (_check_params(@_) eq 1) {
302 if (exists($self->{$attr})) {
303 return $self->{$attr};
310 =head2 $template->set_attr(attr, value)
312 Invoking the I<set_attr> method will set the value of the supplied attribute to the supplied value.
315 $template->set_attr(attr => value);
321 if (_check_params(@_) eq 1) {
325 foreach my $attrib (keys(%attrs)) {
326 $self->{$attrib} = $attrs{$attrib};
330 =head2 $template->get_label_position($start_label)
332 Invoking the I<get_label_position> method will return the row, column coordinates on the starting page
333 and the lower left x,y coordinates on the starting label for the template object.
336 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
340 sub get_label_position {
341 my ($self, $start_label) = @_;
342 my ($row_count, $col_count, $llx, $lly) = 0,0,0,0;
343 if ($start_label eq 1) {
346 $llx = $self->{'left_margin'};
347 $lly = ($self->{'page_height'} - $self->{'top_margin'} - $self->{'label_height'});
348 return ($row_count, $col_count, $llx, $lly);
351 $row_count = ceil($start_label / $self->{'cols'});
352 $col_count = ($start_label - (($row_count - 1) * $self->{'cols'}));
353 $llx = $self->{'left_margin'} + ($self->{'label_width'} * ($col_count - 1)) + ($self->{'col_gap'} * ($col_count - 1));
354 $lly = $self->{'page_height'} - $self->{'top_margin'} - ($self->{'label_height'} * $row_count) - ($self->{'row_gap'} * ($row_count - 1));
355 return ($row_count, $col_count, $llx, $lly);
364 Chris Nighswonger <cnighswonger AT foundations DOT edu>