modules/api/v2/controllers/FavouriteProductController.php
/**
     * Creates a new SearchHistory model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
Creates a new SearchHistory model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
Creates a new SearchHistory model . If creation is successful the browser will be redirected to the ' view ' page . @return mixed
public function actionCreate()
    {
        $model = new FavouriteProduct();
        $postData = Yii::$app->request->post();
        $favouriteProduct['FavouriteProduct'] = $postData;
        $favouriteProduct['FavouriteProduct']['user_id'] = Yii::$app->user->identity->id;
        $modelFavourite = FavouriteProduct::find()->where(['product_id' => $favouriteProduct['FavouriteProduct']['product_id'], 'user_id' => Yii::$app->user->identity->id])->one();
        if (!empty($modelFavourite)) {
            throw new BadRequestHttpException('This product has been already favourited!');
        }
        if ($model->load($favouriteProduct) && $model->validate()) {
            if ($model->save()) {
                $getUsers[] = $model->product->user;
                if (!empty($getUsers)) {
                    foreach ($getUsers as $keys => $userROW) {
                        if ($userROW instanceof User && ($model->user_id != $userROW->id)) {
                            if (!empty($userROW->userDevice)) {
                                $userDevice = $userROW->userDevice;
                                $notificationText = Yii::$app->user->identity->first_name . " " . Yii::$app->user->identity->last_name . " has liked your product";
                                $modelNotification = new Notification();
                                $modelNotification->owner_id = $model->user_id;
                                $modelNotification->notification_receiver_id = $userROW->id;
                                $modelNotification->ref_id = $model->id;
                                $modelNotification->notification_text = $notificationText;
                                $modelNotification->action = "Add";
                                $modelNotification->ref_type = "product_favourite"; 
                                $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);
                                }
                            }
                        }
                    }
                }
            }
        }
        return $model;
    }
public function actionCreate ( ) { $ model = new FavouriteProduct ( ) ; $ postData = Yii :: $ app -> request -> post ( ) ; $ favouriteProduct [ 'FavouriteProduct' ] = $ postData ; $ favouriteProduct [ 'FavouriteProduct' ] [ 'user_id' ] = Yii :: $ app -> user -> identity -> id ; $ modelFavourite = FavouriteProduct :: find ( ) -> where ( [ 'product_id' => $ favouriteProduct [ 'FavouriteProduct' ] [ 'product_id' ] , 'user_id' => Yii :: $ app -> user -> identity -> id ] ) -> one ( ) ; if ( ! empty ( $ modelFavourite ) ) { throw new BadRequestHttpException ( 'This product has been already favourited!' ) ; } if ( $ model -> load ( $ favouriteProduct ) && $ model -> validate ( ) ) { if ( $ model -> save ( ) ) { $ getUsers [ ] = $ model -> product -> user ; if ( ! empty ( $ getUsers ) ) { foreach ( $ getUsers as $ keys => $ userROW ) { if ( $ userROW instanceof User && ( $ model -> user_id != $ userROW -> id ) ) { if ( ! empty ( $ userROW -> userDevice ) ) { $ userDevice = $ userROW -> userDevice ; $ notificationText = Yii :: $ app -> user -> identity -> first_name . " " . Yii :: $ app -> user -> identity -> last_name . "  has liked your product " ; $ modelNotification = new Notification ( ) ; $ modelNotification -> owner_id = $ model -> user_id ; $ modelNotification -> notification_receiver_id = $ userROW -> id ; $ modelNotification -> ref_id = $ model -> id ; $ modelNotification -> notification_text = $ notificationText ; $ modelNotification -> action = " Add " ; $ modelNotification -> ref_type = " product_favourite " ; $ 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 ) ; } } } } } } } return $ model ; }
Creates a new SearchHistory model.
Creates a new SearchHistory model .
// Send Push notification start // Insert into notification. // For Product favourite // Send Push notification end