Your IP : 216.73.216.79


Current Path : /var/www/html/akept/backend/modules/refdynawebv2/controllers/
Upload File :
Current File : /var/www/html/akept/backend/modules/refdynawebv2/controllers/RefController.php

<?php

namespace backend\modules\refdynawebv2\controllers;

use Yii;
use backend\modules\refdynawebv2\models\Ref;
use backend\modules\refdynawebv2\models\RefSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\components\AccessRule;
use yii\helpers\Url;

/**
 * RefController implements the CRUD actions for Ref model.
 */
class RefController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'ruleConfig' => [
                    'class' => AccessRule::className(),
                ],
                'rules' => [
                    [
                        'actions' => ['index', 'create', 'update', 'view', 'delete'],
                        'allow' => true,
                        'roles' => ['@'],
                    ], [
                        'allow' => true,
                        'actions' => ['getreflist', 'getreflistmy'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

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

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

    /**
     * Displays a single Ref 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 Ref model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Ref();
        $model->sort = 1;
        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('create', ['model' => $model,]);
    }

    /**
     * Updates an existing Ref 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);
        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                $this->getView()->registerJs("swal.fire({
                    title:'Saved',
                    icon: 'success', 
                    text: 'Data has been saved successfully', 
                }).then(function(result){
                        window.location = '" . Url::toRoute(['index']) . "';
                });");
            }
        }
        return $this->render('update', ['model' => $model,]);
    }

    /**
     * Deletes an existing Ref 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'];
        $model = $this->findModel($id);
        if (!empty($model))
            $model->delete();
        return 'success';
    }

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

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

    public function actionGetreflist($code, $cat) {
        $output = '<option value="">-- Please choose --</option>';
        if (($model2 = Ref::findOne(['code' => $code, 'cat' => $cat])) !== null) {
            $model = Ref::find()->where(['param1' => $model2->id])->orderBy(['descr' => SORT_ASC])->all();
            if (!empty($model)) {
                foreach ($model as $row) {
                    $output .= '<option value="' . $row->code . '">' . $row->descr . '</option>';
                }
            }
        }
        return $output;
    }

    public function actionGetreflistmy($code, $cat) {
        $output = '<option value="">-- Sila pilih --</option>';
        if (($model2 = Ref::findOne(['code' => $code, 'cat' => $cat])) !== null) {
            $model = Ref::find()->where(['param1' => $model2->id])->orderBy(['descr' => SORT_ASC])->all();
            if (!empty($model)) {
                foreach ($model as $row) {
                    $output .= '<option value="' . $row->code . '">' . $row->descr . '</option>';
                }
            }
        }
        return $output;
    }

}