Bug 19753: Move template JavaScript to the footer: Acquisitions
[koha.git] / misc / maintenance / borrowers-force-messaging-defaults.pl
blobe21b562cc65fe2768d5a3627fc8130efe26e8b13
1 #!/usr/bin/perl
3 # Copyright (C) 2011 Tamil s.a.r.l.
5 # This file is part of Koha.
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>.
20 use strict;
21 use warnings;
22 BEGIN {
23 # find Koha's Perl modules
24 # test carefully before changing this
25 use FindBin;
26 eval { require "$FindBin::Bin/../kohalib.pl" };
29 use C4::Context;
30 use C4::Members::Messaging;
31 use Getopt::Long;
32 use Pod::Usage;
35 sub usage {
36 pod2usage( -verbose => 2 );
37 exit;
41 sub force_borrower_messaging_defaults {
42 my ($doit, $truncate, $since, $not_expired) = @_;
44 $since = '0000-00-00' if (!$since);
45 print "Since: $since\n";
47 my $dbh = C4::Context->dbh;
48 $dbh->{AutoCommit} = 0;
50 if ( $doit && $truncate ) {
51 $dbh->do(q|SET FOREIGN_KEY_CHECKS = 0|);
52 $dbh->do(q|TRUNCATE borrower_message_transport_preferences|);
53 $dbh->do(q|TRUNCATE borrower_message_preferences|);
54 $dbh->do(q|SET FOREIGN_KEY_CHECKS = 1|);
57 my $sql = "SELECT borrowernumber, categorycode FROM borrowers WHERE dateenrolled >= ?";
58 if ($not_expired) {
59 $sql .= " AND dateexpiry >= NOW()"
61 my $sth = $dbh->prepare($sql);
62 $sth->execute($since);
63 while ( my ($borrowernumber, $categorycode) = $sth->fetchrow ) {
64 print "$borrowernumber: $categorycode\n";
65 next unless $doit;
66 C4::Members::Messaging::SetMessagingPreferencesFromDefaults( {
67 borrowernumber => $borrowernumber,
68 categorycode => $categorycode,
69 } );
71 $dbh->commit();
75 my ($doit, $truncate, $since, $help, $not_expired);
76 my $result = GetOptions(
77 'doit' => \$doit,
78 'truncate' => \$truncate,
79 'since:s' => \$since,
80 'not-expired' => \$not_expired,
81 'help|h' => \$help,
84 usage() if $help;
86 force_borrower_messaging_defaults( $doit, $truncate, $since, $not_expired );
88 =head1 NAME
90 borrowers-force-messaging-defaults.pl
92 =head1 SYNOPSIS
94 borrowers-force-messaging-defaults.pl
95 borrowers-force-messaging-defaults.pl --help
96 borrowers-force-messaging-defaults.pl --doit
97 borrowers-force-messaging-defaults.pl --doit --truncate
98 borrowers-force-messaging-defaults.pl --doit --not-expired
100 =head1 DESCRIPTION
102 If the EnhancedMessagingPreferences syspref is enabled after borrowers have
103 been created in the DB, those borrowers won't have messaging transport
104 preferences default values as defined for their borrower category. So you would
105 have to modify each borrower one by one if you would like to send them 'Hold
106 Filled' notice for example.
108 This script create transport preferences for all existing borrowers and set
109 them to default values defined for the category they belong to.
111 =over 8
113 =item B<--help>
115 Prints this help
117 =item B<--doit>
119 Actually update the borrowers.
121 =item B<--truncate>
123 Truncate all borrowers transport preferences before (re-)creating them. It
124 affects borrower_message_preferences table.
126 =item B<--not-expired>
128 Will only update active borrowers (borrowers who didn't pass their expiration date).
130 =back
132 =cut