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>.
25 use C4
::Members
qw(checkcardnumber);
31 use Koha
::Patron
::Categories
;
33 use Koha
::CirculationRules
;
38 unless ( C4
::Context
->preference('Version') ) {
39 print $input->redirect("/cgi-bin/koha/installer/install.pl");
43 my ( $user, $cookie, $sessionID, $flags ) =
44 C4
::InstallAuth
::checkauth
( $input, 0, undef, 'intranet' );
47 ; # Should not happen, we should be redirect if the user is not logged in. But do not trust authentication...
49 my $step = $input->param('step') || 1;
50 my $op = $input->param('op') || '';
52 my $template_params = {};
53 $template_params->{op
} = $op;
55 my $schema = Koha
::Database
->new()->schema();
61 if ( $op eq 'add_validate_library' ) {
63 my $branchcode = $input->param('branchcode');
64 $branchcode = uc($branchcode);
66 $branchcode =~ s
|\s
||g
67 ; # Use a regular expression to check the value of the inputted branchcode
69 my $library = Koha
::Library
->new(
71 branchcode
=> $branchcode,
72 branchname
=> scalar $input->param('branchname'),
76 eval { $library->store; };
78 push @messages, { code
=> 'success_on_insert_library' };
81 push @messages, { code
=> 'error_on_insert_library' };
85 $step++ if Koha
::Libraries
->count;
88 if ( $op eq "add_validate_category" ) {
90 my $searchfield = $input->param('description') // q
||;
91 my $categorycode = $input->param('categorycode');
93 $template_params->{categorycode
} = $categorycode;
95 $categorycode = $input->param('categorycode');
96 my $description = $input->param('description');
97 my $overduenoticerequired = $input->param('overduenoticerequired');
98 my $category_type = $input->param('category_type');
99 my $default_privacy = $input->param('default_privacy');
100 my $enrolmentperiod = $input->param('enrolmentperiod');
101 my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
103 #Converts the string into a date format
104 if ($enrolmentperioddate) {
105 $enrolmentperioddate = output_pref
(
107 dt
=> dt_from_string
($enrolmentperioddate),
108 dateformat
=> 'DateTime',
114 #Adds a new patron category to the database
115 $category = Koha
::Patron
::Category
->new(
117 categorycode
=> $categorycode,
118 description
=> $description,
119 overduenoticerequired
=> $overduenoticerequired,
120 category_type
=> $category_type,
121 default_privacy
=> $default_privacy,
122 enrolmentperiod
=> $enrolmentperiod,
123 enrolmentperioddate
=> $enrolmentperioddate
127 eval { $category->store; };
130 push @messages, { code
=> 'success_on_insert_category' };
133 push @messages, { code
=> 'error_on_insert_category' };
137 $step++ if Koha
::Patron
::Categories
->count;
140 if ( $op eq 'add_validate_patron' ) {
143 my $firstpassword = $input->param('password') || '';
144 my $secondpassword = $input->param('password2') || '';
145 my $cardnumber = $input->param('cardnumber');
146 my $userid = $input->param('userid');
148 my ( $is_valid, $passworderror) = Koha
::AuthUtils
::is_password_valid
( $firstpassword );
150 if ( my $error_code = checkcardnumber
($cardnumber) ) {
151 if ( $error_code == 1 ) {
152 push @messages, { code
=> 'ERROR_cardnumber_already_exists' };
154 elsif ( $error_code == 2 ) {
155 push @messages, { code
=> 'ERROR_cardnumber_length' };
158 elsif ( $firstpassword ne $secondpassword ) {
160 push @messages, { code
=> 'ERROR_password_mismatch' };
162 elsif ( $passworderror) {
163 push @messages, { code
=> 'ERROR_password_too_short'} if $passworderror eq 'too_short';
164 push @messages, { code
=> 'ERROR_password_too_weak'} if $passworderror eq 'too_weak';
165 push @messages, { code
=> 'ERROR_password_has_whitespaces'} if $passworderror eq 'has_whitespaces';
170 surname
=> scalar $input->param('surname'),
171 firstname
=> scalar $input->param('firstname'),
172 cardnumber
=> scalar $input->param('cardnumber'),
173 branchcode
=> scalar $input->param('libraries'),
174 categorycode
=> scalar $input->param('categorycode_entry'),
175 userid
=> scalar $input->param('userid'),
176 privacy
=> "default",
179 flags
=> 1, # Will be superlibrarian
182 my $patron_category =
183 Koha
::Patron
::Categories
->find( $patron_data->{categorycode
} );
184 $patron_data->{dateexpiry
} =
185 $patron_category->get_expiry_date( $patron_data->{dateenrolled
} );
188 my $patron = Koha
::Patron
->new($patron_data)->store;
189 $patron->set_password({ password
=> $firstpassword });
192 #Error handling checking if the patron was created successfully
194 push @messages, { code
=> 'success_on_insert_patron' };
198 push @messages, { code
=> 'error_on_insert_patron' };
203 $step++ if Koha
::Patrons
->search( { flags
=> 1 } )->count;
206 if ( $op eq 'add_validate_itemtype' ) {
207 my $description = $input->param('description');
208 my $itemtype_code = $input->param('itemtype');
209 $itemtype_code = uc($itemtype_code);
211 my $itemtype = Koha
::ItemType
->new(
213 itemtype
=> $itemtype_code,
214 description
=> $description,
217 eval { $itemtype->store; };
220 push @messages, { code
=> 'success_on_insert_itemtype' };
223 push @messages, { code
=> 'error_on_insert_itemtype' };
227 $step++ if Koha
::ItemTypes
->count;
231 if ( $op eq 'add_validate_circ_rule' ) {
233 #If no libraries exist then set the $branch value to *
234 my $branch = $input->param('branch') || '*';
236 my $type = $input->param('type');
237 my $branchcode = $input->param('branch');
238 my $categorycode = $input->param('categorycode');
239 my $itemtype = $input->param('itemtype');
240 my $maxissueqty = $input->param('maxissueqty');
241 my $issuelength = $input->param('issuelength');
242 my $lengthunit = $input->param('lengthunit');
243 my $renewalsallowed = $input->param('renewalsallowed');
244 my $renewalperiod = $input->param('renewalperiod');
245 my $reservesallowed = $input->param('reservesallowed');
246 my $holds_per_day = $input->param('holds_per_day');
247 my $holds_per_record = $input->param('holds_per_record');
248 my $onshelfholds = $input->param('onshelfholds') || 0;
249 $maxissueqty =~ s/\s//g;
250 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
251 $issuelength = $issuelength eq q{} ?
undef : $issuelength;
254 branchcode
=> $branchcode,
255 categorycode
=> $categorycode,
256 itemtype
=> $itemtype,
258 renewalsallowed
=> $renewalsallowed,
259 renewalperiod
=> $renewalperiod,
260 issuelength
=> $issuelength,
261 lengthunit
=> $lengthunit,
262 onshelfholds
=> $onshelfholds,
263 article_requests
=> "no",
265 cap_fine_to_replacement_price
=> 0,
267 chargeperiod_charge_at
=> 0,
272 hardduedatecompare
=> -1,
273 holds_per_day
=> $holds_per_day,
274 holds_per_record
=> $holds_per_record,
276 maxonsiteissueqty
=> "",
277 maxsuspensiondays
=> "",
278 no_auto_renewal_after
=> "",
279 no_auto_renewal_after_hard_limit
=> "",
280 norenewalbefore
=> "",
281 opacitemholds
=> "N",
282 overduefinescap
=> "",
284 reservesallowed
=> $reservesallowed,
285 suspension_chargeperiod
=> undef,
290 branchcode
=> $branchcode,
291 categorycode
=> $categorycode,
293 patron_maxissueqty
=> "",
294 patron_maxonsiteissueqty
=> "",
300 branchcode
=> $branchcode,
301 itemtype
=> $itemtype,
304 hold_fulfillment_policy
=> "",
310 Koha
::CirculationRules
->set_rules($params);
311 Koha
::CirculationRules
->set_rules($params_2);
312 Koha
::CirculationRules
->set_rules($params_3);
317 push @messages, { code
=> 'error_on_insert_circ_rule' };
321 Koha
::CirculationRules
->set_rules(
323 categorycode
=> $categorycode,
324 itemtype
=> $itemtype,
325 branchcode
=> $branchcode,
327 maxissueqty
=> $maxissueqty,
334 push @messages, { code
=> 'success_on_insert_circ_rule' };
337 push @messages, { code
=> 'error_on_insert_circ_rule' };
342 $step++ if Koha
::CirculationRules
->count;
345 my $libraries = Koha
::Libraries
->search( {}, { order_by
=> ['branchcode'] }, );
346 $template_params->{libraries
} = $libraries;
349 $template_params->{all_done
} = 1; # If step 5 is complete, we are done!
353 #Getting the appropriate template to display to the user
354 my ( $template, $loggedinuser );
355 ( $template, $loggedinuser, $cookie ) = C4
::InstallAuth
::get_template_and_user
(
357 template_name
=> "onboarding/onboardingstep${step}.tt",
360 authnotrequired
=> 0,
365 $template_params->{messages
} = \
@messages;
366 my $categories = Koha
::Patron
::Categories
->search();
367 $template_params->{categories
} = $categories;
369 my $itemtypes = Koha
::ItemTypes
->search();
370 $template_params->{itemtypes
} = $itemtypes;
372 $template->param(%$template_params);
374 output_html_with_http_headers
$input, $cookie, $template->output;