Your IP : 216.73.217.143


Current Path : /var/www/html/project/controllers/
Upload File :
Current File : /var/www/html/project/controllers/StepController.php

<?php

namespace app\controllers;

use Yii;
use app\models\Step;
use app\models\StepSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\Log;
use yii\helpers\Url;

/**
 * StepController implements the CRUD actions for Step model.
 */
class StepController extends Controller {

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

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

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

    /**
     * Displays a single Step 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 Step model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($phase_id) {
        $model = new Step();
        $model->created_dt = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->updated_dt = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $model->status = 1;
        $model->track = 'project';
        $model->phase_id = $phase_id;
        if ($model->load(Yii::$app->request->post())) {
            if (empty($model->sort))
                $model->sort = 1;
            else
                $model->sort = $model->sort + 1;
            if ($model->save()) {
                $this->updatesorting($model->phase_id, $model->sort, $model->id);
                $Log = new Log();
                $Log->insertlog('step', 'create', $model->id);
                $this->getView()->registerJs("swal.fire({
                    position: 'center-center',
                    type: 'success',
                    title: 'Data has been saved',
                    showConfirmButton: false,timer: 1500,
                }).then(function(result){
                    window.location = '" . Url::previous() . "';
               });");
            }
        }
        return $this->render('create', ['model' => $model]);
    }

    public function updatesorting($phase_id, $sort, $id) {
        $count = $sort;
        $model = Step::find()
                ->where(['=', 'phase_id', $phase_id])
                ->andwhere(['=', 'status', 1])
                ->andwhere(['!=', 'id', $id])
                ->andwhere(['>=', 'sort', $sort])
                ->orderBy(['sort' => SORT_ASC])
                ->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $count++;
                Yii::$app->db->createCommand("UPDATE step SET sort='$count' WHERE id=" . $row->id)->execute();
            }
        } 
    }
    
    public function updatesorting2($phase_id) {
        $count = 0;
        $model = Step::find()
                ->where(['=', 'phase_id', $phase_id])
                ->andwhere(['=', 'status', 1])
                ->orderBy(['sort' => SORT_ASC])
                ->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $count++;
                Yii::$app->db->createCommand("UPDATE step SET sort='$count' WHERE id=" . $row->id)->execute();
            }
        } 
    }

    /**
     * Updates an existing Step 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_dt = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        $ori_sort = $model->sort;
        $change_sort = 0;
        if ($model->load(Yii::$app->request->post())) {
            if ($model->sort != $ori_sort) {
                $change_sort = 1;
            }
            if ($change_sort == 1) {
                if (empty($model->sort))
                    $model->sort = 1;
                else
                    $model->sort = $model->sort + 1;
            }
            if ($model->save()) {
                if ($change_sort == 1) {
                    $this->updatesorting($model->phase_id, $model->sort, $model->id);
                    $this->updatesorting2($model->phase_id);
                }
                $Log = new Log();
                $Log->insertlog('step', 'update', $model->id);
                $this->getView()->registerJs("swal.fire({
                    position: 'center-center',
                    type: 'success',
                    title: 'Data has been saved',
                    showConfirmButton: false,timer: 1500,
                }).then(function(result){
                    window.location = '" . Url::previous() . "';
               });");
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing Step 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 = $_POST['id'];
        $status = 'failed';
        $model = Step::findOne($id);
        if (!empty($model)) {
            $model->status = 0;
            $model->sort = 0;
            if ($model->save(false)) {
                $this->updatesorting2($model->phase_id);
                $Log = new Log();
                $Log->insertlog('step', 'delete', $model->id);
                $status = 'success';
            }
        }
        return $status;
    }

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

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

}