Bug 18789: Send Koha::Patron object to the templates
[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 => 'edit_borrowers' },
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 $borrowernumber = $input->param('borrowernumber');
60 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
61 my $patron = Koha::Patrons->find($borrowernumber);
62 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
63
64 # Get supporting cast
65 my ( $branch, $category, $houseboundprofile, $visit, $patron_image );
66 if ( $patron ) { # FIXME This test is not needed - output_and_exit_if_error handles it
67     $patron_image = $patron->image;
68     $category = Koha::Patron::Categories->new->find($patron->categorycode);
69     $houseboundprofile = $patron->housebound_profile;
70 }
71 if ( $visit_id ) {
72     $visit = eval {
73         return Koha::Patron::HouseboundVisits->find($visit_id);
74     };
75     push @messages, { type => 'error', code => 'error_on_visit_load' }
76         if ( $@ or !$visit );
77 }
78
79 # Main processing
80 my ( $deliverers, $choosers, $houseboundvisit );
81
82 if ( $method eq 'updateconfirm' and $houseboundprofile ) {
83     # We have received the input from the profile edit form.  We must save the
84     # changes, and return to simple display.
85     $houseboundprofile->set({
86         day           => scalar $input->param('day')           // q{},
87         frequency     => scalar $input->param('frequency')     // q{},
88         fav_itemtypes => scalar $input->param('fav_itemtypes') // q{},
89         fav_subjects  => scalar $input->param('fav_subjects')  // q{},
90         fav_authors   => scalar $input->param('fav_authors')   // q{},
91         referral      => scalar $input->param('referral')      // q{},
92         notes         => scalar $input->param('notes')         // q{},
93     });
94     my $success = eval { return $houseboundprofile->store };
95     push @messages, { type => 'error', code => 'error_on_profile_store' }
96         if ( $@ or !$success );
97     $method = undef;
98 } elsif ( $method eq 'createconfirm' ) {
99     # We have received the input necessary to create a new profile.  We must
100     # save it, and return to simple display.
101     $houseboundprofile = Koha::Patron::HouseboundProfile->new({
102         borrowernumber => $patron->borrowernumber,
103         day            => scalar $input->param('day')           // q{},
104         frequency      => scalar $input->param('frequency')     // q{},
105         fav_itemtypes  => scalar $input->param('fav_itemtypes') // q{},
106         fav_subjects   => scalar $input->param('fav_subjects')  // q{},
107         fav_authors    => scalar $input->param('fav_authors')   // q{},
108         referral       => scalar $input->param('referral')      // q{},
109         notes          => scalar $input->param('notes')         // q{},
110     });
111     my $success = eval { return $houseboundprofile->store };
112     push @messages, { type => 'error', code => 'error_on_profile_create' }
113         if ( $@ or !$success );
114     $method = undef;
115 } elsif ( $method eq 'visit_update_or_create' ) {
116     # We want to edit, edit a visit, so we must pass its details.
117     $deliverers = Koha::Patrons->search_housebound_deliverers;
118     $choosers = Koha::Patrons->search_housebound_choosers;
119     $houseboundvisit = $visit;
120 } elsif ( $method eq 'visit_delete' and $visit ) {
121     # We want ot delete a specific visit.
122     my $success = eval { return $visit->delete };
123     push @messages, { type => 'error', code => 'error_on_visit_delete' }
124         if ( $@ or !$success );
125     $method = undef;
126 } elsif ( $method eq 'editvisitconfirm' and $visit ) {
127     # We have received input for editing a visit.  We must store and return to
128     # simple display.
129     $visit->set({
130         borrowernumber      => scalar $input->param('borrowernumber')      // q{},
131         appointment_date    => dt_from_string($input->param('date') // q{}),
132         day_segment         => scalar $input->param('segment')             // q{},
133         chooser_brwnumber   => scalar $input->param('chooser')             // q{},
134         deliverer_brwnumber => scalar $input->param('deliverer')           // q{},
135     });
136     my $success = eval { return $visit->store };
137     push @messages, { type => 'error', code => 'error_on_visit_store' }
138         if ( $@ or !$success );
139     $method = undef;
140 } elsif ( $method eq 'addvisitconfirm' and !$visit ) {
141     # We have received input for creating a visit.  We must store and return
142     # to simple display.
143     my $visit = Koha::Patron::HouseboundVisit->new({
144         borrowernumber      => scalar $input->param('borrowernumber')      // q{},
145         appointment_date    => dt_from_string($input->param('date') // q{}),
146         day_segment         => scalar $input->param('segment')             // q{},
147         chooser_brwnumber   => scalar $input->param('chooser')             // q{},
148         deliverer_brwnumber => scalar $input->param('deliverer')           // q{},
149     });
150     my $success = eval { return $visit->store };
151     push @messages, { type => 'error', code => 'error_on_visit_create' }
152         if ( $@ or !$success );
153     $method = undef;
154 }
155
156 # We don't have any profile information, so we must display a creation form.
157 $method = 'update_or_create' if ( !$houseboundprofile );
158
159 # Ensure template has all patron details.
160 $template->param( patron => $patron );
161
162 # Load extended patron attributes if necessary (taken from members/files.pl).
163 if ( C4::Context->preference('ExtendedPatronAttributes') and $patron ) {
164     my $attributes = GetBorrowerAttributes($patron->borrowernumber);
165     $template->param(
166         ExtendedPatronAttributes => 1,
167         extendedattributes => $attributes
168     );
169 }
170
171 $template->param( adultborrower => 1 ) if ( $category->category_type eq 'A' || $category->category_type eq 'I' );
172 $template->param(
173     picture            => $patron_image,
174     housebound_profile => $houseboundprofile,
175     visit              => $houseboundvisit,
176     messages           => \@messages,
177     method             => $method,
178     choosers           => $choosers,
179     deliverers         => $deliverers,
180     houseboundview     => 'on',
181 );
182
183 output_html_with_http_headers $input, $cookie, $template->output;
184
185 =head1 AUTHOR
186
187 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
188
189 =cut