| Current Path : /var/www/html/hr/controllers/ |
| Current File : /var/www/html/hr/controllers/MenuController.php |
<?php
namespace app\controllers;
use Yii;
use app\models\Menu;
use app\models\MenuSearch;
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;
/**
* MenuController implements the CRUD actions for Menu model.
*/
class MenuController extends Controller {
public $menu_id = 4;
/**
* {@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 Menu models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new MenuSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
}
public function actionLog($uuid) {
$searchModel = new LogSearch();
$searchModel->menu_id = $this->menu_id;
$searchModel->data_uuid = $uuid;
$dataProvider = $searchModel->log(Yii::$app->request->queryParams);
return $this->render('log', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
}
public function actionTree() {
//$searchModel = new MenuSearch();
//$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
//return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider,]);
return $this->render('tree', ['menu_id' => $this->menu_id]);
}
/**
* Displays a single Menu 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 Menu model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new Menu();
$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();
$model->status = 1;
$model->left_menu = 1;
$model->separator = 0;
if (!empty(Yii::$app->getRequest()->getQueryParam('parent')))
$model->parent = $model->getid(Yii::$app->getRequest()->getQueryParam('parent'));
if ($model->load(Yii::$app->request->post())) {
if (empty($model->sort))
$model->sort = 1;
else
$model->sort = $model->sort + 1;
if ($model->save()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'create', $model->uuid);
if ($model->left_menu == 1)
$this->updatesorting($model->parent, $model->sort, $model->id);
if (!empty($model->user_access)) {
foreach ($model->user_access as $user_role) {
//insert user roles
$model2 = new MenuAccess();
$model2->menu_id = $model->id;
$model2->user_role = $user_role;
$model2->created_dt = date("Y-m-d H:i:s");
$model2->created_by = Yii::$app->user->identity->id;
$model2->save();
}
}
$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::toRoute('menu/' . Yii::$app->getRequest()->getQueryParam('returnUrl')) . "';});");
}
}
return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
}
/**
* Updates an existing Menu 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->user_access = ArrayHelper::map(MenuAccess::find()->where(['menu_id' => $model->id])->all(), 'user_role', 'user_role');
$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()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'update', $model->uuid);
MenuAccess::deleteAll(['menu_id' => $model->id]);
if (!empty($model->user_access)) {
foreach ($model->user_access as $user_role) {
//insert user roles
$model2 = new MenuAccess();
$model2->menu_id = $model->id;
$model2->user_role = $user_role;
$model2->created_dt = date("Y-m-d H:i:s");
$model2->created_by = Yii::$app->user->identity->id;
$model2->save();
}
}
if ($change_sort == 1 && $model->left_menu == 1) {
$this->updatesorting($model->parent, $model->sort, $model->id);
}
if ($model->left_menu == 1)
$this->updatesorting2($model->parent);
$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::toRoute('menu/' . Yii::$app->getRequest()->getQueryParam('returnUrl')) . "';});");
}
}
return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
}
public function updatesorting($parent, $sort, $current_id) {
$count = $sort;
$model = Menu::find()->where(['and', "parent=$parent", "sort>=$sort", "id!=$current_id", "left_menu=1"])->orderBy(['sort' => SORT_ASC])->all();
if (!empty($model)) {
foreach ($model as $row) {
$count++;
Yii::$app->db->createCommand("UPDATE menu SET sort='$count' WHERE id=" . $row->id)->execute();
}
}
}
public function updatesorting2($parent) {
$count = 1;
$model = Menu::find()->where(["and", "parent=$parent", "left_menu=1"])->orderBy(['sort' => SORT_ASC])->all();
if (!empty($model)) {
foreach ($model as $row) {
Yii::$app->db->createCommand("UPDATE menu SET sort='$count' WHERE id=" . $row->id)->execute();
$count++;
}
}
}
/**
* Deletes an existing Menu 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, $uuid) {
$this->findModel($id, $uuid)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Menu 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 Menu the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($uuid) {
if (($model = Menu::findOne(['uuid' => $uuid])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionGetsortlist() {
$parent_val = $_POST['parent_val'];
$output = '';
$model = Menu::find()->where(['parent' => $parent_val, 'left_menu' => 1])->orderBy(['sort' => SORT_ASC])->all();
if (!empty($model)) {
foreach ($model as $row) {
$output.= '<option value="' . $row->sort . '">' . $row->title . '</option>';
}
}
return $output;
}
}