modules/api/v1/controllers/TrialController.php
/**
     * Creates a new Trial model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
Creates a new Trial model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
Creates a new Trial model . If creation is successful the browser will be redirected to the ' view ' page . @return mixed
public function actionCreate()
    {
        $model = new Trial();
        $post = Yii::$app->request->post();
        $postData['Trial'] = Yii::$app->request->post();
        $modelProduct = Product::findOne($postData['Trial']['product_id']);
        $postData['Trial']['sender_id'] = Yii::$app->user->identity->id;
        $postData['Trial']['receiver_id'] = (!empty($modelProduct) && !empty($modelProduct->user_id)) ? $modelProduct->user_id : "";
        if ($model->load($postData) && $model->validate()) {
            if ($model->save()) {
                $getUsers[] = $model->receiver;
                if (!empty($getUsers)) {
                    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;
                                $notificationText = $model->name . "has create a request for trial of " . $modelProduct->name . " on date" . $model->date . " at " . $model->time;
                                $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 = "Add";
                                $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);
                                } 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,
                                        ]);
                                    $response = Yii::$app->fcm->send($message);
                                }
                            }
                            if ($userROW->is_click_and_try_email_notification_on == User::IS_NOTIFICATION_ON) {
                                $message = $model->name . "has create a request for trial of " . $modelProduct->name . " on date" . $model->date . " at " . $model->time;
                            }
                        }
                    }
                }
            }
        }
        return $model;
    }
public function actionCreate ( ) { $ model = new Trial ( ) ; $ post = Yii :: $ app -> request -> post ( ) ; $ postData [ 'Trial' ] = Yii :: $ app -> request -> post ( ) ; $ modelProduct = Product :: findOne ( $ postData [ 'Trial' ] [ 'product_id' ] ) ; $ postData [ 'Trial' ] [ 'sender_id' ] = Yii :: $ app -> user -> identity -> id ; $ postData [ 'Trial' ] [ 'receiver_id' ] = ( ! empty ( $ modelProduct ) && ! empty ( $ modelProduct -> user_id ) ) ? $ modelProduct -> user_id : " " ; if ( $ model -> load ( $ postData ) && $ model -> validate ( ) ) { if ( $ model -> save ( ) ) { $ getUsers [ ] = $ model -> receiver ; if ( ! empty ( $ getUsers ) ) { 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 ; $ notificationText = $ model -> name . " has create a request for trial of  " . $ modelProduct -> name . "  on date " . $ model -> date . "  at  " . $ model -> time ; $ 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 = " Add " ; $ 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 ) ; } 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 , ] ) ; $ response = Yii :: $ app -> fcm -> send ( $ message ) ; } } if ( $ userROW -> is_click_and_try_email_notification_on == User :: IS_NOTIFICATION_ON ) { $ message = $ model -> name . " has create a request for trial of  " . $ modelProduct -> name . "  on date " . $ model -> date . "  at  " . $ model -> time ; } } } } } } return $ model ; }
Creates a new Trial model.
Creates a new Trial model .
// Send Push notification and email notification start // Insert into notification. //                                if (!empty($userROW->email)) { //                                    Yii::$app->mailer->compose('api/addNewTrialBooking', ['sender' => $userROW, 'receiver' => $model->receiver, 'product' => $modelProduct, 'message' => $message, 'model' => $model]) //                                        ->setFrom([Yii::$app->params['adminEmail'] => Yii::$app->name]) //                                        ->setTo($userROW->email) //                                        ->setSubject('Request for trial of your product') //                                        ->send(); //                                } // Send Push notification and email notification start