Your IP : 216.73.216.79


Current Path : /var/www/html/dosm/backend/controllers/
Upload File :
Current File : /var/www/html/dosm/backend/controllers/ContentContactusController.php

<?php

namespace backend\controllers;

use Yii;
use backend\models\ContentContactus;
use backend\models\ContentContactusTranslation;
use backend\models\ContentContactusSearch;
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 backend\modules\refdynawebv2\models\Ref;

/**
 * ContentcontactusController implements the CRUD actions for Contentcontactus model.
 */
class ContentContactusController extends Controller {

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

    /**
     * Lists all Contentcontactus models.
     * @return mixed
     */
    public function actionIndex($category = '') {
        $searchModel = new ContentContactusSearch();
        $searchModel->contactus_category = $category;
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]);
    }

    /**
     * Displays a single Contentcontactus 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 Contentcontactus model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($category = '') {
        $model = new ContentContactus();
        $model->contactus_category = $category;
        $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->contactus_language = 'English';
        $model->contactus_code = (new \yii\db\Query)->select(new \yii\db\Expression('UUID()'))->scalar();
        if ($model->load(Yii::$app->request->post())) {
            $model->contactus_image = UploadedFile::getInstance($model, 'contactus_image');
            if (!empty($model->contactus_image)) {
                $contactus_image = 'images/modules/contentcontactus_' . date("YmdHis") . '.' . $model->contactus_image->extension;
                if ($model->contactus_image->saveAs($contactus_image))
                    $model->contactus_image = $contactus_image;
            }
            if (!empty($model->contactus_portal_category))
                $model->contactus_portal_category = json_encode($model->contactus_portal_category);
            if ($model->save()) {

                $modeltranslation = new ContentContactusTranslation();
                $modeltranslation->contactus_translation_parent_id = $model->contactus_id;
                $modeltranslation->contactus_translation_title = $model->contactus_title;
                $modeltranslation->contactus_translation_content = $model->contactus_content;
                $modeltranslation->contactus_translation_language = $model->contactus_language;
                $modeltranslation->contactus_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();

                if (empty($category))
                    return $this->redirect(['index']);
                else
                    return $this->redirect(['index', 'category' => $category]);
            }
        }
        return $this->render('create', [
                    'model' => $model,
        ]);
    }

    /**
     * Updates an existing Contentcontactus 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, $category = '') {
        $model = $this->findModel($id);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $original_thumbnail = $model->contactus_image;
        if (!empty($model->contactus_portal_category))
            $model->contactus_portal_category = json_decode($model->contactus_portal_category);
        $translation_id = '';

        if (($modeltranslation = ContentContactusTranslation::findOne(['contactus_translation_parent_id' => $model->contactus_id, 'contactus_translation_main' => 1])) !== null) {
            $translation_id = $modeltranslation->contactus_translation_id;
            $model->contactus_title = $modeltranslation->contactus_translation_title;
            $model->contactus_content = $modeltranslation->contactus_translation_content;
            $model->contactus_language = $modeltranslation->contactus_translation_language;
        }

        if ($model->load(Yii::$app->request->post())) {
            $model->contactus_image = UploadedFile::getInstance($model, 'contactus_image');
            if (!empty($model->contactus_image)) {
                $contactus_image = 'images/modules/contentcontactus_' . date("YmdHis") . '.' . $model->contactus_image->extension;
                if ($model->contactus_image->saveAs($contactus_image))
                    $model->contactus_image = $contactus_image;
            } else
                $model->contactus_image = $original_thumbnail;
            if (!empty($model->contactus_portal_category))
                $model->contactus_portal_category = json_encode($model->contactus_portal_category);
            if ($model->save()) {
                if (($modeltranslation = ContentContactusTranslation::findOne($translation_id)) !== null) {
                    $modeltranslation->contactus_translation_title = $model->contactus_title;
                    $modeltranslation->contactus_translation_content = $model->contactus_content;
                    $modeltranslation->contactus_translation_language = $model->contactus_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 ContentContactusTranslation();
                        $modeltranslationnew->contactus_translation_parent_id = $model->contactus_id;
                        $modeltranslationnew->contactus_translation_title = $_POST['new_title_' . $newno];
                        $modeltranslationnew->contactus_translation_content = $_POST['new_content_' . $newno];
                        $modeltranslationnew->contactus_translation_language = $_POST['new_language_' . $newno];
                        $modeltranslationnew->contactus_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 = ContentContactusTranslation::findOne($oldtranslation)) !== null) {
                            $modeltranslationupdate->contactus_translation_title = $_POST['old_title_' . $oldtranslation];
                            $modeltranslationupdate->contactus_translation_content = $_POST['old_content_' . $oldtranslation];
                            $modeltranslationupdate->contactus_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();
                        }
                    }
                }

                if (empty($category))
                    return $this->redirect(['index']);
                else
                    return $this->redirect(['index', 'category' => $category]);
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing Contentcontactus 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, $category = '') {
        ContentContactusTranslation::deleteAll(['contactus_translation_parent_id' => $id]);
        $this->findModel($id)->delete();
        if (empty($category))
            return $this->redirect(['index']);
        else
            return $this->redirect(['index', 'category' => $category]);
    }

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

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

    public function actionGetsubcategory($parent_code = '') {
        $output = '<option value="">-- Please choose --</option>';
        if (($model2 = Ref::findOne(['code' => $parent_code, 'cat' => 'ARTICLECATEGORY'])) !== null) {
            $model = Ref::find()->where(['param1' => $model2->id])->orderBy(['descr_en' => SORT_ASC])->all();
            if (!empty($model)) {
                foreach ($model as $row) {
                    $output .= '<option value="' . $row->code . '">' . $row->descr_en . '</option>';
                }
            }
        }
        return $output;
    }

    public function actionAddtranslation($id, $category = '') {
        $modeltranslation = new ContentContactusTranslation();
        $modeltranslation->contactus_translation_parent_id = $id;
        $modeltranslation->contactus_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)) {
            if (empty($category))
                return $this->redirect(['update', 'id' => $id]);
            else
                return $this->redirect(['update', 'id' => $id, 'category' => $category]);
        }
    }

    /* 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 contactus 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 class="form-group">
      <label>Content</label>
      <textarea class="summernote" name="new_content_' . $no . '"></textarea>
      </div>
      </div>
      </div>';
      return $output;
      } */

    public function actionDeletetranslation($id) {
        if (($model = ContentContactusTranslation::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 = ContentContactus::findOne($slider_id);
                if (!empty($model)) {
                    $count++;
                    $model->contactus_sorting = $count;
                    $model->save(false);
                }
            }
        }

        return $data;
    }

}