Bug 15128 - (QA Followup) Fix use of 'my' variable causing loss of data
[koha.git] / C4 / SMS.pm
1 package C4::SMS;
2
3 # Copyright 2007 Liblime
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 =head1 NAME
21
22 C4::SMS - send SMS messages
23
24 =head1 SYNOPSIS
25
26 my $success = C4::SMS->send_sms({ message     => 'This is my text message',
27                                   destination => '212-555-1212' });
28
29 =head1 DESCRIPTION
30
31
32
33 =cut
34
35 use strict;
36 use warnings;
37
38 use C4::Context;
39
40
41
42 =head1 METHODS
43
44 =cut
45
46 # The previous implmentation used username and password.
47 # our $user = C4::Context->config('smsuser');
48 # our $pwd  = C4::Context->config('smspass');
49
50 =head2 send_sms
51
52 =cut
53
54 sub send_sms {
55     my $self = shift;
56     my $params= shift;
57
58     foreach my $required_parameter ( qw( message destination ) ) {
59         # Should I warn in some way?
60         return unless defined $params->{ $required_parameter };
61     }
62
63     eval { require SMS::Send; };
64     if ( $@ ) {
65         # we apparently don't have SMS::Send. Return a failure.
66         return;
67     }
68
69     # This allows the user to override the driver. See SMS::Send::Test
70     my $driver = exists $params->{'driver'} ? $params->{'driver'} : $self->driver();
71     return unless $driver;
72
73     # warn "using driver: $driver to send message to $params->{'destination'}";
74
75     my ($sent, $sender);
76     eval {
77         # Create a sender
78         $sender = SMS::Send->new( $driver,
79                                  _login    => C4::Context->preference('SMSSendUsername'),
80                                  _password => C4::Context->preference('SMSSendPassword'),
81                             );
82     
83         # Send a message
84         $sent = $sender->send_sms( to   => $params->{'destination'},
85                                   text => $params->{'message'},
86                              );
87     };
88     #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
89     #Catch those errors and fail the sms-sending gracefully.
90     if ($@) {
91         warn $@;
92         return;
93     }
94     # warn 'failure' unless $sent;
95     return $sent;
96 }
97
98 =head2 driver
99
100 =cut
101
102 sub driver {
103     my $self = shift;
104
105     # return 'US::SprintPCS';
106     return C4::Context->preference('SMSSendDriver');
107
108 }
109
110 1;
111
112 __END__
113