Your IP : 216.73.216.79


Current Path : /var/www/html/dosm/backend/modules/contentStatistics/controllers/
Upload File :
Current File : /var/www/html/dosm/backend/modules/contentStatistics/controllers/ContentStatisticsController.php

<?php

namespace backend\modules\contentStatistics\controllers;

use Yii;
use backend\modules\contentStatistics\models\ContentStatistics;
use backend\modules\contentStatistics\models\ContentStatisticsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;

/**
 * ContentStatisticsController implements the CRUD actions for ContentStatistics model.
 */
class ContentStatisticsController extends Controller {

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

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

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

    /**
     * Displays a single ContentStatistics 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 ContentStatistics model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new ContentStatistics();
        $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->stats_main = 1;
        $model->stats_code = (new \yii\db\Query)->select(new \yii\db\Expression('UUID()'))->scalar();
        
        if ($model->load(Yii::$app->request->post())) {

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

//            $stats_item_arr = [];
//            if (!empty($_POST['item_main_no'])) {
//                foreach ($_POST['item_main_no'] as $item_main_no) {
//                    $stats_item_arr [] = [
//                        'category' => $_POST['item_category_' . $item_main_no],
//                        'subcategory' => $_POST['item_subcategory_' . $item_main_no],
//                        'figure' => $_POST['item_figure_' . $item_main_no],
//                    ];
//                }
//                $model->stats_item = json_encode($stats_item_arr);
//            }

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

    /**
     * Updates an existing ContentStatistics 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_thumbnail = $model->stats_img;

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

            $model->stats_img = UploadedFile::getInstance($model, 'stats_img');
            if (!empty($model->stats_img)) {
                $stats_img = 'images/modules/contentStatistics_' . date("YmdHis") . '.' . $model->stats_img->extension;
                if ($model->stats_img->saveAs($stats_img))
                    $model->stats_img = $stats_img;
            } else {
                $model->stats_img = $original_thumbnail;
            }

//            $stats_item_arr = [];
//            if (!empty($_POST['item_main_no'])) {
//                foreach ($_POST['item_main_no'] as $item_main_no) {
//                    $stats_item_arr [] = [
//                        'category' => $_POST['item_category_' . $item_main_no],
//                        'subcategory' => $_POST['item_subcategory_' . $item_main_no],
//                        'figure' => $_POST['item_figure_' . $item_main_no],
//                    ];
//                }
//                $model->stats_item = json_encode($stats_item_arr);
//            }

            if ($model->save()) {

                //update translation
                if (isset($_POST['oldtranslation']) && !empty($_POST['oldtranslation'])) {
                    foreach ($_POST['oldtranslation'] as $oldtranslation) {
                        if (($modeltranslationupdate = ContentStatistics::findOne($oldtranslation)) !== null) {


//                            $stats_item_arr2 = [];
//                            if (!empty($_POST['translation_' . $oldtranslation . '_item_no'])) {
//                                foreach ($_POST['translation_' . $oldtranslation . '_item_no'] as $item_translation_no) {
//                                    $stats_item_arr2 [] = [
//                                        'category' => $_POST['translation_' . $oldtranslation . '_item_category_' . $item_translation_no],
//                                        'subcategory' => $_POST['translation_' . $oldtranslation . '_item_subcategory_' . $item_translation_no],
//                                        'figure' => $_POST['translation_' . $oldtranslation . '_item_figure_' . $item_translation_no],
//                                    ];
//                                }
//                                $modeltranslationupdate->stats_item = json_encode($stats_item_arr2);
//                            }


                            $modeltranslationupdate->stats_language = $_POST['old_language_' . $oldtranslation];
                            $modeltranslationupdate->stats_title = $_POST['old_title_' . $oldtranslation];
                            $modeltranslationupdate->stats_content = $_POST['old_content_' . $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 ContentStatistics 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) {
        ContentStatistics::deleteAll(['stats_parent_id' => $id]);
        $this->findModel($id)->delete();
        return $this->redirect(['index']);
    }

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

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

    public function actionAddtranslation($id) {
        $model = $this->findModel($id);
        if (!empty($model)) {
            $modeltranslation = new ContentStatistics();
            $modeltranslation->stats_item = $model->stats_item;
            $modeltranslation->stats_parent_id = $id;
            $modeltranslation->stats_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 = ContentStatistics::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 = ContentStatistics::findOne($slider_id);
                if (!empty($model)) {
                    $count++;
                    $model->stats_sort = $count;
                    $model->save(false);
                }
            }
        }

        return $data;
    }

}