From 0ed6bcdabbfc82d603b8c15da0f699887c5c2f45 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Andr=C3=A9=20Santoni?= Date: Thu, 10 Dec 2009 17:17:11 +0100 Subject: [PATCH] [MT1234] Huge rewritte to use the API instead of hardcoded SQL in the script Also fixes a bug in the template. --- C4/Contract.pm | 73 ++++++++ admin/aqcontract.pl | 168 ++++++------------ .../prog/en/modules/admin/aqcontract.tmpl | 1 + 3 files changed, 129 insertions(+), 113 deletions(-) create mode 100644 C4/Contract.pm diff --git a/C4/Contract.pm b/C4/Contract.pm new file mode 100644 index 0000000000..01dadd23de --- /dev/null +++ b/C4/Contract.pm @@ -0,0 +1,73 @@ +package C4::Contract; + +# Copyright 2009-2010 BibLibre SARL +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA + +use strict; +use C4::SQLHelper qw(:all); + +use vars qw($VERSION @ISA @EXPORT); + +BEGIN { + # set the version for version checking + $VERSION = 3.2; + require Exporter; + @ISA = qw(Exporter); + @EXPORT = qw( + &GetContract + &AddContract + &ModContract + &DelContract + ); +} + +=head1 NAME + +C4::Contract - Koha functions for dealing with bookseller contracts. + +=head1 SYNOPSIS + +use C4::Contract; + +=head1 DESCRIPTION + +The functions in this module deal with contracts. They allow to +add a new contract, to modify it or to get some informations around +a contract. + +This module is just a wrapper for C4::SQLHelper functions, so take a look at +SQLHelper centralised documentation to know how to use the following subs. + +=cut + +sub GetContract { SearchInTable("aqcontract", shift); } + +sub AddContract { InsertInTable("aqcontract", shift); } + +sub ModContract { UpdateInTable("aqcontract", shift); } + +sub DelContract { DeleteInTable("aqcontract", shift); } + +1; + +__END__ + +=head1 AUTHOR + +Koha Developement team + +=cut diff --git a/admin/aqcontract.pl b/admin/aqcontract.pl index 0d9a8583dc..9476f06464 100755 --- a/admin/aqcontract.pl +++ b/admin/aqcontract.pl @@ -28,26 +28,9 @@ use C4::Auth; use C4::Output; use C4::Dates qw/format_date format_date_in_iso/; use C4::Bookseller qw/GetBookSellerFromId/; - -sub StringSearch { - my ($searchstring)=@_; - my $dbh = C4::Context->dbh; - $searchstring=~ s/\'/\\\'/g; - my @data=split(' ',$searchstring); - $data[0]='' unless $data[0]; - my $sth=$dbh->prepare("Select * from aqcontract where (contractdescription like ? or contractname like ?) order by contractnumber"); - $sth->execute("%$data[0]%","%$data[0]%"); - my @results; - while (my $row=$sth->fetchrow_hashref){ - push(@results,$row); - } - $sth->finish; - return (scalar(@results),\@results); -} +use C4::Contract; my $input = new CGI; -my $searchfield = $input->param('searchfield') || ''; -my $script_name = "/cgi-bin/koha/admin/aqcontract.pl"; my $contractnumber = $input->param('contractnumber'); my $booksellerid = $input->param('booksellerid'); my $op = $input->param('op') || ''; @@ -65,9 +48,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( ); $template->param( - script_name => $script_name, contractnumber => $contractnumber, - searchfield => $searchfield, booksellerid => $booksellerid, booksellername => $bookseller[0]->{name}, DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), @@ -76,24 +57,17 @@ $template->param( #ADD_FORM: called if $op is 'add_form'. Used to create form to add or modify a record if ( $op eq 'add_form' ) { $template->param( add_form => 1 ); - my $data; - - #---- if primkey exists, it's a modify action, so read values to modify... - if ($contractnumber) { - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("select * from aqcontract where contractnumber=?"); - $sth->execute($contractnumber); - $data = $sth->fetchrow_hashref; - $sth->finish; - } + + # if contractnumber exists, it's a modify action, so read values to modify... + my $contract = @{GetContract( { contractnumber => $contractnumber } )}[0] if $contractnumber; $template->param( - contractnumber => $data->{'contractnumber'}, - contractname => $data->{'contractname'}, - contractdescription => $data->{'contractdescription'}, - contractstartdate => format_date( $data->{'contractstartdate'} ), - contractenddate => format_date( $data->{'contractenddate'} ), - DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), + contractnumber => $$contract{contractnumber}, + contractname => $$contract{contractname}, + contractdescription => $$contract{contractdescription}, + contractstartdate => format_date( $$contract{contractstartdate} ), + contractenddate => format_date( $$contract{contractenddate} ), + DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar, ); # END $OP eq ADD_FORM @@ -102,37 +76,29 @@ if ( $op eq 'add_form' ) { elsif ( $op eq 'add_validate' ) { ## Please see file perltidy.ERR $template->param( add_validate => 1 ); + my $is_a_modif = $input->param("is_a_modif"); - my $dbh = C4::Context->dbh; - if ($is_a_modif) { - my $sth = $dbh->prepare( - "UPDATE aqcontract SET contractstartdate=?, - contractenddate=?, - contractname=?, - contractdescription=?, - booksellerid=? WHERE contractnumber=?" - ); - $sth->execute( - format_date_in_iso( $input->param('contractstartdate') ), - format_date_in_iso( $input->param('contractenddate') ), - $input->param('contractname'), - $input->param('contractdescription'), - $input->param('booksellerid'), - $input->param('contractnumber') - ); - $sth->finish; + + if ( $is_a_modif ) { + ModContract({ + contractstartdate => format_date_in_iso( $input->param('contractstartdate') ), + contractenddate => format_date_in_iso( $input->param('contractenddate') ), + contractname => $input->param('contractname'), + contractdescription => $input->param('contractdescription'), + booksellerid => $input->param('booksellerid'), + contractnumber => $input->param('contractnumber'), + }); } else { - my $sth = $dbh->prepare("INSERT INTO aqcontract (contractname,contractdescription,booksellerid,contractstartdate,contractenddate) values (?, ?, ?, ?, ?)"); - $sth->execute( - $input->param('contractname'), - $input->param('contractdescription'), - $input->param('booksellerid'), - format_date_in_iso( $input->param('contractstartdate') ), - format_date_in_iso( $input->param('contractenddate') ) - ); - $sth->finish; + AddContract({ + contractname => $input->param('contractname'), + contractdescription => $input->param('contractdescription'), + booksellerid => $input->param('booksellerid'), + contractstartdate => format_date_in_iso( $input->param('contractstartdate') ), + contractenddate => format_date_in_iso( $input->param('contractenddate') ), + }); } - print "Content-Type: text/html\n\n"; + + print $input->redirect("aqcontract.pl?booksellerid=$booksellerid"); exit; # END $OP eq ADD_VALIDATE @@ -141,28 +107,14 @@ elsif ( $op eq 'add_validate' ) { elsif ( $op eq 'delete_confirm' ) { $template->param( delete_confirm => 1 ); - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("select contractnumber,contractstartdate,contractenddate, - contractname,contractdescription,booksellerid - from aqcontract where contractnumber=?"); - $sth->execute($contractnumber); - my $data = $sth->fetchrow_hashref; - $sth->finish; - - my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $data->{'booksellerid'}"; - my $sth2 = $dbh->prepare($query); - $sth2->execute; - my $result = $sth2->fetchrow; - my $booksellername = $result; + my $contract = @{GetContract( { contractnumber => $contractnumber } )}[0]; $template->param( - contractnumber => $data->{'contractnumber'}, - contractname => $data->{'contractname'}, - contractdescription => $data->{'contractdescription'}, - contractstartdate => format_date( $data->{'contractstartdate'} ), - contractenddate => format_date( $data->{'contractenddate'} ), - booksellerid => $data->{'booksellerid'}, - booksellername => $booksellername, + contractnumber => $$contract{contractnumber}, + contractname => $$contract{contractname}, + contractdescription => $$contract{contractdescription}, + contractstartdate => format_date( $$contract{contractstartdate} ), + contractenddate => format_date( $$contract{contractenddate} ), ); # END $OP eq DELETE_CONFIRM @@ -170,12 +122,10 @@ elsif ( $op eq 'delete_confirm' ) { #DELETE_CONFIRMED: called by delete_confirm, used to effectively confirm deletion of data in DB elsif ( $op eq 'delete_confirmed' ) { $template->param( delete_confirmed => 1 ); - my $dbh = C4::Context->dbh; - my $contractnumber = $input->param('contractnumber'); - my $sth = $dbh->prepare("delete from aqcontract where contractnumber=?"); - $sth->execute($contractnumber); - $sth->finish; - print "Content-Type: text/html\n\n"; + + DelContract( { contractnumber => $contractnumber } ); + + print $input->redirect("aqcontract.pl?booksellerid=$booksellerid"); exit; # END $OP eq DELETE_CONFIRMED @@ -183,28 +133,20 @@ elsif ( $op eq 'delete_confirmed' ) { # DEFAULT: Builds a list of contracts and displays them else { $template->param(else => 1); - my @loop; - my ($count,$results)=StringSearch($searchfield); - for (my $i=0; $i < $count; $i++){ - if ( ($input->param('booksellerid') && $results->[$i]{'booksellerid'} == $input->param('booksellerid')) || ! $input->param('booksellerid') ) { - push @loop, { - contractnumber => $results->[$i]{'contractnumber'}, - contractname => $results->[$i]{'contractname'}, - contractdescription => $results->[$i]{'contractdescription'}, - contractstartdate => format_date($results->[$i]{'contractstartdate'}), - contractenddate => format_date($results->[$i]{'contractenddate'}), - booksellerid => $results->[$i]{'booksellerid'}, - }; - } - } - for my $contract (@loop) { - my $dbh = C4::Context->dbh; - my $query = "SELECT name FROM aqbooksellers WHERE id LIKE $contract->{'booksellerid'}"; - my $sth =$dbh->prepare($query); - $sth->execute; - my $result=$sth->fetchrow; - $contract->{'booksellername'}=$result; + + # get contracts + my @contracts = @{GetContract( { booksellerid => $booksellerid } )}; + + # format dates + for ( @contracts ) { + $$_{contractstartdate} = format_date($$_{contractstartdate}); + $$_{contractenddate} = format_date($$_{contractenddate}); } - $template->param(loop => \@loop); -} #---- END $OP eq DEFAULT + + $template->param(loop => \@contracts); + + #---- END $OP eq DEFAULT +} + output_html_with_http_headers $input, $cookie, $template->output; + diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqcontract.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqcontract.tmpl index a0f6b20f6b..ae85de202d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqcontract.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/aqcontract.tmpl @@ -87,6 +87,7 @@ function Check(ff) {
" method="post"> + " />

Modify contract for

-- 2.39.5