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>.
22 use C4::InstallAuth qw( checkauth get_template_and_user );
24 use C4::Output qw( output_html_with_http_headers );
25 use C4::Members qw( checkcardnumber );
30 use Koha::Patron::Categories;
32 use Koha::CirculationRules;
37 unless ( C4::Context->preference('Version') ) {
38 print $input->redirect("/cgi-bin/koha/installer/install.pl");
42 my ( $user, $cookie, $sessionID, $flags ) =
43 C4::InstallAuth::checkauth( $input, 0, undef, 'intranet' );
46 ; # Should not happen, we should be redirect if the user is not logged in. But do not trust authentication...
48 my $step = $input->param('step') || 1;
49 my $op = $input->param('op') || '';
51 my $template_params = {};
52 $template_params->{op} = $op;
54 my $schema = Koha::Database->new()->schema();
60 if ( $op eq 'add_validate_library' ) {
62 my $branchcode = $input->param('branchcode');
63 $branchcode = uc($branchcode);
65 $branchcode =~ s|\s||g
66 ; # Use a regular expression to check the value of the inputted branchcode
68 my $library = Koha::Library->new(
70 branchcode => $branchcode,
71 branchname => scalar $input->param('branchname'),
75 eval { $library->store; };
77 push @messages, { code => 'success_on_insert_library' };
80 push @messages, { code => 'error_on_insert_library' };
84 $step++ if Koha::Libraries->count;
87 if ( $op eq "add_validate_category" ) {
89 my $searchfield = $input->param('description') // q||;
90 my $categorycode = $input->param('categorycode');
92 $template_params->{categorycode} = $categorycode;
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;
102 #Adds a new patron category to the database
103 $category = Koha::Patron::Category->new(
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
115 eval { $category->store; };
118 push @messages, { code => 'success_on_insert_category' };
121 push @messages, { code => 'error_on_insert_category' };
125 $step++ if Koha::Patron::Categories->count;
128 if ( $op eq 'add_validate_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 );
139 my ( $is_valid, $passworderror ) =
140 Koha::AuthUtils::is_password_valid( $firstpassword,
144 if ( my $error_code = checkcardnumber($cardnumber) ) {
145 if ( $error_code == 1 ) {
146 push @messages, { code => 'ERROR_cardnumber_already_exists' };
148 elsif ( $error_code == 2 ) {
149 push @messages, { code => 'ERROR_cardnumber_length' };
152 elsif ( $firstpassword ne $secondpassword ) {
154 push @messages, { code => 'ERROR_password_mismatch' };
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';
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",
173 flags => 1, # Will be superlibrarian
176 $patron_data->{dateexpiry} =
177 $patron_category->get_expiry_date( $patron_data->{dateenrolled} );
180 my $patron = Koha::Patron->new($patron_data)->store;
181 $patron->set_password({ password => $firstpassword });
184 #Error handling checking if the patron was created successfully
186 push @messages, { code => 'success_on_insert_patron' };
190 push @messages, { code => 'error_on_insert_patron' };
195 $step++ if Koha::Patrons->search( { flags => 1 } )->count;
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);
203 my $itemtype = Koha::ItemType->new(
205 itemtype => $itemtype_code,
206 description => $description,
209 eval { $itemtype->store; };
212 push @messages, { code => 'success_on_insert_itemtype' };
215 push @messages, { code => 'error_on_insert_itemtype' };
219 $step++ if Koha::ItemTypes->count;
223 if ( $op eq 'add_validate_circ_rule' ) {
225 #If no libraries exist then set the $branch value to *
226 my $branch = $input->param('branch') || '*';
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+/;
245 branchcode => $branchcode,
246 categorycode => $categorycode,
247 itemtype => $itemtype,
249 renewalsallowed => $renewalsallowed,
250 renewalperiod => $renewalperiod,
251 issuelength => $issuelength,
252 lengthunit => $lengthunit,
253 onshelfholds => $onshelfholds,
254 article_requests => "no",
256 cap_fine_to_replacement_price => 0,
258 chargeperiod_charge_at => 0,
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 => "",
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,
289 Koha::CirculationRules->set_rules($params);
294 push @messages, { code => 'error_on_insert_circ_rule' };
296 push @messages, { code => 'success_on_insert_circ_rule' };
300 $step++ if Koha::CirculationRules->count;
303 my $libraries = Koha::Libraries->search( {}, { order_by => ['branchcode'] }, );
304 $template_params->{libraries} = $libraries;
307 $template_params->{all_done} = 1; # If step 5 is complete, we are done!
311 #Getting the appropriate template to display to the user
312 my ( $template, $loggedinuser );
313 ( $template, $loggedinuser, $cookie ) = C4::InstallAuth::get_template_and_user(
315 template_name => "onboarding/onboardingstep${step}.tt",
321 $template_params->{messages} = \@messages;
322 my $categories = Koha::Patron::Categories->search();
323 $template_params->{categories} = $categories;
325 my $itemtypes = Koha::ItemTypes->search();
326 $template_params->{itemtypes} = $itemtypes;
328 $template->param(%$template_params);
330 output_html_with_http_headers $input, $cookie, $template->output;