3 # This file is part of Koha.
5 # Copyright (C) 2017 Catalyst IT
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.
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.
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>.
31 use Koha::Patron::Categories;
32 use Koha::Patron::Category;
34 use Koha::IssuingRule;
35 use Koha::IssuingRules;
39 my $step = $input->param('step');
41 #Getting the appropriate template to display to the user
42 my ( $template, $loggedinuser, $cookie ) =
43 C4::InstallAuth::get_template_and_user(
45 template_name => "/onboarding/onboardingstep"
46 . ( $step ? $step : 1 ) . ".tt",
54 #Check database connection
56 $info{'dbname'} = C4::Context->config("database");
58 C4::Context->config("db_scheme")
59 ? C4::Context->config("db_scheme")
63 $info{'hostname'} = C4::Context->config("hostname");
64 $info{'port'} = C4::Context->config("port");
65 $info{'user'} = C4::Context->config("user");
66 $info{'password'} = C4::Context->config("pass");
67 my $dbh = DBI->connect(
68 "DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}"
69 . ( $info{port} ? ";port=$info{port}" : "" ),
70 $info{'user'}, $info{'password'}
73 #Store the value of the template input name='op' in the variable $op so we can check if the user has pressed the button with the name="op" and value="finish" meaning the user has finished the onboarding tool.
74 my $op = $input->param('op') || '';
75 $template->param( 'op' => $op );
77 my $schema = Koha::Database->new()->schema();
79 if ( $op && $op eq 'finish' )
80 { #If the value of $op equals 'finish' then redirect user to /cgi-bin/koha/mainpage.pl
81 print $input->redirect("/cgi-bin/koha/mainpage.pl");
85 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
87 libraries => $libraries,
90 categorytype => 'searchdomain',
92 Koha::LibraryCategories->search(
93 { categorytype => 'searchdomain' }
98 categorytype => 'properties',
100 Koha::LibraryCategories->search(
101 { categorytype => 'properties' }
109 #Select all the patron category records in the categories database table and give them to the template
110 my $categories = Koha::Patron::Categories->search();
111 $template->param( 'categories' => $categories, );
113 #Check if the $step variable equals 1 i.e. the user has clicked to create a library in the create library screen 1
114 my $itemtypes = Koha::ItemTypes->search();
115 $template->param( 'itemtypes' => $itemtypes, );
117 if ( $step && $step == 1 ) {
118 #store inputted parameters in variables
119 my $branchcode = $input->param('branchcode');
120 $branchcode = uc($branchcode);
121 my $categorycode = $input->param('categorycode');
122 my $op = $input->param('op') || 'list';
126 #Take the text 'branchname' and store it in the @fields array
131 $template->param( 'branchcode' => $branchcode );
132 $branchcode =~ s|\s||g
133 ; # Use a regular expression to check the value of the inputted branchcode
135 #Create a new library object and store the branchcode and @fields array values in this new library object
136 $library = Koha::Library->new(
138 branchcode => $branchcode,
139 ( map { $_ => scalar $input->param($_) || undef } @fields )
143 eval { $library->store; }; #Use the eval{} function to store the library object
145 $message = 'success_on_insert';
148 $message = 'error_on_insert';
150 $template->param( 'message' => $message );
152 #Check if the $step variable equals 2 i.e. the user has clicked to create a patron category in the create patron category screen 1
154 elsif ( $step && $step == 2 ) {
155 if ($op eq "add_validate_category"){
157 my $searchfield = $input->param('description') // q||;
158 my $categorycode = $input->param('categorycode');
159 my $op = $input->param('op') // 'list';
162 $template->param( 'categorycode' => $categorycode );
164 my ( $template, $loggedinuser, $cookie ) =
165 C4::InstallAuth::get_template_and_user(
167 template_name => "/onboarding/onboardingstep2.tt",
170 authnotrequired => 0,
172 { parameters => 'parameters_remaining_permissions' },
177 #Once the user submits the page, this code validates the input and adds it
178 #to the database as a new patron category
179 $categorycode = $input->param('categorycode');
180 my $description = $input->param('description');
181 my $overduenoticerequired = $input->param('overduenoticerequired');
182 my $category_type = $input->param('category_type');
183 my $default_privacy = $input->param('default_privacy');
184 my $enrolmentperiod = $input->param('enrolmentperiod');
185 my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
187 #Converts the string into a date format
188 if ($enrolmentperioddate) {
189 $enrolmentperioddate = output_pref(
191 dt => dt_from_string($enrolmentperioddate),
198 #Adds a new patron category to the database
199 $category = Koha::Patron::Category->new(
201 categorycode => $categorycode,
202 description => $description,
203 overduenoticerequired => $overduenoticerequired,
204 category_type => $category_type,
205 default_privacy => $default_privacy,
206 enrolmentperiod => $enrolmentperiod,
207 enrolmentperioddate => $enrolmentperioddate,
211 eval { $category->store; };
215 $message = 'success_on_insert';
218 $message = 'error_on_insert';
221 $template->param( 'message' => $message );
225 elsif ( $step && $step == 3 ) {
226 my $firstpassword = $input->param('password') || '';
227 my $secondpassword = $input->param('password2') || '';
230 #Find all patron records in the database and hand them to the template
231 my %currentpatrons = Koha::Patrons->search();
232 my $currentpatrons = values %currentpatrons;
233 $template->param( 'patrons' =>$currentpatrons);
236 #Find all library records in the database and hand them to the template to display in the library dropdown box
238 Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
240 libraries => $libraries,
243 categorytype => 'searchdomain',
245 Koha::LibraryCategories->search(
246 { categorytype => 'searchdomain' }
251 categorytype => 'properties',
253 Koha::LibraryCategories->search(
254 { categorytype => 'properties' }
261 #Find all patron categories in the database and hand them to the template to display in the patron category dropdown box
262 my $categories = Koha::Patron::Categories->search();
263 $template->param( 'categories' => $categories, );
265 #Incrementing the highest existing patron cardnumber to prevent duplicate cardnumber entry
267 my $existing_cardnumber = $schema->resultset('Borrower')->get_column('cardnumber')->max() // 0;
269 my $new_cardnumber = $existing_cardnumber + 1;
270 $template->param( "newcardnumber" => $new_cardnumber );
272 my $op = $input->param('op') // 'list';
273 my $minpw = C4::Context->preference("minPasswordLength");
274 $template->param( "minPasswordLength" => $minpw );
277 my $nok = $input->param('nok');
278 my $cardnumber = $input->param('cardnumber');
279 my $borrowernumber = $input->param('borrowernumber');
280 my $userid = $input->param('userid');
282 # function to designate mandatory fields (visually with css)
283 my $check_BorrowerMandatoryField =
284 C4::Context->preference("BorrowerMandatoryField");
285 my @field_check = split( /\|/, $check_BorrowerMandatoryField );
286 foreach (@field_check) {
287 $template->param( "mandatory$_" => 1 );
289 BorrowerMandatoryField =>
290 C4::Context->preference("BorrowerMandatoryField")
291 , #field to test with javascript
295 #If the entered cardnumber causes an error hand this error to the @errors array
296 if ( my $error_code = checkcardnumber( $cardnumber, $borrowernumber ) ) {
298 $error_code == 1 ? 'ERROR_cardnumber_already_exists'
299 : $error_code == 2 ? 'ERROR_cardnumber_length'
303 #If the entered password causes an error hand this error to the @errors array
304 push @errors, "ERROR_password_mismatch"
305 if $firstpassword ne $secondpassword;
306 push @errors, "ERROR_short_password"
309 && $firstpassword ne '****'
310 && ( length($firstpassword) < $minpw ) );
312 #Passing errors to template
313 $nok = $nok || scalar(@errors);
315 #If errors have been generated from the users inputted cardnumber or password then display the error and do not insert the patron into the borrowers table
317 foreach my $error (@errors) {
318 if ( $error eq 'ERROR_password_mismatch' ) {
319 $template->param( errorpasswordmismatch => 1 );
321 if ( $error eq 'ERROR_login_exist' ) {
322 $template->param( errorloginexists => 1 );
324 if ( $error eq 'ERROR_cardnumber_already_exists' ) {
325 $template->param( errorcardnumberexists => 1 );
327 if ( $error eq 'ERROR_cardnumber_length' ) {
328 $template->param( errorcardnumberlength => 1 );
330 if ( $error eq 'ERROR_short_password' ) {
331 $template->param( errorshortpassword => 1 );
334 $template->param( 'nok' => 1 );
336 #Else if no errors have been caused by the users inputted card number or password then insert the patron into the borrowers table
339 my ( $template, $loggedinuser, $cookie ) =
340 C4::InstallAuth::get_template_and_user(
342 template_name => "/onboarding/onboardingstep3.tt",
345 authnotrequired => 0,
346 flagsrequired => { borrowers => 1 },
351 if ( $op eq 'add_validate' ) {
354 #Store the template form values in the newdata hash
355 $newdata{borrowernumber} = $input->param('borrowernumber');
356 $newdata{surname} = $input->param('surname');
357 $newdata{firstname} = $input->param('firstname');
358 $newdata{cardnumber} = $input->param('cardnumber');
359 $newdata{branchcode} = $input->param('libraries');
360 $newdata{categorycode} = $input->param('categorycode_entry');
361 $newdata{userid} = $input->param('userid');
362 $newdata{password} = $input->param('password');
363 $newdata{password2} = $input->param('password2');
364 $newdata{privacy} = "default";
365 $newdata{address} = "";
368 #Hand tne the dateexpiry of the patron based on the patron category it is created from
369 my $patron_category = Koha::Patron::Categories->find( $newdata{categorycode} );
370 $newdata{dateexpiry} = $patron_category->get_expiry_date( $newdata{dateenrolled} );
372 #Hand the newdata hash to the AddMember subroutine in the C4::Members module and it creates a patron and hands back a borrowernumber which is being stored
373 my $borrowernumber = &AddMember(%newdata);
375 #Create a hash named member2 and fill it with the borrowernumber of the borrower that has just been created
377 $member2{'borrowernumber'} = $borrowernumber;
379 #Perform data validation on the flag that has been handed to onboarding.pl by the template
380 my $flag = $input->param('flag');
381 if ( $input->param('newflags') ) {
382 my $dbh = C4::Context->dbh();
383 my @perms = $input->multi_param('flag');
384 my %all_module_perms = ();
386 foreach my $perm (@perms) {
387 if ( $perm !~ /:/ ) {
388 $all_module_perms{$perm} = 1;
391 my ( $module, $sub_perm ) = split /:/, $perm, 2;
392 push @{ $sub_perms{$module} }, $sub_perm;
397 my @userflags = $schema->resultset('Userflag')->search({},{
398 order_by => { -asc =>'bit'},
402 #Setting superlibrarian permissions for new patron
403 my $flags = Koha::Patrons->find($borrowernumber)->set({flags=>1})->store;
405 #Error handling checking if the patron was created successfully
406 if ( !$borrowernumber ) {
408 { type => 'error', code => 'error_on_insert' };
412 { type => 'message', code => 'success_on_insert' };
418 elsif ( $step && $step == 4 ) {
419 my ( $template, $borrowernumber, $cookie ) =
420 C4::InstallAuth::get_template_and_user(
422 template_name => "/onboarding/onboardingstep4.tt",
425 authnotrequired => 0,
427 { parameters => 'parameters_remaining_permissions' },
431 if ($op eq "add_validate"){
432 my $description = $input->param('description');
433 my $itemtype_code = $input->param('itemtype');
434 $itemtype_code = uc($itemtype_code);
436 #Create a new itemtype object using the user inputted itemtype and description
437 my $itemtype = Koha::ItemType->new(
439 itemtype => $itemtype_code,
440 description => $description,
443 eval { $itemtype->store; };
446 #Fill the $message variable with an error if the item type object was not successfully created and inserted into the itemtypes table
448 $message = 'success_on_insert';
451 $message = 'error_on_insert';
453 $template->param( 'message' => $message );
456 elsif ( $step && $step == 5 ) {
458 #Find all the existing categories to display in a dropdown box in the template
460 $categories = Koha::Patron::Categories->search();
461 $template->param( categories => $categories, );
463 #Find all the exisiting item types to display in a dropdown box in the template
465 $itemtypes = Koha::ItemTypes->search();
466 $template->param( itemtypes => $itemtypes, );
468 #Find all the exisiting libraries to display in a dropdown box in the template
470 Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
472 libraries => $libraries,
475 categorytype => 'searchdomain',
477 Koha::LibraryCategories->search(
478 { categorytype => 'searchdomain' }
483 categorytype => 'properties',
485 Koha::LibraryCategories->search(
486 { categorytype => 'properties' }
493 my $input = CGI->new;
494 my $dbh = C4::Context->dbh;
496 my ( $template, $loggedinuser, $cookie ) =
497 C4::InstallAuth::get_template_and_user(
499 template_name => "/onboarding/onboardingstep5.tt",
502 authnotrequired => 0,
503 flagsrequired => { parameters => 'manage_circ_rules' },
508 #If no libraries exist then set the $branch value to *
509 my $branch = $input->param('branch');
511 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
513 Koha::Libraries->search->count() == 1
515 : C4::Context::mybranch();
519 C4::Context::only_my_library()
520 ? ( C4::Context::mybranch() || '*' )
524 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
525 my $op = $input->param('op') || q{};
527 if ( $op eq 'add_validate' ) {
528 my $type = $input->param('type');
529 my $br = $input->param('branch');
530 my $bor = $input->param('categorycode');
531 my $itemtype = $input->param('itemtype');
532 my $maxissueqty = $input->param('maxissueqty');
533 my $issuelength = $input->param('issuelength');
534 my $lengthunit = $input->param('lengthunit');
535 my $renewalsallowed = $input->param('renewalsallowed');
536 my $renewalperiod = $input->param('renewalperiod');
537 my $onshelfholds = $input->param('onshelfholds') || 0;
538 $maxissueqty =~ s/\s//g;
539 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
540 $issuelength = $issuelength eq q{} ? undef : $issuelength;
544 categorycode => $bor,
545 itemtype => $itemtype,
546 maxissueqty => $maxissueqty,
547 renewalsallowed => $renewalsallowed,
548 renewalperiod => $renewalperiod,
549 issuelength => $issuelength,
550 lengthunit => $lengthunit,
551 onshelfholds => $onshelfholds,
556 #Allows for the 'All' option to work when selecting all libraries for a circulation rule to apply to.
557 if ( $branch eq "*" ) {
558 my $search_default_rules = $schema->resultset('DefaultCircRule')->count();
559 my $insert_default_rules = $schema->resultset('Issuingrule')->new(
560 { maxissueqty => $maxissueqty, onshelfholds => $onshelfholds }
563 #Allows for the 'All' option to work when selecting all patron categories for a circulation rule to apply to.
564 elsif ( $bor eq "*" ) {
566 my $search_default_rules = $schema->resultset('DefaultCircRule')->count();
567 my $insert_default_rules = $schema->resultset('Issuingrule')->new(
568 { maxissueqty => $maxissueqty}
572 #Allows for the 'All' option to work when selecting all itemtypes for a circulation rule to apply to
573 elsif ( $itemtype eq "*" ) {
574 my $search_default_rules = $schema->resultset('DefaultCircRule')->search({},{
575 branchcode => $branch
580 my $insert_default_rules = $schema->resultset('Issuingrule')->new(
581 { branchcode => $branch, onshelfholds => $onshelfholds }
585 my $issuingrule = Koha::IssuingRules->find(
586 { categorycode => $bor, itemtype => $itemtype, branchcode => $br }
589 $issuingrule->set($params)->store();
593 code => 'error_on_insert'
594 }; #Stops crash of the onboarding tool if someone makes a circulation rule with the same item type, library and patron categroy as an exisiting circulation rule.
598 Koha::IssuingRule->new()->set($params)->store();
603 output_html_with_http_headers $input, $cookie, $template->output;