Your IP : 216.73.216.79


Current Path : /var/www/html/dosm/backend/modules/populationClock/controllers/
Upload File :
Current File : /var/www/html/dosm/backend/modules/populationClock/controllers/PopulationClockDataController.php

<?php

namespace backend\modules\populationClock\controllers;

use Yii;
use backend\modules\populationClock\models\PopulationClockData;
use backend\modules\populationClock\models\PopulationClockDataSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\filters\AccessControl;

/**
 * PopulationClockDataController implements the CRUD actions for PopulationClockData model.
 */
class PopulationClockDataController extends Controller {

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


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

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

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

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

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

    /**
     * Updates an existing PopulationClockData 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_clock_img = $model->clock_img;
        if ($model->load(Yii::$app->request->post())) {

            $model->clock_img = UploadedFile::getInstance($model, 'clock_img');
            if (!empty($model->clock_img)) {
                $clock_img = 'clock_' . date("YmdHis") . '.' . $model->clock_img->extension;
                if ($model->clock_img->saveAs('../uploads/' . $clock_img))
                    $model->clock_img = $clock_img;
            } else {
                $model->clock_img = $original_clock_img;
            }

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

    /**
     * Deletes an existing PopulationClockData 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 PopulationClockData model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return PopulationClockData the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = PopulationClockData::findOne($id)) !== null) {
            return $model;
        }

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

}