]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/PhoneList.pm
LP#1673857: teach catalog how to search and display copy tags
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / PhoneList.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2011 Merrimack Valley Library Consortium
3 # Jason Stephenson <jstephenson@mvlc.org>
4
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.
9
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 # ---------------------------------------------------------------
15
16 package OpenILS::WWW::PhoneList;
17 use strict;
18 use warnings;
19 use bytes;
20
21 use Apache2::Log;
22 use Apache2::Const -compile => qw(OK FORBIDDEN HTTP_NO_CONTENT :log);
23 use APR::Const    -compile => qw(:error SUCCESS);
24 use APR::Table;
25
26 use Apache2::RequestRec ();
27 use Apache2::RequestIO ();
28 use Apache2::RequestUtil;
29 use CGI;
30
31 use OpenSRF::System;
32 use OpenSRF::Utils::SettingsClient;
33 use OpenSRF::Utils::Logger qw/$logger/;
34 use OpenILS::Utils::Fieldmapper;
35 use OpenILS::Application::AppUtils;
36
37 use Text::CSV; # Still only support CSV output.
38
39 # Our submodules.
40 use OpenILS::WWW::PhoneList::Holds;
41 use OpenILS::WWW::PhoneList::Overdues;
42
43 my $U = 'OpenILS::Application::AppUtils';
44
45 my $bootstrap;
46
47 sub import {
48     my $self = shift;
49     $bootstrap = shift;
50 }
51
52 sub child_init {
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;
58 }
59
60 sub handler {
61     my $r = shift;
62     my $cgi = new CGI;
63     my $authid = $cgi->cookie('ses') || $cgi->param('ses');
64     my $user = $U->simplereq('open-ils.auth', 'open-ils.auth.session.retrieve', $authid);
65     if (!$user || (ref($user) eq 'HASH' && $user->{ilsevent} == 1001)) {
66         return Apache2::Const::FORBIDDEN;
67     }
68
69     my $ou_id = $cgi->cookie("ws_ou") || $cgi->param("ws_ou") || $user->home_ou;
70
71     # Look for optional addcount parameter. If it is present add a
72     # count column to the end of the csv ouput with a count of the
73     # patron's hold items.
74     my $addcount = defined($cgi->param('addcount'));
75
76     # Member staff asked for the option to ignore a patron's
77     # preference to receive both a phone and email notice, and skip
78     # them if it looks like they will get an email notice, too.
79     # So we made it an option on the query string.
80     my $skipemail = defined($cgi->param('skipemail'));
81
82     # Build the args hashref to initialize our functional submodule:
83     my $args = {
84                 'authtoken' => $authid,
85                 'user' => $user->id,
86                 'work_ou' => $ou_id,
87                };
88
89     # Default module to load is Holds.
90     my $module = 'OpenILS::WWW::PhoneList::Holds';
91
92     # If the overdue parameter is specified, we us the Overdues module
93     # and get the number of days from the due date. If no number of
94     # days is given, or if the argument to overdue is not a number,
95     # then we use a default of 14.
96     if (defined($cgi->param('overdue'))) {
97         $module = 'OpenILS::WWW::PhoneList::Overdues';
98         $args->{'days'} =
99             ($cgi->param('overdue') =~ /^[0-9]+$/) ? $cgi->param('overdue')
100                 : 14;
101         $args->{'skipemail'} = $skipemail;
102     } else {
103         $args->{'addcount'} = $addcount;
104         $args->{'skipemail'} = $skipemail;
105     }
106
107     # Load the module.
108     my $source = $module->new($args);
109
110     # check for user permissions:
111     return Apache2::Const::FORBIDDEN unless($source->checkperms);
112
113     # Tell the source to run its query.
114     if ($source->query()) {
115         my $csv = Text::CSV->new();
116         $r->headers_out->set("Content-Disposition" => "attachment; filename=phone.csv");
117         $r->content_type("text/plain");
118         # Print the columns
119         if ($csv->combine(@{$source->columns})) {
120             $r->print($csv->string . "\n");
121         }
122         # Print the results
123         $r->print($csv->string . "\n") while ($csv->combine(@{$source->next}));
124     }
125     else {
126         # Query failed, so we'll return no content error.
127         return Apache2::Const::HTTP_NO_CONTENT;
128     }
129
130     return Apache2::Const::OK;
131 }
132
133 1;