Your IP : 216.73.217.154


Current Path : /var/www/html/chatbot/backend/controllers/application/
Upload File :
Current File : /var/www/html/chatbot/backend/controllers/application/AiDocuploadController.php

<?php

namespace backend\controllers\application;

use Yii;
use backend\models\application\AiDocupload;
use backend\models\application\AiDocuploadSearch;
use backend\models\application\AiApplication;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\UploadedFile;
use yii\filters\VerbFilter;

/**
 * AiDocuploadController implements the CRUD actions for AiDocupload model.
 */
class AiDocuploadController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all AiDocupload models.
     * @return mixed
     */
    public function actionIndex($id)
    {
        $searchModel = new AiDocuploadSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $id);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'id' => $id,
        ]);
    }

    /**
     * Displays a single AiDocupload model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new AiDocupload model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($id)
    {
        $model = new AiDocupload();
        $mapps = AiApplication::findOne($id);
        
        if ($model->load(Yii::$app->request->post())) {
            $path = '../documents';
            $model->file = UploadedFile::getInstance($model, 'file');
            if (!empty($model->file)) {
                if (!is_dir($path)) {
                    mkdir($path, 0777);
                }
                if (!empty($model->aidu_filename)) {
                    unlink($path . '/' . $model->aidu_filename);
                }
                
//                'DLv-'.$tarikhnow1.'|'.$uuid.'|'.$lampiranid.'.'.getFileExtension($_FILES['file']['name']);
                $model->file->saveAs($path . '/' . 'DLv-' . date('YmdHis') . '|' . $mapps->aiap_uuid . '|' .$model->file->baseName . '.' . $model->file->extension);
                $model->aidu_filename = 'DLv-' . date('YmdHis') . '|' . $mapps->aiap_uuid . '|' .$model->file->baseName . '.' . $model->file->extension;
                $model->aidu_fileupload = $model->file->baseName . '.' . $model->file->extension;
            }
            $model->aidu_applicationid = $id;
            $model->aidu_uuid = $mapps->aiap_uuid;
            if ($model->save()) {
                Yii::$app->session->setFlash('success', Yii::$app->params['successCreate']);
                return $this->redirect(Yii::$app->request->referrer);
            }
        }

        return $this->renderAjax('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing AiDocupload 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 = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            
            $path = '../documents';
            $model->file = UploadedFile::getInstance($model, 'file');
            if (!empty($model->file)) {
                if (!is_dir($path)) {
                    mkdir($path, 0777);
                }
                if (!empty($model->aidu_filename)) {
                    unlink($path . '/' . $model->aidu_filename);
                }
                
//                'DLv-'.$tarikhnow1.'|'.$uuid.'|'.$lampiranid.'.'.getFileExtension($_FILES['file']['name']);
                $model->file->saveAs($path . '/' . 'DLv-' . date('YmdHis') . '|' . $mapps->aiap_uuid . '|' .$model->file->baseName . '.' . $model->file->extension);
                $model->aidu_filename = $path . '/' . 'DLv-' . date('YmdHis') . '|' . $mapps->aiap_uuid . '|' .$model->file->baseName . '.' . $model->file->extension;
                $model->aidu_fileupload = $model->file->baseName . '.' . $model->file->extension;
            }
            if ($model->save()) {
                Yii::$app->session->setFlash('success', Yii::$app->params['successUpdate']);
                return $this->redirect(Yii::$app->request->referrer);
            }
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing AiDocupload model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        Yii::$app->session->setFlash('success', Yii::$app->params['successDelete']);
        return $this->redirect(Yii::$app->request->referrer);
    }

    /**
     * Finds the AiDocupload model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return AiDocupload the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = AiDocupload::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}