Bug 30318: Don't reset messaging preferences when form is empty
[koha.git] / C4 / SMS.pm
1 package C4::SMS;
2
3 # Copyright 2007 Liblime
4 # Copyright 2015 Biblibre
5 # Copyright 2016 Catalyst
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 =head1 NAME
23
24 C4::SMS - send SMS messages
25
26 =head1 SYNOPSIS
27
28 my ( $success, $error ) = C4::SMS->send_sms(
29     {
30         message     => 'This is my text message',
31         destination => '212-555-1212'
32     }
33 );
34
35 =head1 DESCRIPTION
36
37 A wrapper for SMS::Send.
38
39 Can use a yaml file for config, the path to which is in the koha-conf.xml
40 <sms_send_config>__KOHA_CONF_DIR__/sms_send/</sms_send_config>
41
42 Each file needs to be in the format of
43 __KOHA_CONF_DIR__/sms_send/<driver>.yaml
44
45 For example for SMS::Send::UK::Kapow the config would be
46
47 /etc/koha/sites/instancename/sms_send/UK/Kapow.yaml for package install
48 or
49 /etc/koha/sms_send/UK/Kapow.yaml for tarball
50
51 A underscore character is prepended to all parameter names so they are
52 treated as driver-specific options (leading underscore must not appear
53 in config file).
54
55 =cut
56
57 use strict;
58 use warnings;
59
60 use C4::Context;
61 use File::Spec;
62
63
64
65 =head1 METHODS
66
67 =cut
68
69 # The previous implmentation used username and password.
70 # our $user = C4::Context->config('smsuser');
71 # our $pwd  = C4::Context->config('smspass');
72
73 =head2 send_sms
74
75 =cut
76
77 sub send_sms {
78     my $self = shift;
79     my $params= shift;
80
81     foreach my $required_parameter ( qw( message destination ) ) {
82         # Should I warn in some way?
83         return unless defined $params->{ $required_parameter };
84     }
85
86     eval { require SMS::Send; };
87     if ( $@ ) {
88         # we apparently don't have SMS::Send. Return a failure.
89         return;
90     }
91
92     # This allows the user to override the driver. See SMS::Send::Test
93     my $driver = exists $params->{'driver'} ? $params->{'driver'} : $self->driver();
94     return ( undef, 'SMS_SEND_DRIVER_MISSING' ) unless $driver;
95
96     my ($sent, $sender);
97
98     my $subpath = $driver;
99     $subpath =~ s|::|/|g;
100
101     my $sms_send_config = C4::Context->config('sms_send_config');
102     my $conf_file = defined $sms_send_config
103         ? File::Spec->catfile( $sms_send_config, $subpath )
104         : $subpath;
105     $conf_file .= q{.yaml};
106
107     my %args;
108     if ( -f $conf_file ) {
109         require YAML::XS;
110         my $conf = YAML::XS::LoadFile( $conf_file );
111         %args = map { q{_} . $_ => $conf->{$_} } keys %$conf;
112     }
113
114     eval {
115         # Create a sender
116         $sender = SMS::Send->new(
117             $driver,
118             _login    => C4::Context->preference('SMSSendUsername'),
119             _password => C4::Context->preference('SMSSendPassword'),
120             %args,
121         );
122
123         # Send a message
124         $sent = $sender->send_sms(
125             to   => $params->{destination},
126             text => $params->{message},
127         );
128     };
129
130     #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
131     #Catch those errors and fail the sms-sending gracefully.
132     if ($@) {
133         warn $@;
134         return ( undef, $@ );
135     }
136     # warn 'failure' unless $sent;
137     return $sent;
138 }
139
140 =head2 driver
141
142 =cut
143
144 sub driver {
145     my $self = shift;
146
147     # return 'US::SprintPCS';
148     return C4::Context->preference('SMSSendDriver');
149
150 }
151
152 1;
153
154 __END__
155