Bug 16011: $VERSION - Remove the $VERSION init
[koha.git] / C4 / Creators / Profile.pm
blobde3723d91517b036fe7f8e982829927201036989
1 package C4::Creators::Profile;
3 use strict;
4 use warnings;
6 use autouse 'Data::Dumper' => qw(Dumper);
8 use C4::Context;
9 use C4::Debug;
10 use C4::Creators::Lib qw(get_unit_values);
12 BEGIN {
15 sub _check_params {
16 my $given_params = {};
17 my $exit_code = 0;
18 my @valid_profile_params = (
19 'printer_name',
20 'template_id',
21 'paper_bin',
22 'offset_horz',
23 'offset_vert',
24 'creep_horz',
25 'creep_vert',
26 'units',
27 'creator',
29 if (scalar(@_) >1) {
30 $given_params = {@_};
31 foreach my $key (keys %{$given_params}) {
32 if (!(grep m/$key/, @valid_profile_params)) {
33 warn sprintf('Unrecognized parameter type of "%s".', $key);
34 $exit_code = 1;
38 else {
39 if (!(grep m/$_/, @valid_profile_params)) {
40 warn sprintf('Unrecognized parameter type of "%s".', $_);
41 $exit_code = 1;
44 return $exit_code;
47 sub _conv_points {
48 my $self = shift;
49 my @unit_value = grep {$_->{'type'} eq $self->{units}} @{get_unit_values()};
50 $self->{offset_horz} = $self->{offset_horz} * $unit_value[0]->{'value'};
51 $self->{offset_vert} = $self->{offset_vert} * $unit_value[0]->{'value'};
52 $self->{creep_horz} = $self->{creep_horz} * $unit_value[0]->{'value'};
53 $self->{creep_vert} = $self->{creep_vert} * $unit_value[0]->{'value'};
54 return $self;
57 sub new {
58 my $invocant = shift;
59 if (_check_params(@_) eq 1) {
60 return -1;
62 my $type = ref($invocant) || $invocant;
63 my $self = {
64 printer_name => 'Default Printer',
65 template_id => '',
66 paper_bin => 'Tray 1',
67 offset_horz => 0,
68 offset_vert => 0,
69 creep_horz => 0,
70 creep_vert => 0,
71 units => 'POINT',
72 @_,
74 bless ($self, $type);
75 return $self;
78 sub retrieve {
79 my $invocant = shift;
80 my %opts = @_;
81 my $type = ref($invocant) || $invocant;
82 my $query = "SELECT * FROM printers_profile WHERE profile_id = ? AND creator = ?";
83 my $sth = C4::Context->dbh->prepare($query);
84 $sth->execute($opts{'profile_id'}, $opts{'creator'});
85 if ($sth->err) {
86 warn sprintf('Database returned the following error: %s', $sth->errstr);
87 return -1;
89 my $self = $sth->fetchrow_hashref;
90 $self = _conv_points($self) if ($opts{convert} && $opts{convert} == 1);
91 bless ($self, $type);
92 return $self;
95 sub delete {
96 my $self = {};
97 my %opts = ();
98 my $call_type = '';
99 my @params = ();
100 if (ref($_[0])) {
101 $self = shift; # check to see if this is a method call
102 $call_type = 'C4::'. $self->{'creator'} .'::Profile->delete';
103 push @params, $self->{'profile_id'}, $self->{'creator'};
105 else {
106 my $class = shift; #XXX: is this too hackish?
107 %opts = @_;
108 $call_type = $class . "::delete";
109 push @params, $opts{'profile_id'}, $opts{'creator'};
111 if (scalar(@params) < 2) { # If there is no profile id or creator type then we cannot delete it
112 warn sprintf('%s : Cannot delete profile as the profile id is invalid or non-existent.', $call_type) if !$params[0];
113 warn sprintf('%s : Cannot delete profile as the creator type is invalid or non-existent.', $call_type) if !$params[1];
114 return -1;
116 my $query = "DELETE FROM printers_profile WHERE profile_id = ? AND creator = ?";
117 my $sth = C4::Context->dbh->prepare($query);
118 # $sth->{'TraceLevel'} = 3;
119 $sth->execute(@params);
120 if ($sth->err) {
121 warn sprintf('Database returned the following error on attempted DELETE: %s', $sth->errstr);
122 return -1;
124 return 0;
127 sub save {
128 my $self = shift;
129 if ($self->{'profile_id'}) { # if we have an profile_id, the record exists and needs UPDATE
130 my @params;
131 my $query = "UPDATE printers_profile SET ";
132 foreach my $key (keys %{$self}) {
133 next if ($key eq 'profile_id') || ($key eq 'creator');
134 push (@params, $self->{$key});
135 $query .= "$key=?, ";
137 $query = substr($query, 0, (length($query)-2));
138 push (@params, $self->{'profile_id'}, $self->{'creator'});
139 $query .= " WHERE profile_id=? AND creator=?;";
140 my $sth = C4::Context->dbh->prepare($query);
141 # $sth->{'TraceLevel'} = 3;
142 $sth->execute(@params);
143 if ($sth->err) {
144 warn sprintf('Database returned the following error on attempted UPDATE: %s', $sth->errstr);
145 return -1;
147 return $self->{'profile_id'};
149 else { # otherwise create a new record
150 my @params;
151 my $query = "INSERT INTO printers_profile (";
152 foreach my $key (keys %{$self}) {
153 push (@params, $self->{$key});
154 $query .= "$key, ";
156 $query = substr($query, 0, (length($query)-2));
157 $query .= ") VALUES (";
158 for (my $i=1; $i<=(scalar keys %$self); $i++) {
159 $query .= "?,";
161 $query = substr($query, 0, (length($query)-1));
162 $query .= ");";
163 my $sth = C4::Context->dbh->prepare($query);
164 $sth->execute(@params);
165 if ($sth->err) {
166 warn sprintf('Database returned the following error on attempted INSERT: %s', $sth->errstr);
167 return -1;
169 my $sth1 = C4::Context->dbh->prepare("SELECT MAX(profile_id) FROM printers_profile;");
170 $sth1->execute();
171 my $tmpl_id = $sth1->fetchrow_array;
172 return $tmpl_id;
176 sub get_attr {
177 my $self = shift;
178 if (_check_params(@_) eq 1) {
179 return -1;
181 my ($attr) = @_;
182 if (exists($self->{$attr})) {
183 return $self->{$attr};
185 else {
186 warn sprintf('%s is currently undefined.', $attr);
187 return -1;
191 sub set_attr {
192 my $self = shift;
193 if (_check_params(@_) eq 1) {
194 return -1;
196 my %attrs = @_;
197 foreach my $attrib (keys(%attrs)) {
198 $self->{$attrib} = $attrs{$attrib};
200 return 0;
204 __END__
206 =head1 NAME
208 C4::Labels::Profile - A class for creating and manipulating profile objects in Koha
210 =head1 ABSTRACT
212 This module provides methods for creating, retrieving, and otherwise manipulating label profile objects used by Koha to create and export labels.
214 =head1 METHODS
216 =head2 new()
218 Invoking the I<new> method constructs a new profile object containing the default values for a template.
219 The following parameters are optionally accepted as key => value pairs:
221 C<printer_name> The name of the printer to which this profile applies.
222 C<template_id> The template to which this profile may be applied. NOTE: There may be multiple profiles which may be applied to the same template.
223 C<paper_bin> The paper bin of the above printer to which this profile applies. NOTE: printer name, template id, and paper bin must form a unique combination.
224 C<offset_horz> Amount of compensation for horizontal offset (position of text on a single label). This amount is measured in the units supplied by the units parameter in this profile.
225 C<offset_vert> Amount of compensation for vertical offset.
226 C<creep_horz> Amount of compensation for horizontal creep (tendency of text to 'creep' off of the labels over the span of the entire page).
227 C<creep_vert> Amount of compensation for vertical creep.
228 C<units> The units of measure used for this template. These B<must> match the measures you supply above or
229 bad things will happen to your document. NOTE: The only supported units at present are:
231 =over 9
233 =item .
234 POINT = Postscript Points (This is the base unit in the Koha label creator.)
236 =item .
237 AGATE = Adobe Agates (5.1428571 points per)
239 =item .
240 INCH = US Inches (72 points per)
242 =item .
243 MM = SI Millimeters (2.83464567 points per)
245 =item .
246 CM = SI Centimeters (28.3464567 points per)
248 =back
250 example:
251 C<my $profile = C4::Labels::Profile->new(); # Creates and returns a new profile object>
253 C<my $profile = C4::Labels::Profile->new(template_id => 1, paper_bin => 'Bypass Tray', offset_horz => 0.02, units => 'POINT'); # Creates and returns a new profile object using
254 the supplied values to override the defaults>
256 B<NOTE:> This profile is I<not> written to the database until save() is invoked. You have been warned!
258 =head2 retrieve(profile_id => $profile_id, convert => 1)
260 Invoking the I<retrieve> method constructs a new profile object containing the current values for profile_id. The method returns a new object upon success and 1 upon failure.
261 Errors are logged to the Apache log. One further option maybe accessed. See the examples below for further description.
263 examples:
265 C<my $profile = C4::Labels::Profile->retrieve(profile_id => 1); # Retrieves profile record 1 and returns an object containing the record>
267 C<my $profile = C4::Labels::Profile->retrieve(profile_id => 1, convert => 1); # Retrieves profile record 1, converts the units to points and returns an object containing the record>
269 =head2 delete()
271 Invoking the delete method attempts to delete the profile from the database. The method returns -1 upon failure. Errors are logged to the Apache log.
272 NOTE: This method may also be called as a function and passed a key/value pair simply deleteing that profile from the database. See the example below.
274 examples:
275 C<my $exitstat = $profile->delete(); # to delete the record behind the $profile object>
276 C<my $exitstat = C4::Labels::Profile::delete(profile_id => 1); # to delete profile record 1>
278 =head2 save()
280 Invoking the I<save> method attempts to insert the profile into the database if the profile is new and update the existing profile record if the profile exists. The method returns
281 the new record profile_id upon success and -1 upon failure (This avoids conflicting with a record profile_id of 1). Errors are logged to the Apache log.
283 example:
284 C<my $exitstat = $profile->save(); # to save the record behind the $profile object>
286 =head2 get_attr($attribute)
288 Invoking the I<get_attr> method will return the value of the requested attribute or -1 on errors.
290 example:
291 C<my $value = $profile->get_attr($attribute);>
293 =head2 set_attr(attribute => value, attribute_2 => value)
295 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 commas.
297 example:
298 $profile->set_attr(attribute => value);
300 =head1 AUTHOR
302 Chris Nighswonger <cnighswonger AT foundations DOT edu>
304 =head1 COPYRIGHT
306 Copyright 2009 Foundations Bible College.
308 =head1 LICENSE
310 This file is part of Koha.
312 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
313 Foundation; either version 2 of the License, or (at your option) any later version.
315 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,
316 Fifth Floor, Boston, MA 02110-1301 USA.
318 =head1 DISCLAIMER OF WARRANTY
320 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
321 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
323 =cut
325 #=head1
326 #drawbox( ($left_margin), ($top_margin), ($page_width-(2*$left_margin)), ($page_height-(2*$top_margin)) ); # FIXME: Breakout code to print alignment page for printer profile setup
328 #=head2 draw_boundaries
330 # sub draw_boundaries ($llx_spine, $llx_circ1, $llx_circ2,
331 # $lly, $spine_width, $label_height, $circ_width)
333 #This sub draws boundary lines where the label outlines are, to aid in printer testing, and debugging.
335 #=cut
337 ## FIXME: Template use for profile adjustment...
338 ##sub draw_boundaries {
340 ## my (
341 ## $llx_spine, $llx_circ1, $llx_circ2, $lly,
342 ## $spine_width, $label_height, $circ_width
343 ## ) = @_;
345 ## my $lly_initial = ( ( 792 - 36 ) - 90 );
346 ## $lly = $lly_initial; # FIXME - why are we ignoring the y_pos parameter by redefining it?
347 ## my $i = 1;
349 ## for ( $i = 1 ; $i <= 8 ; $i++ ) {
351 ## _draw_box( $llx_spine, $lly, ($spine_width), ($label_height) );
353 ## #warn "OLD BOXES x=$llx_spine, y=$lly, w=$spine_width, h=$label_height";
354 ## _draw_box( $llx_circ1, $lly, ($circ_width), ($label_height) );
355 ## _draw_box( $llx_circ2, $lly, ($circ_width), ($label_height) );
357 ## $lly = ( $lly - $label_height );
359 ## }
364 #=cut