|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
# |
| 3 |
# Copyright (C) 2019 Koha-Suomi Oy |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 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. |
| 11 |
# |
| 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. |
| 16 |
# |
| 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>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
use Getopt::Long; |
| 22 |
use Pod::Usage; |
| 23 |
|
| 24 |
use Koha::Patrons; |
| 25 |
use Koha::Script; |
| 26 |
use Koha::Validation; |
| 27 |
|
| 28 |
sub usage { |
| 29 |
pod2usage( -verbose => 2 ); |
| 30 |
exit; |
| 31 |
} |
| 32 |
|
| 33 |
sub validate_patrons { |
| 34 |
my ($not_expired, $category, $no_email, $no_emailpro, |
| 35 |
$no_B_email, $no_phone, $no_phonepro, $no_B_phone, |
| 36 |
$no_mobile, $no_smsalertnumber) = @_; |
| 37 |
|
| 38 |
my $search = {}; |
| 39 |
|
| 40 |
if ($not_expired) { |
| 41 |
$search->{dateexpiry} = |
| 42 |
dt_from_string(undef, undef, 'floating')->truncate( to => 'day' ); |
| 43 |
} |
| 44 |
|
| 45 |
if ($category) { |
| 46 |
$search->{categorycode} = $category; |
| 47 |
} |
| 48 |
|
| 49 |
my $emailvalidator = 'email'; |
| 50 |
my $phonevalidator = 'phone'; |
| 51 |
|
| 52 |
my $validatephones = C4::Context->preference("ValidatePhoneNumber"); |
| 53 |
$no_phone = 1 if !$validatephones; |
| 54 |
$no_phonepro = 1 if !$validatephones; |
| 55 |
$no_B_phone = 1 if !$validatephones; |
| 56 |
$no_mobile = 1 if !$validatephones; |
| 57 |
$no_smsalertnumber = 1 if !$validatephones; |
| 58 |
|
| 59 |
my $validate = { |
| 60 |
'email' => !$no_email ? $emailvalidator : undef, |
| 61 |
'emailpro' => !$no_emailpro ? $emailvalidator : undef, |
| 62 |
'B_email' => !$no_email ? $emailvalidator : undef, |
| 63 |
'phone' => !$no_phone ? $phonevalidator : undef, |
| 64 |
'phonepro' => !$no_phonepro ? $phonevalidator : undef, |
| 65 |
'B_phone' => !$no_B_phone ? $phonevalidator : undef, |
| 66 |
'mobile' => !$no_mobile ? $phonevalidator : undef, |
| 67 |
'smsalertnumber' => !$no_smsalertnumber ? $phonevalidator : undef, |
| 68 |
}; |
| 69 |
|
| 70 |
my $patrons = Koha::Patrons->search($search)->as_list; |
| 71 |
format STDOUT_TOP = |
| 72 |
=============== =============== ======================================== |
| 73 |
Borrowernumber Field Value |
| 74 |
=============== =============== ======================================== |
| 75 |
. |
| 76 |
|
| 77 |
foreach my $patron (@$patrons) { |
| 78 |
my $bn = $patron->borrowernumber; |
| 79 |
foreach my $field (keys %{$validate}) { |
| 80 |
my $validator = $validate->{$field}; |
| 81 |
my $value = $patron->$field; |
| 82 |
next unless $value; |
| 83 |
unless (Koha::Validation->$validator($value)) { |
| 84 |
format STDOUT = |
| 85 |
@<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |
| 86 |
$bn, $field, $value |
| 87 |
. |
| 88 |
|
| 89 |
write; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
my $footer = <<"END_FOOTER" |
| 94 |
=============== =============== ======================================== |
| 95 |
Borrowernumber Field Value |
| 96 |
=============== =============== ======================================== |
| 97 |
END_FOOTER |
| 98 |
; |
| 99 |
print "$footer\nDONE.\n"; |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
my ($confirm, $since, $help, $not_expired, $category,$no_email, |
| 105 |
$no_emailpro, $no_B_email, $no_phone, $no_phonepro, $no_B_phone, |
| 106 |
$no_mobile, $no_smsalertnumber); |
| 107 |
my $result = GetOptions( |
| 108 |
'confirm' => \$confirm, |
| 109 |
'not-expired' => \$not_expired, |
| 110 |
'category:s' => \$category, |
| 111 |
'no-email' => \$no_email, |
| 112 |
'no-emailpro' => \$no_emailpro, |
| 113 |
'no-B_email' => \$no_B_email, |
| 114 |
'no-phone' => \$no_phone, |
| 115 |
'no-phonepro' => \$no_phonepro, |
| 116 |
'no-B_phone' => \$no_B_phone, |
| 117 |
'no-mobile' => \$no_mobile, |
| 118 |
'no-smsalertnumber' => \$no_smsalertnumber, |
| 119 |
'help|h' => \$help, |
| 120 |
); |
| 121 |
|
| 122 |
usage() if $help; |
| 123 |
|
| 124 |
validate_patrons($not_expired, $category, $no_email, $no_emailpro, |
| 125 |
$no_B_email, $no_phone, $no_phonepro, $no_B_phone, |
| 126 |
$no_mobile, $no_smsalertnumber) if $confirm; |
| 127 |
|
| 128 |
=head1 NAME |
| 129 |
|
| 130 |
validate_patron_contact_data.pl |
| 131 |
|
| 132 |
=head1 SYNOPSIS |
| 133 |
|
| 134 |
validate_patron_contact_data.pl |
| 135 |
validate_patron_contact_data.pl --help |
| 136 |
validate_patron_contact_data.pl --confirm |
| 137 |
validate_patron_contact_data.pl --confirm --not-expired |
| 138 |
validate_patron_contact_data.pl --confirm --category PT |
| 139 |
validate_patron_contact_data.pl --confirm --no-email --no-emailpro --no-B_email |
| 140 |
validate_patron_contact_data.pl --confirm --no-phone --no-phonepro --no-B_phone --no-smsalertnumber |
| 141 |
|
| 142 |
=head1 DESCRIPTION |
| 143 |
|
| 144 |
This script iterates patron database and reports invalid emails and phone |
| 145 |
numbers to STDOUT. |
| 146 |
|
| 147 |
=over 8 |
| 148 |
|
| 149 |
=item B<--help> |
| 150 |
|
| 151 |
Prints this help |
| 152 |
|
| 153 |
=item B<--confirm> |
| 154 |
|
| 155 |
Actually start the script. |
| 156 |
|
| 157 |
=item B<--not-expired> |
| 158 |
|
| 159 |
Will only check active patrons (patrons who didn't pass their expiration date). |
| 160 |
|
| 161 |
=item B<--category> |
| 162 |
|
| 163 |
Will only update patrons in the category specified. |
| 164 |
|
| 165 |
=item B<--no-email> |
| 166 |
|
| 167 |
Exclude field email from validation. |
| 168 |
|
| 169 |
=item B<--no-emailpro> |
| 170 |
|
| 171 |
Exclude field emailpro from validation. |
| 172 |
|
| 173 |
=item B<--no-B_email> |
| 174 |
|
| 175 |
Exclude field B_email from validation. |
| 176 |
|
| 177 |
=item B<--no-phone> |
| 178 |
|
| 179 |
Exclude field phone from validation. |
| 180 |
|
| 181 |
=item B<--no-phonepro> |
| 182 |
|
| 183 |
Exclude field phonepro from validation. |
| 184 |
|
| 185 |
=item B<--no-B_phone> |
| 186 |
|
| 187 |
Exclude field B_phone from validation. |
| 188 |
|
| 189 |
=item B<--no-mobile> |
| 190 |
|
| 191 |
Exclude field mobile from validation. |
| 192 |
|
| 193 |
=item B<--no-smsalertnumber> |
| 194 |
|
| 195 |
Exclude field smsalertnumber from validation. |
| 196 |
|
| 197 |
=back |
| 198 |
|
| 199 |
=cut |