Your IP : 216.73.216.79


Current Path : /var/www/html/mnrb/backend/modules/contentVideo/controllers/
Upload File :
Current File : /var/www/html/mnrb/backend/modules/contentVideo/controllers/ContentVideoController.php

<?php

namespace backend\modules\contentVideo\controllers;

use Yii;
use backend\modules\contentVideo\models\ContentVideo;
use backend\modules\contentVideo\models\ContentVideoTranslation;
use backend\modules\contentVideo\models\ContentVideoSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\filters\AccessControl;
use yii\helpers\Url;

/**
 * ContentVideoController implements the CRUD actions for ContentVideo model.
 */
class ContentVideoController extends Controller {

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

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

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

    /**
     * Displays a single ContentVideo 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 ContentVideo model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new ContentVideo();
        $model->created_at = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->video_language = 'Malay';
        $model->video_code = (new \yii\db\Query)->select(new \yii\db\Expression('UUID()'))->scalar();

        if ($model->load(Yii::$app->request->post())) {
            if (!empty($_POST['ContentVideo']['publish_time_start'])) {
                $model->publish_time_start = $_POST['ContentVideo']['publish_time_start'] . ":00";
            }

            if (!empty($_POST['ContentVideo']['publish_time_end'])) {
                $model->publish_time_end = $_POST['ContentVideo']['publish_time_end'] . ":00";
            }

            $model->meta_img = UploadedFile::getInstance($model, 'meta_img');
            if (!empty($model->meta_img)) {
                $meta_img = 'uploads/metadata/' . date("YmdHis") . '.' . $model->meta_img->extension;
                if ($model->meta_img->saveAs('../' . $meta_img))
                    $model->meta_img = $meta_img;
            }

            $model->video_img = UploadedFile::getInstance($model, 'video_img');
            if (!empty($model->video_img)) {
                $video_img = 'uploads/thumbnail_' . date("YmdHis") . '.' . $model->video_img->extension;
                if ($model->video_img->saveAs('../' . $video_img))
                    $model->video_img = $video_img;
            }


            $model->video_upload = UploadedFile::getInstance($model, 'video_upload');
            if (!empty($model->video_upload)) {
                $video_upload = 'uploads/video_' . date("YmdHis") . '.' . $model->video_upload->extension;
                if ($model->video_upload->saveAs('../' . $video_upload))
                    $model->video_upload = $video_upload;
            }

            if (!empty($model->video_page))
                $model->video_page = json_encode($model->video_page);

            if ($model->save()) {
                $modeltranslation = new ContentVideoTranslation();
                $modeltranslation->video_translation_parent_id = $model->video_id;
                $modeltranslation->video_translation_title = $model->video_title;
                $modeltranslation->video_translation_language = $model->video_language;
                $modeltranslation->video_translation_main = 1;
                $modeltranslation->created_at = date("Y-m-d H:i:s");
                $modeltranslation->created_by = Yii::$app->user->identity->id;
                $modeltranslation->updated_at = date("Y-m-d H:i:s");
                $modeltranslation->updated_by = Yii::$app->user->identity->id;
                $modeltranslation->save();

                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('create', [
                    'model' => $model,
        ]);
    }

    /**
     * Updates an existing ContentVideo 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);
        $original_meta_img = $model->meta_img;
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $original_thumbnail = $model->video_img;
        $original_video = $model->video_upload;
        $publish_time_start = $model->publish_time_start;
        $publish_time_end = $model->publish_time_end;
        $translation_id = '';

        if (!empty($model->video_page))
            $model->video_page = json_decode($model->video_page);

        if (($modeltranslation = ContentVideoTranslation::findOne(['video_translation_parent_id' => $model->video_id, 'video_translation_main' => 1])) !== null) {
            $translation_id = $modeltranslation->video_translation_id;
            $model->video_title = $modeltranslation->video_translation_title;
            $model->video_language = $modeltranslation->video_translation_language;
        }

        if ($model->load(Yii::$app->request->post())) {

            $model->meta_img = UploadedFile::getInstance($model, 'meta_img');
            if (!empty($model->meta_img)) {
                $meta_img = 'uploads/metadata/' . date("YmdHis") . '.' . $model->meta_img->extension;
                if ($model->meta_img->saveAs('../' . $meta_img))
                    $model->meta_img = $meta_img;
            } else {
                $model->meta_img = $original_meta_img;
            }

            $model->video_img = UploadedFile::getInstance($model, 'video_img');
            if (!empty($model->video_img)) {
                $video_img = 'uploads/thumbnail_' . date("YmdHis") . '.' . $model->video_img->extension;
                if ($model->video_img->saveAs('../' . $video_img))
                    $model->video_img = $video_img;
            } else {
                $model->video_img = $original_thumbnail;
            }

            $model->video_upload = UploadedFile::getInstance($model, 'video_upload');
            if (!empty($model->video_upload)) {
                $video_upload = 'uploads/video_' . date("YmdHis") . '.' . $model->video_upload->extension;
                if ($model->video_upload->saveAs('../' . $video_upload))
                    $model->video_upload = $video_upload;
            } else {
                $model->video_upload = $original_video;
            }

            if (!empty($_POST['ContentVideo']['publish_time_start'])) {
                $model->publish_time_start = $_POST['ContentVideo']['publish_time_start'] . ":00";
            } else {
                $model->publish_time_start = $publish_time_start;
            }

            if (!empty($_POST['ContentVideo']['publish_time_end'])) {
                $model->publish_time_end = $_POST['ContentVideo']['publish_time_end'] . ":00";
            } else {
                $model->publish_time_end = $publish_time_end;
            }

            if (!empty($model->video_page))
                $model->video_page = json_encode($model->video_page);

            if ($model->save()) {
                if (($modeltranslation = ContentVideoTranslation::findOne($translation_id)) !== null) {
                    $modeltranslation->video_translation_title = $model->video_title;
                    $modeltranslation->video_translation_language = $model->video_language;
                    $modeltranslation->updated_at = date("Y-m-d H:i:s");
                    $modeltranslation->updated_by = Yii::$app->user->identity->id;
                    $modeltranslation->save();
                }

                //insert new translation
                if (isset($_POST['newtranslation']) && !empty($_POST['newtranslation'])) {
                    foreach ($_POST['newtranslation'] as $newno) {
                        $modeltranslationnew = new ContentVideoTranslation();
                        $modeltranslationnew->video_translation_parent_id = $model->video_id;
                        $modeltranslationnew->video_translation_title = $_POST['new_title_' . $newno];
                        $modeltranslationnew->video_translation_language = $_POST['new_language_' . $newno];
                        $modeltranslationnew->video_translation_main = 0;
                        $modeltranslationnew->created_at = date("Y-m-d H:i:s");
                        $modeltranslationnew->created_by = Yii::$app->user->identity->id;
                        $modeltranslationnew->updated_at = date("Y-m-d H:i:s");
                        $modeltranslationnew->updated_by = Yii::$app->user->identity->id;
                        $modeltranslationnew->save();
                    }
                }

                //update translation
                if (isset($_POST['oldtranslation']) && !empty($_POST['oldtranslation'])) {
                    foreach ($_POST['oldtranslation'] as $oldtranslation) {
                        if (($modeltranslationupdate = ContentVideoTranslation::findOne($oldtranslation)) !== null) {
                            $modeltranslationupdate->video_translation_title = $_POST['old_title_' . $oldtranslation];
                            $modeltranslationupdate->video_translation_language = $_POST['old_language_' . $oldtranslation];
                            $modeltranslationupdate->updated_at = date("Y-m-d H:i:s");
                            $modeltranslationupdate->updated_by = Yii::$app->user->identity->id;
                            $modeltranslationupdate->save();
                        }
                    }
                }

                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing ContentVideo 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 = $_POST['id'];
        $status = 'failed';
        ContentVideoTranslation::deleteAll(['video_translation_parent_id' => $id]);
        if ($this->findModel($id)->delete())
            $status = 'success';
        return $status;
    }

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

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

    /* public function actionAddtranslation($no = '') {
      $output = '<div class="ibox float-e-margins" id="new-translation-' . $no . '">
      <input type="hidden" name="newtranslation[]" value="' . $no . '">
      <div class="ibox-title">
      <button type="button" class="btn btn-primary btn-sm" onclick="deletenewtranslation(' . $no . ')">Delete</button>&nbsp;&nbsp;&nbsp;
      New Video Translation
      </div>
      <div class="ibox-content">
      <div class="row">
      <div class="col-sm-3">
      <div class="form-group">
      <label>Language</label>
      ' . Html::dropDownList('new_language_' . $no, NULL, ArrayHelper::map(
      (new \yii\db\Query())->select(['code', 'descr'])
      ->from('ref')
      ->where(['=', 'cat', 'CONTENT_LANGUAGE'])
      ->orderBy(['sort' => SORT_ASC])
      ->all(), 'code', 'descr'), [
      'class' => 'form-control',]) . '
      </div>
      </div>
      <div class="col-sm-9"><div class="form-group"><label>Title</label><input type="text" class="form-control" name="new_title_' . $no . '"></div></div>
      </div>
      </div>
      </div>';
      return $output;
      } */

    public function actionAddtranslation($id) {
        $modeltranslation = new ContentVideoTranslation();
        $modeltranslation->video_translation_parent_id = $id;
        $modeltranslation->video_translation_main = 0;
        $modeltranslation->created_at = date("Y-m-d H:i:s");
        $modeltranslation->created_by = Yii::$app->user->identity->id;
        $modeltranslation->updated_at = date("Y-m-d H:i:s");
        $modeltranslation->updated_by = Yii::$app->user->identity->id;
        if ($modeltranslation->save(false))
            return $this->redirect(['update', 'id' => $id]);
    }

    public function actionDeletetranslation($id) {
        if (($model = ContentVideoTranslation::findOne($id)) !== null) {
            $model->delete();
        }
    }

    public function actionSorting($data = '') {
        $count = 0;
        $data_arr = explode(',', $data);
        if (!empty($data_arr)) {
            foreach ($data_arr as $slider_id) {
                $model = ContentVideo::findOne($slider_id);
                if (!empty($model)) {
                    $count++;
                    $model->video_sorting = $count;
                    $model->save(false);
                }
            }
        }

        return $data;
    }

}