Bug 12497: Fix search history non-accessible when OPAC was private
[koha.git] / members / housebound.pl
1 #!/usr/bin/perl
2
3 # Copyright 2016 PTFS-Europe Ltd
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 housebound.pl
21
22  Script to handle housebound management for patrons.  This single script
23  handles display, creation, deletion and management of profiles and visits.
24
25 =cut
26
27 use Modern::Perl;
28 use CGI;
29 use C4::Auth;
30 use C4::Context;
31 use C4::Members::Attributes qw(GetBorrowerAttributes);
32 use C4::Output;
33 use DateTime;
34 use Koha::DateUtils;
35 use Koha::Libraries;
36 use Koha::Patrons;
37 use Koha::Patron::Categories;
38 use Koha::Patron::HouseboundProfile;
39 use Koha::Patron::HouseboundVisit;
40 use Koha::Patron::HouseboundVisits;
41
42 my $input = CGI->new;
43
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45     {
46         template_name   => 'members/housebound.tt',
47         query           => $input,
48         type            => 'intranet',
49         authnotrequired => 0,
50         flagsrequired   => { borrowers => 1 },
51     }
52 );
53
54 my @messages;                   # For error messages.
55 my $method = $input->param('method') // q{};
56 my $visit_id = $input->param('visit_id') // q{};
57
58 # Get patron
59 my $patron = eval {
60     my $borrowernumber = $input->param('borrowernumber') // q{};
61     return Koha::Patrons->find($borrowernumber);
62 };
63 push @messages, { type => 'error', code => 'error_on_patron_load' }
64     if ( $@ or !$patron );
65
66 # Get supporting cast
67 my ( $branch, $category, $houseboundprofile, $visit, $patron_image );
68 if ( $patron ) {
69     $patron_image = $patron->image;
70     $branch = Koha::Libraries->new->find($patron->branchcode);
71     $category = Koha::Patron::Categories->new->find($patron->categorycode);
72     $houseboundprofile = $patron->housebound_profile;
73 }
74 if ( $visit_id ) {
75     $visit = eval {
76         return Koha::Patron::HouseboundVisits->find($visit_id);
77     };
78     push @messages, { type => 'error', code => 'error_on_visit_load' }
79         if ( $@ or !$visit );
80 }
81
82 # Main processing
83 my ( $deliverers, $choosers, $houseboundvisit );
84
85 if ( $method eq 'updateconfirm' and $houseboundprofile ) {
86     # We have received the input from the profile edit form.  We must save the
87     # changes, and return to simple display.
88     $houseboundprofile->set({
89         day           => scalar $input->param('day')           // q{},
90         frequency     => scalar $input->param('frequency')     // q{},
91         fav_itemtypes => scalar $input->param('fav_itemtypes') // q{},
92         fav_subjects  => scalar $input->param('fav_subjects')  // q{},
93         fav_authors   => scalar $input->param('fav_authors')   // q{},
94         referral      => scalar $input->param('referral')      // q{},
95         notes         => scalar $input->param('notes')         // q{},
96     });
97     my $success = eval { return $houseboundprofile->store };
98     push @messages, { type => 'error', code => 'error_on_profile_store' }
99         if ( $@ or !$success );
100     $method = undef;
101 } elsif ( $method eq 'createconfirm' ) {
102     # We have received the input necessary to create a new profile.  We must
103     # save it, and return to simple display.
104     $houseboundprofile = Koha::Patron::HouseboundProfile->new({
105         borrowernumber => $patron->borrowernumber,
106         day            => scalar $input->param('day')           // q{},
107         frequency      => scalar $input->param('frequency')     // q{},
108         fav_itemtypes  => scalar $input->param('fav_itemtypes') // q{},
109         fav_subjects   => scalar $input->param('fav_subjects')  // q{},
110         fav_authors    => scalar $input->param('fav_authors')   // q{},
111         referral       => scalar $input->param('referral')      // q{},
112         notes          => scalar $input->param('notes')         // q{},
113     });
114     my $success = eval { return $houseboundprofile->store };
115     push @messages, { type => 'error', code => 'error_on_profile_create' }
116         if ( $@ or !$success );
117     $method = undef;
118 } elsif ( $method eq 'visit_update_or_create' ) {
119     # We want to edit, edit a visit, so we must pass its details.
120     $deliverers = Koha::Patrons->search_housebound_deliverers;
121     $choosers = Koha::Patrons->search_housebound_choosers;
122     $houseboundvisit = $visit;
123 } elsif ( $method eq 'visit_delete' and $visit ) {
124     # We want ot delete a specific visit.
125     my $success = eval { return $visit->delete };
126     push @messages, { type => 'error', code => 'error_on_visit_delete' }
127         if ( $@ or !$success );
128     $method = undef;
129 } elsif ( $method eq 'editvisitconfirm' and $visit ) {
130     # We have received input for editing a visit.  We must store and return to
131     # simple display.
132     $visit->set({
133         borrowernumber      => scalar $input->param('borrowernumber')      // q{},
134         appointment_date    => dt_from_string($input->param('date') // q{}),
135         day_segment         => scalar $input->param('segment')             // q{},
136         chooser_brwnumber   => scalar $input->param('chooser')             // q{},
137         deliverer_brwnumber => scalar $input->param('deliverer')           // q{},
138     });
139     my $success = eval { return $visit->store };
140     push @messages, { type => 'error', code => 'error_on_visit_store' }
141         if ( $@ or !$success );
142     $method = undef;
143 } elsif ( $method eq 'addvisitconfirm' and !$visit ) {
144     # We have received input for creating a visit.  We must store and return
145     # to simple display.
146     my $visit = Koha::Patron::HouseboundVisit->new({
147         borrowernumber      => scalar $input->param('borrowernumber')      // q{},
148         appointment_date    => dt_from_string($input->param('date') // q{}),
149         day_segment         => scalar $input->param('segment')             // q{},
150         chooser_brwnumber   => scalar $input->param('chooser')             // q{},
151         deliverer_brwnumber => scalar $input->param('deliverer')           // q{},
152     });
153     my $success = eval { return $visit->store };
154     push @messages, { type => 'error', code => 'error_on_visit_create' }
155         if ( $@ or !$success );
156     $method = undef;
157 }
158
159 # We don't have any profile information, so we must display a creation form.
160 $method = 'update_or_create' if ( !$houseboundprofile );
161
162 # Ensure template has all patron details.
163 $template->param(%{$patron->unblessed}) if ( $patron );
164
165 # Load extended patron attributes if necessary (taken from members/files.pl).
166 if ( C4::Context->preference('ExtendedPatronAttributes') and $patron ) {
167     my $attributes = GetBorrowerAttributes($patron->borrowernumber);
168     $template->param(
169         ExtendedPatronAttributes => 1,
170         extendedattributes => $attributes
171     );
172 }
173
174 $template->param( adultborrower => 1 ) if ( $category->category_type eq 'A' || $category->category_type eq 'I' );
175 $template->param(
176     picture            => $patron_image,
177     housebound_profile => $houseboundprofile,
178     visit              => $houseboundvisit,
179     branch             => $branch,
180     category           => $category,
181     messages           => \@messages,
182     method             => $method,
183     choosers           => $choosers,
184     deliverers         => $deliverers,
185     houseboundview     => 'on',
186 );
187
188 output_html_with_http_headers $input, $cookie, $template->output;
189
190 =head1 AUTHOR
191
192 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
193
194 =cut