Your IP : 216.73.216.79


Current Path : /var/www/html/dosm/backend/modules/statisticsFun/controllers/
Upload File :
Current File : /var/www/html/dosm/backend/modules/statisticsFun/controllers/StatsFunMainController.php

<?php

namespace backend\modules\statisticsFun\controllers;

use Yii;
use backend\modules\statisticsFun\models\StatsFunMain;
use backend\modules\statisticsFun\models\StatsFunMainSearch;
use backend\modules\statisticsFun\models\StatsFunForm;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\filters\AccessControl;

/**
 * StatsFunMainController implements the CRUD actions for StatsFunMain model.
 */
class StatsFunMainController extends Controller {

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

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

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

    /**
     * Displays a single StatsFunMain 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 StatsFunMain model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new StatsFunMain();
        $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_slug = (new \yii\db\Query)->select(new \yii\db\Expression('UUID()'))->scalar();
        $model->stats_slug = $this->uuid_v4();
        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;
            }

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

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

            if ($model->save()) {
                //insert into stats_fun_form
                if (!empty($_POST['newdata'])) {
                    foreach ($_POST['newdata'] as $newdatano) {
                        $modelForm = new StatsFunForm();
                        $modelForm->main_id = $model->stats_id;
                        $modelForm->form_name = $_POST['new_form_name_' . $newdatano];
                        $modelForm->form_type = $_POST['new_form_type_' . $newdatano];
                        $modelForm->created_at = date("Y-m-d H:i:s");
                        $modelForm->created_by = Yii::$app->user->identity->id;
                        $modelForm->updated_at = date("Y-m-d H:i:s");
                        $modelForm->updated_by = Yii::$app->user->identity->id;

                        $form_selection_arr = [];
                        if ($modelForm->form_type == 'select') {
                            if (!empty($_POST['newselectdata_' . $newdatano])) {
                                foreach ($_POST['newselectdata_' . $newdatano] as $newselectno) {
                                    $form_selection_arr[] = $_POST['new_select_val_' . $newselectno];
                                }
                            }
                        }
                        if (!empty($form_selection_arr))
                            $modelForm->form_selection = json_encode($form_selection_arr);

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

    /**
     * Updates an existing StatsFunMain 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_stats_thumbnail = $model->stats_thumbnail;
        $original_stats_background = $model->stats_background;

        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->stats_thumbnail = UploadedFile::getInstance($model, 'stats_thumbnail');
            if (!empty($model->stats_thumbnail)) {
                $stats_thumbnail = 'thumbnail_' . date("YmdHis") . '.' . $model->stats_thumbnail->extension;
                if ($model->stats_thumbnail->saveAs('../uploads/statistics-fun/' . $stats_thumbnail))
                    $model->stats_thumbnail = $stats_thumbnail;
            } else {
                $model->stats_thumbnail = $original_stats_thumbnail;
            }

            $model->stats_background = UploadedFile::getInstance($model, 'stats_background');
            if (!empty($model->stats_background)) {
                $stats_background = 'background_' . date("YmdHis") . '.' . $model->stats_background->extension;
                if ($model->stats_background->saveAs('../uploads/statistics-fun/' . $stats_background))
                    $model->stats_background = $stats_background;
            } else {
                $model->stats_background = $original_stats_background;
            }

            if ($model->save()) {
                //insert into stats_fun_form
                if (!empty($_POST['newdata'])) {
                    foreach ($_POST['newdata'] as $newdatano) {
                        $modelForm = new StatsFunForm();
                        $modelForm->main_id = $model->stats_id;
                        $modelForm->form_name = $_POST['new_form_name_' . $newdatano];
                        $modelForm->form_type = $_POST['new_form_type_' . $newdatano];
                        $modelForm->created_at = date("Y-m-d H:i:s");
                        $modelForm->created_by = Yii::$app->user->identity->id;
                        $modelForm->updated_at = date("Y-m-d H:i:s");
                        $modelForm->updated_by = Yii::$app->user->identity->id;

                        $form_selection_arr = [];
                        if ($modelForm->form_type == 'select') {
                            if (!empty($_POST['newselectdata_' . $newdatano])) {
                                foreach ($_POST['newselectdata_' . $newdatano] as $newselectno) {
                                    $form_selection_arr[] = $_POST['new_select_val_' . $newselectno];
                                }
                            }
                        }
                        if (!empty($form_selection_arr))
                            $modelForm->form_selection = json_encode($form_selection_arr);

                        $modelForm->save(false);
                    }
                }

                if (!empty($_POST['olddata'])) {
                    foreach ($_POST['olddata'] as $olddatano) {
                        if (($modelUpdateForm = StatsFunForm::findOne($olddatano)) !== null) {
                            $modelUpdateForm->form_name = $_POST['old_form_name_' . $olddatano];
                            $modelUpdateForm->form_type = $_POST['old_form_type_' . $olddatano];
                            $modelUpdateForm->updated_at = date("Y-m-d H:i:s");
                            $modelUpdateForm->updated_by = Yii::$app->user->identity->id;

                            $form_selection_arr2 = [];
                            if ($modelUpdateForm->form_type == 'select') {
                                if (!empty($_POST['oldselectdata_' . $olddatano])) {
                                    foreach ($_POST['oldselectdata_' . $olddatano] as $oldselectinput) {
                                        $form_selection_arr2[] = $_POST[$oldselectinput];
                                    }
                                }
                            }
                            if (!empty($form_selection_arr2))
                                $modelUpdateForm->form_selection = json_encode($form_selection_arr2);

                            $modelUpdateForm->save(false);
                        }
                    }
                }
                return $this->redirect(['index']);
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing StatsFunMain 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) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

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

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    public function actionDeleteform($id) {
        if (($model = StatsFunForm::findOne($id)) !== null) {
            $model->delete();
        }
    }
    
    public function actionSorting($data = '') {
        $count = 0;
        $data_arr = explode(',', $data);
        if (!empty($data_arr)) {
            foreach ($data_arr as $id) {
                $model = StatsFunMain::findOne($id);
                if (!empty($model)) {
                    $count++;
                    $model->sorting = $count;
                    $model->save(false);
                }
            }
        }

        return $data;
    }


    public function uuid_v4(): string {
        return sprintf(
            '%s-%s-4%s-%x%s-%s',
            bin2hex(random_bytes(4)),
            bin2hex(random_bytes(2)),
            substr(bin2hex(random_bytes(2)), 1, 3),
            random_int(8, 11),
            substr(bin2hex(random_bytes(2)), 1, 3),
            bin2hex(random_bytes(6))
        );
    }

}