Bug 12063 - make perl atomic update follow skeleton.perl
[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;
26 use Koha::Patrons;
27 use Koha::Libraries;
28 use Koha::Database;
29 use Koha::DateUtils;
30 use Koha::Patron::Categories;
31 use Koha::Patron::Category;
32 use Koha::ItemTypes;
33 use Koha::IssuingRule;
34 use Koha::IssuingRules;
35
36 #Setting variables
37 my $input = new CGI;
38
39 unless ( C4::Context->preference('Version') ) {
40     print $input->redirect("/cgi-bin/koha/installer/install.pl");
41     exit;
42 }
43
44 my ( $user, $cookie, $sessionID, $flags ) =
45   C4::InstallAuth::checkauth( $input, 0, undef, 'intranet' );
46 die "Not logged in"
47   unless $user
48   ; # Should not happen, we should be redirect if the user is not logged in. But do not trust authentication...
49
50 my $step = $input->param('step') || 1;
51 my $op   = $input->param('op')   || '';
52
53 my $template_params = {};
54 $template_params->{op} = $op;
55
56 my $schema = Koha::Database->new()->schema();
57
58 my @messages;
59
60 if ( $step == 1 ) {
61
62     if ( $op eq 'add_validate_library' ) {
63
64         my $branchcode = $input->param('branchcode');
65         $branchcode = uc($branchcode);
66
67         $branchcode =~ s|\s||g
68           ; # Use a regular expression to check the value of the inputted branchcode
69
70         my $library = Koha::Library->new(
71             {
72                 branchcode => $branchcode,
73                 branchname => scalar $input->param('branchname'),
74             }
75         );
76
77         eval { $library->store; };
78         unless ($@) {
79             push @messages,
80               { type => 'message', code => 'success_on_insert_library' };
81         }
82         else {
83             push @messages,
84               { type => 'message', code => 'error_on_insert_library' };
85         }
86     }
87
88     $step++ if Koha::Libraries->count;
89 }
90 if ( $step == 2 ) {
91     if ( $op eq "add_validate_category" ) {
92
93         my $searchfield = $input->param('description') // q||;
94         my $categorycode = $input->param('categorycode');
95         my $category;
96         $template_params->{categorycode} = $categorycode;
97
98         $categorycode = $input->param('categorycode');
99         my $description           = $input->param('description');
100         my $overduenoticerequired = $input->param('overduenoticerequired');
101         my $category_type         = $input->param('category_type');
102         my $default_privacy       = $input->param('default_privacy');
103         my $enrolmentperiod       = $input->param('enrolmentperiod');
104         my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
105
106         #Converts the string into a date format
107         if ($enrolmentperioddate) {
108             $enrolmentperioddate = output_pref(
109                 {
110                     dt         => dt_from_string($enrolmentperioddate),
111                     dateformat => 'DateTime',
112                     dateonly   => 1,
113                 }
114             );
115         }
116
117         #Adds a new patron category to the database
118         $category = Koha::Patron::Category->new(
119             {
120                 categorycode          => $categorycode,
121                 description           => $description,
122                 overduenoticerequired => $overduenoticerequired,
123                 category_type         => $category_type,
124                 default_privacy       => $default_privacy,
125                 enrolmentperiod       => $enrolmentperiod,
126                 enrolmentperioddate   => $enrolmentperioddate
127             }
128         );
129
130         eval { $category->store; };
131
132         unless ($@) {
133             push @messages,
134               { type => 'message', code => 'success_on_insert_category' };
135         }
136         else {
137             push @messages,
138               { type => 'message', code => 'error_on_insert_category' };
139         }
140     }
141
142     $step++ if Koha::Patron::Categories->count;
143 }
144 if ( $step == 3 ) {
145     if ( $op eq 'add_validate_patron' ) {
146
147         #Create a patron
148         my $firstpassword  = $input->param('password')  || '';
149         my $secondpassword = $input->param('password2') || '';
150         my $cardnumber     = $input->param('cardnumber');
151         my $userid         = $input->param('userid');
152
153         if ( my $error_code = checkcardnumber($cardnumber) ) {
154             if ( $error_code == 1 ) {
155                 push @messages,
156                   {
157                     type => 'alert',
158                     code => 'ERROR_cardnumber_already_exists'
159                   };
160             }
161             elsif ( $error_code == 2 ) {
162                 push @messages,
163                   { type => 'alert', code => 'ERROR_cardnumber_length' };
164             }
165         }
166         elsif ( $firstpassword ne $secondpassword ) {
167
168             push @messages,
169               { type => 'alert', code => 'ERROR_password_mismatch' };
170         }
171         else {
172
173             my $patron_data = {
174                 surname      => scalar $input->param('surname'),
175                 firstname    => scalar $input->param('firstname'),
176                 cardnumber   => scalar $input->param('cardnumber'),
177                 branchcode   => scalar $input->param('libraries'),
178                 categorycode => scalar $input->param('categorycode_entry'),
179                 userid       => scalar $input->param('userid'),
180                 password     => scalar $input->param('password'),
181                 password2    => scalar $input->param('password2'),
182                 privacy      => "default",
183                 address      => "",
184                 city         => "",
185                 flags => 1,    # Will be superlibrarian
186             };
187
188             my $patron_category =
189               Koha::Patron::Categories->find( $patron_data->{categorycode} );
190             $patron_data->{dateexpiry} =
191               $patron_category->get_expiry_date( $patron_data->{dateenrolled} );
192
193             my $borrowernumber = C4::Members::AddMember(%$patron_data);
194
195             #Error handling checking if the patron was created successfully
196             if ($borrowernumber) {
197                 push @messages,
198                   { type => 'message', code => 'success_on_insert_patron' };
199             }
200             else {
201                 push @messages,
202                   { type => 'error', code => 'error_on_insert_patron' };
203             }
204         }
205     }
206
207     $step++ if Koha::Patrons->search( { flags => 1 } )->count;
208 }
209 if ( $step == 4 ) {
210     if ( $op eq 'add_validate_itemtype' ) {
211         my $description   = $input->param('description');
212         my $itemtype_code = $input->param('itemtype');
213         $itemtype_code = uc($itemtype_code);
214
215         my $itemtype = Koha::ItemType->new(
216             {
217                 itemtype    => $itemtype_code,
218                 description => $description,
219             }
220         );
221         eval { $itemtype->store; };
222
223         unless ($@) {
224             push @messages,
225               { type => 'message', code => 'success_on_insert_itemtype' };
226         }
227         else {
228             push @messages,
229               { type => 'message', code => 'error_on_insert_itemtype' };
230         }
231     }
232
233     $step++ if Koha::ItemTypes->count;
234 }
235 if ( $step == 5 ) {
236
237     if ( $op eq 'add_validate_circ_rule' ) {
238
239         #If no libraries exist then set the $branch value to *
240         my $branch = $input->param('branch') || '*';
241
242         my $type            = $input->param('type');
243         my $branchcode      = $input->param('branch');
244         my $categorycode    = $input->param('categorycode');
245         my $itemtype        = $input->param('itemtype');
246         my $maxissueqty     = $input->param('maxissueqty');
247         my $issuelength     = $input->param('issuelength');
248         my $lengthunit      = $input->param('lengthunit');
249         my $renewalsallowed = $input->param('renewalsallowed');
250         my $renewalperiod   = $input->param('renewalperiod');
251         my $onshelfholds    = $input->param('onshelfholds') || 0;
252         $maxissueqty =~ s/\s//g;
253         $maxissueqty = undef if $maxissueqty !~ /^\d+/;
254         $issuelength = $issuelength eq q{} ? undef : $issuelength;
255
256         my $params = {
257             branchcode      => $branchcode,
258             categorycode    => $categorycode,
259             itemtype        => $itemtype,
260             maxissueqty     => $maxissueqty,
261             renewalsallowed => $renewalsallowed,
262             renewalperiod   => $renewalperiod,
263             issuelength     => $issuelength,
264             lengthunit      => $lengthunit,
265             onshelfholds    => $onshelfholds,
266         };
267
268         my $issuingrule = Koha::IssuingRule->new($params);
269         eval { $issuingrule->store; };
270
271         unless ($@) {
272             push @messages,
273               { type => 'message', code => 'success_on_insert_circ_rule' };
274         }
275         else {
276             push @messages,
277               { type => 'message', code => 'error_on_insert_circ_rule' };
278         }
279     }
280
281     $step++ if Koha::IssuingRules->count;
282 }
283
284 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
285 $template_params->{libraries}   = $libraries;
286 $template_params->{group_types} = [
287     {
288         categorytype => 'searchdomain',
289         categories   => [
290             Koha::LibraryCategories->search(
291                 { categorytype => 'searchdomain' }
292             )
293         ],
294     },
295     {
296         categorytype => 'properties',
297         categories   => [
298             Koha::LibraryCategories->search( { categorytype => 'properties' } )
299         ],
300     },
301 ];
302
303 if ( $step > 5 ) {
304     $template_params->{all_done} = 1;    # If step 5 is complete, we are done!
305     $step = 5;
306 }
307
308 #Getting the appropriate template to display to the user
309 my ( $template, $loggedinuser );
310 ( $template, $loggedinuser, $cookie ) = C4::InstallAuth::get_template_and_user(
311     {
312         template_name   => "onboarding/onboardingstep${step}.tt",
313         query           => $input,
314         type            => "intranet",
315         authnotrequired => 0,
316         debug           => 1,
317     }
318 );
319
320 $template_params->{messages} = \@messages;
321 my $categories = Koha::Patron::Categories->search();
322 $template_params->{categories} = $categories;
323
324 my $itemtypes = Koha::ItemTypes->search();
325 $template_params->{itemtypes} = $itemtypes;
326
327 $template->param(%$template_params);
328
329 output_html_with_http_headers $input, $cookie, $template->output;