Your IP : 216.73.216.79


Current Path : /var/www/html/hr/controllers/
Upload File :
Current File : /var/www/html/hr/controllers/ScheduleController.php

<?php

namespace app\controllers;

use Yii;
use app\models\Schedule;
use app\models\ScheduleSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use yii\helpers\Url;
use app\models\MenuAccess;
use yii\helpers\ArrayHelper;
use app\models\Log;
use app\models\LogSearch;
use app\models\User;
use app\models\ScheduleTime;

/**
 * ScheduleController implements the CRUD actions for Schedule model.
 */
class ScheduleController extends Controller {

    public $menu_id = 85;

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

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

    /**
     * Displays a single Schedule model.
     * @param integer $id
     * @param string $uuid
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id, $uuid) {
        return $this->render('view', ['model' => $this->findModel($id, $uuid),]);
    }

    /**
     * Creates a new Schedule model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Schedule();
        $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->uuid = Uuid::uuid4();
        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                //save schedule_time
                $modelUser = User::find()->where(['=', 'status', '1'])->orderBy(['name' => SORT_ASC])->all();
                if (!empty($modelUser)) {
                    foreach ($modelUser as $rowUser) {
                        $day_arr = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
                        foreach ($day_arr as $day) {
                            $modelTime = new ScheduleTime();
                            $modelTime->parent_id = $model->id;
                            $modelTime->user_id = $rowUser->id;
                            $modelTime->day = $day;

                            $modelTime->start_time = $_POST[$day . '_start_time_' . $rowUser->id];
                            $modelTime->end_time = $_POST[$day . '_end_time_' . $rowUser->id];

                            if (!empty($_POST[$day . '_rest_day_' . $rowUser->id]))
                                $modelTime->rest_day = $_POST[$day . '_rest_day_' . $rowUser->id];

                            if (!empty($_POST[$day . '_opening_pic_' . $rowUser->id]))
                                $modelTime->opening_pic = $_POST[$day . '_opening_pic_' . $rowUser->id];

                            if (!empty($_POST[$day . '_closing_pic_' . $rowUser->id]))
                                $modelTime->closing_pic = $_POST[$day . '_closing_pic_' . $rowUser->id];
                            
                            $modelTime->remarks = $_POST[$day . '_remarks_' . $rowUser->id];
                            $modelTime->created_dt = date("Y-m-d H:i:s");
                            $modelTime->created_by = Yii::$app->user->identity->id;
                            $modelTime->updated_dt = date("Y-m-d H:i:s");
                            $modelTime->updated_by = Yii::$app->user->identity->id;
                            $modelTime->save(false);
                        }
                    }
                }

                $Log = new Log();
                $Log->insertlog($this->menu_id, 'create', $model->uuid);
                $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(['schedule/index']) . "';});");
            }
        }
        return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Updates an existing Schedule model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @param string $uuid
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($uuid) {
        $model = $this->findModel($uuid);
        $model->updated_dt = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                
                ScheduleTime::deleteAll(['parent_id' => $model->id]);
                
                //save schedule_time
                $modelUser = User::find()->where(['=', 'status', '1'])->orderBy(['name' => SORT_ASC])->all();
                if (!empty($modelUser)) {
                    foreach ($modelUser as $rowUser) {
                        $day_arr = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
                        foreach ($day_arr as $day) {
                            $modelTime = new ScheduleTime();
                            $modelTime->parent_id = $model->id;
                            $modelTime->user_id = $rowUser->id;
                            $modelTime->day = $day;

                            $modelTime->start_time = $_POST[$day . '_start_time_' . $rowUser->id];
                            $modelTime->end_time = $_POST[$day . '_end_time_' . $rowUser->id];

                            if (!empty($_POST[$day . '_rest_day_' . $rowUser->id]))
                                $modelTime->rest_day = $_POST[$day . '_rest_day_' . $rowUser->id];

                            if (!empty($_POST[$day . '_opening_pic_' . $rowUser->id]))
                                $modelTime->opening_pic = $_POST[$day . '_opening_pic_' . $rowUser->id];

                            if (!empty($_POST[$day . '_closing_pic_' . $rowUser->id]))
                                $modelTime->closing_pic = $_POST[$day . '_closing_pic_' . $rowUser->id];
                            
                            $modelTime->remarks = $_POST[$day . '_remarks_' . $rowUser->id];
                            $modelTime->created_dt = date("Y-m-d H:i:s");
                            $modelTime->created_by = Yii::$app->user->identity->id;
                            $modelTime->updated_dt = date("Y-m-d H:i:s");
                            $modelTime->updated_by = Yii::$app->user->identity->id;
                            $modelTime->save(false);
                        }
                    }
                }
                
                $Log = new Log();
                $Log->insertlog($this->menu_id, 'update', $model->uuid);
                $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(['schedule/index']) . "';});");
            }
        }
        return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Deletes an existing Schedule model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @param string $uuid
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id) {
        if (($model = Schedule::findOne($id)) !== null) {
            $Log = new Log();
            $Log->insertlog($this->menu_id, 'delete', $model->uuid);
            if ($model->delete())
                return 'success';
        }
    }

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

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

}