diff --git a/modules/sms_sendtophone/sms_sendtophone.module b/modules/sms_sendtophone/sms_sendtophone.module index d4a01e5..f071090 100644 --- a/modules/sms_sendtophone/sms_sendtophone.module +++ b/modules/sms_sendtophone/sms_sendtophone.module @@ -429,7 +429,7 @@ function sms_sendtophone_form_validate($form, &$form_state) { * @see http://drupal.org/node/1354 */ function sms_sendtophone_form_submit($form, &$form_state) { - if (sms_send($form_state['values']['number'], $form_state['values']['message'], $form_state['values']['gateway'])) { + if (sms_send($form_state['values']['number'], $form_state['values']['message'], (array) $form_state['values']['gateway'])) { drupal_set_message(t('The message "@message" sent to @number.', array('@message' => $form_state['values']['message'], '@number' => $form_state['values']['number']))); } } diff --git a/modules/sms_track/sms_track.module b/modules/sms_track/sms_track.module index 03cc7c9..f35b561 100644 --- a/modules/sms_track/sms_track.module +++ b/modules/sms_track/sms_track.module @@ -8,6 +8,8 @@ */ use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\sms\Gateway\GatewayInterface; +use Drupal\sms\Message\SmsMessageInterface; use Drupal\views\Plugin\views\query\QueryPluginBase; use \Drupal\views\ViewExecutable; @@ -42,7 +44,9 @@ function sms_track_cron() { /** * Implements hook_sms_send(). */ -function sms_track_sms_send($number, $message, &$options = array(), $gateway) { +function sms_track_sms_send(SmsMessageInterface $sms, array $options, GatewayInterface $gateway) { + $message = $sms->getMessage(); + $number = $sms->getRecipients()[0]; // Tracking. // Place a tracking reference on a sent message if we need to. if (!empty($options) && !array_key_exists('reference', $options)) { @@ -55,23 +59,23 @@ function sms_track_sms_send($number, $message, &$options = array(), $gateway) { * * @param string $op * Operation string (pre process, process, post process). - * @param string $number - * MSISDN of recipient. - * @param string $message - * SMS message body. + * @param \Drupal\sms\Message\SmsMessageInterface $sms + * The sms message object. * @param array $options * Additional options array including sender. - * @param array $gateway + * @param \Drupal\sms\Gateway\GatewayInterface $gateway * Gateway array for the active gateway. * @param array $result * Result array from the gateway response handler. */ -function sms_track_sms_send_process($op, $number, $message, $options, $gateway, $result) { +function sms_track_sms_send_process($op, SmsMessageInterface $sms, $options, GatewayInterface $gateway, $result) { + $message = $sms->getMessage(); + $number = $sms->getRecipients()[0]; if ($op == 'post process') { // Archiving (outgoing == 0) $dir = 0; $options = (isset($options) && is_array($options))? $options : array(); - $options['gateway_id'] = $gateway['identifier']; + $options['gateway_id'] = $gateway->getIdentifier(); $options['result'] = $result; sms_track_archive_write($dir, $number, $message, $options); } @@ -83,14 +87,14 @@ function sms_track_sms_send_process($op, $number, $message, $options, $gateway, * * @param string $op * SMS incoming operation string (pre process, process, post process). - * @param string $number - * MSISDN of sender. - * @param string $message - * SMS message body string. + * @param \Drupal\sms\Message\SmsMessageInterface $sms + * The sms message object. * @param array $options * Additional options including receiver MSISDN. */ -function sms_track_sms_incoming($op, $number, $message, $options) { +function sms_track_sms_incoming($op, SmsMessageInterface $sms, $options) { + $number = $sms->getRecipients()[0]; + $message = $sms->getMessage(); if ($op == 'pre process' && isset($number)) { // Archiving (incoming == 1) $dir = 1; @@ -156,10 +160,10 @@ function sms_track_archive_write($dir, $number, $message, $options = array()) { // Or render a status code from a simple true/false result if (! $status) { if ($options['result']) { - $status = SMS_GW_OK; + $status = GatewayInterface::STATUS_OK; } else { - $status = SMS_GW_ERR_OTHER; + $status = GatewayInterface::STATUS_ERR_OTHER; } } // Add the author and recipient @@ -175,7 +179,7 @@ function sms_track_archive_write($dir, $number, $message, $options = array()) { if ($archive_dir == SMS_DIR_ALL || $archive_dir == SMS_DIR_IN) { $reference = NULL; // Inbound message status is always the same. - $status = SMS_MSG_STATUS_OK; + $status = SmsMessageInterface::STATUS_OK; // Add the author and recipient $recipient = $user->id(); $author = ($remote_user) ? $remote_user : 0; diff --git a/modules/sms_user/sms_user.module b/modules/sms_user/sms_user.module index 8e077b2..67ad11e 100644 --- a/modules/sms_user/sms_user.module +++ b/modules/sms_user/sms_user.module @@ -8,6 +8,8 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; +use Drupal\sms\Gateway\GatewayInterface; +use Drupal\sms\Message\SmsMessageInterface; use Drupal\user\Entity\User; use Drupal\user\UserInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; @@ -37,7 +39,7 @@ function sms_user_send($uid, $message) { // Check if the user is enabled to receive SMS. if ($account->hasPermission('receive sms')) { if ($account->sms_user['status'] >= SMS_USER_CONFIRMED) { - return sms_send($account->sms_user['number'], $message, $account->sms_user['gateway']); + return sms_send($account->sms_user['number'], $message, (array) $account->sms_user['gateway']); } else { \Drupal::logger('sms_user')->alert("User %user has not validated mobile number", array('%user' => $account->getUsername())); @@ -97,18 +99,25 @@ function sms_user_get_uid($number, $status = NULL) { /** * Implements hook_sms_send(). + * + * Aborts sending if the user has opted out or is in sleep mode. */ -function sms_user_sms_send(&$number, &$message, &$options, &$gateway) { - if (\Drupal::config('sms_user.settings')->get('enable_sleep', 1) && $uid = sms_user_get_uid($number)) { - $account = User::load($uid); +function sms_user_sms_send_process($step, SmsMessageInterface $sms, array $options, GatewayInterface $gateway) { + if ($step == 'pre process') { + $number = $sms->getRecipients()[0]; + if (\Drupal::config('sms_user.settings')->get('enable_sleep', 1) && $uid = sms_user_get_uid($number)) { + $account = User::load($uid); - if (_sms_user_opted_out($account)) { - unset($gateway['send']); - \Drupal::logger('sms_user')->info('Message was not sent to @user because user opted out.', array('@user' => $account->getUsername())); - } - else if (_sms_user_sleep_active($account)) { - unset($gateway['send']); - \Drupal::logger('sms_user')->info('Message was not sent to @user due to sleep settings.', array('@user' => $account->getUsername())); + if (_sms_user_opted_out($account)) { + \Drupal::logger('sms_user')->info('Message was not sent to @user because user opted out.', array('@user' => $account->getUsername())); + return FALSE; + } + else { + if (_sms_user_sleep_active($account)) { + \Drupal::logger('sms_user')->info('Message was not sent to @user due to sleep settings.', array('@user' => $account->getUsername())); + return FALSE; + } + } } } } @@ -374,7 +383,7 @@ function sms_user_user_cancel($edit, $account, $method) { function sms_user_user_login(AccountInterface $account) { // Check if it's the user's first time logging in. if (!$account->getLastAccessedTime() && !empty($account->sms_user['number']) && $account->sms_user['status'] <= SMS_USER_CONFIRMED) { - sms_user_send_confirmation($account, $account->sms_user['number'], $account->sms_user['gateway']); + sms_user_send_confirmation($account, $account->sms_user['number'], (array) $account->sms_user['gateway']); drupal_set_message(t('A confirmation message has been sent to your mobile phone. Please @link.', array('@link' => l(t('confirm your number'), 'user/' . $account->id() . '/edit/mobile'))), 'status'); } } @@ -560,7 +569,9 @@ function sms_user_token_info() { /** * Implements hook_sms_incoming(). */ -function sms_user_sms_incoming($op, $number, $message, $options) { +function sms_user_sms_incoming($op, SmsMessageInterface $sms, $options) { + $number = $sms->getRecipients()[0]; + $message = $sms->getMessage(); $config = \Drupal::config('sms_user.settings'); switch ($op) { case 'pre process': diff --git a/modules/sms_user/src/Form/SettingsAddForm.php b/modules/sms_user/src/Form/SettingsAddForm.php index eef1697..e9447c2 100644 --- a/modules/sms_user/src/Form/SettingsAddForm.php +++ b/modules/sms_user/src/Form/SettingsAddForm.php @@ -55,7 +55,7 @@ class SettingsAddForm extends ConfigFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { $account = User::load($form_state->getValue('uid')); - sms_user_send_confirmation($account, $form_state->getValue('number'), $form_state->getValue('gateway')); + sms_user_send_confirmation($account, $form_state->getValue('number'), (array) $form_state->getValue('gateway')); } /** diff --git a/modules/sms_user/src/Tests/SmsUserWebTest.php b/modules/sms_user/src/Tests/SmsUserWebTest.php index 6ffac29..90edcc8 100644 --- a/modules/sms_user/src/Tests/SmsUserWebTest.php +++ b/modules/sms_user/src/Tests/SmsUserWebTest.php @@ -27,7 +27,10 @@ class SmsUserWebTest extends WebTestBase { */ public function testNumberConfirmationAndSmsUserSend() { // Set up test default gateway. - $this->config('sms.settings')->set('default_gateway', 'test')->save(); + /** @var \Drupal\sms\Gateway\GatewayManagerInterface $gateway_manager */ + $gateway_manager = $this->container->get('plugin.manager.sms_gateway'); + $gateway_manager->setEnabledGateways(['test']); + $gateway_manager->setDefaultGateway('test'); $user = $this->drupalCreateUser(array('receive sms', 'edit own sms number')); $this->drupalLogin($user); diff --git a/sms.module b/sms.module index 4db981b..14b5e6a 100644 --- a/sms.module +++ b/sms.module @@ -6,6 +6,7 @@ * sending and receiving SMS messages. */ +use Drupal\Core\Form\FormState; use Drupal\Core\Form\FormStateInterface; // Direction codes. @@ -60,7 +61,8 @@ function sms_cron_queue_info() { * @see sms_handle_result(). */ function sms_send($number, $message, $options = array()) { - $sms = new SmsMessage($options['sender'], explode(',', $number), $message, $options); + $sender = isset($options['sender']) ? $options['sender'] : ''; + $sms = new SmsMessage($sender, explode(',', $number), $message, $options); return \Drupal::service('sms_provider.default')->send($sms, $options); } @@ -93,7 +95,8 @@ function sms_incoming_queue_worker(array $item) { * An array of additional options. */ function sms_incoming($number, $message, $options = array()) { - $sms = new SmsMessage($options['sender'], explode(',', $number), $message, $options); + $sender = isset($options['sender']) ? $options['sender'] : ''; + $sms = new SmsMessage($sender, explode(',', $number), $message, $options); \Drupal::service('sms_provider.default')->incoming($sms, $options); } @@ -193,7 +196,7 @@ function sms_send_form($required = FALSE) { ); // Add gateway defined fields - $form_state = array(); + $form_state = new FormState(); $form['gateway']['#tree'] = TRUE; $form['gateway'] = array_merge($gateway->sendForm($form, $form_state), $form['gateway']); diff --git a/src/Gateway/GatewayManager.php b/src/Gateway/GatewayManager.php index 7b77bd6..105b633 100644 --- a/src/Gateway/GatewayManager.php +++ b/src/Gateway/GatewayManager.php @@ -132,8 +132,8 @@ class GatewayManager extends DefaultPluginManager implements GatewayManagerInter /** * {@inheritdoc} */ - public function setEnabledGateways(array $gateways) { - foreach ($gateways as $gateway_id) { + public function setEnabledGateways(array $gateway_ids) { + foreach ($gateway_ids as $gateway_id) { $gateway = $this->getGateway($gateway_id); $gateway->setEnabled(TRUE); $this->saveGateway($gateway); diff --git a/src/Gateway/GatewayManagerInterface.php b/src/Gateway/GatewayManagerInterface.php index 5c173ec..a127611 100644 --- a/src/Gateway/GatewayManagerInterface.php +++ b/src/Gateway/GatewayManagerInterface.php @@ -44,10 +44,10 @@ interface GatewayManagerInterface { /** * Sets the list of gateways that are enabled. * - * @param array $gateways + * @param array $gateway_ids * An array of ids of the gateways to be enabled. */ - public function setEnabledGateways(array $gateways); + public function setEnabledGateways(array $gateway_ids); /** * Sets the default sms gateway for messaging. diff --git a/src/Message/SmsMessageResult.php b/src/Message/SmsMessageResult.php index c21508a..31cdd46 100644 --- a/src/Message/SmsMessageResult.php +++ b/src/Message/SmsMessageResult.php @@ -54,6 +54,7 @@ class SmsMessageResult implements SmsMessageResultInterface { * Information to be used to instantiate the SmsMessageResult. */ public function __construct($data) { + $data += $this->defaultData(); $this->status = $data['status']; $this->creditBalance = $data['credit_balance']; $this->creditsUsed = $data['credit_used']; @@ -137,4 +138,17 @@ class SmsMessageResult implements SmsMessageResultInterface { } } + /** + * Returns default data for initializing the value object. + */ + protected function defaultData() { + return array( + 'status' => '', + 'error_message' => '', + 'credit_used' => 0, + 'credit_balance' => 0, + 'report' => array(), + ); + } + } diff --git a/src/Plugin/Gateway/LogGateway.php b/src/Plugin/Gateway/LogGateway.php index a3a8309..4e5c08d 100644 --- a/src/Plugin/Gateway/LogGateway.php +++ b/src/Plugin/Gateway/LogGateway.php @@ -8,6 +8,7 @@ namespace Drupal\sms\Plugin\Gateway; use Drupal\sms\Gateway\GatewayBase; use Drupal\sms\Message\SmsMessageInterface; +use Drupal\sms\Message\SmsMessageResult; use Symfony\Component\HttpFoundation\Request; /** @@ -26,8 +27,8 @@ class LogGateway extends GatewayBase { */ public function send(SmsMessageInterface $sms, array $options) { $this->logger()->notice('SMS message sent to %number with the text: @message', - array('%number' => implode(', ', $sms->getRecipients()), '@message' => $sms->getMessage())); -// return array('status' => TRUE); + ['%number' => implode(', ', $sms->getRecipients()), '@message' => $sms->getMessage()]); + return new SmsMessageResult(['status' => TRUE]); } diff --git a/src/Provider/DefaultSmsProvider.php b/src/Provider/DefaultSmsProvider.php index 17961c6..e1a603e 100644 --- a/src/Provider/DefaultSmsProvider.php +++ b/src/Provider/DefaultSmsProvider.php @@ -97,9 +97,9 @@ class DefaultSmsProvider implements SmsProviderInterface { */ protected function preProcess(SmsMessageInterface $sms, array $options, GatewayInterface $gateway) { // Call the send pre process hooks. - $return = $this->moduleHandler->invokeAll('sms_send_process', ['pre process', $sms, $options, $gateway]); + $return = $this->moduleHandler->invokeAll('sms_send_process', ['pre process', $sms, $options, $gateway, NULL]); // Return FALSE if any of the hooks returned FALSE. - return !in_array(FALSE, $return); + return !in_array(FALSE, $return, TRUE); } /** @@ -116,7 +116,7 @@ class DefaultSmsProvider implements SmsProviderInterface { */ protected function postProcess(SmsMessageInterface $sms, array $options, GatewayInterface $gateway, $result) { // Call the send post process hooks. - $this->moduleHandler->invokeAll('sms_send_process', ['post process', $sms, $options, $gateway]); + $this->moduleHandler->invokeAll('sms_send_process', ['post process', $sms, $options, $gateway, $result]); } /** @@ -151,15 +151,15 @@ class DefaultSmsProvider implements SmsProviderInterface { /** * {@inheritdoc} */ - public function incoming(SmsMessageInterface $message, array $options) { + public function incoming(SmsMessageInterface $sms, array $options) { // if (module_exists('rules')) { // $options += array('number' => $number, 'message' => $message); // rules_invoke_event('sms_incoming', $options); // } // Execute three phases - $this->moduleHandler->invokeAll('sms_incoming', array('pre process', $message, $options)); - $this->moduleHandler->invokeAll('sms_incoming', array('process', $message, $options)); - $this->moduleHandler->invokeAll('sms_incoming', array('post process', $message, $options)); + $this->moduleHandler->invokeAll('sms_incoming', array('pre process', $sms, $options)); + $this->moduleHandler->invokeAll('sms_incoming', array('process', $sms, $options)); + $this->moduleHandler->invokeAll('sms_incoming', array('post process', $sms, $options)); } /** diff --git a/tests/modules/sms_test_gateway/sms_test_gateway.module b/tests/modules/sms_test_gateway/sms_test_gateway.module index 1d527a8..81cf65b 100644 --- a/tests/modules/sms_test_gateway/sms_test_gateway.module +++ b/tests/modules/sms_test_gateway/sms_test_gateway.module @@ -70,14 +70,14 @@ function sms_test_gateway_result($reset = FALSE) { /** * Implements hook_sms_incoming(). */ -function sms_test_gateway_sms_incoming($op, $number, $message, $options) { +function sms_test_gateway_sms_incoming($op, SmsMessageInterface $message, $options) { // Save incoming result for later retrieval. $key = 'sms_test_gateway_get_incoming' . $op; $result = &drupal_static($key); - if (!is_null($number) && !is_null($message)) { - $result['number'] = $number; - $result['message'] = $message; + if (!is_null($message->getRecipients()) && !is_null($message->getMessage())) { + $result['number'] = $message->getRecipients()[0]; + $result['message'] = $message->getMessage(); $result['options'] = $options; \Drupal::state()->set($key, $result); }