Your IP : 216.73.216.79


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

<?php

namespace backend\controllers;

use Yii;
use backend\models\ContentMylocalstats;
use backend\models\ContentMylocalstatsSearch;
use backend\models\ContentMylocalstatsTranslation;
use backend\models\ContentMylocalstatsTranslationSearch;
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 backend\modules\fileArchive\models\FileArchive;

error_reporting(0);

/**
 * ContentPhotoGalleryController implements the CRUD actions for ContentPhotoGallery model.
 */
class ContentMylocalstatsController extends Controller {

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

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

    /**
     * Displays a single ContentPhotoGallery 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 ContentPhotoGallery model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new ContentMylocalstats();
        //$model->scenario = 'create';
        $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->mylocalstats_main = 1;
        $model->mylocalstats_code = (new \yii\db\Query)->select(new \yii\db\Expression('UUID()'))->scalar();
        $model->mylocalstats_status = 'active';
        $model->mylocalstats_language = 'English';
        if ($model->load(Yii::$app->request->post())) {
            $model->mylocalstats_thumbnail = UploadedFile::getInstance($model, 'mylocalstats_thumbnail');
            if (!empty($model->mylocalstats_thumbnail)) {
                $mylocalstats_thumbnail = 'images/icon/contentMylocalstatsThumbnail_' . date("YmdHis") . '.' . $model->mylocalstats_thumbnail->extension;
                if ($model->mylocalstats_thumbnail->saveAs($mylocalstats_thumbnail))
                    $model->mylocalstats_thumbnail = $mylocalstats_thumbnail;
            }
            if (empty($_POST['ContentMylocalstats']['embargo_start_date'])) {
                $model->embargo_start_date = NULL;
            } else {
                $model->embargo_start_date = $_POST['ContentMylocalstats']['embargo_start_date'];
            }
            if (empty($_POST['ContentMylocalstats']['embargo_start_time'])) {
                $model->embargo_start_time = NULL;
            } else {
                $model->embargo_start_time = $_POST['ContentMylocalstats']['embargo_start_time'];
            }
            if (empty($_POST['ContentMylocalstats']['embargo_end_date'])) {
                $model->embargo_end_date = NULL;
            } else {
                $model->embargo_end_date = $_POST['ContentMylocalstats']['embargo_end_date'];
            }
            if (empty($_POST['ContentMylocalstats']['embargo_end_time'])) {
                $model->embargo_end_time = NULL;
            } else {
                $model->embargo_end_time = $_POST['ContentMylocalstats']['embargo_end_time'];
            }
            if (!empty($model->mylocalstats_portal))
                $model->mylocalstats_portal = json_encode($model->mylocalstats_portal);
            if ($model->save()) {
                $modeltranslation = new ContentMylocalstatsTranslation();
                $modeltranslation->mylocalstats_translation_parent_id = $model->mylocalstats_id;
                $modeltranslation->mylocalstats_translation_title = $model->mylocalstats_title;
                $modeltranslation->mylocalstats_translation_language = $model->mylocalstats_language;
                $modeltranslation->mylocalstats_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();
                return $this->redirect(['index']);
            }
        }
        return $this->render('create', ['model' => $model,]);
    }

    /**
     * Updates an existing ContentPhotoGallery 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_mylocalstats_thumbnail = $model->mylocalstats_thumbnail;
        $mylocalstats_document_upload = $model->mylocalstats_document_upload;
        if (!empty($model->mylocalstats_portal))
            $model->mylocalstats_portal = json_decode($model->mylocalstats_portal);
        if ($model->load(Yii::$app->request->post())) {
            $model->mylocalstats_thumbnail = UploadedFile::getInstance($model, 'mylocalstats_thumbnail');
            if (!empty($model->mylocalstats_thumbnail)) {
                $mylocalstats_thumbnail = 'images/icon/contentMylocalstatsThumbnail_' . date("YmdHis") . '.' . $model->mylocalstats_thumbnail->extension;
                if ($model->mylocalstats_thumbnail->saveAs($mylocalstats_thumbnail)) {
                    $model->mylocalstats_thumbnail = $mylocalstats_thumbnail;
                    if (!empty($original_mylocalstats_thumbnail)) {
                        $modelArchive = new FileArchive;
                        $modelArchive->file_source = 'MyLocalStats';
                        if (($model2 = ContentMylocalstatsTranslation::findOne(['mylocalstats_translation_parent_id' => $id, 'mylocalstats_translation_main' => 1])) !== null) {
                            $modelArchive->file_name = $model2->mylocalstats_translation_title;
                        }
                        $modelArchive->file_path = 'admin/' . $original_mylocalstats_thumbnail;
                        $modelArchive->file_updated_at = date("Y-m-d H:i:s");
                        $modelArchive->file_updated_by = Yii::$app->user->identity->id;
                        $modelArchive->save(false);
                    }
                }
            } else {
                $model->mylocalstats_thumbnail = $original_mylocalstats_thumbnail;
            }
            if (empty($_POST['ContentMylocalstats']['embargo_start_date'])) {
                $model->embargo_start_date = NULL;
            } else {
                $model->embargo_start_date = $_POST['ContentMylocalstats']['embargo_start_date'];
            }
            if (empty($_POST['ContentMylocalstats']['embargo_start_time'])) {
                $model->embargo_start_time = NULL;
            } else {
                $model->embargo_start_time = $_POST['ContentMylocalstats']['embargo_start_time'];
            }
            if (empty($_POST['ContentMylocalstats']['embargo_end_date'])) {
                $model->embargo_end_date = NULL;
            } else {
                $model->embargo_end_date = $_POST['ContentMylocalstats']['embargo_end_date'];
            }
            if (empty($_POST['ContentMylocalstats']['embargo_end_time'])) {
                $model->embargo_end_time = NULL;
            } else {
                $model->embargo_end_time = $_POST['ContentMylocalstats']['embargo_end_time'];
            }
            if (!empty($model->mylocalstats_portal))
                $model->mylocalstats_portal = json_encode($model->mylocalstats_portal);
            if ($model->save()) {
                //update translation
                if (isset($_POST['oldtranslation']) && !empty($_POST['oldtranslation'])) {
                    foreach ($_POST['oldtranslation'] as $oldtranslation) {
                        if (($modeltranslationupdate = ContentMylocalstatsTranslation::findOne($oldtranslation)) !== null) {
                            $modeltranslationupdate->mylocalstats_translation_title = $_POST['old_title_' . $oldtranslation];
                            $modeltranslationupdate->mylocalstats_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(false);
                        }
                    }
                }
                return $this->redirect(['index']);
            }
        }
        return $this->render('update', [
                    'model' => $model,
        ]);
    }

    /**
     * Deletes an existing ContentPhotoGallery 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) {
        $model = $this->findModel($id);
        if (!empty($model->mylocalstats_thumbnail)) {
            $modelArchive = new FileArchive;
            $modelArchive->file_source = 'MyLocalStats';
            if (($model2 = ContentMylocalstatsTranslation::findOne(['mylocalstats_translation_parent_id' => $id, 'mylocalstats_translation_main' => 1])) !== null) {
                $modelArchive->file_name = $model2->mylocalstats_translation_title;
            }
            $modelArchive->file_path = 'admin/' . $model->mylocalstats_thumbnail;
            $modelArchive->file_updated_at = date("Y-m-d H:i:s");
            $modelArchive->file_updated_by = Yii::$app->user->identity->id;
            $modelArchive->save(false);
        }
        ContentMylocalstats::deleteAll(['mylocalstats_parent_id' => $id]);
        $model->delete();
        return $this->redirect(['index']);
    }

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

    public function actionAddtranslation($id) {
        $modeltranslation = new ContentMylocalstatsTranslation();
        $modeltranslation->mylocalstats_translation_parent_id = $id;
        $modeltranslation->mylocalstats_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 = ContentMylocalstatsTranslation::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 = ContentMylocalstats::findOne($slider_id);
                if (!empty($model)) {
                    $count++;
                    $model->mylocalstats_sort = $count;
                    $model->save(false);
                }
            }
        }
        return $data;
    }
}