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