Bug 29393: Add send_messages_to_borrowers permission
[koha.git] / installer / onboarding.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2017 Catalyst IT
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use C4::Context;
22 use C4::InstallAuth qw( checkauth get_template_and_user );
23 use CGI qw ( -utf8 );
24 use C4::Output qw( output_html_with_http_headers );
25 use Koha::Patrons;
26 use Koha::Libraries;
27 use Koha::Database;
28 use Koha::Patrons;
29 use Koha::Patron::Categories;
30 use Koha::Policy::Patrons::Cardnumber;
31 use Koha::ItemTypes;
32 use Koha::CirculationRules;
33
34 #Setting variables
35 my $input = CGI->new;
36
37 unless ( C4::Context->preference('Version') ) {
38     print $input->redirect("/cgi-bin/koha/installer/install.pl");
39     exit;
40 }
41
42 my ( $user, $cookie, $sessionID, $flags ) =
43   C4::InstallAuth::checkauth( $input, 0, undef, 'intranet' );
44 die "Not logged in"
45   unless $user
46   ; # Should not happen, we should be redirect if the user is not logged in. But do not trust authentication...
47
48 my $step = $input->param('step') || 1;
49 my $op   = $input->param('op')   || '';
50
51 my $template_params = {};
52 $template_params->{op} = $op;
53
54 my $schema = Koha::Database->new()->schema();
55
56 my @messages;
57
58 if ( $step == 1 ) {
59
60     if ( $op eq 'cud-add_validate_library' ) {
61
62         my $branchcode = $input->param('branchcode');
63         $branchcode = uc($branchcode);
64
65         $branchcode =~ s|\s||g
66           ; # Use a regular expression to check the value of the inputted branchcode
67
68         my $library = Koha::Library->new(
69             {
70                 branchcode => $branchcode,
71                 branchname => scalar $input->param('branchname'),
72             }
73         );
74
75         eval { $library->store; };
76         unless ($@) {
77             push @messages, { code => 'success_on_insert_library' };
78         }
79         else {
80             push @messages, { code => 'error_on_insert_library' };
81         }
82     }
83
84     $step++ if Koha::Libraries->count;
85 }
86 if ( $step == 2 ) {
87     if ( $op eq "cud-add_validate_category" ) {
88
89         my $searchfield = $input->param('description') // q||;
90         my $categorycode = $input->param('categorycode');
91         my $category;
92         $template_params->{categorycode} = $categorycode;
93
94         $categorycode = $input->param('categorycode');
95         my $description           = $input->param('description');
96         my $overduenoticerequired = $input->param('overduenoticerequired');
97         my $category_type         = $input->param('category_type');
98         my $default_privacy       = $input->param('default_privacy');
99         my $enrolmentperiod       = $input->param('enrolmentperiod');
100         my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
101
102         #Adds a new patron category to the database
103         $category = Koha::Patron::Category->new(
104             {
105                 categorycode          => $categorycode,
106                 description           => $description,
107                 overduenoticerequired => $overduenoticerequired,
108                 category_type         => $category_type,
109                 default_privacy       => $default_privacy,
110                 enrolmentperiod       => $enrolmentperiod,
111                 enrolmentperioddate   => $enrolmentperioddate
112             }
113         );
114
115         eval { $category->store; };
116
117         unless ($@) {
118             push @messages, { code => 'success_on_insert_category' };
119         }
120         else {
121             push @messages, { code => 'error_on_insert_category' };
122         }
123     }
124
125     $step++ if Koha::Patron::Categories->count;
126 }
127 if ( $step == 3 ) {
128     if ( $op eq 'cud-add_validate_patron' ) {
129
130         #Create a patron
131         my $firstpassword  = $input->param('password')  || '';
132         my $secondpassword = $input->param('password2') || '';
133         my $cardnumber     = $input->param('cardnumber');
134         my $userid         = $input->param('userid');
135         my $categorycode = $input->param('categorycode_entry');
136         my $patron_category =
137           Koha::Patron::Categories->find( $categorycode );
138
139         my ( $is_valid, $passworderror ) =
140           Koha::AuthUtils::is_password_valid( $firstpassword,
141             $patron_category );
142
143
144         my $is_cardnumber_valid = Koha::Policy::Patrons::Cardnumber->is_valid($cardnumber);
145         unless ( $is_cardnumber_valid ) {
146             for my $m ( @{ $is_cardnumber_valid->messages } ) {
147                 my $message = $m->message;
148                 if ( $message eq 'already_exists' ) {
149                     push @messages, { code => 'ERROR_cardnumber_already_exists' };
150                 }
151                 elsif ( $message eq 'invalid_length' ) {
152                     push @messages, { code => 'ERROR_cardnumber_length' };
153                 }
154             }
155         }
156         elsif ( $firstpassword ne $secondpassword ) {
157
158             push @messages, { code => 'ERROR_password_mismatch' };
159         }
160         elsif ( $passworderror) {
161                 push @messages, { code => 'ERROR_password_too_short'} if $passworderror eq 'too_short';
162                 push @messages, { code => 'ERROR_password_too_weak'} if $passworderror eq 'too_weak';
163                 push @messages, { code => 'ERROR_password_has_whitespaces'} if $passworderror eq 'has_whitespaces';
164
165         }
166         else {
167             my $patron_data = {
168                 surname      => scalar $input->param('surname'),
169                 firstname    => scalar $input->param('firstname'),
170                 cardnumber   => scalar $input->param('cardnumber'),
171                 branchcode   => scalar $input->param('libraries'),
172                 categorycode => $categorycode,
173                 userid       => scalar $input->param('userid'),
174                 privacy      => "default",
175                 address      => "",
176                 city         => "",
177                 flags        => 1,    # Will be superlibrarian
178             };
179
180             $patron_data->{dateexpiry} =
181               $patron_category->get_expiry_date( $patron_data->{dateenrolled} );
182
183             eval {
184                 my $patron = Koha::Patron->new($patron_data)->store;
185                 $patron->set_password({ password =>  $firstpassword });
186             };
187
188             #Error handling checking if the patron was created successfully
189             unless ($@) {
190                 push @messages, { code => 'success_on_insert_patron' };
191             }
192             else {
193                 warn $@;
194                 push @messages, { code => 'error_on_insert_patron' };
195             }
196         }
197     }
198
199     $step++ if Koha::Patrons->search( { flags => 1 } )->count;
200 }
201 if ( $step == 4 ) {
202     if ( $op eq 'cud-add_validate_itemtype' ) {
203         my $description   = $input->param('description');
204         my $itemtype_code = $input->param('itemtype');
205         $itemtype_code = uc($itemtype_code);
206
207         my $itemtype = Koha::ItemType->new(
208             {
209                 itemtype    => $itemtype_code,
210                 description => $description,
211             }
212         );
213         eval { $itemtype->store; };
214
215         unless ($@) {
216             push @messages, { code => 'success_on_insert_itemtype' };
217         }
218         else {
219             push @messages, { code => 'error_on_insert_itemtype' };
220         }
221     }
222
223     $step++ if Koha::ItemTypes->count;
224 }
225 if ( $step == 5 ) {
226
227     if ( $op eq 'cud-add_validate_circ_rule' ) {
228
229         #If no libraries exist then set the $branch value to *
230         my $branch = $input->param('branch') || '*';
231
232         my $type            = $input->param('type');
233         my $branchcode      = $input->param('branch');
234         my $categorycode    = $input->param('categorycode');
235         my $itemtype        = $input->param('itemtype');
236         my $maxissueqty     = $input->param('maxissueqty');
237         my $issuelength     = $input->param('issuelength') || 0;
238         my $lengthunit      = $input->param('lengthunit');
239         my $renewalsallowed = $input->param('renewalsallowed');
240         my $renewalperiod   = $input->param('renewalperiod');
241         my $reservesallowed = $input->param('reservesallowed');
242         my $holds_per_day   = $input->param('holds_per_day');
243         my $holds_per_record = $input->param('holds_per_record');
244         my $onshelfholds    = $input->param('onshelfholds') || 0;
245         $maxissueqty =~ s/\s//g;
246         $maxissueqty = undef if $maxissueqty !~ /^\d+/;
247
248         my $params = {
249             branchcode      => $branchcode,
250             categorycode    => $categorycode,
251             itemtype        => $itemtype,
252             rules => {
253                 renewalsallowed                  => $renewalsallowed,
254                 renewalperiod                    => $renewalperiod,
255                 issuelength                      => $issuelength,
256                 lengthunit                       => $lengthunit,
257                 onshelfholds                     => $onshelfholds,
258                 article_requests                 => "no",
259                 auto_renew                       => 0,
260                 cap_fine_to_replacement_price    => 0,
261                 chargeperiod                     => 0,
262                 chargeperiod_charge_at           => 0,
263                 fine                             => 0,
264                 finedays                         => 0,
265                 firstremind                      => 0,
266                 hardduedate                      => "",
267                 hardduedatecompare               => -1,
268                 holds_per_day                    => $holds_per_day,
269                 holds_per_record                 => $holds_per_record,
270                 maxissueqty                      => $maxissueqty,
271                 maxonsiteissueqty                => "",
272                 maxsuspensiondays                => "",
273                 no_auto_renewal_after            => "",
274                 no_auto_renewal_after_hard_limit => "",
275                 norenewalbefore                  => "",
276                 noautorenewalbefore              => "",
277                 opacitemholds                    => "N",
278                 overduefinescap                  => "",
279                 rentaldiscount                   => 0,
280                 reservesallowed                  => $reservesallowed,
281                 suspension_chargeperiod          => 1,
282                 decreaseloanholds                => "",
283                 unseen_renewals_allowed          => "",
284                 recalls_allowed                  => undef,
285                 recalls_per_record               => undef,
286                 on_shelf_recalls                 => undef,
287                 recall_due_date_interval         => undef,
288                 recall_overdue_fine              => undef,
289                 recall_shelf_time                => undef,
290                 holds_pickup_period              => undef,
291               }
292         };
293
294         eval {
295             Koha::CirculationRules->set_rules($params);
296         };
297
298         if ($@) {
299             warn $@;
300             push @messages, { code => 'error_on_insert_circ_rule' };
301         } else {
302             push @messages, { code => 'success_on_insert_circ_rule' };
303         }
304     }
305
306     $step++ if Koha::CirculationRules->count;
307 }
308
309 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
310 $template_params->{libraries}   = $libraries;
311
312 if ( $step > 5 ) {
313     $template_params->{all_done} = 1;    # If step 5 is complete, we are done!
314     $step = 5;
315 }
316
317 #Getting the appropriate template to display to the user
318 my ( $template, $loggedinuser );
319 ( $template, $loggedinuser, $cookie ) = C4::InstallAuth::get_template_and_user(
320     {
321         template_name   => "onboarding/onboardingstep${step}.tt",
322         query           => $input,
323         type            => "intranet",
324     }
325 );
326
327 $template_params->{messages} = \@messages;
328 my $categories = Koha::Patron::Categories->search();
329 $template_params->{categories} = $categories;
330
331 my $itemtypes = Koha::ItemTypes->search();
332 $template_params->{itemtypes} = $itemtypes;
333
334 $template->param(%$template_params);
335
336 output_html_with_http_headers $input, $cookie, $template->output;