modules/api/v1/controllers/ChatHistoryController.php
/**
     * Creates a new ChatHistory model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
Creates a new ChatHistory model.
If creation is successful, the browser will be redirected to the 'view' page.
@return mixed
Creates a new ChatHistory model . If creation is successful the browser will be redirected to the ' view ' page . @return mixed
public function actionCreate()
    {
        $model = new ChatHistory();
        $post = Yii::$app->request->post();
        $postChatHistory['ChatHistory'] = $post;
        $postChatHistory['ChatHistory']['from_user_id'] = Yii::$app->user->identity->id;
        $imgFile = UploadedFile::getInstanceByName('file');
        $postChatHistory['ChatHistory']['file'] = $imgFile;
        if ($model->load($postChatHistory) && $model->validate()) {
            if (!empty($imgFile) && in_array($model->message_type, [ChatHistory::MESSAGE_TYPE_IMAGE, ChatHistory::MESSAGE_TYPE_VIDEO])) {
                $uploadDirPath = Yii::getAlias('@chatMediaRelativePath');
                $uploadThumbDirPath = Yii::getAlias('@chatMediaThumbRelativePath');
                if ($imgFile instanceof UploadedFile) {
                    if (!is_dir($uploadDirPath)) {
                        mkdir($uploadDirPath, 0777);
                    }
                    if (!is_dir($uploadThumbDirPath)) {
                        mkdir($uploadThumbDirPath, 0777);
                    }
                    $ext = $imgFile->extension;
                    $fileName = pathinfo(str_replace(" ", "", $imgFile->name), PATHINFO_FILENAME);
                    $fileName = $fileName . '_' . time() . '.' . $ext;
                    $imgFile->saveAs($uploadDirPath . '/' . $fileName);
                    $actualImagePath = $uploadDirPath . '/' . $fileName;
                    $thumbImagePath = $uploadThumbDirPath . '/' . $fileName;
                    Image::thumbnail($actualImagePath, Yii::$app->params['profile_picture_thumb_width'], Yii::$app->params['profile_picture_thumb_height'])->save($thumbImagePath, ['quality' => Yii::$app->params['profile_picture_thumb_quality']]);
                    $model->message = $fileName;
                }
            }
            $model->save(false);
            $getUsers[] = $model->toUser;
            if (!empty($getUsers)) {
                foreach ($getUsers as $userROW) {
                    if ($userROW instanceof User && ($model->from_user_id != $userROW->id)) {
                        if ($userROW->is_new_message_notification_on == User::IS_NOTIFICATION_ON && !empty($userROW->userDevice)) {
                            $userDevice = $userROW->userDevice;
                            $notificationText = $model->message;
                            $modelNotification = new Notification();
                            $modelNotification->owner_id = $model->from_user_id;
                            $modelNotification->notification_receiver_id = $userROW->id;
                            $modelNotification->ref_id = $model->id;
                            $modelNotification->notification_text = $notificationText;
                            $modelNotification->action = "Add";
                            $modelNotification->ref_type = "chat_history";
                            $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 = $model->fromUser->first_name . " " . $model->fromUser->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_new_message_email_notification_on == User::IS_NOTIFICATION_ON) {
                            $message = $model->fromUser->first_name . " " . $model->fromUser->last_name . " Send new message\n" . $model->message;
                        }
                    }
                }
            }
            if (!empty($model) && !empty($model->message_type) && in_array($model->message_type, [ChatHistory::MESSAGE_TYPE_IMAGE, ChatHistory::MESSAGE_TYPE_VIDEO])) {
                $imgFile = Yii::$app->request->getHostInfo() . Yii::getAlias('@uploadsAbsolutePath') . '/no-image.jpg';
                if (!empty($model->message) && file_exists(Yii::getAlias('@chatMediaThumbRelativePath') . '/' . $model->message)) {
                    $imgFile = Yii::$app->request->getHostInfo() . Yii::getAlias('@chatMediaThumbAbsolutePath') . '/' . $model->message;
                }
                $model->message = $imgFile;
            } else {
                $model->message = (!empty($model) && !empty($model->message)) ? $model->message : "";
            }
        }
        return $model;
    }
public function actionCreate ( ) { $ model = new ChatHistory ( ) ; $ post = Yii :: $ app -> request -> post ( ) ; $ postChatHistory [ 'ChatHistory' ] = $ post ; $ postChatHistory [ 'ChatHistory' ] [ 'from_user_id' ] = Yii :: $ app -> user -> identity -> id ; $ imgFile = UploadedFile :: getInstanceByName ( 'file' ) ; $ postChatHistory [ 'ChatHistory' ] [ 'file' ] = $ imgFile ; if ( $ model -> load ( $ postChatHistory ) && $ model -> validate ( ) ) { if ( ! empty ( $ imgFile ) && in_array ( $ model -> message_type , [ ChatHistory :: MESSAGE_TYPE_IMAGE , ChatHistory :: MESSAGE_TYPE_VIDEO ] ) ) { $ uploadDirPath = Yii :: getAlias ( '@chatMediaRelativePath' ) ; $ uploadThumbDirPath = Yii :: getAlias ( '@chatMediaThumbRelativePath' ) ; if ( $ imgFile instanceof UploadedFile ) { if ( ! is_dir ( $ uploadDirPath ) ) { mkdir ( $ uploadDirPath , 0777 ) ; } if ( ! is_dir ( $ uploadThumbDirPath ) ) { mkdir ( $ uploadThumbDirPath , 0777 ) ; } $ ext = $ imgFile -> extension ; $ fileName = pathinfo ( str_replace ( " " , " " , $ imgFile -> name ) , PATHINFO_FILENAME ) ; $ fileName = $ fileName . '_' . time ( ) . '.' . $ ext ; $ imgFile -> saveAs ( $ uploadDirPath . '/' . $ fileName ) ; $ actualImagePath = $ uploadDirPath . '/' . $ fileName ; $ thumbImagePath = $ uploadThumbDirPath . '/' . $ fileName ; Image :: thumbnail ( $ actualImagePath , Yii :: $ app -> params [ 'profile_picture_thumb_width' ] , Yii :: $ app -> params [ 'profile_picture_thumb_height' ] ) -> save ( $ thumbImagePath , [ 'quality' => Yii :: $ app -> params [ 'profile_picture_thumb_quality' ] ] ) ; $ model -> message = $ fileName ; } } $ model -> save ( false ) ; $ getUsers [ ] = $ model -> toUser ; if ( ! empty ( $ getUsers ) ) { foreach ( $ getUsers as $ userROW ) { if ( $ userROW instanceof User && ( $ model -> from_user_id != $ userROW -> id ) ) { if ( $ userROW -> is_new_message_notification_on == User :: IS_NOTIFICATION_ON && ! empty ( $ userROW -> userDevice ) ) { $ userDevice = $ userROW -> userDevice ; $ notificationText = $ model -> message ; $ modelNotification = new Notification ( ) ; $ modelNotification -> owner_id = $ model -> from_user_id ; $ modelNotification -> notification_receiver_id = $ userROW -> id ; $ modelNotification -> ref_id = $ model -> id ; $ modelNotification -> notification_text = $ notificationText ; $ modelNotification -> action = " Add " ; $ modelNotification -> ref_type = " chat_history " ; $ 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 = $ model -> fromUser -> first_name . " " . $ model -> fromUser -> 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_new_message_email_notification_on == User :: IS_NOTIFICATION_ON ) { $ message = $ model -> fromUser -> first_name . " " . $ model -> fromUser -> last_name . "  Send new message \n " . $ model -> message ; } } } } if ( ! empty ( $ model ) && ! empty ( $ model -> message_type ) && in_array ( $ model -> message_type , [ ChatHistory :: MESSAGE_TYPE_IMAGE , ChatHistory :: MESSAGE_TYPE_VIDEO ] ) ) { $ imgFile = Yii :: $ app -> request -> getHostInfo ( ) . Yii :: getAlias ( '@uploadsAbsolutePath' ) . '/no-image.jpg' ; if ( ! empty ( $ model -> message ) && file_exists ( Yii :: getAlias ( '@chatMediaThumbRelativePath' ) . '/' . $ model -> message ) ) { $ imgFile = Yii :: $ app -> request -> getHostInfo ( ) . Yii :: getAlias ( '@chatMediaThumbAbsolutePath' ) . '/' . $ model -> message ; } $ model -> message = $ imgFile ; } else { $ model -> message = ( ! empty ( $ model ) && ! empty ( $ model -> message ) ) ? $ model -> message : " " ; } } return $ model ; }
Creates a new ChatHistory model.
Creates a new ChatHistory model .
// file upload // Create profile upload directory if not exist // Create profile thumb upload directory if not exist // Upload file //chmod($uploadDirPath . '/' . $fileName, 0777); // Create thumb of file //chmod($thumbImagePath, 0777); // Insert file name into database // Send Push notification start // Insert into notification. //$notificationText = $userROW->first_name . " " . $userROW->last_name . " has sent new message"; //$modelNotification->created_at = time(); //                            if (!empty($userROW->email)) { //                                Yii::$app->mailer->compose('api/addSendNewMessage', ['sender' => $model->fromUser, 'receiver' => $userROW, 'message' => $message]) //                                    ->setFrom([Yii::$app->params['adminEmail'] => Yii::$app->name]) //                                    ->setTo($userROW->email) //                                    ->setSubject('Send new message') //                                    ->send(); //                            } // Send Push notification end