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/StatsFunChildController.php

<?php

namespace backend\modules\statisticsFun\controllers;

use Yii;
use backend\modules\statisticsFun\models\StatsFunChild;
use backend\modules\statisticsFun\models\StatsFunChildSearch;
use backend\modules\statisticsFun\models\StatsFunChildPoll;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\filters\AccessControl;

/**
 * StatsFunChildController implements the CRUD actions for StatsFunChild model.
 */
class StatsFunChildController extends Controller {

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

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

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

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

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

            if ($model->save()) {
                //insert into stats_fun_child_poll
                if (!empty($_POST['newdata'])) {
                    foreach ($_POST['newdata'] as $newdatano) {
                        $modelForm = new StatsFunChildPoll();
                        $modelForm->main_id = $model->child_id;
                        $modelForm->poll_name = $_POST['new_form_name_' . $newdatano];
                        $modelForm->poll_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->poll_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->poll_selection = json_encode($form_selection_arr);

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

    /**
     * Updates an existing StatsFunChild 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_child_thumbnail = $model->child_thumbnail;
        $original_child_background = $model->child_background;

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

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

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

            if ($model->save()) {
                //insert into stats_fun_child_poll
                if (!empty($_POST['newdata'])) {
                    foreach ($_POST['newdata'] as $newdatano) {
                        $modelForm = new StatsFunChildPoll();
                        $modelForm->main_id = $model->child_id;
                        $modelForm->poll_name = $_POST['new_form_name_' . $newdatano];
                        $modelForm->poll_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->poll_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->poll_selection = json_encode($form_selection_arr);

                        $modelForm->save(false);
                    }
                }
                
                if (!empty($_POST['olddata'])) {
                    foreach ($_POST['olddata'] as $olddatano) {
                        if (($modelUpdateForm = StatsFunChildPoll::findOne($olddatano)) !== null) {
                            $modelUpdateForm->poll_name = $_POST['old_form_name_' . $olddatano];
                            $modelUpdateForm->poll_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->poll_type == 'select') {
                                if (!empty($_POST['oldselectdata_' . $olddatano])) {
                                    foreach ($_POST['oldselectdata_' . $olddatano] as $oldselectinput) {
                                        $form_selection_arr2[] = $_POST[$oldselectinput];
                                    }
                                }
                            }
                            if (!empty($form_selection_arr2))
                                $modelUpdateForm->poll_selection = json_encode($form_selection_arr2);

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

    /**
     * Deletes an existing StatsFunChild 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) {
        $main_id = '';
        if (($model = StatsFunChild::findOne($id)) !== null) {
            $main_id = $model->main_id;
            $model->delete();
        }
        return $this->redirect(['index', 'main_id' => $main_id]);
    }

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

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

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

}