2b2ab924b5a0994e64ebb16f5eb3d3dc020924a7
[srvgit] / C4 / Creators / Profile.pm
1 package C4::Creators::Profile;
2
3 use strict;
4 use warnings;
5
6 use autouse 'Data::Dumper' => qw(Dumper);
7
8 use C4::Context;
9 use C4::Creators::Lib qw(get_unit_values);
10
11
12 sub _check_params {
13     my $given_params = {};
14     my $exit_code = 0;
15     my @valid_profile_params = (
16         'printer_name',
17         'template_id',
18         'paper_bin',
19         'offset_horz',
20         'offset_vert',
21         'creep_horz',
22         'creep_vert',
23         'units',
24         'creator',
25     );
26     if (scalar(@_) >1) {
27         $given_params = {@_};
28         foreach my $key (keys %{$given_params}) {
29             if (!(grep m/$key/, @valid_profile_params)) {
30                 warn sprintf('Unrecognized parameter type of "%s".', $key);
31                 $exit_code = 1;
32             }
33         }
34     }
35     else {
36         if (!(grep m/$_/, @valid_profile_params)) {
37             warn sprintf('Unrecognized parameter type of "%s".', $_);
38             $exit_code = 1;
39         }
40     }
41     return $exit_code;
42 }
43
44 sub _conv_points {
45     my $self = shift;
46     my @unit_value = grep {$_->{'type'} eq $self->{units}} @{get_unit_values()};
47     $self->{offset_horz}        = $self->{offset_horz} * $unit_value[0]->{'value'};
48     $self->{offset_vert}        = $self->{offset_vert} * $unit_value[0]->{'value'};
49     $self->{creep_horz}         = $self->{creep_horz} * $unit_value[0]->{'value'};
50     $self->{creep_vert}         = $self->{creep_vert} * $unit_value[0]->{'value'};
51     return $self;
52 }
53
54 sub new {
55     my $invocant = shift;
56     if (_check_params(@_) eq 1) {
57         return -1;
58     }
59     my $type = ref($invocant) || $invocant;
60     my $self = {
61         printer_name    => 'Default Printer',
62         template_id     => '',
63         paper_bin       => 'Tray 1',
64         offset_horz     => 0,
65         offset_vert     => 0,
66         creep_horz      => 0,
67         creep_vert      => 0,
68         units           => 'POINT',
69         @_,
70     };
71     bless ($self, $type);
72     return $self;
73 }
74
75 sub retrieve {
76     my $invocant = shift;
77     my %opts = @_;
78     my $type = ref($invocant) || $invocant;
79     my $query = "SELECT * FROM printers_profile WHERE profile_id = ? AND creator = ?";
80     my $sth = C4::Context->dbh->prepare($query);
81     $sth->execute($opts{'profile_id'}, $opts{'creator'});
82     if ($sth->err) {
83         warn sprintf('Database returned the following error: %s', $sth->errstr);
84         return -1;
85     }
86     my $self = $sth->fetchrow_hashref;
87     $self = _conv_points($self) if ($opts{convert} && $opts{convert} == 1);
88     bless ($self, $type);
89     return $self;
90 }
91
92 sub delete {
93     my $self = {};
94     my %opts = ();
95     my $call_type = '';
96     my @params = ();
97     if (ref($_[0])) {
98         $self = shift;  # check to see if this is a method call
99         $call_type = 'C4::'. $self->{'creator'} .'::Profile->delete';
100         push @params, $self->{'profile_id'}, $self->{'creator'};
101     }
102     else {
103         my $class = shift; #XXX: is this too hackish?
104         %opts = @_;
105         $call_type = $class . "::delete";
106         push @params, $opts{'profile_id'}, $opts{'creator'};
107     }
108     if (scalar(@params) < 2) {   # If there is no profile id or creator type then we cannot delete it
109         warn sprintf('%s : Cannot delete profile as the profile id is invalid or non-existent.', $call_type) if !$params[0];
110         warn sprintf('%s : Cannot delete profile as the creator type is invalid or non-existent.', $call_type) if !$params[1];
111         return -1;
112     }
113     my $query = "DELETE FROM printers_profile WHERE profile_id = ? AND creator = ?";
114     my $sth = C4::Context->dbh->prepare($query);
115 #    $sth->{'TraceLevel'} = 3;
116     $sth->execute(@params);
117     if ($sth->err) {
118         warn sprintf('Database returned the following error on attempted DELETE: %s', $sth->errstr);
119         return -1;
120     }
121     return 0;
122 }
123
124 sub save {
125     my $self = shift;
126     if ($self->{'profile_id'}) {        # if we have an profile_id, the record exists and needs UPDATE
127         my @params;
128         my $query = "UPDATE printers_profile SET ";
129         foreach my $key (keys %{$self}) {
130             next if ($key eq 'profile_id') || ($key eq 'creator');
131             push (@params, $self->{$key});
132             $query .= "$key=?, ";
133         }
134         $query = substr($query, 0, (length($query)-2));
135         push (@params, $self->{'profile_id'}, $self->{'creator'});
136         $query .= " WHERE profile_id=? AND creator=?;";
137         my $sth = C4::Context->dbh->prepare($query);
138 #        $sth->{'TraceLevel'} = 3;
139         $sth->execute(@params);
140         if ($sth->err) {
141             warn sprintf('Database returned the following error on attempted UPDATE: %s', $sth->errstr);
142             return -1;
143         }
144         return $self->{'profile_id'};
145     }
146     else {                      # otherwise create a new record
147         my @params;
148         my $query = "INSERT INTO printers_profile (";
149         foreach my $key (keys %{$self}) {
150             push (@params, $self->{$key});
151             $query .= "$key, ";
152         }
153         $query = substr($query, 0, (length($query)-2));
154         $query .= ") VALUES (";
155         for (my $i=1; $i<=(scalar keys %$self); $i++) {
156             $query .= "?,";
157         }
158         $query = substr($query, 0, (length($query)-1));
159         $query .= ");";
160         my $sth = C4::Context->dbh->prepare($query);
161         $sth->execute(@params);
162         if ($sth->err) {
163             warn sprintf('Database returned the following error on attempted INSERT: %s', $sth->errstr);
164             return -1;
165         }
166         my $sth1 = C4::Context->dbh->prepare("SELECT MAX(profile_id) FROM printers_profile;");
167         $sth1->execute();
168         my $tmpl_id = $sth1->fetchrow_array;
169         return $tmpl_id;
170     }
171 }
172
173 sub get_attr {
174     my $self = shift;
175     if (_check_params(@_) eq 1) {
176         return -1;
177     }
178     my ($attr) = @_;
179     if (exists($self->{$attr})) {
180         return $self->{$attr};
181     }
182     else {
183         warn sprintf('%s is currently undefined.', $attr);
184         return -1;
185     }
186 }
187
188 sub set_attr {
189     my $self = shift;
190     if (_check_params(@_) eq 1) {
191         return -1;
192     }
193     my %attrs = @_;
194     foreach my $attrib (keys(%attrs)) {
195         $self->{$attrib} = $attrs{$attrib};
196     };
197     return 0;
198 }
199
200 1;
201 __END__
202
203 =head1 NAME
204
205 C4::Labels::Profile - A class for creating and manipulating profile objects in Koha
206
207 =head1 ABSTRACT
208
209 This module provides methods for creating, retrieving, and otherwise manipulating label profile objects used by Koha to create and export labels.
210
211 =head1 METHODS
212
213 =head2 new()
214
215     Invoking the I<new> method constructs a new profile object containing the default values for a template.
216     The following parameters are optionally accepted as key => value pairs:
217
218         C<printer_name>         The name of the printer to which this profile applies.
219         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.
220         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.
221         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.
222         C<offset_vert>          Amount of compensation for vertical offset.
223         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).
224         C<creep_vert>           Amount of compensation for vertical creep.
225         C<units>                The units of measure used for this template. These B<must> match the measures you supply above or
226                                 bad things will happen to your document. NOTE: The only supported units at present are:
227
228 =over 9
229
230 =item .
231 POINT   = Postscript Points (This is the base unit in the Koha label creator.)
232
233 =item .
234 AGATE   = Adobe Agates (5.1428571 points per)
235
236 =item .
237 INCH    = US Inches (72 points per)
238
239 =item .
240 MM      = SI Millimeters (2.83464567 points per)
241
242 =item .
243 CM      = SI Centimeters (28.3464567 points per)
244
245 =back
246
247     example:
248         C<my $profile = C4::Labels::Profile->new(); # Creates and returns a new profile object>
249
250         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
251             the supplied values to override the defaults>
252
253     B<NOTE:> This profile is I<not> written to the database until save() is invoked. You have been warned!
254
255 =head2 retrieve(profile_id => $profile_id, convert => 1)
256
257     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.
258     Errors are logged to the Apache log. One further option maybe accessed. See the examples below for further description.
259
260     examples:
261
262         C<my $profile = C4::Labels::Profile->retrieve(profile_id => 1); # Retrieves profile record 1 and returns an object containing the record>
263
264         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>
265
266 =head2 delete()
267
268     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.
269     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.
270
271     examples:
272         C<my $exitstat = $profile->delete(); # to delete the record behind the $profile object>
273         C<my $exitstat = C4::Labels::Profile::delete(profile_id => 1); # to delete profile record 1>
274
275 =head2 save()
276
277     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
278     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.
279
280     example:
281         C<my $exitstat = $profile->save(); # to save the record behind the $profile object>
282
283 =head2 get_attr($attribute)
284
285     Invoking the I<get_attr> method will return the value of the requested attribute or -1 on errors.
286
287     example:
288         C<my $value = $profile->get_attr($attribute);>
289
290 =head2 set_attr(attribute => value, attribute_2 => value)
291
292     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.
293
294     example:
295         $profile->set_attr(attribute => value);
296
297 =head1 AUTHOR
298
299 Chris Nighswonger <cnighswonger AT foundations DOT edu>
300
301 =head1 COPYRIGHT
302
303 Copyright 2009 Foundations Bible College.
304
305 =head1 LICENSE
306
307 This file is part of Koha.
308
309 Koha is free software; you can redistribute it and/or modify it
310 under the terms of the GNU General Public License as published by
311 the Free Software Foundation; either version 3 of the License, or
312 (at your option) any later version.
313
314 Koha is distributed in the hope that it will be useful, but
315 WITHOUT ANY WARRANTY; without even the implied warranty of
316 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
317 GNU General Public License for more details.
318
319 You should have received a copy of the GNU General Public License
320 along with Koha; if not, see <http://www.gnu.org/licenses>.
321
322 =head1 DISCLAIMER OF WARRANTY
323
324 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
325 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
326
327 =cut
328
329 #=head1
330 #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
331 #
332 #=head2 draw_boundaries
333 #
334 # sub draw_boundaries ($llx_spine, $llx_circ1, $llx_circ2,
335 #                $lly, $spine_width, $label_height, $circ_width)
336 #
337 #This sub draws boundary lines where the label outlines are, to aid in printer testing, and debugging.
338 #
339 #=cut
340 #
341 ##       FIXME: Template use for profile adjustment...
342 ##sub draw_boundaries {
343 ##
344 ##    my (
345 ##        $llx_spine, $llx_circ1,  $llx_circ2, $lly,
346 ##        $spine_width, $label_height, $circ_width
347 ##    ) = @_;
348 ##
349 ##    my $lly_initial = ( ( 792 - 36 ) - 90 );
350 ##    $lly            = $lly_initial; # FIXME - why are we ignoring the y_pos parameter by redefining it?
351 ##    my $i             = 1;
352 ##
353 ##    for ( $i = 1 ; $i <= 8 ; $i++ ) {
354 ##
355 ##        _draw_box( $llx_spine, $lly, ($spine_width), ($label_height) );
356 ##
357 ##   #warn "OLD BOXES  x=$llx_spine, y=$lly, w=$spine_width, h=$label_height";
358 ##        _draw_box( $llx_circ1, $lly, ($circ_width), ($label_height) );
359 ##        _draw_box( $llx_circ2, $lly, ($circ_width), ($label_height) );
360 ##
361 ##        $lly = ( $lly - $label_height );
362 ##
363 ##    }
364 ##}
365 #
366 #
367 #
368 #=cut