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