Ejemplo n.º 1
0
 public function actionUpdateEmailNotification($id)
 {
     $user = $this->_loadUser($id);
     $newValues = array();
     if (!empty($_POST['value'])) {
         $newValues = (array) $_POST['value'];
     }
     $oldValues = UserNotification::model()->findAll('user_id=:user_id', array(':user_id' => $user->id));
     if (!empty($oldValues)) {
         foreach ($oldValues as $key => $value) {
             if (false !== ($key_ar = array_search($value->notification_id, $newValues))) {
                 // get key of $newValues array if value = id
                 $value->by_email = true;
                 $value->save(false);
                 unset($newValues[$key_ar]);
             } else {
                 $value->by_email = false;
                 $value->save(false);
             }
         }
     }
     if (!empty($newValues)) {
         foreach ($newValues as $value) {
             $userNotification = new UserNotification();
             $userNotification->user_id = $user->id;
             $userNotification->notification_id = (int) $value;
             $userNotification->by_email = true;
             if (!$userNotification->save()) {
                 header('HTTP 400 Bad Request', true, 400);
                 echo CJSON::encode($userNotification->errors);
                 Yii::app()->end();
             }
         }
     }
     echo '[]';
 }
Ejemplo n.º 2
0
    /**
     * @inheritdoc
     */
    public function send($message, $category = null, $object_pk = null, $subject_pk = null, $notification = null)
    {
        

        $by_email = false; $notifications = null;

        $subscriptions = NfyDbSubscription::model()->current()->withQueue($this->id)->matchingCategory($category)->findAll();

        if($message == null && !empty($notification)){
            
            $notifications = Notifications::model()->findByPk($notification);

            if($notifications){
                $message = $notifications->body;
            }

            if(empty($message)){
                $success = false;
            } else {
                $queueMessage = $this->createNotification($message, $notifications->id, $object_pk, $subject_pk);
                
            }

            
        } else {
            $queueMessage = $this->createMessage($message, $object_pk, $subject_pk);
        }

        if ($this->beforeSend($queueMessage) !== true) {
            Yii::log(Yii::t('NfyModule.app', "Not sending message '{msg}' to queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_INFO, 'nfy');
            return;
        }

        $success = true;

        

        $trx = $queueMessage->getDbConnection()->getCurrentTransaction() !== null ? null : $queueMessage->getDbConnection()->beginTransaction();

       
        if (!$queueMessage->save()) {
            Yii::log(Yii::t('NfyModule.app', "Failed to save message '{msg}' in queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_ERROR, 'nfy');
            return false;
        }
        
        if(!empty($subscriptions)){
            foreach ($subscriptions as $subscription) {
                $simple = true;
                $subscriptionMessage = clone $queueMessage;
                $subscriptionMessage->subscription_id = $subscription->id;
                $subscriptionMessage->message_id = $queueMessage->id;
                if ($this->beforeSendSubscription($subscriptionMessage, $subscription->subscriber_id) !== true) {
                    continue;
                }
                if($notifications){
                    $user_notification = UserNotification::model()->find('user_id=:user_id and notification_id=:notification_id',array(':user_id'=>$subscription->subscriber_id,':notification_id'=>$notification));
                
                    if($user_notification && $user_notification->by_email == true){
                        if(!$this->sendMessageByEmail($queueMessage, $subscription)) {
                           Yii::log(Yii::t('NfyModule.app', "Failed to send message '{msg}' by email in queue {queue_label} for the subscription {subscription_id}.", array(
                                '{msg}' => $queueMessage->body,
                                '{queue_label}' => $this->label,
                                '{subscription_id}' => $subscription->id,
                            )), CLogger::LEVEL_ERROR, 'nfy');
                            $success = false;  
                        }
                    }

                    if($notifications->available == false){ // not send simple notification
                        $simple = false;
                    }
                    
                } 
                if ($simple == false) {
                    continue;
                }
                
                if (!$subscriptionMessage->save()) {
                    Yii::log(Yii::t('NfyModule.app', "Failed to save message '{msg}' in queue {queue_label} for the subscription {subscription_id}.", array(
                        '{msg}' => $queueMessage->body,
                        '{queue_label}' => $this->label,
                        '{subscription_id}' => $subscription->id,
                    )), CLogger::LEVEL_ERROR, 'nfy');
                    $success = false;
                }

                $this->afterSendSubscription($subscriptionMessage, $subscription->subscriber_id);
            }
        }

        $this->afterSend($queueMessage);

        if ($trx !== null) {
            $trx->commit();
        }

        Yii::log(Yii::t('NfyModule.app', "Sent message '{msg}' to queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_INFO, 'nfy');

        return $success;
    }