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);
31 use version; our $VERSION = qv('1.0.0_1');
34 # FIXME: Consider this style parameter verification instead...
38 # if (exists $default{$lc})
39 # { $default{$lc} = $param{$_};
42 # { print STDERR "Unknown parameter $_ , not used \n";
48 my @valtmpl_id_params = (
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);
69 if (!(grep m/$_/, @valtmpl_id_params)) {
70 syslog("LOG_ERR", "C4::Labels::Layout : (Single parameter) Unrecognized parameter type of \"%s\".", $_);
79 C4::Labels::Layout -A class for creating and manipulating layout objects in Koha
85 =head2 C4::Labels::Layout->new()
87 Invoking the I<new> method constructs a new layout object containing the default values for a layout.
90 my $layout = Layout->new(); # Creates and returns a new layout object
92 B<NOTE:> This layout is I<not> written to the database untill $layout->save() is invoked. You have been warned!
98 if (_check_params(@_) eq 1) {
101 my $type = ref($invocant) || $invocant;
103 barcode_type => 'CODE39',
104 printing_type => 'BAR',
105 layout_name => 'DEFAULT',
111 format_string => 'title, author, isbn, issn, itemtype, barcode, callnumber',
114 bless ($self, $type);
118 =head2 Layout->retrieve(layout_id => layout_id)
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.
124 my $layout = Layout->retrieve(layout_id => 1); # Retrieves layout record 1 and returns an object containing the record
129 my $invocant = shift;
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'});
136 syslog("LOG_ERR", "Database returned the following error: %s", $sth->errstr);
139 my $self = $sth->fetchrow_hashref;
140 bless ($self, $type);
144 =head2 Layout->delete(layout_id => layout_id) | $layout->delete()
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.
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
159 my $query_param = '';
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'};
167 $call_type = 'C4::Labels::Layout::delete';
168 $query_param = $opts{'layout_id'};
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);
174 my $query = "DELETE FROM labels_layouts WHERE layout_id = ?";
175 my $sth = C4::Context->dbh->prepare($query);
176 $sth->execute($query_param);
178 syslog("LOG_ERR", "%s : Database returned the following error: %s", $call_type, $sth->errstr);
184 =head2 $layout->save()
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.
191 my $exitstat = $layout->save(); # to save the record behind the $layout object
197 if ($self->{'layout_id'}) { # if we have an id, the record exists and needs UPDATE
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=?, ";
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);
212 syslog("LOG_ERR", "C4::Labels::Layout : Database returned the following error: %s", $sth->errstr);
215 return $self->{'layout_id'};
217 else { # otherwise create a new record
219 my $query = "INSERT INTO labels_layouts (";
220 foreach my $key (keys %{$self}) {
221 push (@params, $self->{$key});
224 $query = substr($query, 0, (length($query)-2));
225 $query .= ") VALUES (";
226 for (my $i=1; $i<=(scalar keys %$self); $i++) {
229 $query = substr($query, 0, (length($query)-1));
231 my $sth = C4::Context->dbh->prepare($query);
232 $sth->execute(@params);
234 syslog("LOG_ERR", "C4::Labels::Layout : Database returned the following error: %s", $sth->errstr);
237 my $sth1 = C4::Context->dbh->prepare("SELECT MAX(layout_id) FROM labels_layouts;");
239 my $id = $sth1->fetchrow_array;
244 =head2 $layout->get_attr("attr")
246 Invoking the I<get_attr> method will return the value of the requested attribute or 1 on errors.
249 my $value = $layout->get_attr("attr");
255 if (_check_params(@_) eq 1) {
259 if (exists($self->{$attr})) {
260 return $self->{$attr};
268 =head2 $layout->set_attr(attr => value)
270 Invoking the I<set_attr> method will set the value of the supplied attribute to the supplied value.
273 $layout->set_attr(attr => value);
279 if (_check_params(@_) eq 1) {
283 foreach my $attrib (keys(%attrs)) {
284 $self->{$attrib} = $attrs{$attrib};
293 Chris Nighswonger <cnighswonger AT foundations DOT edu>