3 # Copyright 2000-2002 Katipo Communications
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>.
24 use Date
::Calc
qw( Add_Delta_Days );
28 use Module
::Load
::Conditional
qw(can_load);
31 use C4
::Members
::Attributes
qw(GetBorrowerAttributes);
36 use Koha
::SMS
::Providers
;
39 use Koha
::DateUtils
qw( format_sqldatetime dt_from_string );
41 use vars
qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
47 &GetLetters &GetLettersAvailableForALibrary &GetLetterTemplates &DelLetter &GetPreparedLetter &GetWrappedLetter &addalert &getalert &delalert &findrelatedto &SendAlerts &GetPrintMessages &GetMessageTransportTypes
53 C4::Letters - Give functions for Letters management
61 "Letters" is the tool used in Koha to manage informations sent to the patrons and/or the library. This include some cron jobs like
62 late issues, as well as other tasks like sending a mail to users that have subscribed to a "serial issue alert" (= being warned every time a new issue has arrived at the library)
64 Letters are managed through "alerts" sent by Koha on some events. All "alert" related functions are in this module too.
66 =head2 GetLetters([$module])
68 $letters = &GetLetters($module);
69 returns informations about letters.
70 if needed, $module filters for letters given module
76 my $module = $filters->{module
};
77 my $code = $filters->{code
};
78 my $branchcode = $filters->{branchcode
};
79 my $dbh = C4
::Context
->dbh;
80 my $letters = $dbh->selectall_arrayref(
82 SELECT module
, code
, branchcode
, name
86 . ( $module ? q
| AND module
= ?
| : q
|| )
87 . ( $code ? q
| AND code
= ?
| : q
|| )
88 . ( defined $branchcode ? q
| AND branchcode
= ?
| : q
|| )
89 . q
| GROUP BY code ORDER BY name
|, { Slice
=> {} }
90 , ( $module ?
$module : () )
91 , ( $code ?
$code : () )
92 , ( defined $branchcode ?
$branchcode : () )
98 =head2 GetLetterTemplates
100 my $letter_templates = GetLetterTemplates(
102 module => 'circulation',
104 branchcode => 'CPL', # '' for default,
108 Return a hashref of letter templates.
112 sub GetLetterTemplates
{
115 my $module = $params->{module
};
116 my $code = $params->{code
};
117 my $branchcode = $params->{branchcode
} // '';
118 my $dbh = C4
::Context
->dbh;
119 my $letters = $dbh->selectall_arrayref(
121 SELECT module
, code
, branchcode
, name
, is_html
, title
, content
, message_transport_type
, lang
128 , $module, $code, $branchcode
134 =head2 GetLettersAvailableForALibrary
136 my $letters = GetLettersAvailableForALibrary(
138 branchcode => 'CPL', # '' for default
139 module => 'circulation',
143 Return an arrayref of letters, sorted by name.
144 If a specific letter exist for the given branchcode, it will be retrieve.
145 Otherwise the default letter will be.
149 sub GetLettersAvailableForALibrary
{
151 my $branchcode = $filters->{branchcode
};
152 my $module = $filters->{module
};
154 croak
"module should be provided" unless $module;
156 my $dbh = C4
::Context
->dbh;
157 my $default_letters = $dbh->selectall_arrayref(
159 SELECT module
, code
, branchcode
, name
163 . q
| AND branchcode
= ''|
164 . ( $module ? q
| AND module
= ?
| : q
|| )
165 . q
| ORDER BY name
|, { Slice
=> {} }
166 , ( $module ?
$module : () )
169 my $specific_letters;
171 $specific_letters = $dbh->selectall_arrayref(
173 SELECT module
, code
, branchcode
, name
177 . q
| AND branchcode
= ?
|
178 . ( $module ? q
| AND module
= ?
| : q
|| )
179 . q
| ORDER BY name
|, { Slice
=> {} }
181 , ( $module ?
$module : () )
186 for my $l (@
$default_letters) {
187 $letters{ $l->{code
} } = $l;
189 for my $l (@
$specific_letters) {
190 # Overwrite the default letter with the specific one.
191 $letters{ $l->{code
} } = $l;
194 return [ map { $letters{$_} }
195 sort { $letters{$a}->{name
} cmp $letters{$b}->{name
} }
201 my ( $module, $code, $branchcode, $message_transport_type, $lang) = @_;
202 $message_transport_type //= '%';
203 $lang = 'default' unless( $lang && C4
::Context
->preference('TranslateNotices') );
206 my $only_my_library = C4
::Context
->only_my_library;
207 if ( $only_my_library and $branchcode ) {
208 $branchcode = C4
::Context
::mybranch
();
212 my $dbh = C4
::Context
->dbh;
213 my $sth = $dbh->prepare(q{
216 WHERE module=? AND code=? AND (branchcode = ? OR branchcode = '')
217 AND message_transport_type LIKE ?
219 ORDER BY branchcode DESC LIMIT 1
221 $sth->execute( $module, $code, $branchcode, $message_transport_type, $lang );
222 my $line = $sth->fetchrow_hashref
224 $line->{'content-type'} = 'text/html; charset="UTF-8"' if $line->{is_html
};
234 module => 'circulation',
240 Delete the letter. The mtt parameter is facultative.
241 If not given, all templates mathing the other parameters will be removed.
247 my $branchcode = $params->{branchcode
};
248 my $module = $params->{module
};
249 my $code = $params->{code
};
250 my $mtt = $params->{mtt
};
251 my $lang = $params->{lang
};
252 my $dbh = C4
::Context
->dbh;
259 . ( $mtt ? q
| AND message_transport_type
= ?
| : q
|| )
260 . ( $lang? q
| AND lang
= ?
| : q
|| )
261 , undef, $branchcode, $module, $code, ( $mtt ?
$mtt : () ), ( $lang ?
$lang : () ) );
264 =head2 addalert ($borrowernumber, $type, $externalid)
267 - $borrowernumber : the number of the borrower subscribing to the alert
268 - $type : the type of alert.
269 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
271 create an alert and return the alertid (primary key)
276 my ( $borrowernumber, $type, $externalid ) = @_;
277 my $dbh = C4
::Context
->dbh;
280 "insert into alert (borrowernumber, type, externalid) values (?,?,?)");
281 $sth->execute( $borrowernumber, $type, $externalid );
283 # get the alert number newly created and return it
284 my $alertid = $dbh->{'mysql_insertid'};
288 =head2 delalert ($alertid)
291 - alertid : the alert id
297 my $alertid = shift or die "delalert() called without valid argument (alertid)"; # it's gonna die anyway.
298 $debug and warn "delalert: deleting alertid $alertid";
299 my $sth = C4
::Context
->dbh->prepare("delete from alert where alertid=?");
300 $sth->execute($alertid);
303 =head2 getalert ([$borrowernumber], [$type], [$externalid])
306 - $borrowernumber : the number of the borrower subscribing to the alert
307 - $type : the type of alert.
308 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
309 all parameters NON mandatory. If a parameter is omitted, the query is done without the corresponding parameter. For example, without $externalid, returns all alerts for a borrower on a topic.
314 my ( $borrowernumber, $type, $externalid ) = @_;
315 my $dbh = C4
::Context
->dbh;
316 my $query = "SELECT a.*, b.branchcode FROM alert a JOIN borrowers b USING(borrowernumber) WHERE 1";
318 if ($borrowernumber and $borrowernumber =~ /^\d+$/) {
319 $query .= " AND borrowernumber=?";
320 push @bind, $borrowernumber;
323 $query .= " AND type=?";
327 $query .= " AND externalid=?";
328 push @bind, $externalid;
330 my $sth = $dbh->prepare($query);
331 $sth->execute(@bind);
332 return $sth->fetchall_arrayref({});
335 =head2 findrelatedto($type, $externalid)
338 - $type : the type of alert
339 - $externalid : the id of the "object" to query
341 In the table alert, a "id" is stored in the externalid field. This "id" is related to another table, depending on the type of the alert.
342 When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
347 # When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
350 my $type = shift or return;
351 my $externalid = shift or return;
352 my $q = ($type eq 'issue' ) ?
353 "select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?" :
354 ($type eq 'borrower') ?
355 "select concat(firstname,' ',surname) from borrowers where borrowernumber=?" : undef;
357 warn "findrelatedto(): Illegal type '$type'";
360 my $sth = C4
::Context
->dbh->prepare($q);
361 $sth->execute($externalid);
362 my ($result) = $sth->fetchrow;
368 my $err = &SendAlerts($type, $externalid, $letter_code);
371 - $type : the type of alert
372 - $externalid : the id of the "object" to query
373 - $letter_code : the notice template to use
375 C<&SendAlerts> sends an email notice directly to a patron or a vendor.
377 Currently it supports ($type):
378 - claim serial issues (claimissues)
379 - claim acquisition orders (claimacquisition)
380 - send acquisition orders to the vendor (orderacquisition)
381 - notify patrons about newly received serial issues (issue)
382 - notify patrons when their account is created (members)
384 Returns undef or { error => 'message } on failure.
385 Returns true on success.
390 my ( $type, $externalid, $letter_code ) = @_;
391 my $dbh = C4
::Context
->dbh;
392 if ( $type eq 'issue' ) {
394 # prepare the letter...
395 # search the subscriptionid
398 "SELECT subscriptionid FROM serial WHERE serialid=?");
399 $sth->execute($externalid);
400 my ($subscriptionid) = $sth->fetchrow
401 or warn( "No subscription for '$externalid'" ),
404 # search the biblionumber
407 "SELECT biblionumber FROM subscription WHERE subscriptionid=?");
408 $sth->execute($subscriptionid);
409 my ($biblionumber) = $sth->fetchrow
410 or warn( "No biblionumber for '$subscriptionid'" ),
414 # find the list of borrowers to alert
415 my $alerts = getalert
( '', 'issue', $subscriptionid );
417 my $borinfo = C4
::Members
::GetMember
('borrowernumber' => $_->{'borrowernumber'});
418 my $email = $borinfo->{email
} or next;
420 # warn "sending issues...";
421 my $userenv = C4
::Context
->userenv;
422 my $library = Koha
::Libraries
->find( $_->{branchcode
} );
423 my $letter = GetPreparedLetter
(
425 letter_code
=> $letter_code,
426 branchcode
=> $userenv->{branch
},
428 'branches' => $_->{branchcode
},
429 'biblio' => $biblionumber,
430 'biblioitems' => $biblionumber,
431 'borrowers' => $borinfo,
432 'subscription' => $subscriptionid,
433 'serial' => $externalid,
439 my $message = Koha
::Email
->new();
440 my %mail = $message->create_message_headers(
443 from
=> $library->branchemail,
444 replyto
=> $library->branchreplyto,
445 sender
=> $library->branchreturnpath,
446 subject
=> Encode
::encode
( "UTF-8", "" . $letter->{title
} ),
447 message
=> $letter->{'is_html'}
448 ? _wrap_html
( Encode
::encode
( "UTF-8", $letter->{'content'} ),
449 Encode
::encode
( "UTF-8", "" . $letter->{'title'} ))
450 : Encode
::encode
( "UTF-8", "" . $letter->{'content'} ),
451 contenttype
=> $letter->{'is_html'}
452 ?
'text/html; charset="utf-8"'
453 : 'text/plain; charset="utf-8"',
456 unless( sendmail
(%mail) ) {
457 carp
$Mail::Sendmail
::error
;
458 return { error
=> $Mail::Sendmail
::error
};
462 elsif ( $type eq 'claimacquisition' or $type eq 'claimissues' or $type eq 'orderacquisition' ) {
464 # prepare the letter...
469 if ( $type eq 'claimacquisition') {
471 SELECT aqorders
.*,aqbasket
.*,biblio
.*,biblioitems
.*
473 LEFT JOIN aqbasket ON aqbasket
.basketno
=aqorders
.basketno
474 LEFT JOIN biblio ON aqorders
.biblionumber
=biblio
.biblionumber
475 LEFT JOIN biblioitems ON aqorders
.biblionumber
=biblioitems
.biblionumber
476 WHERE aqorders
.ordernumber IN
(
480 carp
"No order selected";
481 return { error
=> "no_order_selected" };
483 $strsth .= join( ",", ('?') x @
$externalid ) . ")";
484 $action = "ACQUISITION CLAIM";
485 $sthorders = $dbh->prepare($strsth);
486 $sthorders->execute( @
$externalid );
487 $dataorders = $sthorders->fetchall_arrayref( {} );
490 if ($type eq 'claimissues') {
492 SELECT serial
.*,subscription
.*, biblio
.*, aqbooksellers
.*,
493 aqbooksellers
.id AS booksellerid
495 LEFT JOIN subscription ON serial
.subscriptionid
=subscription
.subscriptionid
496 LEFT JOIN biblio ON serial
.biblionumber
=biblio
.biblionumber
497 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
=aqbooksellers
.id
498 WHERE serial
.serialid IN
(
502 carp
"No Order selected";
503 return { error
=> "no_order_selected" };
506 $strsth .= join( ",", ('?') x @
$externalid ) . ")";
507 $action = "CLAIM ISSUE";
508 $sthorders = $dbh->prepare($strsth);
509 $sthorders->execute( @
$externalid );
510 $dataorders = $sthorders->fetchall_arrayref( {} );
513 if ( $type eq 'orderacquisition') {
515 SELECT aqorders
.*,aqbasket
.*,biblio
.*,biblioitems
.*
517 LEFT JOIN aqbasket ON aqbasket
.basketno
=aqorders
.basketno
518 LEFT JOIN biblio ON aqorders
.biblionumber
=biblio
.biblionumber
519 LEFT JOIN biblioitems ON aqorders
.biblionumber
=biblioitems
.biblionumber
520 WHERE aqbasket
.basketno
= ?
521 AND orderstatus IN
('new','ordered')
525 carp
"No basketnumber given";
526 return { error
=> "no_basketno" };
528 $action = "ACQUISITION ORDER";
529 $sthorders = $dbh->prepare($strsth);
530 $sthorders->execute($externalid);
531 $dataorders = $sthorders->fetchall_arrayref( {} );
535 $dbh->prepare("select * from aqbooksellers where id=?");
536 $sthbookseller->execute( $dataorders->[0]->{booksellerid
} );
537 my $databookseller = $sthbookseller->fetchrow_hashref;
539 my $addressee = $type eq 'claimacquisition' || $type eq 'orderacquisition' ?
'acqprimary' : 'serialsprimary';
542 $dbh->prepare("SELECT * FROM aqcontacts WHERE booksellerid=? AND $type=1 ORDER BY $addressee DESC");
543 $sthcontact->execute( $dataorders->[0]->{booksellerid
} );
544 my $datacontact = $sthcontact->fetchrow_hashref;
548 push @email, $databookseller->{bookselleremail
} if $databookseller->{bookselleremail
};
549 push @email, $datacontact->{email
} if ( $datacontact && $datacontact->{email
} );
551 warn "Bookseller $dataorders->[0]->{booksellerid} without emails";
552 return { error
=> "no_email" };
555 while ($addlcontact = $sthcontact->fetchrow_hashref) {
556 push @cc, $addlcontact->{email
} if ( $addlcontact && $addlcontact->{email
} );
559 my $userenv = C4
::Context
->userenv;
560 my $letter = GetPreparedLetter
(
562 letter_code
=> $letter_code,
563 branchcode
=> $userenv->{branch
},
565 'branches' => $userenv->{branch
},
566 'aqbooksellers' => $databookseller,
567 'aqcontacts' => $datacontact,
569 repeat
=> $dataorders,
571 ) or return { error
=> "no_letter" };
573 # Remove the order tag
574 $letter->{content
} =~ s/<order>(.*?)<\/order>/$1/gxms
;
577 my $library = Koha
::Libraries
->find( $userenv->{branch
} );
579 To
=> join( ',', @email),
580 Cc
=> join( ',', @cc),
581 From
=> $library->branchemail || C4
::Context
->preference('KohaAdminEmailAddress'),
582 Subject
=> Encode
::encode
( "UTF-8", "" . $letter->{title
} ),
583 Message
=> $letter->{'is_html'}
584 ? _wrap_html
( Encode
::encode
( "UTF-8", $letter->{'content'} ),
585 Encode
::encode
( "UTF-8", "" . $letter->{'title'} ))
586 : Encode
::encode
( "UTF-8", "" . $letter->{'content'} ),
587 'Content-Type' => $letter->{'is_html'}
588 ?
'text/html; charset="utf-8"'
589 : 'text/plain; charset="utf-8"',
592 if ($type eq 'claimacquisition' || $type eq 'claimissues' ) {
593 $mail{'Reply-to'} = C4
::Context
->preference('ReplytoDefault')
594 if C4
::Context
->preference('ReplytoDefault');
595 $mail{'Sender'} = C4
::Context
->preference('ReturnpathDefault')
596 if C4
::Context
->preference('ReturnpathDefault');
597 $mail{'Bcc'} = $userenv->{emailaddress
}
598 if C4
::Context
->preference("ClaimsBccCopy");
601 unless ( sendmail
(%mail) ) {
602 carp
$Mail::Sendmail
::error
;
603 return { error
=> $Mail::Sendmail
::error
};
611 . join( ',', @email )
616 ) if C4
::Context
->preference("LetterLog");
618 # send an "account details" notice to a newly created user
619 elsif ( $type eq 'members' ) {
620 my $library = Koha
::Libraries
->find( $externalid->{branchcode
} )->unblessed;
621 my $letter = GetPreparedLetter
(
623 letter_code
=> $letter_code,
624 branchcode
=> $externalid->{'branchcode'},
626 'branches' => $library,
627 'borrowers' => $externalid->{'borrowernumber'},
629 substitute
=> { 'borrowers.password' => $externalid->{'password'} },
632 return { error
=> "no_email" } unless $externalid->{'emailaddr'};
633 my $email = Koha
::Email
->new();
634 my %mail = $email->create_message_headers(
636 to
=> $externalid->{'emailaddr'},
637 from
=> $library->{branchemail
},
638 replyto
=> $library->{branchreplyto
},
639 sender
=> $library->{branchreturnpath
},
640 subject
=> Encode
::encode
( "UTF-8", "" . $letter->{'title'} ),
641 message
=> $letter->{'is_html'}
642 ? _wrap_html
( Encode
::encode
( "UTF-8", $letter->{'content'} ),
643 Encode
::encode
( "UTF-8", "" . $letter->{'title'} ) )
644 : Encode
::encode
( "UTF-8", "" . $letter->{'content'} ),
645 contenttype
=> $letter->{'is_html'}
646 ?
'text/html; charset="utf-8"'
647 : 'text/plain; charset="utf-8"',
650 unless( sendmail
(%mail) ) {
651 carp
$Mail::Sendmail
::error
;
652 return { error
=> $Mail::Sendmail
::error
};
656 # If we come here, return an OK status
660 =head2 GetPreparedLetter( %params )
663 module => letter module, mandatory
664 letter_code => letter code, mandatory
665 branchcode => for letter selection, if missing default system letter taken
666 tables => a hashref with table names as keys. Values are either:
667 - a scalar - primary key value
668 - an arrayref - primary key values
669 - a hashref - full record
670 substitute => custom substitution key/value pairs
671 repeat => records to be substituted on consecutive lines:
672 - an arrayref - tries to guess what needs substituting by
673 taking remaining << >> tokensr; not recommended
674 - a hashref token => @tables - replaces <token> << >> << >> </token>
675 subtemplate for each @tables row; table is a hashref as above
676 want_librarian => boolean, if set to true triggers librarian details
677 substitution from the userenv
679 letter fields hashref (title & content useful)
683 sub GetPreparedLetter
{
686 my $module = $params{module
} or croak
"No module";
687 my $letter_code = $params{letter_code
} or croak
"No letter_code";
688 my $branchcode = $params{branchcode
} || '';
689 my $mtt = $params{message_transport_type
} || 'email';
690 my $lang = $params{lang
} || 'default';
692 my $letter = getletter
( $module, $letter_code, $branchcode, $mtt, $lang );
695 $letter = getletter
( $module, $letter_code, $branchcode, $mtt, 'default' )
696 or warn( "No $module $letter_code letter transported by " . $mtt ),
700 my $tables = $params{tables
} || {};
701 my $substitute = $params{substitute
} || {};
702 my $loops = $params{loops
} || {}; # loops is not supported for historical notices syntax
703 my $repeat = $params{repeat
};
704 %$tables || %$substitute || $repeat || %$loops
705 or carp
( "ERROR: nothing to substitute - both 'tables', 'loops' and 'substitute' are empty" ),
707 my $want_librarian = $params{want_librarian
};
710 while ( my ($token, $val) = each %$substitute ) {
711 if ( $token eq 'items.content' ) {
712 $val =~ s
|\n|<br
/>|g
if $letter->{is_html
};
715 $letter->{title
} =~ s/<<$token>>/$val/g;
716 $letter->{content
} =~ s/<<$token>>/$val/g;
720 my $OPACBaseURL = C4
::Context
->preference('OPACBaseURL');
721 $letter->{content
} =~ s/<<OPACBaseURL>>/$OPACBaseURL/go;
723 if ($want_librarian) {
724 # parsing librarian name
725 my $userenv = C4
::Context
->userenv;
726 $letter->{content
} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/go;
727 $letter->{content
} =~ s/<<LibrarianSurname>>/$userenv->{surname}/go;
728 $letter->{content
} =~ s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/go;
731 my ($repeat_no_enclosing_tags, $repeat_enclosing_tags);
734 if (ref ($repeat) eq 'ARRAY' ) {
735 $repeat_no_enclosing_tags = $repeat;
737 $repeat_enclosing_tags = $repeat;
741 if ($repeat_enclosing_tags) {
742 while ( my ($tag, $tag_tables) = each %$repeat_enclosing_tags ) {
743 if ( $letter->{content
} =~ m!<$tag>(.*)</$tag>!s ) {
746 my %subletter = ( title
=> '', content
=> $subcontent );
747 _substitute_tables
( \
%subletter, $_ );
750 $letter->{content
} =~ s!<$tag>.*</$tag>!join( "\n", @lines )!se;
756 _substitute_tables
( $letter, $tables );
759 if ($repeat_no_enclosing_tags) {
760 if ( $letter->{content
} =~ m/[^\n]*<<.*>>[^\n]*/so ) {
765 $c =~ s/<<count>>/$i/go;
766 foreach my $field ( keys %{$_} ) {
767 $c =~ s/(<<[^\.]+.$field>>)/$_->{$field}/;
771 } @
$repeat_no_enclosing_tags;
773 my $replaceby = join( "\n", @lines );
774 $letter->{content
} =~ s/\Q$line\E/$replaceby/s;
778 $letter->{content
} = _process_tt
(
780 content
=> $letter->{content
},
786 $letter->{content
} =~ s/<<\S*>>//go; #remove any stragglers
791 sub _substitute_tables
{
792 my ( $letter, $tables ) = @_;
793 while ( my ($table, $param) = each %$tables ) {
796 my $ref = ref $param;
799 if ($ref && $ref eq 'HASH') {
803 my $sth = _parseletter_sth
($table);
805 warn "_parseletter_sth('$table') failed to return a valid sth. No substitution will be done for that table.";
808 $sth->execute( $ref ? @
$param : $param );
810 $values = $sth->fetchrow_hashref;
814 _parseletter
( $letter, $table, $values );
818 sub _parseletter_sth
{
822 carp
"ERROR: _parseletter_sth() called without argument (table)";
825 # NOTE: we used to check whether we had a statement handle cached in
826 # a %handles module-level variable. This was a dumb move and
827 # broke things for the rest of us. prepare_cached is a better
828 # way to cache statement handles anyway.
830 ($table eq 'biblio' ) ?
"SELECT * FROM $table WHERE biblionumber = ?" :
831 ($table eq 'biblioitems' ) ?
"SELECT * FROM $table WHERE biblionumber = ?" :
832 ($table eq 'items' ) ?
"SELECT * FROM $table WHERE itemnumber = ?" :
833 ($table eq 'issues' ) ?
"SELECT * FROM $table WHERE itemnumber = ?" :
834 ($table eq 'old_issues' ) ?
"SELECT * FROM $table WHERE itemnumber = ? ORDER BY timestamp DESC LIMIT 1" :
835 ($table eq 'reserves' ) ?
"SELECT * FROM $table WHERE borrowernumber = ? and biblionumber = ?" :
836 ($table eq 'borrowers' ) ?
"SELECT * FROM $table WHERE borrowernumber = ?" :
837 ($table eq 'branches' ) ?
"SELECT * FROM $table WHERE branchcode = ?" :
838 ($table eq 'suggestions' ) ?
"SELECT * FROM $table WHERE suggestionid = ?" :
839 ($table eq 'aqbooksellers') ?
"SELECT * FROM $table WHERE id = ?" :
840 ($table eq 'aqorders' ) ?
"SELECT * FROM $table WHERE ordernumber = ?" :
841 ($table eq 'opac_news' ) ?
"SELECT * FROM $table WHERE idnew = ?" :
842 ($table eq 'article_requests') ?
"SELECT * FROM $table WHERE id = ?" :
843 ($table eq 'borrower_modifications') ?
"SELECT * FROM $table WHERE verification_token = ?" :
844 ($table eq 'subscription') ?
"SELECT * FROM $table WHERE subscriptionid = ?" :
845 ($table eq 'serial') ?
"SELECT * FROM $table WHERE serialid = ?" :
848 warn "ERROR: No _parseletter_sth query for table '$table'";
849 return; # nothing to get
851 unless ($sth = C4
::Context
->dbh->prepare_cached($query)) {
852 warn "ERROR: Failed to prepare query: '$query'";
855 return $sth; # now cache is populated for that $table
858 =head2 _parseletter($letter, $table, $values)
861 - $letter : a hash to letter fields (title & content useful)
862 - $table : the Koha table to parse.
863 - $values_in : table record hashref
864 parse all fields from a table, and replace values in title & content with the appropriate value
865 (not exported sub, used only internally)
870 my ( $letter, $table, $values_in ) = @_;
872 # Work on a local copy of $values_in (passed by reference) to avoid side effects
873 # in callers ( by changing / formatting values )
874 my $values = $values_in ?
{ %$values_in } : {};
876 if ( $table eq 'borrowers' && $values->{'dateexpiry'} ){
877 $values->{'dateexpiry'} = format_sqldatetime
( $values->{'dateexpiry'} );
880 if ( $table eq 'reserves' && $values->{'waitingdate'} ) {
881 $values->{'waitingdate'} = output_pref
({ dt
=> dt_from_string
( $values->{'waitingdate'} ), dateonly
=> 1 });
884 if ($letter->{content
} && $letter->{content
} =~ /<<today>>/) {
885 my $todaysdate = output_pref
( DateTime
->now() );
886 $letter->{content
} =~ s/<<today>>/$todaysdate/go;
889 while ( my ($field, $val) = each %$values ) {
890 $val =~ s/\p{P}$// if $val && $table=~/biblio/;
891 #BZ 9886: Assuming that we want to eliminate ISBD punctuation here
892 #Therefore adding the test on biblio. This includes biblioitems,
893 #but excludes items. Removed unneeded global and lookahead.
895 if ( $table=~/^borrowers$/ && $field=~/^streettype$/ ) {
896 my $av = Koha
::AuthorisedValues
->search({ category
=> 'ROADTYPE', authorised_value
=> $val });
897 $val = $av->count ?
$av->next->lib : '';
901 my $replacedby = defined ($val) ?
$val : '';
903 and not $replacedby =~ m
|0000-00-00|
904 and not $replacedby =~ m
|9999-12-31|
905 and $replacedby =~ m
|^\d
{4}-\d
{2}-\d
{2}( \d
{2}:\d
{2}:\d
{2})?
$| )
907 # If the value is XXXX-YY-ZZ[ AA:BB:CC] we assume it is a date
908 my $dateonly = defined $1 ?
0 : 1; #$1 refers to the capture group wrapped in parentheses. In this case, that's the hours, minutes, seconds.
909 my $re_dateonly_filter = qr{ $field( \s* \| \s* dateonly\s*)?>> }xms;
911 for my $letter_field ( qw( title content ) ) {
912 my $filter_string_used = q{};
913 if ( $letter->{ $letter_field } =~ $re_dateonly_filter ) {
914 # We overwrite $dateonly if the filter exists and we have a time in the datetime
915 $filter_string_used = $1 || q{};
916 $dateonly = $1 unless $dateonly;
918 my $replacedby_date = eval {
919 output_pref
({ dt
=> dt_from_string
( $replacedby ), dateonly
=> $dateonly });
922 if ( $letter->{ $letter_field } ) {
923 $letter->{ $letter_field } =~ s/\Q<<$table.$field$filter_string_used>>\E/$replacedby_date/g;
924 $letter->{ $letter_field } =~ s/\Q<<$field$filter_string_used>>\E/$replacedby_date/g;
928 # Other fields replacement
930 for my $letter_field ( qw( title content ) ) {
931 if ( $letter->{ $letter_field } ) {
932 $letter->{ $letter_field } =~ s/<<$table.$field>>/$replacedby/g;
933 $letter->{ $letter_field } =~ s/<<$field>>/$replacedby/g;
939 if ($table eq 'borrowers' && $letter->{content
}) {
940 if ( my $attributes = GetBorrowerAttributes
($values->{borrowernumber
}) ) {
942 foreach (@
$attributes) {
943 my $code = $_->{code
};
944 my $val = $_->{value_description
} || $_->{value
};
945 $val =~ s/\p{P}(?=$)//g if $val;
946 next unless $val gt '';
948 push @
{ $attr{$code} }, $val;
950 while ( my ($code, $val_ar) = each %attr ) {
951 my $replacefield = "<<borrower-attribute:$code>>";
952 my $replacedby = join ',', @
$val_ar;
953 $letter->{content
} =~ s/$replacefield/$replacedby/g;
962 my $success = EnqueueLetter( { letter => $letter,
963 borrowernumber => '12', message_transport_type => 'email' } )
965 places a letter in the message_queue database table, which will
966 eventually get processed (sent) by the process_message_queue.pl
967 cronjob when it calls SendQueuedMessages.
969 return message_id on success
974 my $params = shift or return;
976 return unless exists $params->{'letter'};
977 # return unless exists $params->{'borrowernumber'};
978 return unless exists $params->{'message_transport_type'};
980 my $content = $params->{letter
}->{content
};
981 $content =~ s/\s+//g if(defined $content);
982 if ( not defined $content or $content eq '' ) {
983 warn "Trying to add an empty message to the message queue" if $debug;
987 # If we have any attachments we should encode then into the body.
988 if ( $params->{'attachments'} ) {
989 $params->{'letter'} = _add_attachments
(
990 { letter
=> $params->{'letter'},
991 attachments
=> $params->{'attachments'},
992 message
=> MIME
::Lite
->new( Type
=> 'multipart/mixed' ),
997 my $dbh = C4
::Context
->dbh();
998 my $statement = << 'ENDSQL';
999 INSERT INTO message_queue
1000 ( borrowernumber
, subject
, content
, metadata
, letter_code
, message_transport_type
, status
, time_queued
, to_address
, from_address
, content_type
)
1002 ( ?
, ?
, ?
, ?
, ?
, ?
, ?
, NOW
(), ?
, ?
, ?
)
1005 my $sth = $dbh->prepare($statement);
1006 my $result = $sth->execute(
1007 $params->{'borrowernumber'}, # borrowernumber
1008 $params->{'letter'}->{'title'}, # subject
1009 $params->{'letter'}->{'content'}, # content
1010 $params->{'letter'}->{'metadata'} || '', # metadata
1011 $params->{'letter'}->{'code'} || '', # letter_code
1012 $params->{'message_transport_type'}, # message_transport_type
1014 $params->{'to_address'}, # to_address
1015 $params->{'from_address'}, # from_address
1016 $params->{'letter'}->{'content-type'}, # content_type
1018 return $dbh->last_insert_id(undef,undef,'message_queue', undef);
1021 =head2 SendQueuedMessages ([$hashref])
1023 my $sent = SendQueuedMessages( { verbose => 1 } );
1025 sends all of the 'pending' items in the message queue.
1027 returns number of messages sent.
1031 sub SendQueuedMessages
{
1034 my $unsent_messages = _get_unsent_messages
();
1035 MESSAGE
: foreach my $message ( @
$unsent_messages ) {
1036 # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
1037 warn sprintf( 'sending %s message to patron: %s',
1038 $message->{'message_transport_type'},
1039 $message->{'borrowernumber'} || 'Admin' )
1040 if $params->{'verbose'} or $debug;
1041 # This is just begging for subclassing
1042 next MESSAGE
if ( lc($message->{'message_transport_type'}) eq 'rss' );
1043 if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
1044 _send_message_by_email
( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1046 elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
1047 if ( C4
::Context
->preference('SMSSendDriver') eq 'Email' ) {
1048 my $member = C4
::Members
::GetMember
( 'borrowernumber' => $message->{'borrowernumber'} );
1049 my $sms_provider = Koha
::SMS
::Providers
->find( $member->{'sms_provider_id'} );
1050 unless ( $sms_provider ) {
1051 warn sprintf( "Patron %s has no sms provider id set!", $message->{'borrowernumber'} ) if $params->{'verbose'} or $debug;
1052 _set_message_status
( { message_id
=> $message->{'message_id'}, status
=> 'failed' } );
1055 unless ( $member->{'smsalertnumber'} ) {
1056 _set_message_status
( { message_id
=> $message->{'message_id'}, status
=> 'failed' } );
1057 warn sprintf( "No smsalertnumber found for patron %s!", $message->{'borrowernumber'} ) if $params->{'verbose'} or $debug;
1060 $message->{to_address
} = $member->{'smsalertnumber'}; #Sometime this is set to email - sms should always use smsalertnumber
1061 $message->{to_address
} .= '@' . $sms_provider->domain();
1062 _update_message_to_address
($message->{'message_id'},$message->{to_address
});
1063 _send_message_by_email
( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1065 _send_message_by_sms
( $message );
1069 return scalar( @
$unsent_messages );
1072 =head2 GetRSSMessages
1074 my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
1076 returns a listref of all queued RSS messages for a particular person.
1080 sub GetRSSMessages
{
1083 return unless $params;
1084 return unless ref $params;
1085 return unless $params->{'borrowernumber'};
1087 return _get_unsent_messages
( { message_transport_type
=> 'rss',
1088 limit
=> $params->{'limit'},
1089 borrowernumber
=> $params->{'borrowernumber'}, } );
1092 =head2 GetPrintMessages
1094 my $message_list = GetPrintMessages( { borrowernumber => $borrowernumber } )
1096 Returns a arrayref of all queued print messages (optionally, for a particular
1101 sub GetPrintMessages
{
1102 my $params = shift || {};
1104 return _get_unsent_messages
( { message_transport_type
=> 'print',
1105 borrowernumber
=> $params->{'borrowernumber'},
1109 =head2 GetQueuedMessages ([$hashref])
1111 my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
1113 fetches messages out of the message queue.
1116 list of hashes, each has represents a message in the message queue.
1120 sub GetQueuedMessages
{
1123 my $dbh = C4
::Context
->dbh();
1124 my $statement = << 'ENDSQL';
1125 SELECT message_id
, borrowernumber
, subject
, content
, message_transport_type
, status
, time_queued
1131 if ( exists $params->{'borrowernumber'} ) {
1132 push @whereclauses, ' borrowernumber = ? ';
1133 push @query_params, $params->{'borrowernumber'};
1136 if ( @whereclauses ) {
1137 $statement .= ' WHERE ' . join( 'AND', @whereclauses );
1140 if ( defined $params->{'limit'} ) {
1141 $statement .= ' LIMIT ? ';
1142 push @query_params, $params->{'limit'};
1145 my $sth = $dbh->prepare( $statement );
1146 my $result = $sth->execute( @query_params );
1147 return $sth->fetchall_arrayref({});
1150 =head2 GetMessageTransportTypes
1152 my @mtt = GetMessageTransportTypes();
1154 returns an arrayref of transport types
1158 sub GetMessageTransportTypes
{
1159 my $dbh = C4
::Context
->dbh();
1160 my $mtts = $dbh->selectcol_arrayref("
1161 SELECT message_transport_type
1162 FROM message_transport_types
1163 ORDER BY message_transport_type
1170 my $message = C4::Letters::Message($message_id);
1175 my ( $message_id ) = @_;
1176 return unless $message_id;
1177 my $dbh = C4
::Context
->dbh;
1178 return $dbh->selectrow_hashref(q
|
1179 SELECT message_id
, borrowernumber
, subject
, content
, metadata
, letter_code
, message_transport_type
, status
, time_queued
, to_address
, from_address
, content_type
1181 WHERE message_id
= ?
1182 |, {}, $message_id );
1185 =head2 ResendMessage
1187 Attempt to resend a message which has failed previously.
1189 my $has_been_resent = C4::Letters::ResendMessage($message_id);
1191 Updates the message to 'pending' status so that
1192 it will be resent later on.
1194 returns 1 on success, 0 on failure, undef if no message was found
1199 my $message_id = shift;
1200 return unless $message_id;
1202 my $message = GetMessage
( $message_id );
1203 return unless $message;
1205 if ( $message->{status
} ne 'pending' ) {
1206 $rv = C4
::Letters
::_set_message_status
({
1207 message_id
=> $message_id,
1208 status
=> 'pending',
1210 $rv = $rv > 0?
1: 0;
1211 # Clear destination email address to force address update
1212 _update_message_to_address
( $message_id, undef ) if $rv &&
1213 $message->{message_transport_type
} eq 'email';
1218 =head2 _add_attachements
1221 letter - the standard letter hashref
1222 attachments - listref of attachments. each attachment is a hashref of:
1223 type - the mime type, like 'text/plain'
1224 content - the actual attachment
1225 filename - the name of the attachment.
1226 message - a MIME::Lite object to attach these to.
1228 returns your letter object, with the content updated.
1232 sub _add_attachments
{
1235 my $letter = $params->{'letter'};
1236 my $attachments = $params->{'attachments'};
1237 return $letter unless @
$attachments;
1238 my $message = $params->{'message'};
1240 # First, we have to put the body in as the first attachment
1242 Type
=> $letter->{'content-type'} || 'TEXT',
1243 Data
=> $letter->{'is_html'}
1244 ? _wrap_html
($letter->{'content'}, $letter->{'title'})
1245 : $letter->{'content'},
1248 foreach my $attachment ( @
$attachments ) {
1250 Type
=> $attachment->{'type'},
1251 Data
=> $attachment->{'content'},
1252 Filename
=> $attachment->{'filename'},
1255 # we're forcing list context here to get the header, not the count back from grep.
1256 ( $letter->{'content-type'} ) = grep( /^Content-Type:/, split( /\n/, $params->{'message'}->header_as_string ) );
1257 $letter->{'content-type'} =~ s/^Content-Type:\s+//;
1258 $letter->{'content'} = $message->body_as_string;
1264 sub _get_unsent_messages
{
1267 my $dbh = C4
::Context
->dbh();
1268 my $statement = << 'ENDSQL';
1269 SELECT mq
.message_id
, mq
.borrowernumber
, mq
.subject
, mq
.content
, mq
.message_transport_type
, mq
.status
, mq
.time_queued
, mq
.from_address
, mq
.to_address
, mq
.content_type
, b
.branchcode
, mq
.letter_code
1270 FROM message_queue mq
1271 LEFT JOIN borrowers b ON b
.borrowernumber
= mq
.borrowernumber
1275 my @query_params = ('pending');
1276 if ( ref $params ) {
1277 if ( $params->{'message_transport_type'} ) {
1278 $statement .= ' AND message_transport_type = ? ';
1279 push @query_params, $params->{'message_transport_type'};
1281 if ( $params->{'borrowernumber'} ) {
1282 $statement .= ' AND borrowernumber = ? ';
1283 push @query_params, $params->{'borrowernumber'};
1285 if ( $params->{'limit'} ) {
1286 $statement .= ' limit ? ';
1287 push @query_params, $params->{'limit'};
1291 $debug and warn "_get_unsent_messages SQL: $statement";
1292 $debug and warn "_get_unsent_messages params: " . join(',',@query_params);
1293 my $sth = $dbh->prepare( $statement );
1294 my $result = $sth->execute( @query_params );
1295 return $sth->fetchall_arrayref({});
1298 sub _send_message_by_email
{
1299 my $message = shift or return;
1300 my ($username, $password, $method) = @_;
1302 my $member = C4
::Members
::GetMember
( 'borrowernumber' => $message->{'borrowernumber'} );
1303 my $to_address = $message->{'to_address'};
1304 unless ($to_address) {
1306 warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1307 _set_message_status
( { message_id
=> $message->{'message_id'},
1308 status
=> 'failed' } );
1311 $to_address = C4
::Members
::GetNoticeEmailAddress
( $message->{'borrowernumber'} );
1312 unless ($to_address) {
1313 # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1314 # warning too verbose for this more common case?
1315 _set_message_status
( { message_id
=> $message->{'message_id'},
1316 status
=> 'failed' } );
1321 my $utf8 = decode
('MIME-Header', $message->{'subject'} );
1322 $message->{subject
}= encode
('MIME-Header', $utf8);
1323 my $subject = encode
('UTF-8', $message->{'subject'});
1324 my $content = encode
('UTF-8', $message->{'content'});
1325 my $content_type = $message->{'content_type'} || 'text/plain; charset="UTF-8"';
1326 my $is_html = $content_type =~ m/html/io;
1327 my $branch_email = undef;
1328 my $branch_replyto = undef;
1329 my $branch_returnpath = undef;
1331 my $library = Koha
::Libraries
->find( $member->{branchcode
} );
1332 $branch_email = $library->branchemail;
1333 $branch_replyto = $library->branchreplyto;
1334 $branch_returnpath = $library->branchreturnpath;
1336 my $email = Koha
::Email
->new();
1337 my %sendmail_params = $email->create_message_headers(
1340 from
=> $message->{'from_address'} || $branch_email,
1341 replyto
=> $branch_replyto,
1342 sender
=> $branch_returnpath,
1343 subject
=> $subject,
1344 message
=> $is_html ? _wrap_html
( $content, $subject ) : $content,
1345 contenttype
=> $content_type
1349 $sendmail_params{'Auth'} = {user
=> $username, pass
=> $password, method
=> $method} if $username;
1350 if ( my $bcc = C4
::Context
->preference('NoticeBcc') ) {
1351 $sendmail_params{ Bcc
} = $bcc;
1354 _update_message_to_address
($message->{'message_id'},$to_address) unless $message->{to_address
}; #if initial message address was empty, coming here means that a to address was found and queue should be updated
1356 if ( sendmail
( %sendmail_params ) ) {
1357 _set_message_status
( { message_id
=> $message->{'message_id'},
1358 status
=> 'sent' } );
1361 _set_message_status
( { message_id
=> $message->{'message_id'},
1362 status
=> 'failed' } );
1363 carp
$Mail::Sendmail
::error
;
1369 my ($content, $title) = @_;
1371 my $css = C4
::Context
->preference("NoticeCSS") || '';
1372 $css = qq{<link rel
="stylesheet" type
="text/css" href
="$css">} if $css;
1374 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1375 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1376 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
1378 <title>$title</title>
1379 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1390 my ( $message ) = @_;
1391 my $dbh = C4
::Context
->dbh;
1392 my $count = $dbh->selectrow_array(q
|
1395 WHERE message_transport_type
= ?
1396 AND borrowernumber
= ?
1398 AND CAST
(time_queued AS date
) = CAST
(NOW
() AS date
)
1401 |, {}, $message->{message_transport_type
}, $message->{borrowernumber
}, $message->{letter_code
}, $message->{content
} );
1405 sub _send_message_by_sms
{
1406 my $message = shift or return;
1407 my $member = C4
::Members
::GetMember
( 'borrowernumber' => $message->{'borrowernumber'} );
1409 unless ( $member->{smsalertnumber
} ) {
1410 _set_message_status
( { message_id
=> $message->{'message_id'},
1411 status
=> 'failed' } );
1415 if ( _is_duplicate
( $message ) ) {
1416 _set_message_status
( { message_id
=> $message->{'message_id'},
1417 status
=> 'failed' } );
1421 my $success = C4
::SMS
->send_sms( { destination
=> $member->{'smsalertnumber'},
1422 message
=> $message->{'content'},
1424 _set_message_status
( { message_id
=> $message->{'message_id'},
1425 status
=> ($success ?
'sent' : 'failed') } );
1429 sub _update_message_to_address
{
1431 my $dbh = C4
::Context
->dbh();
1432 $dbh->do('UPDATE message_queue SET to_address=? WHERE message_id=?',undef,($to,$id));
1435 sub _set_message_status
{
1436 my $params = shift or return;
1438 foreach my $required_parameter ( qw( message_id status ) ) {
1439 return unless exists $params->{ $required_parameter };
1442 my $dbh = C4
::Context
->dbh();
1443 my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1444 my $sth = $dbh->prepare( $statement );
1445 my $result = $sth->execute( $params->{'status'},
1446 $params->{'message_id'} );
1451 my ( $params ) = @_;
1453 my $content = $params->{content
};
1454 my $tables = $params->{tables
};
1455 my $loops = $params->{loops
};
1457 my $use_template_cache = C4
::Context
->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE
};
1458 my $template = Template
->new(
1462 PLUGIN_BASE
=> 'Koha::Template::Plugin',
1463 COMPILE_EXT
=> $use_template_cache ?
'.ttc' : '',
1464 COMPILE_DIR
=> $use_template_cache ? C4
::Context
->config('template_cache_dir') : '',
1466 ENCODING
=> 'UTF-8',
1468 ) or die Template
->error();
1470 my $tt_params = { %{ _get_tt_params
( $tables ) }, %{ _get_tt_params
( $loops, 'is_a_loop' ) } };
1472 $content = qq|[% USE KohaDates
%]$content|;
1475 $template->process( \
$content, $tt_params, \
$output ) || croak
"ERROR PROCESSING TEMPLATE: " . $template->error();
1480 sub _get_tt_params
{
1481 my ($tables, $is_a_loop) = @_;
1487 article_requests
=> {
1488 module
=> 'Koha::ArticleRequests',
1489 singular
=> 'article_request',
1490 plural
=> 'article_requests',
1494 module
=> 'Koha::Biblios',
1495 singular
=> 'biblio',
1496 plural
=> 'biblios',
1497 pk
=> 'biblionumber',
1500 module
=> 'Koha::Patrons',
1501 singular
=> 'borrower',
1502 plural
=> 'borrowers',
1503 pk
=> 'borrowernumber',
1506 module
=> 'Koha::Libraries',
1507 singular
=> 'branch',
1508 plural
=> 'branches',
1512 module
=> 'Koha::Items',
1518 module
=> 'Koha::News',
1524 module
=> 'Koha::Tmp::Orders', # Should Koha::Acquisition::Orders when will be based on Koha::Objects
1525 singular
=> 'order',
1527 pk
=> 'ordernumber',
1530 module
=> 'Koha::Holds',
1533 fk
=> [ 'borrowernumber', 'biblionumber' ],
1536 module
=> 'Koha::Serials',
1537 singular
=> 'serial',
1538 plural
=> 'serials',
1542 module
=> 'Koha::Subscriptions',
1543 singular
=> 'subscription',
1544 plural
=> 'subscriptions',
1545 pk
=> 'subscriptionid',
1548 module
=> 'Koha::Suggestions',
1549 singular
=> 'suggestion',
1550 plural
=> 'suggestions',
1551 pk
=> 'suggestionid',
1554 module
=> 'Koha::Checkouts',
1555 singular
=> 'checkout',
1556 plural
=> 'checkouts',
1560 module
=> 'Koha::Old::Checkouts',
1561 singular
=> 'old_checkout',
1562 plural
=> 'old_checkouts',
1565 borrower_modifications
=> {
1566 module
=> 'Koha::Patron::Modifications',
1567 singular
=> 'patron_modification',
1568 plural
=> 'patron_modifications',
1569 fk
=> 'verification_token',
1573 foreach my $table ( keys %$tables ) {
1574 next unless $config->{$table};
1576 my $ref = ref( $tables->{$table} ) || q{};
1577 my $module = $config->{$table}->{module
};
1579 if ( can_load
( modules
=> { $module => undef } ) ) {
1580 my $pk = $config->{$table}->{pk
};
1581 my $fk = $config->{$table}->{fk
};
1584 my $values = $tables->{$table} || [];
1585 unless ( ref( $values ) eq 'ARRAY' ) {
1586 croak
"ERROR processing table $table. Wrong API call.";
1588 my $key = $pk ?
$pk : $fk;
1589 my $objects = $module->search( { $key => { -in => $values } } );
1590 $params->{ $config->{$table}->{plural
} } = $objects;
1592 elsif ( $ref eq q{} || $ref eq 'HASH' ) {
1593 my $id = ref $ref eq 'HASH' ?
$tables->{$table}->{$pk} : $tables->{$table};
1595 if ( $fk ) { # Using a foreign key for lookup
1596 if ( ref( $fk ) eq 'ARRAY' ) { # Foreign key is multi-column
1598 foreach my $key ( @
$fk ) {
1599 $search->{$key} = $id->{$key};
1601 $object = $module->search( $search )->last();
1602 } else { # Foreign key is single column
1603 $object = $module->search( { $fk => $id } )->last();
1605 } else { # using the table's primary key for lookup
1606 $object = $module->find($id);
1608 $params->{ $config->{$table}->{singular
} } = $object;
1610 else { # $ref eq 'ARRAY'
1612 if ( @
{ $tables->{$table} } == 1 ) { # Param is a single key
1613 $object = $module->search( { $pk => $tables->{$table} } )->last();
1615 else { # Params are mutliple foreign keys
1616 croak
"Multiple foreign keys (table $table) should be passed using an hashref";
1618 $params->{ $config->{$table}->{singular
} } = $object;
1622 croak
"ERROR LOADING MODULE $module: $Module::Load::Conditional::ERROR";
1626 $params->{today
} = dt_from_string
();