-=head2 addalert ($borrowernumber, $type, $externalid)
-
- parameters :
- - $borrowernumber : the number of the borrower subscribing to the alert
- - $type : the type of alert.
- - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
-
- create an alert and return the alertid (primary key)
-
-=cut
-
-sub addalert {
- my ( $borrowernumber, $type, $externalid ) = @_;
- my $dbh = C4::Context->dbh;
- my $sth =
- $dbh->prepare(
- "insert into alert (borrowernumber, type, externalid) values (?,?,?)");
- $sth->execute( $borrowernumber, $type, $externalid );
-
- # get the alert number newly created and return it
- my $alertid = $dbh->{'mysql_insertid'};
- return $alertid;
-}
-
-=head2 delalert ($alertid)
-
- parameters :
- - alertid : the alert id
- deletes the alert
-
-=cut
-
-sub delalert {
- my $alertid = shift or die "delalert() called without valid argument (alertid)"; # it's gonna die anyway.
- $debug and warn "delalert: deleting alertid $alertid";
- my $sth = C4::Context->dbh->prepare("delete from alert where alertid=?");
- $sth->execute($alertid);
-}
-
-=head2 getalert ([$borrowernumber], [$type], [$externalid])
-
- parameters :
- - $borrowernumber : the number of the borrower subscribing to the alert
- - $type : the type of alert.
- - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
- 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.
-
-=cut
-
-sub getalert {
- my ( $borrowernumber, $type, $externalid ) = @_;
- my $dbh = C4::Context->dbh;
- my $query = "SELECT a.*, b.branchcode FROM alert a JOIN borrowers b USING(borrowernumber) WHERE 1";
- my @bind;
- if ($borrowernumber and $borrowernumber =~ /^\d+$/) {
- $query .= " AND borrowernumber=?";
- push @bind, $borrowernumber;
- }
- if ($type) {
- $query .= " AND type=?";
- push @bind, $type;
- }
- if ($externalid) {
- $query .= " AND externalid=?";
- push @bind, $externalid;
- }
- my $sth = $dbh->prepare($query);
- $sth->execute(@bind);
- return $sth->fetchall_arrayref({});
-}
-
-=head2 findrelatedto($type, $externalid)
-
- parameters :
- - $type : the type of alert
- - $externalid : the id of the "object" to query
-
- 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.
- When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
-
-=cut
-
-# outmoded POD:
-# When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
-
-sub findrelatedto {
- my $type = shift or return;
- my $externalid = shift or return;
- my $q = ($type eq 'issue' ) ?
-"select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?" :
- ($type eq 'borrower') ?
-"select concat(firstname,' ',surname) from borrowers where borrowernumber=?" : undef;
- unless ($q) {
- warn "findrelatedto(): Illegal type '$type'";
- return;
- }
- my $sth = C4::Context->dbh->prepare($q);
- $sth->execute($externalid);
- my ($result) = $sth->fetchrow;
- return $result;
-}
-