[3/40] Work on C4::Labels::Profile module and tests
[koha.git] / C4 / Labels / Profile.pm
1 package C4::Labels::Profile;
2
3 # Copyright 2009 Foundations Bible College.
4 #
5 # This file is part of Koha.
6 #       
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
10 # version.
11 #
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.
15 #
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
19
20 use strict;
21 use warnings;
22 use Sys::Syslog qw(syslog);
23 use Data::Dumper;
24
25 use C4::Context;
26 use C4::Debug;
27
28 BEGIN {
29     use version; our $VERSION = qv('1.0.0_1');
30 }
31
32 my $unit_values = {
33     POINT       => 1,
34     INCH        => 72,
35     MM          => 2.83464567,
36     CM          => 28.3464567,
37 };
38
39 sub _check_params {
40     my $given_params = {};
41     my $exit_code = 0;
42     my @valid_profile_params = (
43         'printer_name',
44         'tmpl_id',
45         'paper_bin',
46         'offset_horz',
47         'offset_vert',
48         'creep_horz',
49         'creep_vert',
50         'unit',
51     );
52     if (scalar(@_) >1) {
53         $given_params = {@_};
54         foreach my $key (keys %{$given_params}) {
55             if (!(grep m/$key/, @valid_profile_params)) {
56                 syslog("LOG_ERR", "C4::Labels::Profile : Unrecognized parameter type of \"%s\".", $key);
57                 $exit_code = 1;
58             }
59         }
60     }
61     else {
62         if (!(grep m/$_/, @valid_profile_params)) {
63             syslog("LOG_ERR", "C4::Labels::Profile : Unrecognized parameter type of \"%s\".", $_);
64             $exit_code = 1;
65         }
66     }
67     return $exit_code;
68 }
69
70 sub _conv_points {
71     my $self = shift;
72     $self->{offset_horz}        = $self->{offset_horz} * $unit_values->{$self->{unit}};
73     $self->{offset_vert}        = $self->{offset_vert} * $unit_values->{$self->{unit}};
74     $self->{creep_horz}         = $self->{creep_horz} * $unit_values->{$self->{unit}};
75     $self->{creep_vert}         = $self->{creep_vert} * $unit_values->{$self->{unit}};
76     return $self;
77 }
78
79 =head1 NAME
80
81 C4::Labels::Profile - A class for creating and manipulating profile objects in Koha
82
83 =cut
84
85 =head1 METHODS
86
87 =head2 C4::Labels::Profile->new()
88
89     Invoking the I<new> method constructs a new profile object containing the default values for a template.
90
91     example:
92         my $profile = Profile->new(); # Creates and returns a new profile object
93
94     B<NOTE:> This profile is I<not> written to the database untill $profile->save() is invoked. You have been warned!
95
96 =cut
97
98 sub new {
99     my $invocant = shift;
100     if (_check_params(@_) eq 1) {
101         return 1;
102     }
103     my $type = ref($invocant) || $invocant;
104     my $self = {
105         printer_name    => '',
106         tmpl_id         => '',
107         paper_bin       => '',
108         offset_horz     => 0,
109         offset_vert     => 0,
110         creep_horz      => 0,
111         creep_vert      => 0,
112         unit            => 'POINT',
113         @_,
114     };
115     bless ($self, $type);
116     return $self;
117 }
118
119 =head2 C4::Labels::Profile->retrieve(profile_id => profile_id, convert => 1)
120
121     Invoking the I<retrieve> method constructs a new profile object containing the current values for profile_id. The method returns
122     a new object upon success and 1 upon failure. Errors are logged to the syslog. One further option maybe accessed. See the examples
123     below for further description.
124
125     examples:
126
127         my $profile = C4::Labels::Profile->retrieve(profile_id => 1); # Retrieves profile record 1 and returns an object containing the record
128
129         my $profile = C4::Labels::Profile->retrieve(profile_id => 1, convert => 1); # Retrieves profile record 1, converts the units to points,
130         and returns an object containing the record
131
132 =cut
133
134 sub retrieve {
135     my $invocant = shift;
136     my %opts = @_;
137     my $type = ref($invocant) || $invocant;
138     my $query = "SELECT * FROM printers_profile WHERE prof_id = ?";  
139     my $sth = C4::Context->dbh->prepare($query);
140     $sth->execute($opts{profile_id});
141     if ($sth->err) {
142         syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
143         return 1;
144     }
145     my $self = $sth->fetchrow_hashref;
146     $self = _conv_points($self) if ($opts{convert} && $opts{convert} == 1);
147     bless ($self, $type);
148     return $self;
149 }
150
151 =head2 C4::Labels::Profile->delete(prof_id => profile_id) |  $profile->delete()
152
153     Invoking the delete method attempts to delete the profile from the database. The method returns 0 upon success
154     and 1 upon failure. Errors are logged to the syslog.
155
156     examples:
157         my $exitstat = $profile->delete(); # to delete the record behind the $profile object
158         my $exitstat = C4::Labels::Profile->delete(prof_id => 1); # to delete profile record 1
159
160 =cut
161
162 sub delete {
163     my $self = shift;
164     if (!$self->{'prof_id'}) {   # If there is no profile prof_id then we cannot delete it
165         syslog("LOG_ERR", "Cannot delete profile as it has not been saved.");
166         return 1;
167     }
168     my $query = "DELETE FROM printers_profile WHERE prof_id = ?";  
169     my $sth = C4::Context->dbh->prepare($query);
170     $sth->execute($self->{'prof_id'});
171     return 0;
172 }
173
174 =head2 $profile->save()
175
176     Invoking the I<save> method attempts to insert the profile into the database if the profile is new and
177     update the existing profile record if the profile exists. The method returns the new record prof_id upon
178     success and -1 upon failure (This avoids conflicting with a record prof_id of 1). Errors are logged to the syslog.
179
180     example:
181         my $exitstat = $profile->save(); # to save the record behind the $profile object
182
183 =cut
184
185 sub save {
186     my $self = shift;
187     if ($self->{'prof_id'}) {        # if we have an prof_id, the record exists and needs UPDATE
188         my @params;
189         my $query = "UPDATE printers_profile SET ";
190         foreach my $key (keys %{$self}) {
191             next if $key eq 'prof_id';
192             push (@params, $self->{$key});
193             $query .= "$key=?, ";
194         }
195         $query = substr($query, 0, (length($query)-2));
196         push (@params, $self->{'prof_id'});
197         $query .= " WHERE prof_id=?;";
198         warn "DEBUG: Updating: $query\n" if $debug;
199         my $sth = C4::Context->dbh->prepare($query);
200         $sth->execute(@params);
201         if ($sth->err) {
202             syslog("LOG_ERR", "C4::Labels::Profile : Database returned the following error: %s", $sth->errstr);
203             return -1;
204         }
205         return $self->{'prof_id'};
206     }
207     else {                      # otherwise create a new record
208         my @params;
209         my $query = "INSERT INTO printers_profile (";
210         foreach my $key (keys %{$self}) {
211             push (@params, $self->{$key});
212             $query .= "$key, ";
213         }
214         $query = substr($query, 0, (length($query)-2));
215         $query .= ") VALUES (";
216         for (my $i=1; $i<=(scalar keys %$self); $i++) {
217             $query .= "?,";
218         }
219         $query = substr($query, 0, (length($query)-1));
220         $query .= ");";
221         warn "DEBUG: Saving: $query\n" if $debug;
222         my $sth = C4::Context->dbh->prepare($query);
223         $sth->execute(@params);
224         if ($sth->err) {
225             syslog("LOG_ERR", "C4::Labels::Profile : Database returned the following error: %s", $sth->errstr);
226             return -1;
227         }
228         my $sth1 = C4::Context->dbh->prepare("SELECT MAX(prof_id) FROM printers_profile;");
229         $sth1->execute();
230         my $tmpl_id = $sth1->fetchrow_array;
231         return $tmpl_id;
232     }
233 }
234
235 =head2 $profile->get_attr(attr)
236
237     Invoking the I<get_attr> method will return the value of the requested attribute or 1 on errors.
238
239     example:
240         my $value = $profile->get_attr(attr);
241
242 =cut
243
244 sub get_attr {
245     my $self = shift;
246     if (_check_params(@_) eq 1) {
247         return 1;
248     }
249     my ($attr) = @_;
250     if (exists($self->{$attr})) {
251         return $self->{$attr};
252     }
253     else {
254         syslog("LOG_ERR", "C4::Labels::Profile : %s is currently undefined.", $attr);
255         return 1;
256     }
257 }
258
259 =head2 $profile->set_attr(attr => value)
260
261     Invoking the I<set_attr> method will set the value of the supplied attribute to the supplied value.
262
263     example:
264         $profile->set_attr(attr => value);
265
266 =cut
267
268 sub set_attr {
269     my $self = shift;
270     if (_check_params(@_) eq 1) {
271         return 1;
272     }
273     my ($attr, $value) = @_;
274     $self->{$attr} = $value;
275     return 0;
276 }
277
278
279 1;
280 __END__
281
282 =head1 AUTHOR
283
284 Chris Nighswonger <cnighswonger AT foundations DOT edu>
285
286 =cut
287
288
289 =head1
290 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
291
292 ead2 draw_boundaries
293
294  sub draw_boundaries ($llx_spine, $llx_circ1, $llx_circ2,
295                 $lly, $spine_width, $label_height, $circ_width)  
296
297 This sub draws boundary lines where the label outlines are, to aid in printer testing, and debugging.
298
299 =cut
300
301 #       FIXME: Template use for profile adjustment...
302 #sub draw_boundaries {
303 #
304 #    my (
305 #        $llx_spine, $llx_circ1,  $llx_circ2, $lly,
306 #        $spine_width, $label_height, $circ_width
307 #    ) = @_;
308 #
309 #    my $lly_initial = ( ( 792 - 36 ) - 90 );
310 #    $lly            = $lly_initial; # FIXME - why are we ignoring the y_pos parameter by redefining it?
311 #    my $i             = 1;
312 #
313 #    for ( $i = 1 ; $i <= 8 ; $i++ ) {
314 #
315 #        _draw_box( $llx_spine, $lly, ($spine_width), ($label_height) );
316 #
317 #   #warn "OLD BOXES  x=$llx_spine, y=$lly, w=$spine_width, h=$label_height";
318 #        _draw_box( $llx_circ1, $lly, ($circ_width), ($label_height) );
319 #        _draw_box( $llx_circ2, $lly, ($circ_width), ($label_height) );
320 #
321 #        $lly = ( $lly - $label_height );
322 #
323 #    }
324 #}
325
326
327
328 =cut