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