1 # ---------------------------------------------------------------
2 # Copyright (C) 2011 Merrimack Valley Library Consortium
3 # Jason Stephenson <jstephenson@mvlc.org>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
16 package OpenILS::WWW::PhoneList;
22 use Apache2::Const -compile => qw(OK FORBIDDEN HTTP_NO_CONTENT :log);
23 use APR::Const -compile => qw(:error SUCCESS);
26 use Apache2::RequestRec ();
27 use Apache2::RequestIO ();
28 use Apache2::RequestUtil;
32 use OpenSRF::Utils::SettingsClient;
33 use OpenSRF::Utils::Logger qw/$logger/;
34 use OpenILS::Utils::Fieldmapper;
35 use OpenILS::Application::AppUtils;
37 use Text::CSV; # Still only support CSV output.
40 use OpenILS::WWW::PhoneList::Holds;
41 use OpenILS::WWW::PhoneList::Overdues;
43 my $U = 'OpenILS::Application::AppUtils';
53 OpenSRF::System->bootstrap_client(config_file => $bootstrap);
54 my $idl = OpenSRF::Utils::SettingsClient->new->config_value("IDL");
55 Fieldmapper->import(IDL => $idl);
56 OpenILS::Utils::CStoreEditor->init;
57 return Apache2::Const::OK;
63 my $authid = $cgi->cookie('ses') || $cgi->param('ses') || $cgi->cookie('eg.auth.token');
64 if ($authid =~ /^"(.+)"$/) {
67 my $user = $U->simplereq('open-ils.auth', 'open-ils.auth.session.retrieve', $authid);
68 if (!$user || (ref($user) eq 'HASH' && $user->{ilsevent} == 1001)) {
69 return Apache2::Const::FORBIDDEN;
72 my $ou_id = $cgi->cookie("ws_ou") || $cgi->param("ws_ou") || $user->home_ou;
74 # Look for optional addcount parameter. If it is present add a
75 # count column to the end of the csv ouput with a count of the
76 # patron's hold items.
77 my $addcount = defined($cgi->param('addcount'));
79 # Member staff asked for the option to ignore a patron's
80 # preference to receive both a phone and email notice, and skip
81 # them if it looks like they will get an email notice, too.
82 # So we made it an option on the query string.
83 my $skipemail = defined($cgi->param('skipemail'));
85 # Build the args hashref to initialize our functional submodule:
87 'authtoken' => $authid,
92 # Default module to load is Holds.
93 my $module = 'OpenILS::WWW::PhoneList::Holds';
95 # If the overdue parameter is specified, we us the Overdues module
96 # and get the number of days from the due date. If no number of
97 # days is given, or if the argument to overdue is not a number,
98 # then we use a default of 14.
99 if (defined($cgi->param('overdue'))) {
100 $module = 'OpenILS::WWW::PhoneList::Overdues';
102 ($cgi->param('overdue') =~ /^[0-9]+$/) ? $cgi->param('overdue')
104 $args->{'skipemail'} = $skipemail;
106 $args->{'addcount'} = $addcount;
107 $args->{'skipemail'} = $skipemail;
111 my $source = $module->new($args);
113 # check for user permissions:
114 return Apache2::Const::FORBIDDEN unless($source->checkperms);
116 # Tell the source to run its query.
117 if ($source->query()) {
118 my $csv = Text::CSV->new();
119 $r->headers_out->set("Content-Disposition" => "attachment; filename=phone.csv");
120 $r->content_type("text/plain");
122 if ($csv->combine(@{$source->columns})) {
123 $r->print($csv->string . "\n");
126 $r->print($csv->string . "\n") while ($csv->combine(@{$source->next}));
129 # Query failed, so we'll return no content error.
130 return Apache2::Const::HTTP_NO_CONTENT;
133 return Apache2::Const::OK;