From 2149191ca8754d325cd66edd4860e475553ff189 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] [SIGNED-OFF] Bug 8352 [QA Followup] - Don't use SQL in pl files
Signed-off-by: Jenice Tate <jtate@mdah.state.ms.us>
---
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 d8f2b42..d404817 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 37d6bc7..57926b5 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": {
@@ -55,7 +54,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");
@@ -192,7 +190,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