From 37bfe446fa908d4c367dab85d936466e087e90fa Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 25 Apr 2014 12:00:30 -0400 Subject: [PATCH] Bug 8352 - Add printer administration, update script to use Koha defined printers This patch adds the ability to define printers within Koha. The printer CRUD is based on the code for the quote of the day system. This patch extends the existing ability to print via LPR to any network printer with the added ability to print to a CUPS printer configured on the Koha server. Test Plan: 1) Apply this patch set 2) Run updatedatabase.pl 3) Browse to admin/printers.pl 4) Configure a network printer via LPR ( explained in the printers help ) 5) Create a hold 6) Execute the command "misc/cronjobs/holds/print_holds.pl -v --printer $BRANCHCODE=$PRINTER_ID where branchcode is the pickup branch for the hold, and printer_id is the id number for the printer. 7) See your reserve slip is printer! Bonus points: Repeat the test plan, but for a printer you set up via CUPS on the Koha server --- C4/Installer/PerlDependencies.pm | 10 + Koha/Schema/Result/Printer.pm | 98 ++++-- admin/printers.pl | 145 +------- installer/data/mysql/updatedatabase.pl | 34 +- .../prog/en/includes/printers-admin-search.inc | 28 -- .../prog/en/includes/printers-toolbar.inc | 4 + .../prog/en/modules/admin/admin-home.tt | 6 +- .../prog/en/modules/admin/printers.tt | 373 +++++++++++--------- .../prog/en/modules/help/admin/printers.tt | 34 ++ misc/cronjobs/holds/print_holds.pl | 51 ++-- svc/printer | 114 ++++++ 11 files changed, 500 insertions(+), 397 deletions(-) delete mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/printers-admin-search.inc create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/printers-toolbar.inc create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/help/admin/printers.tt create mode 100755 svc/printer diff --git a/C4/Installer/PerlDependencies.pm b/C4/Installer/PerlDependencies.pm index 080130b..c4e399c 100644 --- a/C4/Installer/PerlDependencies.pm +++ b/C4/Installer/PerlDependencies.pm @@ -647,11 +647,21 @@ our $PERL_DEPS = { 'required' => '1', 'min_ver' => '0.22', }, + 'Printer' => { + 'usage' => 'Push to printer', + 'required' => '0', + 'min_ver' => '0.98', + }, 'Net::Printer' => { 'usage' => 'Push to printer', 'required' => '0', 'min_ver' => '1.11', }, + 'HTML::HTMLDoc' => { + 'usage' => 'Push to printer', + 'required' => '0', + 'min_ver' => '0.10', + }, 'File::Temp' => { 'usage' => 'Plugins', 'required' => '0', diff --git a/Koha/Schema/Result/Printer.pm b/Koha/Schema/Result/Printer.pm index 7386f2a..986b78c 100644 --- a/Koha/Schema/Result/Printer.pm +++ b/Koha/Schema/Result/Printer.pm @@ -1,21 +1,17 @@ -use utf8; package Koha::Schema::Result::Printer; # Created by DBIx::Class::Schema::Loader # DO NOT MODIFY THE FIRST PART OF THIS FILE -=head1 NAME - -Koha::Schema::Result::Printer - -=cut - use strict; use warnings; use base 'DBIx::Class::Core'; -=head1 TABLE: C + +=head1 NAME + +Koha::Schema::Result::Printer =cut @@ -23,52 +19,102 @@ __PACKAGE__->table("printers"); =head1 ACCESSORS -=head2 printername +=head2 id + + data_type: 'integer' + is_auto_increment: 1 + is_nullable: 0 + +=head2 name data_type: 'varchar' default_value: (empty string) is_nullable: 0 size: 40 -=head2 printqueue +=head2 queue data_type: 'varchar' is_nullable: 1 size: 20 -=head2 printtype +=head2 type - data_type: 'varchar' + data_type: 'text' is_nullable: 1 - size: 20 =cut __PACKAGE__->add_columns( - "printername", + "id", + { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, + "name", { data_type => "varchar", default_value => "", is_nullable => 0, size => 40 }, - "printqueue", - { data_type => "varchar", is_nullable => 1, size => 20 }, - "printtype", + "queue", { data_type => "varchar", is_nullable => 1, size => 20 }, + "type", + { data_type => "text", is_nullable => 1 }, ); +__PACKAGE__->set_primary_key("id"); -=head1 PRIMARY KEY -=over 4 +# Created by DBIx::Class::Schema::Loader v0.07000 @ 2014-05-19 09:04:19 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:uapO0jjBiwFJZjpbJMas/g -=item * L +sub print { + my ( $self, $params ) = @_; -=back + my $data = $params->{data}; + my $is_html = $params->{is_html}; -=cut + 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; -__PACKAGE__->set_primary_key("printername"); + my $printer_name = $self->name(); + my $printer = new Printer( 'linux' => 'lp' ); + $printer->print_command( + linux => { + type => 'pipe', + command => "lp -d $printer_name", + } + ); -# Created by DBIx::Class::Schema::Loader v0.07025 @ 2013-10-14 20:56:21 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:OaIxwtdwn0TJLggwOygC2w + $result = $printer->print($data); + } + return ( $result eq '1', $error ); +} -# You can replace this text with custom content, and it will be preserved on regeneration 1; diff --git a/admin/printers.pl b/admin/printers.pl index c7e7492..97f64cf 100755 --- a/admin/printers.pl +++ b/admin/printers.pl @@ -1,26 +1,7 @@ #!/usr/bin/perl -#script to administer the aqbudget table -#written 20/02/2002 by paul.poulain@free.fr -# This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html) - -# ALGO : -# this script use an $op to know what to do. -# if $op is empty or none of the above values, -# - the default screen is build (with all records, or filtered datas). -# - the user can clic on add, modify or delete record. -# if $op=add_form -# - if primkey exists, this is a modification,so we read the $primkey record -# - builds the add/modify form -# if $op=add_validate -# - the user has just send datas, so we create/modify the record -# if $op=delete_form -# - we show the record having primkey=$primkey and ask for deletion validation form -# if $op=delete_confirm -# - we delete the record having primkey=$primkey - - -# Copyright 2000-2002 Katipo Communications +# Copyright 2014 ByWater Solutions +# Copyright 2012 Foundations Bible College Inc. # # This file is part of Koha. # @@ -38,113 +19,27 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. use strict; -#use warnings; FIXME - Bug 2505 +use warnings; + use CGI; +use autouse 'Data::Dumper' => qw(Dumper); + +use C4::Auth; +use C4::Koha; use C4::Context; use C4::Output; -use C4::Auth; - -sub StringSearch { - my ($searchstring,$type)=@_; # why bother with $type if we don't use it?! - $searchstring=~ s/\'/\\\'/g; - my @data=split(' ',$searchstring); - my $sth = C4::Context->dbh->prepare(" - SELECT printername,printqueue,printtype from printers - WHERE (printername like ?) order by printername - "); - $sth->execute("$data[0]%"); - my $data=$sth->fetchall_arrayref({}); - return (scalar(@$data),$data); -} - -my $input = new CGI; -my $searchfield=$input->param('searchfield'); -#my $branchcode=$input->param('branchcode'); -my $offset=$input->param('offset') || 0; -my $script_name="/cgi-bin/koha/admin/printers.pl"; - -my $pagesize=20; -my $op = $input->param('op'); -$searchfield=~ s/\,//g; - -my ($template, $loggedinuser, $cookie) = get_template_and_user({ - template_name => "admin/printers.tmpl", - query => $input, - type => "intranet", - authnotrequired => 0, - flagsrequired => {parameters => 'parameters_remaining_permissions'}, - debug => 1, -}); - -$template->param(searchfield => $searchfield, - script_name => $script_name); - -#start the page and read in includes - -my $dbh = C4::Context->dbh; -################## ADD_FORM ################################## -# called by default. Used to create form to add or modify a record -if ($op eq 'add_form') { - $template->param(add_form => 1); - #---- if primkey exists, it's a modify action, so read values to modify... - my $data; - if ($searchfield) { - my $sth=$dbh->prepare("SELECT printername,printqueue,printtype from printers where printername=?"); - $sth->execute($searchfield); - $data=$sth->fetchrow_hashref; - } - - $template->param(printqueue => $data->{'printqueue'}, - printtype => $data->{'printtype'}); - # END $OP eq ADD_FORM -################## ADD_VALIDATE ################################## -# called by add_form, used to insert/modify data in DB -} elsif ($op eq 'add_validate') { - $template->param(add_validate => 1); - if ($input->param('add')){ - my $sth=$dbh->prepare("INSERT INTO printers (printername,printqueue,printtype) VALUES (?,?,?)"); - $sth->execute($input->param('printername'),$input->param('printqueue'),$input->param('printtype')); - } else { - my $sth=$dbh->prepare("UPDATE printers SET printqueue=?,printtype=? WHERE printername=?"); - $sth->execute($input->param('printqueue'),$input->param('printtype'),$input->param('printername')); - } - # END $OP eq ADD_VALIDATE -################## DELETE_CONFIRM ################################## -# called by default form, used to confirm deletion of data in DB -} elsif ($op eq 'delete_confirm') { - $template->param(delete_confirm => 1); - my $sth=$dbh->prepare("select printername,printqueue,printtype from printers where printername=?"); - $sth->execute($searchfield); - my $data=$sth->fetchrow_hashref; - $template->param(printqueue => $data->{'printqueue'}, - printtype => $data->{'printtype'}); - # END $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 $sth=$dbh->prepare("delete from printers where printername=?"); - $sth->execute($searchfield); - # END $OP eq DELETE_CONFIRMED -################## DEFAULT ########################################### -} else { # DEFAULT - $template->param(else => 1); - my ($count,$results)=StringSearch($searchfield,'web'); - my $max = ($offset+$pagesize < $count) ? $offset+$pagesize : $count; - my @loop = (@$results)[$offset..$max]; - - $template->param(loop => \@loop); - - if ($offset>0) { - $template->param(offsetgtzero => 1, - prevpage => $offset-$pagesize); - } - if ($offset+$pagesize<$count) { - $template->param(ltcount => 1, - nextpage => $offset+$pagesize); - } -} #---- END $OP eq DEFAULT +my $cgi = new CGI; -output_html_with_http_headers $input, $cookie, $template->output; +my ( $template, $borrowernumber, $cookie ) = get_template_and_user( + { + template_name => "admin/printers.tt", + query => $cgi, + type => "intranet", + authnotrequired => 0, + flagsrequired => { parameters => 'parameters_remaining_permissions' }, + debug => 1, + } +); +output_html_with_http_headers $cgi, $cookie, $template->output; diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 26acbeb..0b0ea68 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -8553,25 +8553,29 @@ if (CheckVersion($DBversion)) { $DBversion = "3.17.00.XXX"; if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) { - $dbh->do("ALTER TABLE reserves ADD printed BOOLEAN NULL AFTER suspend_until"); - $dbh->do("INSERT INTO `letter` ( - `module` , - `code` , - `branchcode` , - `name` , - `is_html` , - `title` , - `content` - ) - VALUES ( - 'reserves', 'HOLD_PLACED_PRINT', '', 'Hold Placed ( Auto-Print )', '0', 'Hold Placed ( Auto-Print )', 'Hold to pull at <> + $dbh->do("ALTER TABLE reserves ADD printed DATETIME NULL AFTER suspend_until"); + $dbh->do(" + INSERT INTO letter ( + module, code, branchcode, name, is_html, title, content + ) VALUES ( + 'reserves', 'HOLD_PLACED_PRINT', '', 'Hold Placed ( Auto-Print )', '0', 'Hold Placed ( Auto-Print )', +'Hold to pull at <> - For <> <> ( <> ) +For <> <> ( <> ) - <> by <>' +<> by <>' )"); - print "Upgrade to $DBversion done (Add print holds to pull notices on demand feature)\n"; + $dbh->do(q{ + ALTER TABLE printers + DROP PRIMARY KEY, + ADD id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST, + CHANGE printername name TEXT NOT NULL DEFAULT '', + CHANGE printqueue queue TEXT NULL DEFAULT NULL, + CHANGE printtype type TEXT NULL DEFAULT NULL + }); + + print "Upgrade to $DBversion done (Bug 8352 - Add automatic printing of 'hold to pull' notices)\n"; SetVersion($DBversion); } diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/printers-admin-search.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/printers-admin-search.inc deleted file mode 100644 index 0fbc282..0000000 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/printers-admin-search.inc +++ /dev/null @@ -1,28 +0,0 @@ -
-

[% LibraryName %]

- -
- diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/printers-toolbar.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/printers-toolbar.inc new file mode 100644 index 0000000..416de09 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/printers-toolbar.inc @@ -0,0 +1,4 @@ + diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt index 217b069..ccad4e3 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/admin-home.tt @@ -98,10 +98,12 @@

Additional parameters

- +
Printers
+
Define your configured network printers
+
Z39.50 client targets
Define which servers to query for MARC data in the integrated Z39.50 client.
+
Did you mean?
Choose which plugins to use to suggest searches to patrons and staff.
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 151e8d6..5d6eaea 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/printers.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/printers.tt @@ -1,191 +1,212 @@ [% INCLUDE 'doc-head-open.inc' %] -Koha › Administration › -[% IF ( add_form ) %][% IF ( searchfield ) %] Printers › Modify printer '[% searchfield %]'[% ELSE %] Printers › New printer[% END %][% END %] -[% IF ( add_validate ) %] Printers › Printer added[% END %] -[% IF ( delete_confirm ) %] Printers › Confirm deletion of printer '[% searchfield %]'[% END %] -[% IF ( delete_confirmed ) %] Printers › Printer deleted[% END %] -[% IF ( else ) %]Printers[% END %] +Koha › Administration › Printer editor [% INCLUDE 'doc-head-close.inc' %] -[% IF ( add_form ) %] + + - [% END %] - + [% INCLUDE 'header.inc' %] -[% INCLUDE 'printers-admin-search.inc' %] +[% INCLUDE 'cat-search.inc' %] - +
- -
-
-
- -[% IF ( add_form ) %] - -[% IF ( searchfield ) %] -

Modify printer

- [% ELSE %] -

New printer

- [% END %] -
- - [% IF ( searchfield ) %] - - [% ELSE %] - - [% END %] -
-
    [% IF ( searchfield ) %] -
  1. - Printer name: - [% searchfield %] -
  2. - [% ELSE %] -
  3. - - -
  4. - [% END %] -
  5. - - -
  6. -
  7. - - -
-
-
Cancel
-
- -[% END %] - -[% IF ( add_validate ) %] -

Printer added

-
-
-
-[% END %] - -[% IF ( delete_confirm ) %] -

Confirm deletion of printer [% searchfield %]

-
    -
  • - Printer: - [% searchfield %] -
  • -
  • - Queue: - [% printqueue %] -
  • -
  • - Type: - [% printtype %] -
  • -
-
- - - -
- -
-[% END %] - -[% IF ( delete_confirmed ) %] -

Printer deleted

-
-
-
-[% END %] - -[% IF ( else ) %] - - - -

Printers

- [% IF ( searchfield ) %] - You searched for [% searchfield %] - [% END %] - -[% IF ( loop ) %] - - - - - - - [% FOREACH loo IN loop %] - [% IF ( loop.odd ) %][% ELSE %][% END %] - - - - - - [% END %] -
NameQueueType 
[% loo.printername %][% loo.printqueue %][% loo.printtype %]Edit Delete
[% ELSE %]
No printers defined.
[% END %] - - [% IF ( offsetgtzero ) %] - << Previous - [% END %] - - [% IF ( ltcount ) %] - Next >> - [% END %] -[% END %] - -
-
-
-[% INCLUDE 'admin-menu.inc' %] -
+
+
+
+ [% INCLUDE 'printers-toolbar.inc' %] +

Printers

+ + + + + + + + + +
IDNameServer:PortType
+
+
+
+
[% INCLUDE 'intranet-bottom.inc' %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/help/admin/printers.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/help/admin/printers.tt new file mode 100644 index 0000000..36abeed --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/help/admin/printers.tt @@ -0,0 +1,34 @@ +[% INCLUDE 'help-top.inc' %] + +

Printers

+ +

Adding and modifying printers

+ +

Click on the 'Add printer' button to add a single printer; Press the <Enter> key to save the printer.

+

Click on any field to edit the contents; Press the <Enter> key to save edit.

+

Click on one or more printer IDs to select entire printers for deletion; Click the 'Delete printer(s)' button to delete selected printers.

+ +

Printers defined in this area can be used to print automatic reserve notices

+ +

How printers are defined

+

Printers can be utilized in two ways, either by sending the data directly to a networked printer via lpr, or by printing to a printer set up on the Koha server via CUPS.

+ +

Printing via LPR

+

+ To configure a printer for this mode, create a new printer in Koha and give it a name, and under the Queue / Server:Port field enter the printer address and the port the LPR service is running under ( usually port 515 ). +

+ +

Printing via CUPS

+

+ To configure a printer for this mode, simply enter the CUPS name for this printer in the Name field. No other options are needed. +

+ +

Caveats

+

+ Printing plain text is supported by both methods. When printing HTML, the output will be sent to the printer as PostScript. If you are printing via LPR, and you want to print HTML, you will need to ensure that your printer can recieve and print PostScript data. +

+

+ For printing slips and notices, this behavior can be controlled via the "HTML message" flag of the HOLD_PLACED_PRINT notice. Leaving this checkbox unchecked will result in plain text being printed, whereas checking this checkbox will result in HTML converted to PostScript being printed. + +

+[% INCLUDE 'help-bottom.inc' %] diff --git a/misc/cronjobs/holds/print_holds.pl b/misc/cronjobs/holds/print_holds.pl index 05d0db6..dd97c7b 100755 --- a/misc/cronjobs/holds/print_holds.pl +++ b/misc/cronjobs/holds/print_holds.pl @@ -34,6 +34,7 @@ use Net::Printer; use C4::Context; use C4::Members qw(GetMember); use C4::Letters qw(GetPreparedLetter); +use Koha::Database; my $help = 0; my $test = 0; @@ -48,14 +49,12 @@ GetOptions( pod2usage(1) if $help; pod2usage(1) unless @printers; +my $schema = Koha::Database->new()->schema(); + my %printers; foreach my $p (@printers) { - my ( $branchcode, $printer, $server, $port ) = split( /,/, $p ); - my %options; - $options{'printer'} = $printer if ($printer); - $options{'server'} = $server if ($server); - $options{'port'} = $port if ($port); - $printers{$branchcode} = new Net::Printer(%options); + my ( $branchcode, $printer_id ) = split( /:/, $p ); + $printers{$branchcode} = $schema->resultset('Printer')->find($printer_id); } my $dbh = C4::Context->dbh; @@ -65,17 +64,17 @@ $sth->execute(); my $set_printed_query = " UPDATE reserves - SET printed = 1 + SET printed = NOW() WHERE reserve_id = ? "; my $set_printed_sth = $dbh->prepare($set_printed_query); while ( my $hold = $sth->fetchrow_hashref() ) { if ($verbose) { - print "\nFound Notice to Print\n"; - print "Borrowernumber: " . $hold->{'borrowernumber'} . "\n"; - print "Biblionumber: " . $hold->{'biblionumber'} . "\n"; - print "Branch: " . $hold->{'branchcode'} . "\n"; + say "\nFound Notice to Print"; + say "Borrowernumber: " . $hold->{'borrowernumber'}; + say "Biblionumber: " . $hold->{'biblionumber'}; + say "Branch: " . $hold->{'branchcode'}; } my $borrower = @@ -96,12 +95,14 @@ while ( my $hold = $sth->fetchrow_hashref() ) { if ( defined( $printers{ $hold->{branchcode} } ) ) { unless ($test) { - my $result = - $printers{ $hold->{'branchcode'} } - ->printstring( $letter->{'content'} ); - my $error = $printers{ $hold->{'branchcode'} }->printerror(); - - unless ($error) { + my ( $success, $error ) = $printers{ $hold->{'branchcode'} }->print( + { + data => $letter->{content}, + is_html => $letter->{is_html}, + } + ); + + if ($success) { $set_printed_sth->execute( $hold->{'reserve_id'} ); } else { @@ -109,12 +110,12 @@ while ( my $hold = $sth->fetchrow_hashref() ) { } } else { - print "TEST MODE, notice will not be printed\n"; - print $letter->{'content'} . "\n"; + say "TEST MODE, notice will not be printed"; + say $letter->{'content'}; } } else { - print "WARNING: No printer defined for branchcode " + say "WARNING: No printer defined for branchcode " . $hold->{'branchcode'} if ($verbose); } @@ -129,21 +130,21 @@ Print Holds =head1 SYNOPSIS -print_holds.pl --printer ,,, +print_holds.pl --printer = =head1 OPTIONS =over 8 -=item B<-help> +=item B<--help> Print a brief help message and exits. -=item B<-printer> +=item B<--printer> -Adds a printer, the value is the branchcode, printer name, server name and server port separated by commas. +Adds a printer to use in the format BRANCHCODE=PRINTER_ID. -e.g. print_holds.pl --printer MPL,lp,printserver,515 --printer CPL,lp2,printserver,516 +e.g. print_holds.pl --printer MPL:1 --printer CPL:2 would add printers for branchcodes MPL and CPL. If a printer is not defined for a given branch, notices for that branch will not be printed. diff --git a/svc/printer b/svc/printer new file mode 100755 index 0000000..12f4580 --- /dev/null +++ b/svc/printer @@ -0,0 +1,114 @@ +#!/usr/bin/perl + +# Copyright 2014 ByWater Solutions +# Copyright 2012 Foundations Bible College Inc. +# +# 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., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use strict; +use warnings; + +use CGI; +use JSON; +use autouse 'Data::Dumper' => qw(Dumper); + +use C4::Auth; +use C4::Context; + +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 $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; + + print to_json( + { + iTotalRecords => @$aaData, + iTotalDisplayRecords => @$aaData, + sEcho => $params->{sEcho}, + aaData => $aaData, + }, + { utf8 => 1 } + ); +} +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 } ); + 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 $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; + } + + $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 ); + + 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; +} -- 1.7.2.5