Your IP : 216.73.216.79


Current Path : /var/www/html/akept/backend/modules/contentCalendar/controllers/
Upload File :
Current File : /var/www/html/akept/backend/modules/contentCalendar/controllers/ContentCalendarController.php

<?php

namespace backend\modules\contentCalendar\controllers;

use Yii;
use backend\modules\contentCalendar\models\ContentCalendar;
use backend\modules\contentCalendar\models\ContentCalendarTranslation;
use backend\modules\contentCalendar\models\ContentCalendarSearch;
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\helpers\Url;

/**
 * ContentCalendarController implements the CRUD actions for ContentCalendar model.
 */
class ContentCalendarController extends Controller {

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

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

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

    /**
     * Displays a single ContentCalendar 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 ContentCalendar model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new ContentCalendar();
        $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->calendar_language = 'Malay';
        $model->calendar_countdown_status = 0;
        if ($model->load(Yii::$app->request->post())) {

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

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

            if ($model->save()) {

                $modeltranslation = new ContentCalendarTranslation();
                $modeltranslation->calendar_translation_parent_id = $model->calendar_id;
                $modeltranslation->calendar_translation_title = $model->calendar_title;
                $modeltranslation->calendar_translation_content = $model->calendar_content;
                $modeltranslation->calendar_translation_location = $model->calendar_location;
                $modeltranslation->calendar_translation_language = $model->calendar_language;
                $modeltranslation->calendar_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:'Berjaya',
                    icon: 'success', 
                    text: 'Data telah disimpan', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('create', [
                    'model' => $model,
        ]);
    }

    /**
     * Updates an existing ContentCalendar 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);
        $model->updated_at = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $original_calendar_thumbnail = $model->calendar_thumbnail;
        $translation_id = '';

        if (($modeltranslation = ContentCalendarTranslation::findOne(['calendar_translation_parent_id' => $model->calendar_id, 'calendar_translation_main' => 1])) !== null) {
            $translation_id = $modeltranslation->calendar_translation_id;
            $model->calendar_title = $modeltranslation->calendar_translation_title;
            $model->calendar_content = $modeltranslation->calendar_translation_content;
            $model->calendar_location = $modeltranslation->calendar_translation_location;
            $model->calendar_language = $modeltranslation->calendar_translation_language;
        }

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

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

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

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

            if ($model->save()) {
                if (($modeltranslation = ContentCalendarTranslation::findOne($translation_id)) !== null) {
                    $modeltranslation->calendar_translation_title = $model->calendar_title;
                    $modeltranslation->calendar_translation_content = $model->calendar_content;
                    $modeltranslation->calendar_translation_location = $model->calendar_location;
                    $modeltranslation->calendar_translation_language = $model->calendar_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 ContentCalendarTranslation();
                        $modeltranslationnew->calendar_translation_parent_id = $model->calendar_id;
                        $modeltranslationnew->calendar_translation_title = $_POST['new_title_' . $newno];
                        $modeltranslationnew->calendar_translation_content = $_POST['new_content_' . $newno];
                        $modeltranslationnew->calendar_translation_location = $_POST['new_location_' . $newno];
                        $modeltranslationnew->calendar_translation_language = $_POST['new_language_' . $newno];
                        $modeltranslationnew->calendar_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 = ContentCalendarTranslation::findOne($oldtranslation)) !== null) {
                            $modeltranslationupdate->calendar_translation_title = $_POST['old_title_' . $oldtranslation];
                            $modeltranslationupdate->calendar_translation_content = $_POST['old_content_' . $oldtranslation];
                            $modeltranslationupdate->calendar_translation_location = $_POST['old_location_' . $oldtranslation];
                            $modeltranslationupdate->calendar_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:'Berjaya',
                    icon: 'success', 
                    text: 'Data telah disimpan', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

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

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

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

    public function actionAddtranslation($id) {
        $modeltranslation = new ContentCalendarTranslation();
        $modeltranslation->calendar_translation_parent_id = $id;
        $modeltranslation->calendar_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 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 Calendar 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>Event Location</label><input type="text" class="form-control" name="new_location_' . $no . '"></div></div>
      <div class="col-sm-12"><div class="form-group"><label>Event Name</label><input type="text" class="form-control" name="new_title_' . $no . '"></div></div>
      </div>
      <div class="form-group">
      <label>Event Description</label>
      <textarea class="summernote" name="new_content_' . $no . '"></textarea>
      </div>
      </div>
      </div>';
      return $output;
      } */

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

    public function actionChangecategory($cat) {
        $arr = [];
        $output = '';

        if ($cat == 'bahagian') {
            $arr = ArrayHelper::map((new \yii\db\Query())->from('ref')
                            ->where(['=', 'cat', 'DEPT'])
                            ->orderBy(['descr_en' => SORT_ASC])
                            ->all(), 'code', 'descr_en');
        }

        if ($cat == 'staff') {
            $arr = ArrayHelper::map((new \yii\db\Query())->from('frontend_user')->orderBy(['fullname' => SORT_ASC])->all(), 'id', 'fullname');
        }

        if (!empty($arr)) {
            $output = '<div class="form-group field-contentcalendar-calendar_owner_id">
                <label class="control-label" for="contentcalendar-calendar_owner_id">Owner</label>
                ' . Html::dropDownList('ContentCalendar[calendar_owner_id]', null, $arr, ['class' => 'select2', 'prompt' => '-- Please choose --']) . '
            </div>';
        }

        return $output;
    }
}