Bugzilla – Attachment 41583 Details for
Bug 8352
Add automatic printing of 'hold to pull' notices
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 8352 [QA Followup] - Don't use SQL in pl files
Bug-8352-QA-Followup---Dont-use-SQL-in-pl-files.patch (text/plain), 17.42 KB, created by
Kyle M Hall (khall)
on 2015-08-18 13:48:08 UTC
(
hide
)
Description:
Bug 8352 [QA Followup] - Don't use SQL in pl files
Filename:
MIME Type:
Creator:
Kyle M Hall (khall)
Created:
2015-08-18 13:48:08 UTC
Size:
17.42 KB
patch
obsolete
>From a6f46f2ab99ff69ac1fc0e477f61d176d5bb2cc4 Mon Sep 17 00:00:00 2001 >From: Kyle M Hall <kyle@bywatersolutions.com> >Date: Tue, 18 Aug 2015 09:39:04 -0400 >Subject: [PATCH] Bug 8352 [QA Followup] - Don't use SQL in pl files > >--- > Koha/Object.pm | 12 ++ > Koha/Objects.pm | 13 +++ > Koha/Printer.pm | 114 ++++++++++++++++++++ > Koha/Printers.pm | 58 ++++++++++ > Koha/Schema/Result/Printer.pm | 56 ---------- > .../prog/en/modules/admin/printers.tt | 3 - > misc/cronjobs/holds/print_holds.pl | 66 +++++------- > svc/printer | 78 ++++---------- > 8 files changed, 249 insertions(+), 151 deletions(-) > create mode 100644 Koha/Printer.pm > create mode 100644 Koha/Printers.pm > >diff --git a/Koha/Object.pm b/Koha/Object.pm >index a77eb5c..6e53461 100644 >--- a/Koha/Object.pm >+++ b/Koha/Object.pm >@@ -207,6 +207,18 @@ sub id { > return $id; > } > >+=head3 $object->unblessed(); >+ >+Returns an unblessed representation of object. >+ >+=cut >+ >+sub unblessed { >+ my ($self) = @_; >+ >+ return { $self->_result->get_columns }; >+} >+ > =head3 $object->_result(); > > Returns the internal DBIC Row object >diff --git a/Koha/Objects.pm b/Koha/Objects.pm >index 8ef6390..4ce42ad6 100644 >--- a/Koha/Objects.pm >+++ b/Koha/Objects.pm >@@ -181,6 +181,19 @@ sub as_list { > return wantarray ? @objects : \@objects; > } > >+=head3 Koha::Objects->unblessed >+ >+Returns an unblessed representation of objects. >+ >+=cut >+ >+sub unblessed { >+ my ($self) = @_; >+ >+ return [ map { $_->unblessed } $self->as_list ]; >+} >+ >+ > =head3 Koha::Objects->_wrap > > wraps the DBIC object in a corresponding Koha object >diff --git a/Koha/Printer.pm b/Koha/Printer.pm >new file mode 100644 >index 0000000..c7965e2 >--- /dev/null >+++ b/Koha/Printer.pm >@@ -0,0 +1,114 @@ >+package Koha::Printer; >+ >+# Copyright ByWater Solutions 2014 >+# >+# 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 3 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., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+use Modern::Perl; >+ >+use Carp; >+ >+use Koha::Database; >+ >+use base qw(Koha::Object); >+ >+=head1 NAME >+ >+Koha::Printer - Koha Printer Object class >+ >+=head1 API >+ >+=head2 Class Methods >+ >+=cut >+ >+=head3 print >+ >+$printer->print( { data => $data, is_html => $is_html } ); >+ >+=cut >+ >+sub print { >+ my ( $self, $params ) = @_; >+ >+ my $data = $params->{data}; >+ my $is_html = $params->{is_html}; >+ >+ return unless ($data); >+ >+ if ($is_html) { >+ require HTML::HTMLDoc; >+ my $htmldoc = new HTML::HTMLDoc(); >+ $htmldoc->set_output_format('ps'); >+ $htmldoc->set_html_content($data); >+ my $doc = $htmldoc->generate_pdf(); >+ $data = $doc->to_string(); >+ } >+ >+ my ( $result, $error ); >+ >+ if ( $self->queue() =~ /:/ ) { >+ >+ # Printer has a server:port address, use Net::Printer >+ require Net::Printer; >+ >+ my ( $server, $port ) = split( /:/, $self->queue() ); >+ >+ my $printer = new Net::Printer( >+ printer => $self->name(), >+ server => $server, >+ port => $port, >+ lineconvert => "YES" >+ ); >+ >+ $result = $printer->printstring($data); >+ >+ $error = $printer->printerror(); >+ } >+ else { >+ require Printer; >+ >+ my $printer_name = $self->name(); >+ >+ my $printer = new Printer( 'linux' => 'lp' ); >+ $printer->print_command( >+ linux => { >+ type => 'pipe', >+ command => "lp -d $printer_name", >+ } >+ ); >+ >+ $result = $printer->print($data); >+ } >+ >+ return ( $result eq '1', $error ); >+} >+ >+=head3 type >+ >+=cut >+ >+sub type { >+ return 'Printer'; >+} >+ >+=head1 AUTHOR >+ >+Kyle M Hall <kyle@bywatersolutions.com> >+ >+=cut >+ >+1; >diff --git a/Koha/Printers.pm b/Koha/Printers.pm >new file mode 100644 >index 0000000..a67da1a >--- /dev/null >+++ b/Koha/Printers.pm >@@ -0,0 +1,58 @@ >+package Koha::Printers; >+ >+# Copyright ByWater Solutions 2014 >+# >+# 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 3 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., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+use Modern::Perl; >+ >+use Carp; >+ >+use Koha::Database; >+ >+use Koha::Printer; >+ >+use base qw(Koha::Objects); >+ >+=head1 NAME >+ >+Koha::Printer - Koha Printer Object set class >+ >+=head1 API >+ >+=head2 Class Methods >+ >+=cut >+ >+=head3 type >+ >+=cut >+ >+sub type { >+ return 'Printer'; >+} >+ >+sub object_class { >+ return 'Koha::Printer'; >+} >+ >+=head1 AUTHOR >+ >+Kyle M Hall <kyle@bywatersolutions.com> >+ >+=cut >+ >+1; >diff --git a/Koha/Schema/Result/Printer.pm b/Koha/Schema/Result/Printer.pm >index 986b78c..d628557 100644 >--- a/Koha/Schema/Result/Printer.pm >+++ b/Koha/Schema/Result/Printer.pm >@@ -61,60 +61,4 @@ __PACKAGE__->set_primary_key("id"); > # Created by DBIx::Class::Schema::Loader v0.07000 @ 2014-05-19 09:04:19 > # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:uapO0jjBiwFJZjpbJMas/g > >-sub print { >- my ( $self, $params ) = @_; >- >- my $data = $params->{data}; >- my $is_html = $params->{is_html}; >- >- return unless ($data); >- >- if ($is_html) { >- require HTML::HTMLDoc; >- my $htmldoc = new HTML::HTMLDoc(); >- $htmldoc->set_output_format('ps'); >- $htmldoc->set_html_content($data); >- my $doc = $htmldoc->generate_pdf(); >- $data = $doc->to_string(); >- } >- >- my ( $result, $error ); >- >- if ( $self->queue() =~ /:/ ) { >- >- # Printer has a server:port address, use Net::Printer >- require Net::Printer; >- >- my ( $server, $port ) = split( /:/, $self->queue() ); >- >- my $printer = new Net::Printer( >- printer => $self->name(), >- server => $server, >- port => $port, >- lineconvert => "YES" >- ); >- >- $result = $printer->printstring($data); >- >- $error = $printer->printerror(); >- } >- else { >- require Printer; >- >- my $printer_name = $self->name(); >- >- my $printer = new Printer( 'linux' => 'lp' ); >- $printer->print_command( >- linux => { >- type => 'pipe', >- command => "lp -d $printer_name", >- } >- ); >- >- $result = $printer->print($data); >- } >- >- return ( $result eq '1', $error ); >-} >- > 1; >diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/printers.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/printers.tt >index 065e62a..e11219b 100644 >--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/printers.tt >+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/printers.tt >@@ -23,7 +23,6 @@ $(document).ready(function() { > { "sWidth": "5%" }, > { "sWidth": "40%" }, > { "sWidth": "55%" }, >- { "bVisible": false }, > ], > "oLanguage" : { > "oPaginate": { >@@ -56,7 +55,6 @@ $(document).ready(function() { > if ( printerID == "NA") { > $('td', nRow)[0].setAttribute("id","no_edit"); > $('td', nRow)[1].setAttribute("id","no_edit"); >- $('td', nRow)[2].setAttribute("id","no_edit"); > } > else { > $('td', nRow)[0].setAttribute("id","no_edit"); >@@ -198,7 +196,6 @@ $(document).ready(function() { > <th><span style="cursor: help" onclick="event.stopPropagation();alert(MSG_ID_HELP);">ID</span></th> > <th>Name</th> > <th>Server:Port</th> >- <th>Type</th> > </tr> > </thead> > </table> >diff --git a/misc/cronjobs/holds/print_holds.pl b/misc/cronjobs/holds/print_holds.pl >index 095b068..826e5be 100755 >--- a/misc/cronjobs/holds/print_holds.pl >+++ b/misc/cronjobs/holds/print_holds.pl >@@ -1,12 +1,12 @@ > #!/usr/bin/perl > >-# Copyright 2009-2010 Kyle Hall >+# Copyright 2015 ByWater Solutions > # > # 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 >+# Foundation; either version 3 of the License, or (at your option) any later > # version. > # > # Koha is distributed in the hope that it will be useful, but WITHOUT ANY >@@ -31,16 +31,19 @@ use Getopt::Long; > use Pod::Usage; > use Net::Printer; > >-use C4::Context; >-use C4::Members qw(GetMember); > use C4::Letters qw(GetPreparedLetter); >-use Koha::Database; >+ >+use Koha::DateUtils; >+use Koha::Holds; >+use Koha::Printers; >+use Koha::Borrowers; > > my $help = 0; > my $test = 0; > my $verbose = 0; > my $confirm = 0; > my @printers; >+ > GetOptions( > "help|?" => \$help, > "verbose|v" => \$verbose, >@@ -48,60 +51,48 @@ GetOptions( > "confirm|c" => \$confirm, > "printer|p=s" => \@printers, > ); >+ > pod2usage(1) if $help; > pod2usage(1) unless @printers; > pod2usage(1) unless ( $test || $confirm ); > pod2usage(1) if ( $test && $confirm ); > >-my $schema = Koha::Database->new()->schema(); >- > my %printers; > foreach my $p (@printers) { > my ( $branchcode, $printer_id ) = split( /:/, $p ); >- $printers{$branchcode} = $schema->resultset('Printer')->find($printer_id); >+ $printers{$branchcode} = Koha::Printers->find($printer_id); > } > >-my $dbh = C4::Context->dbh; >-my $query = "SELECT * FROM reserves WHERE printed IS NULL"; >-my $sth = $dbh->prepare($query); >-$sth->execute(); >- >-my $set_printed_query = " >- UPDATE reserves >- SET printed = NOW() >- WHERE reserve_id = ? >-"; >-my $set_printed_sth = $dbh->prepare($set_printed_query); >+my $holds = Koha::Holds->search( { printed => undef } ); > >-while ( my $hold = $sth->fetchrow_hashref() ) { >+while ( my $hold = $holds->next() ) { > if ($verbose) { > say "\nFound Notice to Print"; >- say "Borrowernumber: " . $hold->{'borrowernumber'}; >- say "Biblionumber: " . $hold->{'biblionumber'}; >- say "Branch: " . $hold->{'branchcode'}; >+ say "Borrowernumber: " . $hold->borrowernumber; >+ say "Biblionumber: " . $hold->biblionumber; >+ say "Branch: " . $hold->branchcode; > } > >- my $borrower = >- C4::Members::GetMember( 'borrowernumber' => $hold->{'borrowernumber'} ); >+ my $borrower = Koha::Borrowers->find( $hold->borrowernumber ); > > my $letter = GetPreparedLetter( > module => 'reserves', > letter_code => 'HOLD_PLACED_PRINT', >- branchcode => $hold->{branchcode}, >+ branchcode => $hold->branchcode, > tables => { >- branches => $hold->{'branchcode'}, >- biblio => $hold->{'biblionumber'}, >- biblioitems => $hold->{'biblionumber'}, >- items => $hold->{'itemnumber'}, >- borrowers => $borrower, >- reserves => $hold, >+ branches => $hold->branchcode, >+ biblio => $hold->biblionumber, >+ biblioitems => $hold->biblionumber, >+ items => $hold->itemnumber, >+ borrowers => $borrower->unblessed, >+ reserves => $hold->unblessed, > > } > ); > >- if ( defined( $printers{ $hold->{branchcode} } ) ) { >+ if ( defined( $printers{ $hold->branchcode } ) ) { > unless ($test) { >- my ( $success, $error ) = $printers{ $hold->{'branchcode'} }->print( >+ my ( $success, $error ) = $printers{ $hold->branchcode }->print( > { > data => $letter->{content}, > is_html => $letter->{is_html}, >@@ -109,7 +100,9 @@ while ( my $hold = $sth->fetchrow_hashref() ) { > ); > > if ($success) { >- $set_printed_sth->execute( $hold->{'reserve_id'} ); >+ my $ts = output_pref( { dt => dt_from_string(), dateformat => 'iso', timeformat => '24h' } ); >+ $hold->printed($ts); >+ $hold->store(); > } > else { > warn "ERROR: $error"; >@@ -121,8 +114,7 @@ while ( my $hold = $sth->fetchrow_hashref() ) { > } > } > else { >- say "WARNING: No printer defined for branchcode " >- . $hold->{'branchcode'} >+ say "WARNING: No printer defined for branchcode " . $hold->branchcode > if ($verbose); > } > >diff --git a/svc/printer b/svc/printer >index 12f4580..04cd775 100755 >--- a/svc/printer >+++ b/svc/printer >@@ -18,40 +18,35 @@ > # with Koha; if not, write to the Free Software Foundation, Inc., > # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. > >-use strict; >-use warnings; >+use Modern::Perl; > > use CGI; > use JSON; > use autouse 'Data::Dumper' => qw(Dumper); > >-use C4::Auth; >+use C4::Auth qw(check_cookie_auth); > use C4::Context; > >+use Koha::Printer; >+use Koha::Printers; >+ > my $cgi = CGI->new; > my $dbh = C4::Context->dbh; > my $sort_columns = [ "id", "source", "text", "timestamp" ]; > >-my ( $template, $borrowernumber, $cookie ) = get_template_and_user( >- { >- template_name => "", >- query => $cgi, >- type => "intranet", >- authnotrequired => 0, >- flagsrequired => { tools => 'edit_printers' }, >- debug => 1, >- } >-); >+my ( $auth_status, $sessionID ) = check_cookie_auth( $cgi->cookie('CGISESSID'), { tools => 'edit_printers' } ); >+if ( $auth_status ne "ok" ) { >+ exit 0; >+} > > my $params = $cgi->Vars; > > print $cgi->header('application/json; charset=utf-8'); > > unless ( $params->{action} ) { >- my $sth = $dbh->prepare("SELECT * FROM printers"); >- $sth->execute(); >- >- my $aaData = $sth->fetchall_arrayref; >+ my $printers = Koha::Printers->search(); >+ my @aaData = map { [ $_->id, $_->name, $_->queue ] } $printers->as_list(); >+ my $aaData = \@aaData; > > print to_json( > { >@@ -64,51 +59,24 @@ unless ( $params->{action} ) { > ); > } > elsif ( $params->{action} eq 'add' ) { >- my $sth = $dbh->prepare( >- 'INSERT INTO printers ( name, queue, type ) VALUES (?, ?, ?)'); >- $sth->execute( $params->{name}, $params->{queue}, $params->{type} ); >- if ( $sth->err ) { >- warn >- sprintf( 'Database returned the following error: %s', $sth->errstr ); >- exit 1; >- } >- my $new_printer_id = $dbh->{q{mysql_insertid}}; # ALERT: mysqlism here >- $sth = $dbh->prepare('SELECT * FROM printers WHERE id = ?;'); >- $sth->execute($new_printer_id); >- print to_json( $sth->fetchall_arrayref, { utf8 => 1 } ); >+ my $printer = Koha::Printer->new( { name => $params->{name}, queue => $params->{queue} } )->store(); >+ print to_json( [ [ $printer->id, $printer->name, $printer->queue ] ], { utf8 => 1 } ); > exit 1; > } > elsif ( $params->{action} eq 'edit' ) { >- my $aaData = []; >- my $editable_columns = [qw(name queue type)] >- ; # pay attention to element order; these columns match the printers table columns >+ my $editable_columns = >+ [qw(name queue type)]; # pay attention to element order; these columns match the printers table columns >+ my $column = $editable_columns->[ $params->{column} - 1 ]; > >- my $sth = $dbh->prepare( >-"UPDATE printers SET $editable_columns->[$params->{column}-1] = ? WHERE id = ?;" >- ); >- $sth->execute( $params->{value}, $params->{id} ); >- if ( $sth->err ) { >- warn >- sprintf( 'Database returned the following error: %s', $sth->errstr ); >- exit 1; >- } >+ my $printer = Koha::Printers->find( $params->{id} ); >+ $printer->set( { $column => $params->{value} } )->store(); > >- $sth = $dbh->prepare( >-"SELECT $editable_columns->[$params->{column}-1] FROM printers WHERE id = ?;" >- ); >- $sth->execute( $params->{id} ); >- $aaData = $sth->fetchrow_array(); >- print Encode::encode( 'utf8', $aaData ); >+ print Encode::encode( 'utf8', $printer->$column ); > > exit 1; > } > elsif ( $params->{action} eq 'delete' ) { >- my $sth = $dbh->prepare("DELETE FROM printers WHERE id = ?;"); >- $sth->execute( $params->{id} ); >- if ( $sth->err ) { >- warn >- sprintf( 'Database returned the following error: %s', $sth->errstr ); >- exit 1; >- } >- exit 0; >+ my $printer = Koha::Printers->find( $params->{id} ); >+ $printer->delete(); >+ exit $printer->in_storage(); > } >-- >1.7.2.5
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 8352
:
10627
|
10766
|
12523
|
13465
|
14394
|
16338
|
16543
|
27127
|
28333
|
28334
|
28335
|
28651
|
28652
|
28653
|
28654
|
36833
|
36834
|
40749
|
40750
|
40751
|
41579
|
41580
|
41581
|
41582
|
41583
|
44359
|
44360
|
44361
|
44362
|
44363
|
45275
|
45276
|
45277
|
45278
|
45279
|
45362
|
45365
|
45651
|
45652
|
45653
|
45654
|
45655
|
45656
|
45657
|
45658
|
45659
|
46878
|
46879
|
46880
|
46881
|
46882
|
46883
|
46884
|
48127
|
48128
|
48129
|
48130
|
48131
|
48132
|
48133
|
48134
|
48135