[18/40] Work on label item search.
[koha.git] / C4 / Labels / Layout.pm
1 package C4::Labels::Layout;
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
23 use Sys::Syslog qw(syslog);
24 use DBI qw(neat);
25
26 use C4::Context;
27 use C4::Debug;
28 use Data::Dumper;
29
30 BEGIN {
31     use version; our $VERSION = qv('1.0.0_1');
32 }
33
34 # FIXME: Consider this style parameter verification instead...
35 #  my %param = @_;
36 #   for (keys %param)
37 #    {   my $lc = lc($_); 
38 #        if (exists $default{$lc})
39 #        {  $default{$lc} = $param{$_}; 
40 #        }
41 #        else
42 #        {  print STDERR "Unknown parameter $_ , not used \n";
43 #        }
44 #    }
45
46 sub _check_params {
47     my $exit_code = 0;
48     my @valtmpl_id_params = (
49         'barcode_type',
50         'printing_type',
51         'layout_name',
52         'guidebox',
53         'font',
54         'font_size',
55         'callnum_split',
56         'text_justify',
57         'format_string',
58     );
59     if (scalar(@_) >1) {
60         my %given_params = @_;
61         foreach my $key (keys %given_params) {
62             if (!(grep m/$key/, @valtmpl_id_params)) {
63                 syslog("LOG_ERR", "C4::Labels::Layout : (Multiple parameters) Unrecognized parameter type of \"%s\".", $key);
64                 $exit_code = 1;
65             }
66         }
67     }
68     else {
69         if (!(grep m/$_/, @valtmpl_id_params)) {
70             syslog("LOG_ERR", "C4::Labels::Layout : (Single parameter) Unrecognized parameter type of \"%s\".", $_);
71             $exit_code = 1;
72         }
73     }
74     return $exit_code;
75 }
76
77 =head1 NAME
78
79 C4::Labels::Layout -A class for creating and manipulating layout objects in Koha
80
81 =cut
82
83 =head1 METHODS
84
85 =head2 C4::Labels::Layout->new()
86
87     Invoking the I<new> method constructs a new layout object containing the default values for a layout.
88
89     example:
90         my $layout = Layout->new(); # Creates and returns a new layout object
91
92     B<NOTE:> This layout is I<not> written to the database untill $layout->save() is invoked. You have been warned!
93
94 =cut
95
96 sub new {
97     my $invocant = shift;
98     if (_check_params(@_) eq 1) {
99         return -1;
100     }
101     my $type = ref($invocant) || $invocant;
102     my $self = {
103         barcode_type    =>      'CODE39',
104         printing_type   =>      'BAR',
105         layout_name     =>      'DEFAULT',
106         guidebox        =>      0,
107         font            =>      'TR',
108         font_size       =>      3,
109         callnum_split   =>      0,
110         text_justify    =>      'L',
111         format_string   =>      'title, author, isbn, issn, itemtype, barcode, callnumber',
112         @_,
113     };
114     bless ($self, $type);
115     return $self;
116 }
117
118 =head2 Layout->retrieve(layout_id => layout_id)
119
120     Invoking the I<retrieve> method constructs a new layout object containing the current values for layout_id. The method returns
121     a new object upon success and 1 upon failure. Errors are logged to the syslog.
122
123     example:
124         my $layout = Layout->retrieve(layout_id => 1); # Retrieves layout record 1 and returns an object containing the record
125
126 =cut
127
128 sub retrieve {
129     my $invocant = shift;
130     my %opts = @_;
131     my $type = ref($invocant) || $invocant;
132     my $query = "SELECT * FROM labels_layouts WHERE layout_id = ?";  
133     my $sth = C4::Context->dbh->prepare($query);
134     $sth->execute($opts{'layout_id'});
135     if ($sth->err) {
136         syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
137         return -1;
138     }
139     my $self = $sth->fetchrow_hashref;
140     bless ($self, $type);
141     return $self;
142 }
143
144 =head2 Layout->delete(layout_id => layout_id) |  $layout->delete()
145
146     Invoking the delete method attempts to delete the layout from the database. The method returns 0 upon success
147     and 1 upon failure. Errors are logged to the syslog.
148
149     examples:
150         my $exitstat = $layout->delete(); # to delete the record behind the $layout object
151         my $exitstat = Layout->delete(layout_id => 1); # to delete layout record 1
152
153 =cut
154
155 sub delete {
156     my $self = {};
157     my %opts = ();
158     my $call_type = '';
159     my $query_param = '';
160     if (ref($_[0])) {
161         $self = shift;  # check to see if this is a method call
162         $call_type = 'C4::Labels::Layout->delete';
163         $query_param = $self->{'layout_id'};
164     }
165     else {
166         %opts = @_;
167         $call_type = 'C4::Labels::Layout::delete';
168         $query_param = $opts{'layout_id'};
169     }
170     if ($query_param eq '') {   # If there is no layout id then we cannot delete it
171         syslog("LOG_ERR", "%s : Cannot delete layout as the layout id is invalid or non-existant.", $call_type);
172         return -1;
173     }
174     my $query = "DELETE FROM labels_layouts WHERE layout_id = ?";  
175     my $sth = C4::Context->dbh->prepare($query);
176     $sth->execute($query_param);
177     if ($sth->err) {
178         syslog("LOG_ERR", "%s : Database returned the following error: %s", $call_type, $sth->errstr);
179         return -1;
180     }
181     return 0;
182 }
183
184 =head2 $layout->save()
185
186     Invoking the I<save> method attempts to insert the layout into the database if the layout is new and
187     update the existing layout record if the layout exists. The method returns the new record id upon
188     success and -1 upon failure (This avoids conflicting with a record id of 1). Errors are logged to the syslog.
189
190     example:
191         my $exitstat = $layout->save(); # to save the record behind the $layout object
192
193 =cut
194
195 sub save {
196     my $self = shift;
197     if ($self->{'layout_id'}) {        # if we have an id, the record exists and needs UPDATE
198         my @params;
199         my $query = "UPDATE labels_layouts SET ";
200         foreach my $key (keys %{$self}) {
201             next if $key eq 'layout_id';
202             push (@params, $self->{$key});
203             $query .= "$key=?, ";
204         }
205         $query = substr($query, 0, (length($query)-2));
206         $query .= " WHERE layout_id=?;";
207         push (@params, $self->{'layout_id'});
208         my $sth = C4::Context->dbh->prepare($query);
209         #local $sth->{TraceLevel} = "3";        # enable DBI trace and set level; outputs to STDERR
210         $sth->execute(@params);
211         if ($sth->err) {
212             syslog("LOG_ERR", "C4::Labels::Layout : Database returned the following error: %s", $sth->errstr);
213             return -1;
214         }
215         return $self->{'layout_id'};
216     }
217     else {                      # otherwise create a new record
218         my @params;
219         my $query = "INSERT INTO labels_layouts (";
220         foreach my $key (keys %{$self}) {
221             push (@params, $self->{$key});
222             $query .= "$key, ";
223         }
224         $query = substr($query, 0, (length($query)-2));
225         $query .= ") VALUES (";
226         for (my $i=1; $i<=(scalar keys %$self); $i++) {
227             $query .= "?,";
228         }
229         $query = substr($query, 0, (length($query)-1));
230         $query .= ");";
231         my $sth = C4::Context->dbh->prepare($query);
232         $sth->execute(@params);
233         if ($sth->err) {
234             syslog("LOG_ERR", "C4::Labels::Layout : Database returned the following error: %s", $sth->errstr);
235             return -1;
236         }
237         my $sth1 = C4::Context->dbh->prepare("SELECT MAX(layout_id) FROM labels_layouts;");
238         $sth1->execute();
239         my $id = $sth1->fetchrow_array;
240         return $id;
241     }
242 }
243
244 =head2 $layout->get_attr("attr")
245
246     Invoking the I<get_attr> method will return the value of the requested attribute or 1 on errors.
247
248     example:
249         my $value = $layout->get_attr("attr");
250
251 =cut
252
253 sub get_attr {
254     my $self = shift;
255     if (_check_params(@_) eq 1) {
256         return -1;
257     }
258     my ($attr) = @_;
259     if (exists($self->{$attr})) {
260         return $self->{$attr};
261     }
262     else {
263         return -1;
264     }
265     return;
266 }
267
268 =head2 $layout->set_attr(attr => value)
269
270     Invoking the I<set_attr> method will set the value of the supplied attribute to the supplied value.
271
272     example:
273         $layout->set_attr(attr => value);
274
275 =cut
276
277 sub set_attr {
278     my $self = shift;
279     if (_check_params(@_) eq 1) {
280         return -1;
281     }
282     my %attrs = @_;
283     foreach my $attrib (keys(%attrs)) {
284         $self->{$attrib} = $attrs{$attrib};
285     };
286     return 0;
287 }
288 1;
289 __END__
290
291 =head1 AUTHOR
292
293 Chris Nighswonger <cnighswonger AT foundations DOT edu>
294
295 =cut