Bug 18936: (follow-up) Fix tests, replace old get_onshelfholds_policy method
[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;
23 use CGI qw ( -utf8 );
24 use C4::Output;
25 use C4::Members qw(checkcardnumber);
26 use Koha::Patrons;
27 use Koha::Libraries;
28 use Koha::Database;
29 use Koha::DateUtils;
30 use Koha::Patrons;
31 use Koha::Patron::Categories;
32 use Koha::ItemTypes;
33
34 #Setting variables
35 my $input = new CGI;
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 '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 "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         #Converts the string into a date format
103         if ($enrolmentperioddate) {
104             $enrolmentperioddate = output_pref(
105                 {
106                     dt         => dt_from_string($enrolmentperioddate),
107                     dateformat => 'DateTime',
108                     dateonly   => 1,
109                 }
110             );
111         }
112
113         #Adds a new patron category to the database
114         $category = Koha::Patron::Category->new(
115             {
116                 categorycode          => $categorycode,
117                 description           => $description,
118                 overduenoticerequired => $overduenoticerequired,
119                 category_type         => $category_type,
120                 default_privacy       => $default_privacy,
121                 enrolmentperiod       => $enrolmentperiod,
122                 enrolmentperioddate   => $enrolmentperioddate
123             }
124         );
125
126         eval { $category->store; };
127
128         unless ($@) {
129             push @messages, { code => 'success_on_insert_category' };
130         }
131         else {
132             push @messages, { code => 'error_on_insert_category' };
133         }
134     }
135
136     $step++ if Koha::Patron::Categories->count;
137 }
138 if ( $step == 3 ) {
139     if ( $op eq 'add_validate_patron' ) {
140
141         #Create a patron
142         my $firstpassword  = $input->param('password')  || '';
143         my $secondpassword = $input->param('password2') || '';
144         my $cardnumber     = $input->param('cardnumber');
145         my $userid         = $input->param('userid');
146
147         my ( $is_valid, $passworderror) = Koha::AuthUtils::is_password_valid( $firstpassword );
148
149         if ( my $error_code = checkcardnumber($cardnumber) ) {
150             if ( $error_code == 1 ) {
151                 push @messages, { code => 'ERROR_cardnumber_already_exists' };
152             }
153             elsif ( $error_code == 2 ) {
154                 push @messages, { code => 'ERROR_cardnumber_length' };
155             }
156         }
157         elsif ( $firstpassword ne $secondpassword ) {
158
159             push @messages, { code => 'ERROR_password_mismatch' };
160         }
161         elsif ( $passworderror) {
162                 push @messages, { code => 'ERROR_password_too_short'} if $passworderror eq 'too_short';
163                 push @messages, { code => 'ERROR_password_too_weak'} if $passworderror eq 'too_weak';
164                 push @messages, { code => 'ERROR_password_has_whitespaces'} if $passworderror eq 'has_whitespaces';
165
166         }
167         else {
168             my $patron_data = {
169                 surname      => scalar $input->param('surname'),
170                 firstname    => scalar $input->param('firstname'),
171                 cardnumber   => scalar $input->param('cardnumber'),
172                 branchcode   => scalar $input->param('libraries'),
173                 categorycode => scalar $input->param('categorycode_entry'),
174                 userid       => scalar $input->param('userid'),
175                 privacy      => "default",
176                 address      => "",
177                 city         => "",
178                 flags        => 1,    # Will be superlibrarian
179             };
180
181             my $patron_category =
182               Koha::Patron::Categories->find( $patron_data->{categorycode} );
183             $patron_data->{dateexpiry} =
184               $patron_category->get_expiry_date( $patron_data->{dateenrolled} );
185
186             eval {
187                 my $patron = Koha::Patron->new($patron_data)->store;
188                 $patron->set_password({ password =>  $firstpassword });
189             };
190
191             #Error handling checking if the patron was created successfully
192             unless ($@) {
193                 push @messages, { code => 'success_on_insert_patron' };
194             }
195             else {
196                 warn $@;
197                 push @messages, { code => 'error_on_insert_patron' };
198             }
199         }
200     }
201
202     $step++ if Koha::Patrons->search( { flags => 1 } )->count;
203 }
204 if ( $step == 4 ) {
205     if ( $op eq 'add_validate_itemtype' ) {
206         my $description   = $input->param('description');
207         my $itemtype_code = $input->param('itemtype');
208         $itemtype_code = uc($itemtype_code);
209
210         my $itemtype = Koha::ItemType->new(
211             {
212                 itemtype    => $itemtype_code,
213                 description => $description,
214             }
215         );
216         eval { $itemtype->store; };
217
218         unless ($@) {
219             push @messages, { code => 'success_on_insert_itemtype' };
220         }
221         else {
222             push @messages, { code => 'error_on_insert_itemtype' };
223         }
224     }
225
226     $step++ if Koha::ItemTypes->count;
227 }
228 if ( $step == 5 ) {
229
230     if ( $op eq 'add_validate_circ_rule' ) {
231
232         #If no libraries exist then set the $branch value to *
233         my $branch = $input->param('branch') || '*';
234
235         my $type            = $input->param('type');
236         my $branchcode      = $input->param('branch');
237         my $categorycode    = $input->param('categorycode');
238         my $itemtype        = $input->param('itemtype');
239         my $maxissueqty     = $input->param('maxissueqty');
240         my $issuelength     = $input->param('issuelength');
241         my $lengthunit      = $input->param('lengthunit');
242         my $renewalsallowed = $input->param('renewalsallowed');
243         my $renewalperiod   = $input->param('renewalperiod');
244         my $onshelfholds    = $input->param('onshelfholds') || 0;
245         $maxissueqty =~ s/\s//g;
246         $maxissueqty = undef if $maxissueqty !~ /^\d+/;
247         $issuelength = $issuelength eq q{} ? undef : $issuelength;
248
249         my $params = {
250             branchcode      => $branchcode,
251             categorycode    => $categorycode,
252             itemtype        => $itemtype,
253             rules => {
254                 renewalsallowed => $renewalsallowed,
255                 renewalperiod   => $renewalperiod,
256                 issuelength     => $issuelength,
257                 lengthunit      => $lengthunit,
258                 onshelfholds    => $onshelfholds,
259             }
260         };
261
262         eval { Koha::CirculationRules->set_rules( $params ) };
263
264         if ($@) {
265             push @messages, { code => 'error_on_insert_circ_rule' };
266         } else {
267
268             eval {
269                 Koha::CirculationRules->set_rules(
270                     {
271                         categorycode => $categorycode,
272                         itemtype     => $itemtype,
273                         branchcode   => $branchcode,
274                         rules        => {
275                             maxissueqty => $maxissueqty,
276                         }
277                     }
278                 );
279             };
280
281             unless ($@) {
282                 push @messages, { code => 'success_on_insert_circ_rule' };
283             }
284             else {
285                 push @messages, { code => 'error_on_insert_circ_rule' };
286             }
287         }
288     }
289
290     $step++ if Koha::CirculationRules->count;
291 }
292
293 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
294 $template_params->{libraries}   = $libraries;
295
296 if ( $step > 5 ) {
297     $template_params->{all_done} = 1;    # If step 5 is complete, we are done!
298     $step = 5;
299 }
300
301 #Getting the appropriate template to display to the user
302 my ( $template, $loggedinuser );
303 ( $template, $loggedinuser, $cookie ) = C4::InstallAuth::get_template_and_user(
304     {
305         template_name   => "onboarding/onboardingstep${step}.tt",
306         query           => $input,
307         type            => "intranet",
308         authnotrequired => 0,
309         debug           => 1,
310     }
311 );
312
313 $template_params->{messages} = \@messages;
314 my $categories = Koha::Patron::Categories->search();
315 $template_params->{categories} = $categories;
316
317 my $itemtypes = Koha::ItemTypes->search();
318 $template_params->{itemtypes} = $itemtypes;
319
320 $template->param(%$template_params);
321
322 output_html_with_http_headers $input, $cookie, $template->output;