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

<?php

namespace backend\modules\statisticsFun\controllers;

use Yii;
use backend\modules\statisticsFun\models\StatsFunChildOutput;
use backend\modules\statisticsFun\models\StatsFunChildOutputSearch;
use backend\modules\statisticsFun\models\StatsFunChild;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\modules\statisticsFun\models\StatsFunChildOutputData;
use yii\filters\AccessControl;

/**
 * StatsFunChildOutputController implements the CRUD actions for StatsFunChildOutput model.
 */
class StatsFunChildOutputController extends Controller {

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

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

        $child_parent_id = 0;
        if (($modelChild = StatsFunChild::findOne($child_id)) !== null) {
            $child_parent_id = $modelChild->main_id;
        }

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

    /**
     * Displays a single StatsFunChildOutput 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 StatsFunChildOutput model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($child_id) {
        $model = new StatsFunChildOutput();
        $model->child_id = $child_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;

        $child_parent_id = 0;
        if (($modelChild = StatsFunChild::findOne($model->child_id)) !== null) {
            $child_parent_id = $modelChild->main_id;
        }
        $model->stats_id = $child_parent_id;

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

            if (!empty($_POST['newdata'])) {
                foreach ($_POST['newdata'] as $newdatano) {
                    $modelOutput = new StatsFunChildOutputData();
                    $modelOutput->parent_id = $model->output_id;
                    $modelOutput->input_value = $_POST['new_input_value_' . $newdatano];
                    $modelOutput->output_data = $_POST['new_output_data_' . $newdatano];
                    $modelOutput->created_at = date("Y-m-d H:i:s");
                    $modelOutput->created_by = Yii::$app->user->identity->id;
                    $modelOutput->updated_at = date("Y-m-d H:i:s");
                    $modelOutput->updated_by = Yii::$app->user->identity->id;
                    $modelOutput->save(false);
                }
            }

            return $this->redirect(['index', 'child_id' => $model->child_id]);
        }

        return $this->render('create', ['model' => $model, 'child_parent_id' => $child_parent_id]);
    }

    /**
     * Updates an existing StatsFunChildOutput 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;
        $child_parent_id = $model->stats_id;

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

            if (!empty($_POST['newdata'])) {
                foreach ($_POST['newdata'] as $newdatano) {
                    $modelOutput = new StatsFunChildOutputData();
                    $modelOutput->parent_id = $model->output_id;
                    $modelOutput->input_value = $_POST['new_input_value_' . $newdatano];
                    $modelOutput->output_data = $_POST['new_output_data_' . $newdatano];
                    $modelOutput->created_at = date("Y-m-d H:i:s");
                    $modelOutput->created_by = Yii::$app->user->identity->id;
                    $modelOutput->updated_at = date("Y-m-d H:i:s");
                    $modelOutput->updated_by = Yii::$app->user->identity->id;
                    $modelOutput->save(false);
                }
            }

            if (!empty($_POST['olddata'])) {
                foreach ($_POST['olddata'] as $olddatano) {
                    if (($modelUpdateOutput = StatsFunChildOutputData::findOne($olddatano)) !== null) {
                        $modelUpdateOutput->input_value = $_POST['old_input_value_' . $olddatano];
                        $modelUpdateOutput->output_data = $_POST['old_output_data_' . $olddatano];
                        $modelUpdateOutput->updated_at = date("Y-m-d H:i:s");
                        $modelUpdateOutput->updated_by = Yii::$app->user->identity->id;
                        $modelUpdateOutput->save(false);
                    }
                }
            }

            return $this->redirect(['index', 'child_id' => $model->child_id]);
        }
        return $this->render('update', ['model' => $model, 'child_parent_id' => $child_parent_id]);
    }

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

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

        throw new NotFoundHttpException('The requested page does not exist.');
    }
    
    public function actionDeleteoutput($id) {
        if (($model = StatsFunChildOutputData::findOne($id)) !== null) {
            $model->delete();
        }
    }

}