Bug 36792: Limit POSIX imports
[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 C4::Members qw( checkcardnumber );
26 use Koha::Patrons;
27 use Koha::Libraries;
28 use Koha::Database;
29 use Koha::Patrons;
30 use Koha::Patron::Categories;
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 '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         #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 '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         if ( my $error_code = checkcardnumber($cardnumber) ) {
145             if ( $error_code == 1 ) {
146                 push @messages, { code => 'ERROR_cardnumber_already_exists' };
147             }
148             elsif ( $error_code == 2 ) {
149                 push @messages, { code => 'ERROR_cardnumber_length' };
150             }
151         }
152         elsif ( $firstpassword ne $secondpassword ) {
153
154             push @messages, { code => 'ERROR_password_mismatch' };
155         }
156         elsif ( $passworderror) {
157                 push @messages, { code => 'ERROR_password_too_short'} if $passworderror eq 'too_short';
158                 push @messages, { code => 'ERROR_password_too_weak'} if $passworderror eq 'too_weak';
159                 push @messages, { code => 'ERROR_password_has_whitespaces'} if $passworderror eq 'has_whitespaces';
160
161         }
162         else {
163             my $patron_data = {
164                 surname      => scalar $input->param('surname'),
165                 firstname    => scalar $input->param('firstname'),
166                 cardnumber   => scalar $input->param('cardnumber'),
167                 branchcode   => scalar $input->param('libraries'),
168                 categorycode => $categorycode,
169                 userid       => scalar $input->param('userid'),
170                 privacy      => "default",
171                 address      => "",
172                 city         => "",
173                 flags        => 1,    # Will be superlibrarian
174             };
175
176             $patron_data->{dateexpiry} =
177               $patron_category->get_expiry_date( $patron_data->{dateenrolled} );
178
179             eval {
180                 my $patron = Koha::Patron->new($patron_data)->store;
181                 $patron->set_password({ password =>  $firstpassword });
182             };
183
184             #Error handling checking if the patron was created successfully
185             unless ($@) {
186                 push @messages, { code => 'success_on_insert_patron' };
187             }
188             else {
189                 warn $@;
190                 push @messages, { code => 'error_on_insert_patron' };
191             }
192         }
193     }
194
195     $step++ if Koha::Patrons->search( { flags => 1 } )->count;
196 }
197 if ( $step == 4 ) {
198     if ( $op eq 'add_validate_itemtype' ) {
199         my $description   = $input->param('description');
200         my $itemtype_code = $input->param('itemtype');
201         $itemtype_code = uc($itemtype_code);
202
203         my $itemtype = Koha::ItemType->new(
204             {
205                 itemtype    => $itemtype_code,
206                 description => $description,
207             }
208         );
209         eval { $itemtype->store; };
210
211         unless ($@) {
212             push @messages, { code => 'success_on_insert_itemtype' };
213         }
214         else {
215             push @messages, { code => 'error_on_insert_itemtype' };
216         }
217     }
218
219     $step++ if Koha::ItemTypes->count;
220 }
221 if ( $step == 5 ) {
222
223     if ( $op eq 'add_validate_circ_rule' ) {
224
225         #If no libraries exist then set the $branch value to *
226         my $branch = $input->param('branch') || '*';
227
228         my $type            = $input->param('type');
229         my $branchcode      = $input->param('branch');
230         my $categorycode    = $input->param('categorycode');
231         my $itemtype        = $input->param('itemtype');
232         my $maxissueqty     = $input->param('maxissueqty');
233         my $issuelength     = $input->param('issuelength') || 0;
234         my $lengthunit      = $input->param('lengthunit');
235         my $renewalsallowed = $input->param('renewalsallowed');
236         my $renewalperiod   = $input->param('renewalperiod');
237         my $reservesallowed = $input->param('reservesallowed');
238         my $holds_per_day   = $input->param('holds_per_day');
239         my $holds_per_record = $input->param('holds_per_record');
240         my $onshelfholds    = $input->param('onshelfholds') || 0;
241         $maxissueqty =~ s/\s//g;
242         $maxissueqty = undef if $maxissueqty !~ /^\d+/;
243
244         my $params = {
245             branchcode      => $branchcode,
246             categorycode    => $categorycode,
247             itemtype        => $itemtype,
248             rules => {
249                 renewalsallowed                  => $renewalsallowed,
250                 renewalperiod                    => $renewalperiod,
251                 issuelength                      => $issuelength,
252                 lengthunit                       => $lengthunit,
253                 onshelfholds                     => $onshelfholds,
254                 article_requests                 => "no",
255                 auto_renew                       => 0,
256                 cap_fine_to_replacement_price    => 0,
257                 chargeperiod                     => 0,
258                 chargeperiod_charge_at           => 0,
259                 fine                             => 0,
260                 finedays                         => 0,
261                 firstremind                      => 0,
262                 hardduedate                      => "",
263                 hardduedatecompare               => -1,
264                 holds_per_day                    => $holds_per_day,
265                 holds_per_record                 => $holds_per_record,
266                 maxissueqty                      => $maxissueqty,
267                 maxonsiteissueqty                => "",
268                 maxsuspensiondays                => "",
269                 no_auto_renewal_after            => "",
270                 no_auto_renewal_after_hard_limit => "",
271                 norenewalbefore                  => "",
272                 opacitemholds                    => "N",
273                 overduefinescap                  => "",
274                 rentaldiscount                   => 0,
275                 reservesallowed                  => $reservesallowed,
276                 suspension_chargeperiod          => 1,
277                 decreaseloanholds                => "",
278                 unseen_renewals_allowed          => "",
279                 recalls_allowed                  => undef,
280                 recalls_per_record               => undef,
281                 on_shelf_recalls                 => undef,
282                 recall_due_date_interval         => undef,
283                 recall_overdue_fine              => undef,
284                 recall_shelf_time                => undef,
285               }
286         };
287
288         eval {
289             Koha::CirculationRules->set_rules($params);
290         };
291
292         if ($@) {
293             warn $@;
294             push @messages, { code => 'error_on_insert_circ_rule' };
295         } else {
296             push @messages, { code => 'success_on_insert_circ_rule' };
297         }
298     }
299
300     $step++ if Koha::CirculationRules->count;
301 }
302
303 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
304 $template_params->{libraries}   = $libraries;
305
306 if ( $step > 5 ) {
307     $template_params->{all_done} = 1;    # If step 5 is complete, we are done!
308     $step = 5;
309 }
310
311 #Getting the appropriate template to display to the user
312 my ( $template, $loggedinuser );
313 ( $template, $loggedinuser, $cookie ) = C4::InstallAuth::get_template_and_user(
314     {
315         template_name   => "onboarding/onboardingstep${step}.tt",
316         query           => $input,
317         type            => "intranet",
318     }
319 );
320
321 $template_params->{messages} = \@messages;
322 my $categories = Koha::Patron::Categories->search();
323 $template_params->{categories} = $categories;
324
325 my $itemtypes = Koha::ItemTypes->search();
326 $template_params->{itemtypes} = $itemtypes;
327
328 $template->param(%$template_params);
329
330 output_html_with_http_headers $input, $cookie, $template->output;