Skip to content
Snippets Groups Projects
Commit 0cbf1d97 authored by Demian Katz's avatar Demian Katz
Browse files

Refactored SMS code for greater flexibility; added some new SMS configuration options.

parent aaa6c5e4
Branches
Tags
No related merge requests found
; This file defines the list of carriers and their domain names for the SMS service [General]
; Set smsType to Mailer to use VuFind's internal mailer for SMS (default).
;smsType = Mailer
; This refers to phone number style for validation. At present, only "US" is
; supported (default setting); any other setting will disable validation.
validation = US
; These characters will be filtered from user input phone numbers prior to texting
filter = "-.() "
; This section defines the list of carriers and their domain names for the SMS service
; The format is: unique_identifier = carrier.domain.name:Display Name ; The format is: unique_identifier = carrier.domain.name:Display Name
; ;
;For additional carriers, you may find it useful to refer to http://www.txt2day.com ; For additional carriers, you may find it useful to refer to http://www.txt2day.com.
[Carriers] [Carriers]
; Carriers in the US ; Carriers in the US
virgin = vmobl.com:"Virgin Mobile" virgin = vmobl.com:"Virgin Mobile"
......
...@@ -351,6 +351,7 @@ $config = array( ...@@ -351,6 +351,7 @@ $config = array(
$logger->setConfig(\VuFind\Config\Reader::getConfig()); $logger->setConfig(\VuFind\Config\Reader::getConfig());
return $logger; return $logger;
}, },
'VuFind\SMS' => 'VuFind\SMS\Factory',
'VuFind\Translator' => function ($sm) { 'VuFind\Translator' => function ($sm) {
$factory = new \Zend\I18n\Translator\TranslatorServiceFactory(); $factory = new \Zend\I18n\Translator\TranslatorServiceFactory();
$translator = $factory->createService($sm); $translator = $factory->createService($sm);
...@@ -376,7 +377,6 @@ $config = array( ...@@ -376,7 +377,6 @@ $config = array(
'VuFind\RecordLoader' => 'VuFind\Record\Loader', 'VuFind\RecordLoader' => 'VuFind\Record\Loader',
'VuFind\SearchSpecsReader' => 'VuFind\Config\SearchSpecsReader', 'VuFind\SearchSpecsReader' => 'VuFind\Config\SearchSpecsReader',
'VuFind\SessionManager' => 'Zend\Session\SessionManager', 'VuFind\SessionManager' => 'Zend\Session\SessionManager',
'VuFind\SMS' => 'VuFind\Mailer\SMS',
'VuFind\WorldCatConnection' => 'VuFind\Connection\WorldCat', 'VuFind\WorldCatConnection' => 'VuFind\Connection\WorldCat',
'VuFind\WorldCatUtils' => 'VuFind\Connection\WorldCatUtils', 'VuFind\WorldCatUtils' => 'VuFind\Connection\WorldCatUtils',
), ),
......
...@@ -373,6 +373,7 @@ class AbstractRecord extends AbstractBase ...@@ -373,6 +373,7 @@ class AbstractRecord extends AbstractBase
$sms = $this->getServiceLocator()->get('VuFind\SMS'); $sms = $this->getServiceLocator()->get('VuFind\SMS');
$view = $this->createViewModel(); $view = $this->createViewModel();
$view->carriers = $sms->getCarriers(); $view->carriers = $sms->getCarriers();
$view->validation = $sms->getValidationType();
// Process form submission: // Process form submission:
if ($this->params()->fromPost('submit')) { if ($this->params()->fromPost('submit')) {
...@@ -382,9 +383,11 @@ class AbstractRecord extends AbstractBase ...@@ -382,9 +383,11 @@ class AbstractRecord extends AbstractBase
// Attempt to send the email and show an appropriate flash message: // Attempt to send the email and show an appropriate flash message:
try { try {
$sms->textRecord( $body = $this->getViewRenderer()->partial(
$view->provider, $view->to, $driver, $this->getViewRenderer() 'Email/record-sms.phtml',
array('driver' => $driver, 'to' => $view->to)
); );
$sms->text($view->provider, $view->to, null, $body);
$this->flashMessenger()->setNamespace('info') $this->flashMessenger()->setNamespace('info')
->addMessage('sms_success'); ->addMessage('sms_success');
return $this->redirectToRecord(); return $this->redirectToRecord();
......
...@@ -938,9 +938,12 @@ class AjaxController extends AbstractBase ...@@ -938,9 +938,12 @@ class AjaxController extends AbstractBase
$this->params()->fromPost('id'), $this->params()->fromPost('id'),
$this->params()->fromPost('source', 'VuFind') $this->params()->fromPost('source', 'VuFind')
); );
$this->getServiceLocator()->get('VuFind\SMS')->textRecord( $to = $this->params()->fromPost('to');
$this->params()->fromPost('provider'), $body = $this->getViewRenderer()->partial(
$this->params()->fromPost('to'), $record, $this->getViewRenderer() 'Email/record-sms.phtml', array('driver' => $record, 'to' => $to)
);
$this->getServiceLocator()->get('VuFind\SMS')->text(
$this->params()->fromPost('provider'), $to, null, $body
); );
return $this->output( return $this->output(
$this->translate('sms_success'), self::STATUS_OK $this->translate('sms_success'), self::STATUS_OK
......
<?php
/**
* Base class to enable sharing of common methods between SMS subclasses
*
* PHP version 5
*
* Copyright (C) Villanova University 2009.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package SMS
* @author Ronan McHugh <vufind-tech@lists.sourceforge.net>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/system_classes Wiki
*/
namespace VuFind\SMS;
/**
* Base class to enable sharing of common methods between SMS subclasses
*
* @category VuFind2
* @package SMS
* @author Ronan McHugh <vufind-tech@lists.sourceforge.net>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/system_classes Wiki
*/
abstract class AbstractBase implements SMSInterface
{
/**
* SMS configuration
*
* @var \Zend\Config\Config
*/
protected $smsConfig;
/**
* Constructor
*
* @param \Zend\Config\Config $config SMS configuration
* @param array $options Additional options
*/
public function __construct(\Zend\Config\Config $config, $options = array())
{
$this->smsConfig = $config;
}
/**
* Filter bad characters from a phone number
*
* @param string $num Phone number to filter
*
* @return string
*/
protected function filterPhoneNumber($num)
{
$filter = isset($this->smsConfig->General->filter)
? $this->smsConfig->General->filter : '-.() ';
return str_replace(str_split($filter), '', $num);
}
/**
* Get validation type for phone numbers
*
* @return string
*/
public function getValidationType()
{
// Load setting from config; at present, only US is implemented in templates
return isset($this->smsConfig->General->validation)
? $this->smsConfig->General->validation : 'US';
}
}
<?php
/**
* Factory for instantiating SMS objects
*
* PHP version 5
*
* Copyright (C) Villanova University 2009.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package SMS
* @author Ronan McHugh <vufind-tech@lists.sourceforge.net>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/system_classes Wiki
*/
namespace VuFind\SMS;
use VuFind\Config\Reader as ConfigReader,
Zend\ServiceManager\ServiceLocatorInterface;
/**
* Factory for instantiating SMS objects
*
* @category VuFind2
* @package SMS
* @author Ronan McHugh <vufind-tech@lists.sourceforge.net>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/system_classes Wiki
*/
class Factory implements \Zend\ServiceManager\FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $sm Service manager
*
* @return mixed
*/
public function createService(ServiceLocatorInterface $sm)
{
// Load configurations:
$mainConfig = ConfigReader::getConfig();
$smsConfig = ConfigReader::getConfig('sms');
// Determine SMS type:
$type = isset($smsConfig->General->smsType)
? $smsConfig->General->smsType : 'Mailer';
// Initialize object based on requested type:
switch (strtolower($type)) {
case 'mailer':
$options = array('mailer' => $sm->get('VuFind\Mailer'));
if (isset($mainConfig->Site->email)) {
$options['defaultFrom'] = $mainConfig->Site->email;
}
return new Mailer($smsConfig, $options);
default:
throw new \Exception('Unrecognized SMS type: ' . $type);
}
}
}
...@@ -20,26 +20,29 @@ ...@@ -20,26 +20,29 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* *
* @category VuFind2 * @category VuFind2
* @package Mailer * @package SMS
* @author Demian Katz <demian.katz@villanova.edu> * @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/system_classes Wiki * @link http://vufind.org/wiki/system_classes Wiki
*/ */
namespace VuFind\Mailer; namespace VuFind\SMS;
use VuFind\Config\Reader as ConfigReader, VuFind\Exception\Mail as MailException;
/** /**
* VuFind Mailer Class for SMS messages * VuFind Mailer Class for SMS messages
* *
* @category VuFind2 * @category VuFind2
* @package Mailer * @package SMS
* @author Demian Katz <demian.katz@villanova.edu> * @author Demian Katz <demian.katz@villanova.edu>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/system_classes Wiki * @link http://vufind.org/wiki/system_classes Wiki
*/ */
class SMS extends \VuFind\Mailer class Mailer extends AbstractBase
{ {
// Defaults, usually overridden by contents of sms.ini: /**
* Default carriers, usually overridden by contents of web/conf/sms.ini.
*
* @var array
*/
protected $carriers = array( protected $carriers = array(
'virgin' => array('name' => 'Virgin Mobile', 'domain' => 'vmobl.com'), 'virgin' => array('name' => 'Virgin Mobile', 'domain' => 'vmobl.com'),
'att' => array('name' => 'AT&T', 'domain' => 'txt.att.net'), 'att' => array('name' => 'AT&T', 'domain' => 'txt.att.net'),
...@@ -51,38 +54,53 @@ class SMS extends \VuFind\Mailer ...@@ -51,38 +54,53 @@ class SMS extends \VuFind\Mailer
'Cricket' => array('name' => 'Cricket', 'domain' => 'mms.mycricket.com') 'Cricket' => array('name' => 'Cricket', 'domain' => 'mms.mycricket.com')
); );
// Default "from" address: /**
* Default "from" address
*
* @var string
*/
protected $defaultFrom; protected $defaultFrom;
/** /**
* Constructor * VuFind Mailer object
* *
* Sets up SMS carriers and other settings from sms.ini. * @var \VuFind\Mailer
*/
protected $mailer;
/**
* Constructor
* *
* @param \Zend\Mail\Transport\TransportInterface $transport Mail transport * @param \Zend\Config\Config $config SMS configuration
* object (we'll build our own if none is provided). * @param array $options Additional options: defaultFrom (optional)
* @param \Zend\Config\Config $config VuFind configuration * and mailer (must be a \VuFind\Mailer object)
* object (we'll auto-load if none is provided).
*/ */
public function __construct($transport = null, $config = null) public function __construct(\Zend\Config\Config $config, $options = array())
{ {
// Set up parent object first: // Set up parent object first:
parent::__construct($transport, $config); parent::__construct($config, $options);
// if using sms.ini, then load the carriers from it // If found, use carriers from SMS configuration; otherwise, fall back to the
// otherwise, fall back to the default list of US carriers // default list of US carriers.
$smsConfig = ConfigReader::getConfig('sms'); if (isset($config->Carriers) && count($config->Carriers) > 0) {
if (isset($smsConfig->Carriers) && count($smsConfig->Carriers) > 0) {
$this->carriers = array(); $this->carriers = array();
foreach ($smsConfig->Carriers as $id=>$settings) { foreach ($config->Carriers as $id=>$settings) {
list($domain, $name) = explode(':', $settings, 2); list($domain, $name) = explode(':', $settings, 2);
$this->carriers[$id] = array('name'=>$name, 'domain'=>$domain); $this->carriers[$id] = array('name'=>$name, 'domain'=>$domain);
} }
} }
// Load default "from" address: // Load default "from" address:
$this->defaultFrom = isset($this->config->Site->email) $this->defaultFrom
? $this->config->Site->email : ''; = isset($options['defaultFrom']) ? $options['defaultFrom'] : '';
// Make sure mailer dependency has been injected:
if (!isset($options['mailer'])
|| !($options['mailer'] instanceof \VuFind\Mailer)
) {
throw new \Exception('$options["mailer"] must be a \VuFind\Mailer');
}
$this->mailer = $options['mailer'];
} }
/** /**
...@@ -105,8 +123,8 @@ class SMS extends \VuFind\Mailer ...@@ -105,8 +123,8 @@ class SMS extends \VuFind\Mailer
* @param string $from The email address to use as sender * @param string $from The email address to use as sender
* @param string $message The message to send * @param string $message The message to send
* *
* @throws MailException * @throws \VuFind\Exception\Mail
* @return mixed PEAR error on error, boolean true otherwise * @return void
*/ */
public function text($provider, $to, $from, $message) public function text($provider, $to, $from, $message)
{ {
...@@ -115,31 +133,10 @@ class SMS extends \VuFind\Mailer ...@@ -115,31 +133,10 @@ class SMS extends \VuFind\Mailer
throw new MailException('Unknown Carrier'); throw new MailException('Unknown Carrier');
} }
$badChars = array('-', '.', '(', ')', ' '); $to = $this->filterPhoneNumber($to)
$to = str_replace($badChars, '', $to); . '@' . $this->carriers[$provider]['domain'];
$to = $to . '@' . $this->carriers[$provider]['domain'];
$from = empty($from) ? $this->defaultFrom : $from; $from = empty($from) ? $this->defaultFrom : $from;
$subject = ''; $subject = '';
return $this->send($to, $from, $subject, $message); return $this->mailer->send($to, $from, $subject, $message);
}
/**
* Send a text message representing a record.
*
* @param string $provider Target SMS provider
* @param string $to Recipient phone number
* @param \VuFind\RecordDriver\AbstractBase $record Record being emailed
* @param \Zend\View\Renderer\RendererInterface $view View object (used to
* render email templates)
*
* @throws MailException
* @return void
*/
public function textRecord($provider, $to, $record, $view)
{
$body = $view->partial(
'Email/record-sms.phtml', array('driver' => $record, 'to' => $to)
);
return $this->text($provider, $to, null, $body);
} }
} }
\ No newline at end of file
<?php
/**
* Interface for SMS classes.
*
* PHP version 5
*
* Copyright (C) Villanova University 2009.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package SMS
* @author Ronan McHugh <vufind-tech@lists.sourceforge.net>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/system_classes Wiki
*/
namespace VuFind\SMS;
/**
* Interface for SMS classes.
*
* @category VuFind2
* @package SMS
* @author Ronan McHugh <vufind-tech@lists.sourceforge.net>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org/wiki/system_classes Wiki
*/
interface SMSInterface
{
/**
* Get validation type for phone numbers
*
* @return string
*/
public function getValidationType();
/**
* Get a list of carriers supported by the module. Returned as an array of
* associative arrays indexed by carrier ID and containing "name" and "domain"
* keys.
*
* @return array
*/
public function getCarriers();
/**
* Send a text message to the specified provider.
*
* @param string $provider The provider ID to send to
* @param string $to The phone number at the provider
* @param string $from The email address to use as sender
* @param string $message The message to send
*
* @throws \VuFind\Exception\Mail
* @return void
*/
public function text($provider, $to, $from, $message);
}
...@@ -14,7 +14,13 @@ ...@@ -14,7 +14,13 @@
<input id="sms_to" type="text" name="to" value="<?=isset($this->to) ? $this->to : $this->transEsc('sms_phone_number')?>" <input id="sms_to" type="text" name="to" value="<?=isset($this->to) ? $this->to : $this->transEsc('sms_phone_number')?>"
onfocus="if (this.value=='<?=$this->transEsc('sms_phone_number')?>') this.value=''" onfocus="if (this.value=='<?=$this->transEsc('sms_phone_number')?>') this.value=''"
onblur="if (this.value=='') this.value='<?=$this->transEsc('sms_phone_number')?>'" onblur="if (this.value=='') this.value='<?=$this->transEsc('sms_phone_number')?>'"
class="<?=$this->jqueryValidation(array('required'=>'This field is required', 'phoneUS'=>'Invalid phone number.'))?>"/> class="<?
$options = array('required'=>'This field is required');
if ($this->validation == 'US') {
$options['phoneUS'] = 'Invalid phone number.';
}
echo $this->jqueryValidation($options);
?>"/>
<br/> <br/>
<? if (is_array($this->carriers) && count($this->carriers) > 1): ?> <? if (is_array($this->carriers) && count($this->carriers) > 1): ?>
<label class="span-2" for="sms_provider"><?=$this->transEsc('Provider')?>:</label> <label class="span-2" for="sms_provider"><?=$this->transEsc('Provider')?>:</label>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment