1 package C4::Labels::Layout;
3 # Copyright 2009 Foundations Bible College.
5 # This file is part of Koha.
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
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.
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
23 use Sys::Syslog qw(syslog);
30 use version; our $VERSION = qv('1.0.0_1');
33 # FIXME: Consider this style parameter verification instead...
37 # if (exists $default{$lc})
38 # { $default{$lc} = $param{$_};
41 # { print STDERR "Unknown parameter $_ , not used \n";
46 my $given_params = {};
48 my @valtmpl_id_params = (
50 'start_label', #remove...pass in as a cgi->param
55 'ccode', #remove...depricated...
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);
70 if (!(grep m/$_/, @valtmpl_id_params)) {
71 syslog("LOG_ERR", "C4::Labels::Template : Unrecognized parameter type of \"%s\".", $_);
80 C4::Labels::Layout -A class for creating and manipulating layout objects in Koha
86 =head2 C4::Labels::Layout->new()
88 Invoking the I<new> method constructs a new layout object containing the default values for a layout.
91 my $layout = Layout->new(); # Creates and returns a new layout object
93 B<NOTE:> This layout is I<not> written to the database untill $layout->save() is invoked. You have been warned!
99 if (_check_params(@_) eq 1) {
102 my $type = ref($invocant) || $invocant;
116 bless ($self, $type);
120 =head2 Layout->retrieve(layout_id => layout_id)
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.
126 my $layout = Layout->retrieve(layout_id => 1); # Retrieves layout record 1 and returns an object containing the record
131 my $invocant = shift;
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'});
138 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
141 my $self = $sth->fetchrow_hashref;
142 bless ($self, $type);
146 =head2 Layout->delete(layout_id => layout_id) | $layout->delete()
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.
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
161 my $query_param = '';
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'};
169 $call_type = 'C4::Labels::Layout::delete';
170 $query_param = $opts{'layout_id'};
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);
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);
182 warn "DB error: $sth->errstr\n";
183 syslog("LOG_ERR", "%s : Database returned the following error: %s", $call_type, $sth->errstr);
189 =head2 $layout->save()
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.
196 my $exitstat = $layout->save(); # to save the record behind the $layout object
202 if ($self->{'layout_id'}) { # if we have an id, the record exists and needs UPDATE
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=?, ";
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);
217 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
220 return $self->{'layout_id'};
222 else { # otherwise create a new record
224 my $query = "INSERT INTO labels_layouts (";
225 foreach my $key (keys %{$self}) {
226 push (@params, $self->{$key});
229 $query = substr($query, 0, (length($query)-2));
230 $query .= ") VALUES (";
231 for (my $i=1; $i<=(scalar keys %$self); $i++) {
234 $query = substr($query, 0, (length($query)-1));
236 warn "DEBUG: Saving: $query\n" if $debug;
237 my $sth = C4::Context->dbh->prepare($query);
238 $sth->execute(@params);
240 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
243 my $sth1 = C4::Context->dbh->prepare("SELECT MAX(layout_id) FROM labels_layouts;");
245 my $id = $sth1->fetchrow_array;
250 =head2 $layout->get_attr("attr")
252 Invoking the I<get_attr> method will return the value of the requested attribute or 1 on errors.
255 my $value = $layout->get_attr("attr");
261 if (_check_params(@_) eq 1) {
265 if (exists($self->{$attr})) {
266 return $self->{$attr};
274 =head2 $layout->set_attr(attr => value)
276 Invoking the I<set_attr> method will set the value of the supplied attribute to the supplied value.
279 $layout->set_attr(attr => value);
285 if (_check_params(@_) eq 1) {
288 my ($attr, $value) = @_;
289 $self->{$attr} = $value;
297 Chris Nighswonger <cnighswonger AT foundations DOT edu>