Bug 13029: Allow to pass additional parameters to SMS::Send drivers
authorJulian Maurice <julian.maurice@biblibre.com>
Mon, 6 Oct 2014 13:04:12 +0000 (15:04 +0200)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 16 Dec 2016 11:33:39 +0000 (11:33 +0000)
C4::SMS::send_sms now reads a YAML file and pass all parameters in the
file to SMS::Send::new.

The config file is read in <installdir>/etc/sms/driver/<driver>.yaml.
For instance, if the driver used is SMS::Send::UK::Kapow, the config
file has to be in <installdir>/etc/sms/driver/UK/Kapow.yaml

A underscore character is prepended to all parameter names so they are
treated as driver-specific options (leading underscore must not appear
in config file).

Signed-off-by: Magnus Enger <magnus@libriotech.no>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
C4/SMS.pm

index 41407e3..d011f5f 100644 (file)
--- a/C4/SMS.pm
+++ b/C4/SMS.pm
@@ -36,6 +36,7 @@ use strict;
 use warnings;
 
 use C4::Context;
+use File::Spec;
 
 
 
@@ -70,21 +71,39 @@ sub send_sms {
     my $driver = exists $params->{'driver'} ? $params->{'driver'} : $self->driver();
     return unless $driver;
 
-    # warn "using driver: $driver to send message to $params->{'destination'}";
 
     my ($sent, $sender);
+
+    my $subpath = $driver;
+    $subpath =~ s|::|/|;
+
+    my $conf_file = File::Spec->catfile(
+        C4::Context->config('installdir'),
+        'etc', 'sms', 'driver', $subpath
+    ) . q{.yaml};
+    my %args;
+    if ( -f $conf_file ) {
+        require YAML;
+        my $conf = YAML::LoadFile( $conf_file );
+        %args = map { q{_} . $_ => $conf->{$_} } keys %$conf;
+    }
+
     eval {
         # Create a sender
-        $sender = SMS::Send->new( $driver,
-                                 _login    => C4::Context->preference('SMSSendUsername'),
-                                 _password => C4::Context->preference('SMSSendPassword'),
-                            );
-    
+        $sender = SMS::Send->new(
+            $driver,
+            _login    => C4::Context->preference('SMSSendUsername'),
+            _password => C4::Context->preference('SMSSendPassword'),
+            %args,
+        );
+
         # Send a message
-        $sent = $sender->send_sms( to   => $params->{'destination'},
-                                  text => $params->{'message'},
-                             );
+        $sent = $sender->send_sms(
+            to   => $params->{destination},
+            text => $params->{message},
+        );
     };
+
     #We might die because SMS::Send $driver is not defined or the sms-number has a bad format
     #Catch those errors and fail the sms-sending gracefully.
     if ($@) {