Your IP : 216.73.216.79


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

<?php

namespace app\controllers;

use Yii;
use app\models\Phase;
use app\models\PhaseSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\Log;
use yii\helpers\Url;

/**
 * PhaseController implements the CRUD actions for Phase model.
 */
class PhaseController extends Controller {

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'only' => ['index', 'create', 'update', 'view'],
                'rules' => [
                    // allow authenticated users
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                // everything else is denied
                ],
            ],
        ];
    }

    /**
     * Lists all Phase models.
     * @return mixed
     */
    public function actionIndex($template_id) {
        Url::remember();
        $model = Phase::find()->where(['status' => '1'])->andwhere(['template_id' => $template_id])->orderBy(['sort' => SORT_ASC])->all();
        return $this->render('index', ['model' => $model, 'template_id' => $template_id]);
    }

    /**
     * Displays a single Phase 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 Phase model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($template_id) {
        $model = new Phase();
        $model->template_id = $template_id;
        $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;
        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->sort, $model->id, $model->template_id);
                $Log = new Log();
                $Log->insertlog('phase', '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::to(['phase/index', 'template_id' => $model->template_id]) . "';
               });");
            }
        }
        return $this->render('create', ['model' => $model,]);
    }

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

    /**
     * Updates an existing Phase 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->sort, $model->id, $model->template_id);
                    $this->updatesorting2($model->template_id);
                }
                $Log = new Log();
                $Log->insertlog('phase', '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::to(['phase/index','template_id'=>$model->template_id]) . "';
               });");
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing Phase 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 = Phase::findOne($id);
        if (!empty($model)) {
            $model->status = 0;
            $model->sort = 0;
            if ($model->save(false)) {
                $this->updatesorting(0, $model->id, $model->template_id);
                $Log = new Log();
                $Log->insertlog('phase', 'delete', $model->id);
                $status = 'success';
            }
        }
        return $status;
    }

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

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

}