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>.
25 use C4::Members::Messaging;
26 use Getopt::Long qw( GetOptions );
27 use Pod::Usage qw( pod2usage );
31 pod2usage( -verbose => 2 );
36 sub force_borrower_messaging_defaults {
37 my ($doit, $since, $not_expired, $no_overwrite, $category, $branchcode, $message_name ) = @_;
39 print "Since: $since\n" if $since;
41 my $dbh = C4::Context->dbh;
42 $dbh->{AutoCommit} = 0;
45 q|SELECT DISTINCT bo.borrowernumber, bo.categorycode FROM borrowers bo
46 LEFT JOIN borrower_message_preferences mp USING (borrowernumber)
50 $sql .= " AND bo.dateenrolled >= ?";
53 $sql .= " AND bo.dateexpiry >= NOW()"
56 $sql .= " AND mp.borrowernumber IS NULL";
58 $sql .= " AND bo.categorycode = ?" if $category;
59 $sql .= " AND bo.branchcode = ?" if $branchcode;
60 my $sth = $dbh->prepare($sql);
61 $sth->execute($since || (), $category || (), $branchcode || () );
63 while ( my ($borrowernumber, $categorycode) = $sth->fetchrow ) {
64 print "$borrowernumber: $categorycode\n";
67 borrowernumber => $borrowernumber,
68 categorycode => $categorycode,
70 $options->{message_name} = $message_name if defined $message_name;
71 C4::Members::Messaging::SetMessagingPreferencesFromDefaults($options);
75 print "Total borrowers updated: $cnt\n" if $doit;
79 my ( $doit, $since, $help, $not_expired, $no_overwrite, $category, $branchcode, $message_name );
80 my $result = GetOptions(
83 'not-expired' => \$not_expired,
84 'no-overwrite' => \$no_overwrite,
85 'category:s' => \$category,
86 'library:s' => \$branchcode,
87 'message-name:s' => \$message_name,
93 force_borrower_messaging_defaults( $doit, $since, $not_expired, $no_overwrite, $category, $branchcode, $message_name );
97 borrowers-force-messaging-defaults.pl
101 borrowers-force-messaging-defaults.pl
102 borrowers-force-messaging-defaults.pl --help
103 borrowers-force-messaging-defaults.pl --doit
104 borrowers-force-messaging-defaults.pl --doit --not-expired
105 borrowers-force-messaging-defaults.pl --doit --category PT
106 borrowers-force-messaging-defaults.pl --doit --library CPL
107 borrowers-force-messaging-defaults.pl --doit --message-name 'Item_Due'
111 If the EnhancedMessagingPreferences syspref is enabled after borrowers have
112 been created in the DB, those borrowers won't have messaging transport
113 preferences default values as defined for their borrower category. So you would
114 have to modify each borrower one by one if you would like to send them 'Hold
115 Filled' notice for example.
117 This script creates/overwrites messaging preferences for all borrowers and sets
118 them to default values defined for the category they belong to (unless you
119 use the options -not-expired or -no-overwrite to update a subset).
129 Actually update the borrowers.
131 =item B<--not-expired>
133 Will only update active borrowers (borrowers who didn't pass their expiration date).
135 =item B<--no-overwrite>
137 Will only update patrons without messaging preferences and skip patrons that
138 already set their preferences.
142 Will only update patrons in the category specified.
146 Will only update patrons whose home library matches the given library id
148 =item B<--message-name>
150 Will only update the specified message name.
151 List of values can be found in installer/data/mysql/mandatory/sample_notices_message_attributes.sql
155 Will only update borrowers enrolled since the specified date.
161 --since `date -d "1 day ago" '+%Y-%m-%d'