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>.
30 use Koha
::Patron
::Categories
;
31 use Koha
::Patron
::Category
;
33 use Koha
::IssuingRule
;
34 use Koha
::IssuingRules
;
39 unless ( C4
::Context
->preference('Version') ) {
40 print $input->redirect("/cgi-bin/koha/installer/install.pl");
44 my ( $user, $cookie, $sessionID, $flags ) =
45 C4
::InstallAuth
::checkauth
( $input, 0, undef, 'intranet' );
48 ; # Should not happen, we should be redirect if the user is not logged in. But do not trust authentication...
50 my $step = $input->param('step') || 1;
51 my $op = $input->param('op') || '';
53 my $template_params = {};
54 $template_params->{op
} = $op;
56 my $schema = Koha
::Database
->new()->schema();
62 if ( $op eq 'add_validate_library' ) {
64 my $branchcode = $input->param('branchcode');
65 $branchcode = uc($branchcode);
67 $branchcode =~ s
|\s
||g
68 ; # Use a regular expression to check the value of the inputted branchcode
70 my $library = Koha
::Library
->new(
72 branchcode
=> $branchcode,
73 branchname
=> scalar $input->param('branchname'),
77 eval { $library->store; };
79 push @messages, { code
=> 'success_on_insert_library' };
82 push @messages, { code
=> 'error_on_insert_library' };
86 $step++ if Koha
::Libraries
->count;
89 if ( $op eq "add_validate_category" ) {
91 my $searchfield = $input->param('description') // q
||;
92 my $categorycode = $input->param('categorycode');
94 $template_params->{categorycode
} = $categorycode;
96 $categorycode = $input->param('categorycode');
97 my $description = $input->param('description');
98 my $overduenoticerequired = $input->param('overduenoticerequired');
99 my $category_type = $input->param('category_type');
100 my $default_privacy = $input->param('default_privacy');
101 my $enrolmentperiod = $input->param('enrolmentperiod');
102 my $enrolmentperioddate = $input->param('enrolmentperioddate') || undef;
104 #Converts the string into a date format
105 if ($enrolmentperioddate) {
106 $enrolmentperioddate = output_pref
(
108 dt
=> dt_from_string
($enrolmentperioddate),
109 dateformat
=> 'DateTime',
115 #Adds a new patron category to the database
116 $category = Koha
::Patron
::Category
->new(
118 categorycode
=> $categorycode,
119 description
=> $description,
120 overduenoticerequired
=> $overduenoticerequired,
121 category_type
=> $category_type,
122 default_privacy
=> $default_privacy,
123 enrolmentperiod
=> $enrolmentperiod,
124 enrolmentperioddate
=> $enrolmentperioddate
128 eval { $category->store; };
131 push @messages, { code
=> 'success_on_insert_category' };
134 push @messages, { code
=> 'error_on_insert_category' };
138 $step++ if Koha
::Patron
::Categories
->count;
141 if ( $op eq 'add_validate_patron' ) {
144 my $firstpassword = $input->param('password') || '';
145 my $secondpassword = $input->param('password2') || '';
146 my $cardnumber = $input->param('cardnumber');
147 my $userid = $input->param('userid');
149 my ( $is_valid, $passworderror) = Koha
::AuthUtils
::is_password_valid
( $firstpassword );
151 if ( my $error_code = checkcardnumber
($cardnumber) ) {
152 if ( $error_code == 1 ) {
153 push @messages, { code
=> 'ERROR_cardnumber_already_exists' };
155 elsif ( $error_code == 2 ) {
156 push @messages, { code
=> 'ERROR_cardnumber_length' };
159 elsif ( $firstpassword ne $secondpassword ) {
161 push @messages, { code
=> 'ERROR_password_mismatch' };
163 elsif ( $passworderror) {
164 push @messages, { code
=> 'ERROR_password_too_short'} if $passworderror eq 'too_short';
165 push @messages, { code
=> 'ERROR_password_too_weak'} if $passworderror eq 'too_weak';
166 push @messages, { code
=> 'ERROR_password_has_whitespaces'} if $passworderror eq 'has_whitespaces';
171 surname
=> scalar $input->param('surname'),
172 firstname
=> scalar $input->param('firstname'),
173 cardnumber
=> scalar $input->param('cardnumber'),
174 branchcode
=> scalar $input->param('libraries'),
175 categorycode
=> scalar $input->param('categorycode_entry'),
176 userid
=> scalar $input->param('userid'),
177 password
=> scalar $input->param('password'),
178 password2
=> scalar $input->param('password2'),
179 privacy
=> "default",
182 flags
=> 1, # Will be superlibrarian
185 my $patron_category =
186 Koha
::Patron
::Categories
->find( $patron_data->{categorycode
} );
187 $patron_data->{dateexpiry
} =
188 $patron_category->get_expiry_date( $patron_data->{dateenrolled
} );
190 my $borrowernumber = C4
::Members
::AddMember
(%$patron_data);
192 #Error handling checking if the patron was created successfully
193 if ($borrowernumber) {
194 push @messages, { code
=> 'success_on_insert_patron' };
197 push @messages, { code
=> 'error_on_insert_patron' };
202 $step++ if Koha
::Patrons
->search( { flags
=> 1 } )->count;
205 if ( $op eq 'add_validate_itemtype' ) {
206 my $description = $input->param('description');
207 my $itemtype_code = $input->param('itemtype');
208 $itemtype_code = uc($itemtype_code);
210 my $itemtype = Koha
::ItemType
->new(
212 itemtype
=> $itemtype_code,
213 description
=> $description,
216 eval { $itemtype->store; };
219 push @messages, { code
=> 'success_on_insert_itemtype' };
222 push @messages, { code
=> 'error_on_insert_itemtype' };
226 $step++ if Koha
::ItemTypes
->count;
230 if ( $op eq 'add_validate_circ_rule' ) {
232 #If no libraries exist then set the $branch value to *
233 my $branch = $input->param('branch') || '*';
235 my $type = $input->param('type');
236 my $branchcode = $input->param('branch');
237 my $categorycode = $input->param('categorycode');
238 my $itemtype = $input->param('itemtype');
239 my $maxissueqty = $input->param('maxissueqty');
240 my $issuelength = $input->param('issuelength');
241 my $lengthunit = $input->param('lengthunit');
242 my $renewalsallowed = $input->param('renewalsallowed');
243 my $renewalperiod = $input->param('renewalperiod');
244 my $onshelfholds = $input->param('onshelfholds') || 0;
245 $maxissueqty =~ s/\s//g;
246 $maxissueqty = undef if $maxissueqty !~ /^\d+/;
247 $issuelength = $issuelength eq q{} ?
undef : $issuelength;
250 branchcode
=> $branchcode,
251 categorycode
=> $categorycode,
252 itemtype
=> $itemtype,
253 maxissueqty
=> $maxissueqty,
254 renewalsallowed
=> $renewalsallowed,
255 renewalperiod
=> $renewalperiod,
256 issuelength
=> $issuelength,
257 lengthunit
=> $lengthunit,
258 onshelfholds
=> $onshelfholds,
261 my $issuingrule = Koha
::IssuingRule
->new($params);
262 eval { $issuingrule->store; };
265 push @messages, { code
=> 'success_on_insert_circ_rule' };
268 push @messages, { code
=> 'error_on_insert_circ_rule' };
272 $step++ if Koha
::IssuingRules
->count;
275 my $libraries = Koha
::Libraries
->search( {}, { order_by
=> ['branchcode'] }, );
276 $template_params->{libraries
} = $libraries;
279 $template_params->{all_done
} = 1; # If step 5 is complete, we are done!
283 #Getting the appropriate template to display to the user
284 my ( $template, $loggedinuser );
285 ( $template, $loggedinuser, $cookie ) = C4
::InstallAuth
::get_template_and_user
(
287 template_name
=> "onboarding/onboardingstep${step}.tt",
290 authnotrequired
=> 0,
295 $template_params->{messages
} = \
@messages;
296 my $categories = Koha
::Patron
::Categories
->search();
297 $template_params->{categories
} = $categories;
299 my $itemtypes = Koha
::ItemTypes
->search();
300 $template_params->{itemtypes
} = $itemtypes;
302 $template->param(%$template_params);
304 output_html_with_http_headers
$input, $cookie, $template->output;