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' }
108 #Select all the patron category records in the categories database table and give them to the template
109 my $categories = Koha::Patron::Categories->search();
110 $template->param( 'categories' => $categories, );
112 #Check if the $step variable equals 1 i.e. the user has clicked to create a library in the create library screen 1
113 my $itemtypes = Koha::ItemTypes->search();
114 $template->param( 'itemtypes' => $itemtypes, );
116 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" ) {
158 my $searchfield = $input->param('description') // q||;
159 my $categorycode = $input->param('categorycode');
160 my $op = $input->param('op') // 'list';
163 $template->param( 'categorycode' => $categorycode );
165 my ( $template, $loggedinuser, $cookie ) =
166 C4::InstallAuth::get_template_and_user(
168 template_name => "/onboarding/onboardingstep2.tt",
171 authnotrequired => 0,
173 { parameters => 'parameters_remaining_permissions' },
178 #Once the user submits the page, this code validates the input and adds it
179 #to the database as a new patron category
180 $categorycode = $input->param('categorycode');
181 my $description = $input->param('description');
182 my $overduenoticerequired = $input->param('overduenoticerequired');
183 my $category_type = $input->param('category_type');
184 my $default_privacy = $input->param('default_privacy');
185 my $enrolmentperiod = $input->param('enrolmentperiod');
186 my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
188 #Converts the string into a date format
189 if ($enrolmentperioddate) {
190 $enrolmentperioddate = output_pref(
192 dt => dt_from_string($enrolmentperioddate),
193 dateformat => 'DateTime',
199 #Adds a new patron category to the database
200 $category = Koha::Patron::Category->new(
202 categorycode => $categorycode,
203 description => $description,
204 overduenoticerequired => $overduenoticerequired,
205 category_type => $category_type,
206 default_privacy => $default_privacy,
207 enrolmentperiod => $enrolmentperiod,
208 enrolmentperioddate => $enrolmentperioddate
212 eval { $category->store; };
216 $message = 'success_on_insert';
219 $message = 'error_on_insert';
222 $template->param( 'message' => $message );
227 elsif ( $step && $step == 3 ) {
228 my $firstpassword = $input->param('password') || '';
229 my $secondpassword = $input->param('password2') || '';
231 #Find all patron records in the database and hand them to the template
232 my %currentpatrons = Koha::Patrons->search();
233 my $currentpatrons = values %currentpatrons;
234 $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 =
268 $schema->resultset('Borrower')->get_column('cardnumber')->max() // 0;
270 my $new_cardnumber = $existing_cardnumber + 1;
271 $template->param( "newcardnumber" => $new_cardnumber );
273 my $op = $input->param('op') // 'list';
274 my $minpw = C4::Context->preference("minPasswordLength");
275 $template->param( "minPasswordLength" => $minpw );
278 my $nok = $input->param('nok');
279 my $cardnumber = $input->param('cardnumber');
280 my $borrowernumber = $input->param('borrowernumber');
281 my $userid = $input->param('userid');
283 # function to designate mandatory fields (visually with css)
284 my $check_BorrowerMandatoryField =
285 C4::Context->preference("BorrowerMandatoryField");
286 my @field_check = split( /\|/, $check_BorrowerMandatoryField );
287 foreach (@field_check) {
288 $template->param( "mandatory$_" => 1 );
290 BorrowerMandatoryField =>
291 C4::Context->preference("BorrowerMandatoryField")
292 , #field to test with javascript
296 #If the entered cardnumber causes an error hand this error to the @errors array
297 if ( my $error_code = checkcardnumber( $cardnumber, $borrowernumber ) ) {
299 $error_code == 1 ? 'ERROR_cardnumber_already_exists'
300 : $error_code == 2 ? 'ERROR_cardnumber_length'
304 #If the entered password causes an error hand this error to the @errors array
305 push @errors, "ERROR_password_mismatch"
306 if $firstpassword ne $secondpassword;
307 push @errors, "ERROR_short_password"
310 && $firstpassword ne '****'
311 && ( length($firstpassword) < $minpw ) );
313 #Passing errors to template
314 $nok = $nok || scalar(@errors);
316 #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
318 foreach my $error (@errors) {
319 if ( $error eq 'ERROR_password_mismatch' ) {
320 $template->param( errorpasswordmismatch => 1 );
322 if ( $error eq 'ERROR_login_exist' ) {
323 $template->param( errorloginexists => 1 );
325 if ( $error eq 'ERROR_cardnumber_already_exists' ) {
326 $template->param( errorcardnumberexists => 1 );
328 if ( $error eq 'ERROR_cardnumber_length' ) {
329 $template->param( errorcardnumberlength => 1 );
331 if ( $error eq 'ERROR_short_password' ) {
332 $template->param( errorshortpassword => 1 );
335 $template->param( 'nok' => 1 );
337 #Else if no errors have been caused by the users inputted card number or password then insert the patron into the borrowers table
340 my ( $template, $loggedinuser, $cookie ) =
341 C4::InstallAuth::get_template_and_user(
343 template_name => "/onboarding/onboardingstep3.tt",
346 authnotrequired => 0,
347 flagsrequired => { borrowers => 1 },
352 if ( $op eq 'add_validate' ) {
355 #Store the template form values in the newdata hash
356 $newdata{borrowernumber} = $input->param('borrowernumber');
357 $newdata{surname} = $input->param('surname');
358 $newdata{firstname} = $input->param('firstname');
359 $newdata{cardnumber} = $input->param('cardnumber');
360 $newdata{branchcode} = $input->param('libraries');
361 $newdata{categorycode} = $input->param('categorycode_entry');
362 $newdata{userid} = $input->param('userid');
363 $newdata{password} = $input->param('password');
364 $newdata{password2} = $input->param('password2');
365 $newdata{privacy} = "default";
366 $newdata{address} = "";
369 #Hand tne the dateexpiry of the patron based on the patron category it is created from
370 my $patron_category =
371 Koha::Patron::Categories->find( $newdata{categorycode} );
372 $newdata{dateexpiry} =
373 $patron_category->get_expiry_date( $newdata{dateenrolled} );
375 #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
376 my $borrowernumber = &AddMember(%newdata);
378 #Create a hash named member2 and fill it with the borrowernumber of the borrower that has just been created
380 $member2{'borrowernumber'} = $borrowernumber;
382 #Perform data validation on the flag that has been handed to onboarding.pl by the template
383 my $flag = $input->param('flag');
384 if ( $input->param('newflags') ) {
385 my $dbh = C4::Context->dbh();
386 my @perms = $input->multi_param('flag');
387 my %all_module_perms = ();
389 foreach my $perm (@perms) {
390 if ( $perm !~ /:/ ) {
391 $all_module_perms{$perm} = 1;
394 my ( $module, $sub_perm ) = split /:/, $perm, 2;
395 push @{ $sub_perms{$module} }, $sub_perm;
400 my @userflags = $schema->resultset('Userflag')->search(
403 order_by => { -asc => 'bit' },
407 #Setting superlibrarian permissions for new patron
409 Koha::Patrons->find($borrowernumber)->set( { flags => 1 } )
412 #Error handling checking if the patron was created successfully
413 if ( !$borrowernumber ) {
415 { type => 'error', code => 'error_on_insert' };
419 { type => 'message', code => 'success_on_insert' };
425 elsif ( $step && $step == 4 ) {
426 my ( $template, $borrowernumber, $cookie ) =
427 C4::InstallAuth::get_template_and_user(
429 template_name => "/onboarding/onboardingstep4.tt",
432 authnotrequired => 0,
434 { parameters => 'parameters_remaining_permissions' },
438 if ( $op eq "add_validate" ) {
439 my $description = $input->param('description');
440 my $itemtype_code = $input->param('itemtype');
441 $itemtype_code = uc($itemtype_code);
443 #Create a new itemtype object using the user inputted itemtype and description
444 my $itemtype = Koha::ItemType->new(
446 itemtype => $itemtype_code,
447 description => $description,
450 eval { $itemtype->store; };
453 #Fill the $message variable with an error if the item type object was not successfully created and inserted into the itemtypes table
455 $message = 'success_on_insert';
458 $message = 'error_on_insert';
460 $template->param( 'message' => $message );
463 elsif ( $step && $step == 5 ) {
465 #Find all the existing categories to display in a dropdown box in the template
467 $categories = Koha::Patron::Categories->search();
468 $template->param( categories => $categories, );
470 #Find all the exisiting item types to display in a dropdown box in the template
472 $itemtypes = Koha::ItemTypes->search();
473 $template->param( itemtypes => $itemtypes, );
475 #Find all the exisiting libraries to display in a dropdown box in the template
477 Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
479 libraries => $libraries,
482 categorytype => 'searchdomain',
484 Koha::LibraryCategories->search(
485 { categorytype => 'searchdomain' }
490 categorytype => 'properties',
492 Koha::LibraryCategories->search(
493 { categorytype => 'properties' }
500 my $input = CGI->new;
501 my $dbh = C4::Context->dbh;
503 my ( $template, $loggedinuser, $cookie ) =
504 C4::InstallAuth::get_template_and_user(
506 template_name => "/onboarding/onboardingstep5.tt",
509 authnotrequired => 0,
510 flagsrequired => { parameters => 'manage_circ_rules' },
515 #If no libraries exist then set the $branch value to *
516 my $branch = $input->param('branch');
518 if ( C4::Context->preference('DefaultToLoggedInLibraryCircRules') ) {
520 Koha::Libraries->search->count() == 1
522 : C4::Context::mybranch();
526 C4::Context::only_my_library()
527 ? ( C4::Context::mybranch() || '*' )
531 $branch = '*' if $branch eq 'NO_LIBRARY_SET';
532 my $op = $input->param('op') || q{};
534 if ( $op eq 'add_validate' ) {
535 my $type = $input->param('type');
536 my $br = $input->param('branch');
537 my $bor = $input->param('categorycode');
538 my $itemtype = $input->param('itemtype');
539 my $maxissueqty = $input->param('maxissueqty');
540 my $issuelength = $input->param('issuelength');
541 my $lengthunit = $input->param('lengthunit');
542 my $renewalsallowed = $input->param('renewalsallowed');
543 my $renewalperiod = $input->param('renewalperiod');
544 my $onshelfholds = $input->param('onshelfholds') || 0;
545 $maxissueqty =~ s/\s//g;
546 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
547 $issuelength = $issuelength eq q{} ? undef : $issuelength;
551 categorycode => $bor,
552 itemtype => $itemtype,
553 maxissueqty => $maxissueqty,
554 renewalsallowed => $renewalsallowed,
555 renewalperiod => $renewalperiod,
556 issuelength => $issuelength,
557 lengthunit => $lengthunit,
558 onshelfholds => $onshelfholds,
563 #Allows for the 'All' option to work when selecting all libraries for a circulation rule to apply to.
564 if ( $branch eq "*" ) {
565 my $search_default_rules =
566 $schema->resultset('DefaultCircRule')->count();
567 my $insert_default_rules =
568 $schema->resultset('Issuingrule')
570 { maxissueqty => $maxissueqty, onshelfholds => $onshelfholds }
574 #Allows for the 'All' option to work when selecting all patron categories for a circulation rule to apply to.
575 elsif ( $bor eq "*" ) {
577 my $search_default_rules =
578 $schema->resultset('DefaultCircRule')->count();
579 my $insert_default_rules = $schema->resultset('Issuingrule')
580 ->new( { maxissueqty => $maxissueqty } );
583 #Allows for the 'All' option to work when selecting all itemtypes for a circulation rule to apply to
584 elsif ( $itemtype eq "*" ) {
585 my $search_default_rules =
586 $schema->resultset('DefaultCircRule')->search(
589 branchcode => $branch
594 my $insert_default_rules = $schema->resultset('Issuingrule')
595 ->new( { branchcode => $branch, onshelfholds => $onshelfholds } );
598 my $issuingrule = Koha::IssuingRules->find(
599 { categorycode => $bor, itemtype => $itemtype, branchcode => $br }
602 $issuingrule->set($params)->store();
606 code => 'error_on_insert'
607 }; #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.
611 Koha::IssuingRule->new()->set($params)->store();
616 output_html_with_http_headers $input, $cookie, $template->output;