modules/api/v2/controllers/TrialController.php
/**
     * Updates an existing Trial model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
Updates an existing Trial model.
If update is successful, the browser will be redirected to the 'view' page.
@param integer $id
@return mixed
@throws NotFoundHttpException if the model cannot be found
Updates an existing Trial model . If update is successful the browser will be redirected to the ' view ' page . @param integer $id @return mixed @throws NotFoundHttpException if the model cannot be found
public function actionUpdate($id)
    {
        $model = Trial::findOne($id);
        if (!$model instanceof Trial) {
            throw new NotFoundHttpException('Trial doesn\'t exist.');
        }
        $model->scenario = Trial::SCENARIO_ACCEPT_REJECT;
        $postData = Yii::$app->request->post();
        $trialPostData['Trial'] = $postData;
        $modelProduct = Product::findOne($model->product_id);
        if ($model->load($trialPostData) && $model->validate()) {
            if ($model->save(false)) {
                $getUtcTime = $this->getUTCTimeBasedOnTimeZone($model->timezone_id, $model->time);
                if (!empty($getUtcTime)) {
                    $model->timezone_utc_time = $getUtcTime;
                }
                if ($model->status == Trial::STATUS_ACCEPT) {
                    $action = "accept_trial";
                    $notificationText = $modelProduct->name . "'s" . " seller has accepted your request for trial of " . $modelProduct->name . " on date " . $model->date . " at " . $model->time;
                } elseif ($model->status == Trial::STATUS_REJECT) {
                    $notificationText = $modelProduct->name . "'s" . " seller has rejected your request for trial of " . $modelProduct->name . " on date " . $model->date . " at " . $model->time;
                    $action = "reject_trial";
                } else {
                    $notificationText = $action = "";
                }
                $getUsers[] = $model->sender;
                if (!empty($getUsers) && !empty($notificationText)) {
                    foreach ($getUsers as $userROW) {
                        if ($userROW instanceof User && (Yii::$app->user->identity->id != $userROW->id)) {
                            if ($userROW->is_click_and_try_notification_on == User::IS_NOTIFICATION_ON && !empty($userROW->userDevice)) {
                                $userDevice = $userROW->userDevice;
                                if (!empty($userDevice) && !empty($userDevice->notification_token)) {
                                    $modelNotification = new Notification();
                                    $modelNotification->owner_id = Yii::$app->user->identity->id;
                                    $modelNotification->notification_receiver_id = $userROW->id;
                                    $modelNotification->ref_id = $model->id;
                                    $modelNotification->notification_text = $notificationText;
                                    $modelNotification->action = $action;
                                    $modelNotification->ref_type = "trial_book";
                                    $modelNotification->save(false);
                                    $badge = Notification::find()->where(['notification_receiver_id' => $userROW->id, 'is_read' => Notification::NOTIFICATION_IS_READ_NO])->count();
                                    if ($userDevice->device_platform == 'android') {
                                        $notificationToken = array($userDevice->notification_token);
                                        $senderName = Yii::$app->user->identity->first_name . " " . Yii::$app->user->identity->last_name;
                                        $modelNotification->sendPushNotificationAndroid($modelNotification->ref_id, $modelNotification->ref_type, $notificationToken, $notificationText, $senderName, $modelNotification);
                                    } else {
                                        $note = Yii::$app->fcm->createNotification(Yii::$app->name, $notificationText);
                                        $note->setBadge($badge);
                                        $note->setSound('default');
                                        $message = Yii::$app->fcm->createMessage();
                                        $message->addRecipient(new \paragraph1\phpFCM\Recipient\Device($userDevice->notification_token));
                                        $message->setNotification($note)
                                            ->setData([
                                                'id' => $modelNotification->ref_id,
                                                'type' => $modelNotification->ref_type,
                                                'message' => $notificationText,
                                                'action' => (!empty($modelNotification) && !empty($modelNotification->action)) ? $modelNotification->action : "",
                                            ]);
                                        $response = Yii::$app->fcm->send($message);
                                    }
                                }
                            }
                            if ($userROW->is_click_and_try_email_notification_on == User::IS_NOTIFICATION_ON) {
                                $message = $notificationText;
                                if (($model->status == Trial::STATUS_ACCEPT)) {
                                    $isAccept = 'accepted by seller';
                                } else {
                                    $isAccept = 'rejected by seller';
                                }
                                if (!empty($userROW->email)) {
                                    try {
                                        Yii::$app->mailer->compose('api/trialBookingAcceptReject', ['sender' => Yii::$app->user->identity, 'receiver' => $userROW, 'product' => $modelProduct, 'message' => $message, 'model' => $model])
                                            ->setFrom([Yii::$app->params['adminEmail'] => Yii::$app->name])
                                            ->setTo($userROW->email)
                                            ->setSubject('Request for trial has ' . $isAccept)
                                            ->send();
                                    } catch (httpException $e) {
                                        echo "Error: " . $e->getMessage();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $model = Trial::findOne($id);
        return $model;
    }
public function actionUpdate ( $ id ) { $ model = Trial :: findOne ( $ id ) ; if ( ! $ model instanceof Trial ) { throw new NotFoundHttpException ( 'Trial doesn\'t exist.' ) ; } $ model -> scenario = Trial :: SCENARIO_ACCEPT_REJECT ; $ postData = Yii :: $ app -> request -> post ( ) ; $ trialPostData [ 'Trial' ] = $ postData ; $ modelProduct = Product :: findOne ( $ model -> product_id ) ; if ( $ model -> load ( $ trialPostData ) && $ model -> validate ( ) ) { if ( $ model -> save ( false ) ) { $ getUtcTime = $ this -> getUTCTimeBasedOnTimeZone ( $ model -> timezone_id , $ model -> time ) ; if ( ! empty ( $ getUtcTime ) ) { $ model -> timezone_utc_time = $ getUtcTime ; } if ( $ model -> status == Trial :: STATUS_ACCEPT ) { $ action = " accept_trial " ; $ notificationText = $ modelProduct -> name . " 's " . "  seller has accepted your request for trial of  " . $ modelProduct -> name . "  on date  " . $ model -> date . "  at  " . $ model -> time ; } elseif ( $ model -> status == Trial :: STATUS_REJECT ) { $ notificationText = $ modelProduct -> name . " 's " . "  seller has rejected your request for trial of  " . $ modelProduct -> name . "  on date  " . $ model -> date . "  at  " . $ model -> time ; $ action = " reject_trial " ; } else { $ notificationText = $ action = " " ; } $ getUsers [ ] = $ model -> sender ; if ( ! empty ( $ getUsers ) && ! empty ( $ notificationText ) ) { foreach ( $ getUsers as $ userROW ) { if ( $ userROW instanceof User && ( Yii :: $ app -> user -> identity -> id != $ userROW -> id ) ) { if ( $ userROW -> is_click_and_try_notification_on == User :: IS_NOTIFICATION_ON && ! empty ( $ userROW -> userDevice ) ) { $ userDevice = $ userROW -> userDevice ; if ( ! empty ( $ userDevice ) && ! empty ( $ userDevice -> notification_token ) ) { $ modelNotification = new Notification ( ) ; $ modelNotification -> owner_id = Yii :: $ app -> user -> identity -> id ; $ modelNotification -> notification_receiver_id = $ userROW -> id ; $ modelNotification -> ref_id = $ model -> id ; $ modelNotification -> notification_text = $ notificationText ; $ modelNotification -> action = $ action ; $ modelNotification -> ref_type = " trial_book " ; $ modelNotification -> save ( false ) ; $ badge = Notification :: find ( ) -> where ( [ 'notification_receiver_id' => $ userROW -> id , 'is_read' => Notification :: NOTIFICATION_IS_READ_NO ] ) -> count ( ) ; if ( $ userDevice -> device_platform == 'android' ) { $ notificationToken = array ( $ userDevice -> notification_token ) ; $ senderName = Yii :: $ app -> user -> identity -> first_name . " " . Yii :: $ app -> user -> identity -> last_name ; $ modelNotification -> sendPushNotificationAndroid ( $ modelNotification -> ref_id , $ modelNotification -> ref_type , $ notificationToken , $ notificationText , $ senderName , $ modelNotification ) ; } else { $ note = Yii :: $ app -> fcm -> createNotification ( Yii :: $ app -> name , $ notificationText ) ; $ note -> setBadge ( $ badge ) ; $ note -> setSound ( 'default' ) ; $ message = Yii :: $ app -> fcm -> createMessage ( ) ; $ message -> addRecipient ( new \ paragraph1 \ phpFCM \ Recipient \ Device ( $ userDevice -> notification_token ) ) ; $ message -> setNotification ( $ note ) -> setData ( [ 'id' => $ modelNotification -> ref_id , 'type' => $ modelNotification -> ref_type , 'message' => $ notificationText , 'action' => ( ! empty ( $ modelNotification ) && ! empty ( $ modelNotification -> action ) ) ? $ modelNotification -> action : " " , ] ) ; $ response = Yii :: $ app -> fcm -> send ( $ message ) ; } } } if ( $ userROW -> is_click_and_try_email_notification_on == User :: IS_NOTIFICATION_ON ) { $ message = $ notificationText ; if ( ( $ model -> status == Trial :: STATUS_ACCEPT ) ) { $ isAccept = 'accepted by seller' ; } else { $ isAccept = 'rejected by seller' ; } if ( ! empty ( $ userROW -> email ) ) { try { Yii :: $ app -> mailer -> compose ( 'api/trialBookingAcceptReject' , [ 'sender' => Yii :: $ app -> user -> identity , 'receiver' => $ userROW , 'product' => $ modelProduct , 'message' => $ message , 'model' => $ model ] ) -> setFrom ( [ Yii :: $ app -> params [ 'adminEmail' ] => Yii :: $ app -> name ] ) -> setTo ( $ userROW -> email ) -> setSubject ( 'Request for trial has ' . $ isAccept ) -> send ( ) ; } catch ( httpException $ e ) { echo " Error:  " . $ e -> getMessage ( ) ; } } } } } } } } $ model = Trial :: findOne ( $ id ) ; return $ model ; }
Updates an existing Trial model.
Updates an existing Trial model .
// Send Push notification and email notification start // Insert into notification. // Send Push notification and email notification end