Exemple #1
0
 /**
  * Send broadcast message
  *
  * @throws Mage_Core_Exception
  * @param Mage_XmlConnect_Model_Queue $queue
  */
 public function sendBroadcastMessage(Mage_XmlConnect_Model_Queue $queue)
 {
     if ($queue->getStatus() != Mage_XmlConnect_Model_Queue::STATUS_IN_QUEUE) {
         return;
     }
     try {
         $applicationId = Mage::getModel('xmlconnect/template')->load($queue->getTemplateId())->getApplicationId();
         /** @var $app Mage_XmlConnect_Model_Application */
         $app = Mage::getModel('xmlconnect/application')->load($applicationId);
         if (!$app->getId()) {
             Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t load application with id "%s"', $applicationId));
         }
         if (!$app->isNotificationsActive()) {
             $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_CANCELED);
             return;
         }
         $sendType = $queue->getData('type');
         switch ($sendType) {
             case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL:
                 $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL . '/broadcast_url';
                 $params = $queue->getAirmailBroadcastParams();
                 break;
             case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH:
             default:
                 $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH . '/broadcast_url';
                 $params = $queue->getPushBroadcastParams();
                 break;
         }
         $curl = new Varien_Http_Adapter_Curl();
         $curl->setConfig($this->_getCurlConfig($app->getUserpwd()));
         $urbanUrl = Mage::getStoreConfig($configPath);
         $curl->write(Zend_Http_Client::POST, $urbanUrl, HTTP_REQUEST_HTTP_VER_1_1, $this->getHttpHeaders(), $params);
         if ($curl->read() && $curl->getInfo(CURLINFO_HTTP_CODE) == 200) {
             $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_COMPLETED);
         }
         $curl->close();
         $queue->setIsSent(true);
         $queue->save();
         return;
     } catch (Exception $e) {
         Mage::logException($e);
         throw $e;
     }
 }
Exemple #2
0
 /**
  * Send broadcast message
  *
  * @throws Mage_Core_Exception
  * @param Mage_XmlConnect_Model_Queue $queue
  */
 public function sendBroadcastMessage(Mage_XmlConnect_Model_Queue $queue)
 {
     if ($queue->getStatus() != Mage_XmlConnect_Model_Queue::STATUS_IN_QUEUE) {
         return;
     }
     try {
         /** @var $app Mage_XmlConnect_Model_Application */
         $templateModel = Mage::getModel('xmlconnect/template')->load($queue->getTemplateId());
         if (!$templateModel->getId()) {
             Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t load template with id "%s"', $templateModel->getId()));
         }
         $app = Mage::getModel('xmlconnect/application')->load($templateModel->getApplicationId());
         if (!$app->getId()) {
             Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t load application with id "%s"', $templateModel->getApplicationId()));
         }
         if (!$app->isNotificationsActive()) {
             $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_CANCELED);
             return;
         }
         $sendType = $queue->getData('type');
         switch ($sendType) {
             case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL:
                 $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL . '/broadcast_url';
                 $params = $queue->getAirmailBroadcastParams();
                 break;
             case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH:
             default:
                 $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH . '/broadcast_url';
                 $params = $queue->getPushBroadcastParams();
                 break;
         }
         $curlHandler = curl_init(Mage::getStoreConfig($configPath));
         $httpHeaders = $this->getHttpHeaders();
         curl_setopt($curlHandler, CURLOPT_POST, 1);
         curl_setopt($curlHandler, CURLOPT_HTTPHEADER, $httpHeaders);
         curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $params);
         curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($curlHandler, CURLOPT_USERPWD, $app->getUserpwd());
         curl_setopt($curlHandler, CURLOPT_TIMEOUT, 60);
         // Execute the request.
         $result = curl_exec($curlHandler);
         $succeeded = curl_errno($curlHandler) == 0;
         $responseCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE);
         // close cURL resource, and free up system resources
         curl_close($curlHandler);
         if ($succeeded && $responseCode == 200) {
             $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_COMPLETED);
         } else {
             Mage::log($result);
             $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_CANCELED);
         }
         $queue->setIsSent(true);
         $queue->save();
         return;
     } catch (Exception $e) {
         Mage::logException($e);
         throw $e;
     }
 }
 /**
  * Get queue message action params
  *
  * @param Mage_XmlConnect_Model_Queue $message
  * @return array
  */
 protected function _getQueueMessageParams(Mage_XmlConnect_Model_Queue $message)
 {
     $redirectParams = array();
     if ($message && $message->getId()) {
         $redirectParams['id'] = $message->getId();
     } else {
         $redirectParams['template_id'] = (int) $this->getRequest()->getParam('template_id');
     }
     return $redirectParams;
 }
Exemple #4
0
 /**
  * Send broadcast message
  *
  * @throws Mage_Core_Exception
  * @param Mage_XmlConnect_Model_Queue $queue
  */
 public function sendBroadcastMessage(Mage_XmlConnect_Model_Queue $queue)
 {
     if ($queue->getStatus() != Mage_XmlConnect_Model_Queue::STATUS_IN_QUEUE) {
         return;
     }
     try {
         $appCode = $queue->getAppCode();
         $app = Mage::getModel('xmlconnect/application')->load($appCode, 'code');
         if (!$app->getId()) {
             Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t load application with code "%s"', $appCode));
         }
         $userpwd = $app->getUserpwd();
         $sendType = $queue->getData('type');
         switch ($sendType) {
             case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL:
                 $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL . '/broadcast_url';
                 $broadcastUrl = Mage::getStoreConfig($configPath);
                 $params = $queue->getAirmailBroadcastParams();
                 break;
             case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH:
             default:
                 $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH . '/broadcast_url';
                 $broadcastUrl = Mage::getStoreConfig($configPath);
                 $params = $queue->getPushBroadcastParams();
                 break;
         }
         $ch = curl_init($broadcastUrl);
         $httpHeaders = $this->getHttpHeaders();
         curl_setopt($ch, CURLOPT_POST, 1);
         curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
         curl_setopt($ch, CURLOPT_TIMEOUT, 60);
         // Execute the request.
         $result = curl_exec($ch);
         $succeeded = curl_errno($ch) == 0 ? true : false;
         // close cURL resource, and free up system resources
         curl_close($ch);
         if ($succeeded && (is_null($result) || strtolower($result) == 'null')) {
             $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_COMPLETED);
         }
         $queue->setIsSent(true);
         return;
     } catch (Exception $e) {
         Mage::logException($e);
         throw $e;
     }
 }