Your IP : 216.73.216.79


Current Path : /var/www/html/sprm/backend/controllers/
Upload File :
Current File : /var/www/html/sprm/backend/controllers/LangDocController.php

<?php

namespace backend\controllers;

use Yii;
use backend\models\LangDoc;
use backend\models\LangDocSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

/**
 * LangDocController implements the CRUD actions for LangDoc model.
 */
class LangDocController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
//                    'delete' => ['POST'],
                ],
            ],
        ];
    }

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

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

    /**
     * Displays a single LangDoc model.
     * @param integer $lang_info_id
     * @param integer $doc_attach_id
     * @return mixed
     */
    public function actionView($lang_info_id, $doc_attach_id) {
        return $this->render('view', [
                    'model' => $this->findModel($lang_info_id, $doc_attach_id),
        ]);
    }

    /**
     * Creates a new LangDoc model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($lang_info_id) {
        $model = new LangDoc();
        $model->scenario = 'createLang';

        $modelAttachment = new \backend\models\Attachment();
        if ($model->load(Yii::$app->request->post())) {
            foreach ($_FILES as $cat => $file) {
                $filetype = $file['type']['file'];
            }

            $model->file = UploadedFile::getInstance($model, 'file');
            if (!empty($model->file)) {
                $path = 'uploads/langDoc/';
                $model->file->saveAs($path . date('Y_m_d_H_i_s') . '_' . $model->file->baseName . '.' . $model->file->extension);
                $modelAttachment->file_name_sys = $path . date('Y_m_d_H_i_s') . '_' . $model->file->baseName . '.' . $model->file->extension;
                $modelAttachment->file_name = $model->file->baseName . "." . $model->file->extension;
                $modelAttachment->doc_title = $_POST['LangDoc']['doc_title'];
                $modelAttachment->remarks = $_POST['LangDoc']['remarks'];
//                $modelAttachment->doctype = $filetype;
                if ($modelAttachment->save()) {
                    $model->doc_attach_id = $modelAttachment->id;
                    $model->lang_info_id = $lang_info_id;
                    if ($model->save()) {
                        Yii::$app->session->setFlash('success', Yii::t('app', 'The Language Document has been successfully added.'));
                        return $this->redirect(['lang/update', 'id' => $lang_info_id]);
                    }
                }
            } else {
                $modelAttachment->doc_title = $_POST['LangDoc']['doc_title'];
                $modelAttachment->remarks = $_POST['LangDoc']['remarks'];
                $modelAttachment->save();
                $model->lang_info_id = $lang_info_id;
                $model->save();
                Yii::$app->session->setFlash('success', Yii::t('app', 'The Language Document has been successfully added.'));
                return $this->redirect(['lang/update', 'id' => $lang_info_id]);
            }
        } else {
            return $this->render('create', [
                        'model' => $model,
                        'lang_info_id' => $lang_info_id,
            ]);
        }
    }

    /**
     * Updates an existing LangDoc model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $lang_info_id
     * @param integer $doc_attach_id
     * @return mixed
     */
    public function actionUpdate($lang_info_id, $doc_attach_id) {
        $model = $this->findModel($lang_info_id, $doc_attach_id);
        $modelAttachment = \backend\models\Attachment::findOne($model->doc_attach_id);
        $model->scenario = 'updateLang';
        if ($model->load(Yii::$app->request->post())) {
            foreach ($_FILES as $cat => $file) {
                $filetype = $file['type']['file'];
            }
            $model->file = UploadedFile::getInstance($model, 'file');
            if (!empty($model->file)) {//if ada upload file
                if (!empty($modelAttachment)) { //if ada img_src masa 1create
                    $modelAttachment = \backend\models\Attachment::findOne($model->doc_attach_id);
                } else {
                    $modelAttachment = new Attachment();
                }
                $path = 'uploads/langDoc/';
                $model->file->saveAs($path . date('Y_m_d_H_i_s') . '_' . $model->file->baseName . '.' . $model->file->extension);
                $modelAttachment->file_name_sys = $path . date('Y_m_d_H_i_s') . '_' . $model->file->baseName . '.' . $model->file->extension;
                $modelAttachment->file_name = $model->file->baseName . "." . $model->file->extension;
                $modelAttachment->doc_title = $_POST['LangDoc']['doc_title'];
                $modelAttachment->remarks = $_POST['LangDoc']['remarks'];
//                $modelAttachment->doctype = $filetype;
                if ($modelAttachment->save()) {
                    $model->doc_attach_id = $modelAttachment->id;
                    $model->lang_info_id = $lang_info_id;
                    if ($model->save()) {
                        Yii::$app->session->setFlash('success', Yii::t('app', 'The Language Document has been successfully updated.'));
                        return $this->redirect(['lang/update', 'id' => $lang_info_id]);
                    }
                }
            } else {
                $modelAttachment->doc_title = $_POST['LangDoc']['doc_title'];
                $modelAttachment->remarks = $_POST['LangDoc']['remarks'];
                $modelAttachment->save();
                $model->lang_info_id = $lang_info_id;
                $model->save();
                Yii::$app->session->setFlash('success', Yii::t('app', 'The Language Document has been successfully updated.'));
                return $this->redirect(['lang/update', 'id' => $lang_info_id]);
            }
        } else {
            return $this->render('update', [
                        'model' => $model,
                        'modelAttachment' => $modelAttachment,
                        'lang_info_id' => $lang_info_id
            ]);
        }
    }

    /**
     * Deletes an existing LangDoc model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $lang_info_id
     * @param integer $doc_attach_id
     * @return mixed
     */
    public function actionDelete($lang_info_id, $doc_attach_id) {
        $this->findModel($lang_info_id, $doc_attach_id)->delete();

        return $this->redirect(['lang/update', 'id' => $lang_info_id]);
    }

    /**
     * Finds the LangDoc model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $lang_info_id
     * @param integer $doc_attach_id
     * @return LangDoc the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($lang_info_id, $doc_attach_id) {
        if (($model = LangDoc::findOne(['lang_info_id' => $lang_info_id, 'doc_attach_id' => $doc_attach_id])) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}