Exemple #1
0
 /**
  * Send  SMS
  */
 public static function send()
 {
     $recipients = SmsHandler::$model->recipients;
     $settings = Yii::app()->settings->get(Constants::CATEGORY_SMS, array(Constants::KEY_SMS_AFRICASTALKING_USERNAME, Constants::KEY_SMS_AFRICASTALKING_API_KEY, Constants::KEY_SMS_AFRICASTALKING_SHORTCORD));
     if (!is_array($recipients)) {
         $recipients = explode(',', $recipients);
     }
     if (empty(SmsHandler::$model->from)) {
         SmsHandler::$model->from = $settings[Constants::KEY_SMS_AFRICASTALKING_SHORTCORD];
     }
     $gateway = new AfricaStalkingGateway($settings[Constants::KEY_SMS_AFRICASTALKING_USERNAME], $settings[Constants::KEY_SMS_AFRICASTALKING_API_KEY]);
     $recipients_count = count($recipients);
     $sent_count = 0;
     SmsHandler::$model->batch_id = MsgSmsOutbox::model()->getNextIntegerID('batch_id');
     $success_count = 0;
     $chunks = SmsHandler::getChunks($recipients, self::MAX_CHUNK_LENGTH, $recipients_count);
     $currency = Yii::app()->settings->get(Constants::CATEGORY_GENERAL, Constants::KEY_CURRENCY, SettingsCurrency::CURRENCY_KES);
     $org_model = !empty(SmsHandler::$model->org_id) ? Org::model()->loadModel(SmsHandler::$model->org_id) : NULL;
     $price_per_sms = NULL !== $org_model ? $org_model->price_per_sms : NULL;
     foreach ($chunks as $contacts) {
         $error = FALSE;
         $status_message = NULL;
         if (SmsHandler::$model->recipients_source === MsgSmsOutbox::RECIPIENTS_SOURCE_DEFAULT) {
             $gsm_numbers = SmsHandler::preparePhoneNumbers($contacts, FALSE);
         } else {
             $gsm_numbers = implode(",", $contacts);
         }
         $results = $gateway->sendMessage($gsm_numbers, SmsHandler::$model->message, SmsHandler::$model->from);
         if (empty($results)) {
             $status_message = "Oops, No messages were sent. ErrorMessage: " . $gateway->getErrorMessage();
             $error = TRUE;
         }
         if (!empty(SmsHandler::$queue_id)) {
             if ($error === FALSE) {
                 $sent_count = $sent_count + count($contacts);
                 $status_message = Lang::t("Processed {$sent_count} of {$recipients_count} messages");
             }
             ConsoleTaskQueue::model()->updateProgress(SmsHandler::$queue_id, $status_message);
             //update status
         } else {
             if ($error === TRUE && NULL !== $status_message) {
                 Yii::log($status_message, CLogger::LEVEL_ERROR);
             }
         }
         if ($error === FALSE) {
             $success_count += self::saveRecipients(SmsHandler::$model, $results, $currency, $price_per_sms);
         }
         sleep(1);
     }
     if (NULL !== $org_model) {
         SmsHandler::addBilling($org_model, SmsHandler::$model, $success_count);
     }
     if (!empty(SmsHandler::$queue_id)) {
         ConsoleTaskQueue::model()->updateProgress(SmsHandler::$queue_id, Lang::t('Completed.'), ConsoleTaskQueue::STATUS_COMPLETED);
     }
 }