Your IP : 216.73.216.79


Current Path : /var/www/html/jppmintranetv2/backend/modules/mimin/controllers/
Upload File :
Current File : /var/www/html/jppmintranetv2/backend/modules/mimin/controllers/UserController.php

<?php

namespace backend\modules\mimin\controllers;

use Yii;
use backend\modules\mimin\models\User;
use backend\modules\mimin\models\AuthAssignment;
use backend\modules\mimin\models\AuthItem;
use backend\modules\mimin\models\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use yii\filters\AccessControl;

/**
 * UserController implements the CRUD actions for User model.
 */
class UserController extends Controller {

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

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

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

    /**
     * Displays a single User model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        $model = $this->findModel($id);
        $authAssignments = AuthAssignment::find()->where([
                    'user_id' => $model->id,
                ])->column();

        $authItems = ArrayHelper::map(
                        AuthItem::find()->where([
                            'type' => 1,
                        ])->asArray()->all(),
                        'name', 'name');

        $authAssignment = new AuthAssignment([
            'user_id' => $model->id,
        ]);

        if (Yii::$app->request->post()) {
            $authAssignment->load(Yii::$app->request->post());
            // delete all role
            AuthAssignment::deleteAll(['user_id' => $model->id]);
            if (is_array($authAssignment->item_name)) {
                foreach ($authAssignment->item_name as $item) {
                    if (!in_array($item, $authAssignments)) {
                        $authAssignment2 = new AuthAssignment([
                            'user_id' => $model->id,
                        ]);
                        $authAssignment2->item_name = $item;
                        $authAssignment2->created_at = time();
                        $authAssignment2->save();

                        $authAssignments = AuthAssignment::find()->where([
                                    'user_id' => $model->id,
                                ])->column();
                    }
                }
            }

            Yii::$app->session->setFlash('success', Yii::t('app', 'Data hase been save'));

            return $this->redirect(['index']);
        }
        $authAssignment->item_name = $authAssignments;
        return $this->render('view', [
                    'model' => $model,
                    'authAssignment' => $authAssignment,
                    'authItems' => $authItems,
        ]);
    }

    /**
     * Creates a new User model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new User();
        $model->status = 1;
        if ($model->load(Yii::$app->request->post())) {
            $model->setPassword($model::DEFAULT_PASSWORD);
            $model->status = $model->status == 1 ? 10 : 0;
            if ($model->save()) {
                Yii::$app->session->setFlash('success', Yii::t('app', 'User Has been set with default password ') . $model::DEFAULT_PASSWORD);
            } else {
                Yii::$app->session->setFlash('error', Yii::t('app', 'Failed to save user'));
            }

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing User model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            if (!empty($model->new_password)) {
                $model->setPassword($model->new_password);
            }
            $model->status = $model->status == 1 ? 10 : 0;
            if ($model->save()) {
                Yii::$app->session->setFlash('success', 'User berhasil diupdate');
            } else {
                Yii::$app->session->setFlash('error', 'User gagal diupdate');
            }
            return $this->redirect(['index']);
        } else {
            $model->status = $model->status == 10 ? 1 : 0;
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing User model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $model = $this->findModel($id);
        $authAssignments = AuthAssignment::find()->where([
                    'user_id' => $model->id,
                ])->all();
        foreach ($authAssignments as $authAssignment) {
            $authAssignment->delete();
        }

        Yii::$app->session->setFlash('success', 'Delete success');
        $model->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the User model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return User the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = User::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionResetpwd($id) {
        $model = $this->findModel($id);
        $model->setPassword($model::DEFAULT_PASSWORD);
        if ($model->save(false)) {
            return $this->redirect(['index', 'resetpwd' => 'success']);
        } else {
            return $this->redirect(['index', 'resetpwd' => 'failed']);
        }
    }

}