1 package C4::Labels::Layout;
3 # Copyright 2007 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);
29 #use vars qw($VERSION @ISA @EXPORT);
30 use vars qw($VERSION);
35 # @ISA = qw(Exporter);
39 # FIXME: Consider this style parameter verification instead...
43 # if (exists $default{$lc})
44 # { $default{$lc} = $param{$_};
47 # { print STDERR "Unknown parameter $_ , not used \n";
52 my $given_params = {};
54 my @valtmpl_id_params = (
56 'start_label', #remove...pass in as a cgi->param
61 'ccode', #remove...depricated...
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);
76 if (!(grep m/$_/, @valtmpl_id_params)) {
77 syslog("LOG_ERR", "C4::Labels::Template : Unrecognized parameter type of \"%s\".", $_);
86 C4::Labels::Layout -A class for creating and manipulating layout objects in Koha
92 =head2 C4::Labels::Layout->new()
94 Invoking the I<new> method constructs a new layout object containing the default values for a layout.
97 my $layout = Layout->new(); # Creates and returns a new layout object
99 B<NOTE:> This layout is I<not> written to the database untill $layout->save() is invoked. You have been warned!
104 my $invocant = shift;
105 if (_check_params(@_) eq 1) {
108 my $type = ref($invocant) || $invocant;
122 bless ($self, $type);
126 =head2 Layout->retrieve(layout_id)
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.
132 my $layout = Layout->retrieve(1); # Retrieves layout record 1 and returns an object containing the record
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'});
143 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
146 my $self = $sth->fetchrow_hashref;
147 bless ($self, $type);
151 =head2 Layout->delete(layout_id => layout_id) | $layout->delete()
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.
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
166 my $query_param = '';
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'};
174 $call_type = 'C4::Labels::Layout::delete';
175 $query_param = $opts{'layout_id'};
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);
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);
187 warn "DB error: $sth->errstr\n";
188 syslog("LOG_ERR", "%s : Database returned the following error: %s", $call_type, $sth->errstr);
194 =head2 $layout->save()
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.
201 my $exitstat = $layout->save(); # to save the record behind the $layout object
207 if ($self->{'layout_id'}) { # if we have an id, the record exists and needs UPDATE
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=?, ";
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);
222 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
225 return $self->{'layout_id'};
227 else { # otherwise create a new record
229 my $query = "INSERT INTO labels_layouts (";
230 foreach my $key (keys %{$self}) {
231 push (@params, $self->{$key});
234 $query = substr($query, 0, (length($query)-2));
235 $query .= ") VALUES (";
236 for (my $i=1; $i<=(scalar keys %$self); $i++) {
239 $query = substr($query, 0, (length($query)-1));
241 warn "DEBUG: Saving: $query\n" if $debug;
242 my $sth = C4::Context->dbh->prepare($query);
243 $sth->execute(@params);
245 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
248 my $sth1 = C4::Context->dbh->prepare("SELECT MAX(layout_id) FROM labels_layouts;");
250 my $id = $sth1->fetchrow_array;
255 =head2 $layout->get_attr("attr")
257 Invoking the I<get_attr> method will return the value of the requested attribute or 1 on errors.
260 my $value = $layout->get_attr("attr");
266 if (_check_params(@_) eq 1) {
270 if (exists($self->{$attr})) {
271 return $self->{$attr};
279 =head2 $layout->set_attr(attr => value)
281 Invoking the I<set_attr> method will set the value of the supplied attribute to the supplied value.
284 $layout->set_attr(attr => value);
290 if (_check_params(@_) eq 1) {
293 my ($attr, $value) = @_;
294 $self->{$attr} = $value;
302 Chris Nighswonger <cnighswonger AT foundations DOT edu>