From 06e9a40ef6bf54f876511644f05d68a21d066662 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Fri, 4 Nov 2016 11:37:03 +0900 Subject: [PATCH] Implement splitting long message into multiple SMS or LMS --- common/framework/drivers/sms/coolsms.php | 51 ++++++++++++++-- common/framework/sms.php | 78 +++++++++++++++++++++++- 2 files changed, 121 insertions(+), 8 deletions(-) diff --git a/common/framework/drivers/sms/coolsms.php b/common/framework/drivers/sms/coolsms.php index c528f22f1..058cc52a9 100644 --- a/common/framework/drivers/sms/coolsms.php +++ b/common/framework/drivers/sms/coolsms.php @@ -49,6 +49,10 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface { try { + // Initialize the sender. + $sender = new \Nurigo\Api\Message($this->_config['api_key'], $this->_config['api_secret']); + + // Get recipients. $recipients = $message->getRecipientsWithCountry(); foreach ($recipients as $recipient) { @@ -56,11 +60,12 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface $options = new \stdClass; $options->from = $message->getFrom(); $options->to = $recipient->number; - $options->text = $message->getContent(); $options->charset = 'UTF-8'; + $content_full = $message->getContent(); // Determine the message type based on the length. - $options->type = $message->checkLength($options->text, $this->_maxlength_sms) ? 'SMS' : 'LMS'; + $detected_type = $message->checkLength($content_full, $this->_maxlength_sms) ? 'SMS' : 'LMS'; + $options->type = $detected_type; // If the message has a subject, it must be an LMS. if ($subject = $message->getSubject()) @@ -82,15 +87,49 @@ class CoolSMS extends Base implements \Rhymix\Framework\Drivers\SMSInterface { unset($options->subject); unset($options->image); - $options->text = array_first($message->splitMessage($options->text, $this->_maxlength_sms)); + $options->country = $recipient->country; $options->type = 'SMS'; } + // Split the message if necessary. + if ($options->type === 'SMS' && $detected_type !== 'SMS') + { + $content_split = $message->splitMessage($content_full, $this->_maxlength_sms); + } + elseif ($options->type !== 'SMS' && !$message->checkLength($content_full, $this->_maxlength_lms)) + { + $content_split = $message->splitMessage($content_full, $this->_maxlength_lms); + } + else + { + $content_split = array($content_full); + } + // Send the message. - $sender = new \Nurigo\Api\Message($this->_config['api_key'], $this->_config['api_secret']); - $result = $sender->send($options); - return (isset($result->success_count) && $result->success_count > 0) ? true : false; + $sent_once = false; + foreach ($content_split as $i => $content) + { + // If splitting a message, don't send the subject and image more than once. + if ($sent_once) + { + unset($options->subject); + unset($options->image); + } + + // Set the content and send. + $options->text = $content; + $result = $sender->send($options); + $sent_once = true; + + if (!$result->success_count) + { + $message->errors[] = 'Error while sending message ' . $i . ' of ' . count($content_split) . ' to ' . $options->to; + return false; + } + } } + + return true; } catch (\Nurigo\Exceptions\CoolsmsException $e) { diff --git a/common/framework/sms.php b/common/framework/sms.php index 9aa302286..407466f7c 100644 --- a/common/framework/sms.php +++ b/common/framework/sms.php @@ -18,6 +18,8 @@ class SMS protected $content = ''; protected $attachments = array(); protected $force_sms = false; + protected $allow_split_sms = true; + protected $allow_split_lms = true; public $errors = array(); protected $sent = false; @@ -108,6 +110,8 @@ class SMS public function __construct() { $this->driver = self::getDefaultDriver(); + $this->allow_split_sms = (config('sms.allow_split.sms') !== false); + $this->allow_split_lms = (config('sms.allow_split.lms') !== false); } /** @@ -304,17 +308,87 @@ class SMS } /** - * Check if this message is forced to use SMS. + * Unforce this message to use SMS (not LMS or MMS). * * @return void */ + public function unforceSMS() + { + $this->force_sms = false; + } + + /** + * Check if this message is forced to use SMS. + * + * @return bool + */ public function isForceSMS() { return $this->force_sms; } /** - * Send the email. + * Allow this message to be split into multiple SMS. + * + * @return void + */ + public function allowSplitSMS() + { + $this->allow_split_sms = true; + } + + /** + * Allow this message to be split into multiple LMS. + * + * @return void + */ + public function allowSplitLMS() + { + $this->allow_split_lms = true; + } + + /** + * Disallow this message to be split into multiple SMS. + * + * @return void + */ + public function disallowSplitSMS() + { + $this->allow_split_sms = false; + } + + /** + * Disallow this message to be split into multiple LMS. + * + * @return void + */ + public function disallowSplitLMS() + { + $this->allow_split_lms = false; + } + + /** + * Check if splitting this message into multiple SMS is allowed. + * + * @return bool + */ + public function isSplitSMSAllowed() + { + return $this->allow_split_sms; + } + + /** + * Check if splitting this message into multiple LMS is allowed. + * + * @return bool + */ + public function isSplitLMSAllowed() + { + return $this->allow_split_lms; + } + + /** + * Send the message. * * @return bool */