|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
#Recommended pragmas |
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use diagnostics; |
| 6 |
|
| 7 |
|
| 8 |
use Modern::Perl; |
| 9 |
|
| 10 |
#External modules |
| 11 |
use CGI qw ( -utf8 ); |
| 12 |
use List::MoreUtils qw/uniq/; |
| 13 |
use Digest::MD5 qw(md5_base64); |
| 14 |
use Encode qw( encode ); |
| 15 |
|
| 16 |
#Internal modules |
| 17 |
use C4::Koha; |
| 18 |
use C4::Auth; |
| 19 |
use C4::Context; |
| 20 |
use C4::Output; |
| 21 |
use C4::Members; |
| 22 |
use C4::Members::Attributes; |
| 23 |
use C4::Members::AttributeTypes; |
| 24 |
use C4::Log; |
| 25 |
use C4::Letters; |
| 26 |
use C4::Form::MessagingPreferences; |
| 27 |
use Koha::AuthorisedValues; |
| 28 |
use Koha::Patron::Debarments; |
| 29 |
use Koha::Cities; |
| 30 |
use Koha::Patrons; |
| 31 |
use Koha::Items; |
| 32 |
use Koha::Libraries; |
| 33 |
use Koha::LibraryCategories; |
| 34 |
use Koha::Database; |
| 35 |
use Koha::DateUtils; |
| 36 |
use Koha::Patron::Categories; |
| 37 |
use Koha::ItemTypes; |
| 38 |
use Koha::Patron::HouseboundRole; |
| 39 |
use Koha::Patron::HouseboundRoles; |
| 40 |
use Koha::Token; |
| 41 |
use Email::Valid; |
| 42 |
use Module::Load; |
| 43 |
|
| 44 |
#Setting variables |
| 45 |
my $input = new CGI; |
| 46 |
my $query = new CGI; |
| 47 |
my $step = $query->param('step'); |
| 48 |
|
| 49 |
#Getting the appropriate template to display to the user--> |
| 50 |
my ( $template, $loggedinuser, $cookie) = get_template_and_user( |
| 51 |
{ |
| 52 |
template_name => "/onboarding/onboardingstep" . ( $step ? $step : 0 ) . ".tt", |
| 53 |
query => $query, |
| 54 |
type => "intranet", |
| 55 |
authnotrequired => 0, |
| 56 |
debug => 1, |
| 57 |
} |
| 58 |
); |
| 59 |
|
| 60 |
#Check database connection |
| 61 |
my %info; |
| 62 |
$info{'dbname'} = C4::Context->config("database"); |
| 63 |
$info{'dbms'} = |
| 64 |
( C4::Context->config("db_scheme") |
| 65 |
? C4::Context->config("db_scheme") |
| 66 |
: "mysql" ); |
| 67 |
|
| 68 |
$info{'hostname'} = C4::Context->config("hostname"); |
| 69 |
$info{'port'} = C4::Context->config("port"); |
| 70 |
$info{'user'} = C4::Context->config("user"); |
| 71 |
$info{'password'} = C4::Context->config("pass"); |
| 72 |
my $dbh = DBI->connect( |
| 73 |
"DBI:$info{dbms}:dbname=$info{dbname};host=$info{hostname}" |
| 74 |
. ( $info{port} ? ";port=$info{port}" : "" ), |
| 75 |
$info{'user'}, $info{'password'} |
| 76 |
); |
| 77 |
|
| 78 |
|
| 79 |
#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. |
| 80 |
my $op = $query->param('op'); |
| 81 |
$template->param('op'=>$op); |
| 82 |
if ( $op && $op eq 'finish' ) { #If the value of $op equals 'finish' then redirect user to /cgi-bin/koha/mainpage.pl |
| 83 |
print $query->redirect("/cgi-bin/koha/mainpage.pl"); |
| 84 |
exit; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
#Store the value of the template input name='start' in the variable $start so we can check if the user has pressed this button and starting the onboarding tool process |
| 89 |
my $start = $query->param('start'); |
| 90 |
$template->param('start'=>$start); #Hand the start variable back to the template |
| 91 |
if ( $start && $start eq 'Start setting up my Koha' ){ |
| 92 |
my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, ); |
| 93 |
$template->param(libraries => $libraries, |
| 94 |
group_types => [ |
| 95 |
{ categorytype => 'searchdomain', |
| 96 |
categories => [ Koha::LibraryCategories->search( { categorytype => 'searchdomain' } ) ], |
| 97 |
}, |
| 98 |
{ categorytype => 'properties', |
| 99 |
categories => [ Koha::LibraryCategories->search( { categorytype => 'properties' } ) ], |
| 100 |
}, |
| 101 |
] |
| 102 |
); |
| 103 |
|
| 104 |
|
| 105 |
#Select any library records from the database and hand them back to the template in the libraries variable. |
| 106 |
}elsif ( $start && $start eq 'Add a patron category' ){ |
| 107 |
|
| 108 |
#Select all the patron category records in the categories database table and store them in the newly declared variable $categories |
| 109 |
my $categories = Koha::Patron::Categories->search(); |
| 110 |
$template->param( |
| 111 |
categories => $categories, |
| 112 |
); #Hand the variable categories back to the template |
| 113 |
|
| 114 |
}elsif ( $start && $start eq 'Add an item type' ){ |
| 115 |
my $itemtypes = Koha::ItemTypes->search(); |
| 116 |
$template->param( |
| 117 |
itemtypes => $itemtypes, |
| 118 |
); |
| 119 |
|
| 120 |
#Check if the $step variable equals 1 i.e. the user has clicked to create a library in the create library screen 1 |
| 121 |
}elsif ( $step && $step == 1 ) { |
| 122 |
|
| 123 |
my $createlibrary = $query->param('createlibrary'); #Store the inputted library branch code and name in $createlibrary |
| 124 |
$template->param('createlibrary'=>$createlibrary); # Hand the library values back to the template in the createlibrary variable |
| 125 |
|
| 126 |
#store inputted parameters in variables |
| 127 |
my $branchcode = $input->param('branchcode'); |
| 128 |
my $categorycode = $input->param('categorycode'); |
| 129 |
my $op = $input->param('op') || 'list'; |
| 130 |
my $message; |
| 131 |
my $library; |
| 132 |
#my @messages; |
| 133 |
|
| 134 |
#Take the text 'branchname' and store it in the @fields array |
| 135 |
my @fields = qw( |
| 136 |
branchname |
| 137 |
); |
| 138 |
|
| 139 |
|
| 140 |
#test |
| 141 |
$template->param('branchcode'=>$branchcode); |
| 142 |
|
| 143 |
$branchcode =~ s|\s||g; # Use a regular expression to check the value of the inputted branchcode |
| 144 |
|
| 145 |
#Create a new library object and store the branchcode and @fields array values in this new library object |
| 146 |
$library = Koha::Library->new( |
| 147 |
{ branchcode => $branchcode, |
| 148 |
( map { $_ => scalar $input->param($_) || undef } @fields ) |
| 149 |
} |
| 150 |
); |
| 151 |
|
| 152 |
eval { $library->store; }; #Use the eval{} function to store the library object |
| 153 |
|
| 154 |
if($library){ |
| 155 |
$message = 'success_on_insert'; |
| 156 |
}else{ |
| 157 |
$message = 'error_on_insert'; |
| 158 |
} |
| 159 |
|
| 160 |
$template->param('message' => $message); |
| 161 |
|
| 162 |
|
| 163 |
#Check if the $step vairable equals 2 i.e. the user has clicked to create a patron category in the create patron category screen 1 |
| 164 |
}elsif ( $step && $step == 2 ){ |
| 165 |
my $createcat = $query->param('createcat'); #Store the inputted library branch code and name in $createlibrary |
| 166 |
$template->param('createcat'=>$createcat); # Hand the library values back to the template in the createlibrary variable |
| 167 |
|
| 168 |
|
| 169 |
#Initialising values |
| 170 |
my $input = new CGI; |
| 171 |
my $searchfield = $input->param('description') // q||; |
| 172 |
my $categorycode = $input->param('categorycode'); |
| 173 |
my $op = $input->param('op') // 'list'; |
| 174 |
my $message; |
| 175 |
my $category; |
| 176 |
|
| 177 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
| 178 |
{ |
| 179 |
template_name => "/onboarding/onboardingstep2.tt", |
| 180 |
query => $input, |
| 181 |
type => "intranet", |
| 182 |
authnotrequired => 0, |
| 183 |
flagsrequired => { parameters => 'parameters_remaining_permissions' }, |
| 184 |
debug => 1, |
| 185 |
} |
| 186 |
); |
| 187 |
my $categorycode = $input->param('categorycode'); |
| 188 |
my $description = $input->param('description'); |
| 189 |
my $overduenoticerequired = $input->param('overduenoticerequired'); |
| 190 |
my $category_type = $input->param('category_type'); |
| 191 |
my $default_privacy = $input->param('default_privacy'); |
| 192 |
my $enrolmentperiod = $input->param('enrolmentperiod'); |
| 193 |
my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef; |
| 194 |
|
| 195 |
#Converts the string into a date format |
| 196 |
if ( $enrolmentperioddate) { |
| 197 |
$enrolmentperioddate = output_pref( |
| 198 |
{ |
| 199 |
dt => dt_from_string($enrolmentperioddate), |
| 200 |
dateformat => 'iso', |
| 201 |
dateonly => 1, |
| 202 |
} |
| 203 |
); |
| 204 |
} |
| 205 |
#Adds to the database |
| 206 |
$category = Koha::Patron::Category->new({ |
| 207 |
categorycode=> $categorycode, |
| 208 |
description => $description, |
| 209 |
overduenoticerequired => $overduenoticerequired, |
| 210 |
category_type=> $category_type, |
| 211 |
default_privacy => $default_privacy, |
| 212 |
enrolmentperiod => $enrolmentperiod, |
| 213 |
enrolmentperioddate => $enrolmentperioddate, |
| 214 |
}); |
| 215 |
eval { |
| 216 |
$category->store; |
| 217 |
}; |
| 218 |
|
| 219 |
#Error messages |
| 220 |
if($category){ |
| 221 |
$message = 'success_on_insert'; |
| 222 |
}else{ |
| 223 |
$message = 'error_on_insert'; |
| 224 |
} |
| 225 |
|
| 226 |
$template->param('message' => $message); |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
#Create a patron |
| 231 |
}elsif ( $step && $step == 3 ){ |
| 232 |
my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, ); |
| 233 |
$template->param(libraries => $libraries, |
| 234 |
group_types => [ |
| 235 |
{ categorytype => 'searchdomain', |
| 236 |
categories => [ Koha::LibraryCategories->search( { categorytype => 'searchdomain' } ) ], |
| 237 |
}, |
| 238 |
{ categorytype => 'properties', |
| 239 |
categories => [ Koha::LibraryCategories->search( { categorytype => 'properties' } ) ], |
| 240 |
}, |
| 241 |
] |
| 242 |
); |
| 243 |
|
| 244 |
my $categories; |
| 245 |
$categories= Koha::Patron::Categories->search(); |
| 246 |
$template->param( |
| 247 |
categories => $categories, |
| 248 |
); |
| 249 |
|
| 250 |
my $input = new CGI; |
| 251 |
my $op = $input->param('op') // 'list'; |
| 252 |
|
| 253 |
my @messages; |
| 254 |
my @errors; |
| 255 |
|
| 256 |
my ($template, $loggedinuser, $cookie)= get_template_and_user({ |
| 257 |
template_name => "/onboarding/onboardingstep3.tt", |
| 258 |
query => $input, |
| 259 |
type => "intranet", |
| 260 |
authnotrequired => 0, |
| 261 |
flagsrequired => {borrowers => 1}, |
| 262 |
debug => 1, |
| 263 |
}); |
| 264 |
|
| 265 |
|
| 266 |
if($op eq 'add_validate'){ |
| 267 |
my %newdata; |
| 268 |
|
| 269 |
#Store the template form values in the newdata hash |
| 270 |
$newdata{borrowernumber} = $input->param('borrowernumber'); |
| 271 |
$newdata{surname} = $input->param('surname'); |
| 272 |
$newdata{firstname} = $input->param('firstname'); |
| 273 |
$newdata{cardnumber} = $input->param('cardnumber'); |
| 274 |
$newdata{branchcode} = $input->param('libraries'); |
| 275 |
$newdata{categorycode} = $input->param('categorycode_entry'); |
| 276 |
$newdata{userid} = $input->param('userid'); |
| 277 |
$newdata{password} = $input->param('password'); |
| 278 |
$newdata{password2} = $input->param('password2'); |
| 279 |
$newdata{dateexpiry} = '12/10/2016'; |
| 280 |
$newdata{privacy} = "default"; |
| 281 |
|
| 282 |
if(my $error_code = checkcardnumber($newdata{cardnumber},$newdata{borrowernumber})){ |
| 283 |
push @errors, $error_code == 1 |
| 284 |
? 'ERROR_cardnumber_already_exists' |
| 285 |
:$error_code == 2 |
| 286 |
? 'ERROR_cardnumber_length' |
| 287 |
:() |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
#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 |
| 293 |
my $borrowernumber = &AddMember(%newdata); |
| 294 |
#Create a hash named member2 and fillit with the borrowernumber of the borrower that has just been created |
| 295 |
my %member2; |
| 296 |
$member2{'borrowernumber'}=$borrowernumber; |
| 297 |
|
| 298 |
|
| 299 |
my $flag = $input->param('flag'); |
| 300 |
|
| 301 |
if ($input->param('newflags')) { |
| 302 |
my $dbh=C4::Context->dbh(); |
| 303 |
my @perms = $input->multi_param('flag'); |
| 304 |
my %all_module_perms = (); |
| 305 |
my %sub_perms = (); |
| 306 |
foreach my $perm (@perms) { |
| 307 |
if ($perm !~ /:/) { |
| 308 |
$all_module_perms{$perm} = 1; |
| 309 |
} else { |
| 310 |
my ($module, $sub_perm) = split /:/, $perm, 2; |
| 311 |
push @{ $sub_perms{$module} }, $sub_perm; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
# construct flags |
| 317 |
my $module_flags = 0; |
| 318 |
my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit"); |
| 319 |
$sth->execute(); |
| 320 |
while (my ($bit, $flag) = $sth->fetchrow_array) { |
| 321 |
if (exists $all_module_perms{$flag}) { |
| 322 |
$module_flags += 2**$bit; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
$sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?"); |
| 327 |
$sth->execute($module_flags, $borrowernumber); |
| 328 |
|
| 329 |
|
| 330 |
#Error handling checking if the patron was created successfully |
| 331 |
if(!$borrowernumber){ |
| 332 |
push @messages, {type=> 'error', code => 'error_on_insert'}; |
| 333 |
}else{ |
| 334 |
push @messages, {type=> 'message', code => 'success_on_insert'}; |
| 335 |
} |
| 336 |
|
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
} |
| 341 |
}elsif ( $step && $step == 4){ |
| 342 |
my $createitemtype = $input->param('createitemtype'); |
| 343 |
$template->param('createitemtype'=> $createitemtype ); |
| 344 |
|
| 345 |
my $input = new CGI; |
| 346 |
my $itemtype_code = $input->param('itemtype'); |
| 347 |
my $op = $input->param('op') // 'list'; |
| 348 |
my $message; |
| 349 |
|
| 350 |
my( $template, $borrowernumber, $cookie) = get_template_and_user( |
| 351 |
{ template_name => "/onboarding/onboardingstep4.tt", |
| 352 |
query => $input, |
| 353 |
type => "intranet", |
| 354 |
authnotrequired => 0, |
| 355 |
flagsrequired => { parameters => 'parameters_remaining_permissions'}, |
| 356 |
debug => 1, |
| 357 |
} |
| 358 |
); |
| 359 |
|
| 360 |
if($op eq 'add_form'){ |
| 361 |
my $itemtype = Koha::ItemTypes->find($itemtype_code); |
| 362 |
$template->param(itemtype=> $itemtype,); |
| 363 |
}elsif($op eq 'add_validate'){ |
| 364 |
my $itemtype = Koha::ItemTypes->find($itemtype_code); |
| 365 |
my $description = $input->param('description'); |
| 366 |
|
| 367 |
#store the input from the form - only 2 fields |
| 368 |
my $thisitemtype= Koha::ItemType->new( |
| 369 |
{ itemtype => $itemtype_code, |
| 370 |
description => $description, |
| 371 |
} |
| 372 |
); |
| 373 |
eval{ $thisitemtype->store; }; |
| 374 |
#Error messages |
| 375 |
if($thisitemtype){ |
| 376 |
$message = 'success_on_insert'; |
| 377 |
}else{ |
| 378 |
$message = 'error_on_insert'; |
| 379 |
} |
| 380 |
|
| 381 |
$template->param('message' => $message); |
| 382 |
} |
| 383 |
}elsif ( $step && $step == 5){ |
| 384 |
my $test="a"; |
| 385 |
warn $test; |
| 386 |
|
| 387 |
#Fetching all the existing categories to display in a drop down box |
| 388 |
my $categories; |
| 389 |
$categories= Koha::Patron::Categories->search(); |
| 390 |
$template->param( |
| 391 |
categories => $categories, |
| 392 |
); |
| 393 |
|
| 394 |
my $itemtypes; |
| 395 |
$itemtypes= Koha::ItemTypes->search(); |
| 396 |
$template->param( |
| 397 |
itemtypes => $itemtypes, |
| 398 |
); |
| 399 |
|
| 400 |
my $input = CGI->new; |
| 401 |
my($template, $loggedinuser, $cookie) =get_template_and_user({ |
| 402 |
template_name => "/onboarding/onboardingstep5.tt", |
| 403 |
query => $input, |
| 404 |
type => "intranet", |
| 405 |
authnotrequired=>0, |
| 406 |
flagsrequired=> {parameters => 'manage_circ_rules'}, |
| 407 |
debug =>1, |
| 408 |
}); |
| 409 |
my $type = $input->param('type'); |
| 410 |
my $branch = $input->param('branch'); |
| 411 |
if($op eq 'add_form'){ |
| 412 |
} |
| 413 |
|
| 414 |
elsif($op eq 'add_validate'){ |
| 415 |
my $bor = $input->param('categorycode'); |
| 416 |
my $itemtype = $input->param('itemtype'); |
| 417 |
my $maxissueqty = $input->param('maxissueqty'); |
| 418 |
my $issuelength = $input->param('issuelength'); |
| 419 |
#$issuelength = $issuelength eq q{} ? undef : $issuelength; |
| 420 |
my $lengthunit = $input->param('lengthunit'); |
| 421 |
my $renewalsallowed = $input->param('renewalsallowed'); |
| 422 |
my $renewalperiod = $input->param('renewalperiod'); |
| 423 |
my $onshelfholds = $input->param('onshelfholds'); |
| 424 |
|
| 425 |
my $params ={ |
| 426 |
categorycode => $bor, |
| 427 |
itemtype => $itemtype, |
| 428 |
maxissueqty => $maxissueqty, |
| 429 |
renewalsallowed => $renewalsallowed, |
| 430 |
renewalperiod => $renewalperiod, |
| 431 |
lengthunit => $lengthunit, |
| 432 |
onshelfholds => $onshelfholds, |
| 433 |
}; |
| 434 |
my $issuingrule = Koha::IssuingRules->find({categorycode => $bor, itemtype => $itemtype}); |
| 435 |
if($issuingrule){ |
| 436 |
$issuingrule->set($params)->store(); |
| 437 |
}else{ |
| 438 |
Koha::IssuingRule->new()->set($params)->store(); |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
output_html_with_http_headers $input, $cookie, $template->output; |
| 446 |
|