From: marina Date: Thu, 7 Nov 2024 06:14:07 +0000 (+0300) Subject: ERP-199 Доработка меню (вёрстка+) X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=9d4f4aff4afcc3666d6095961b0a23cf72e11e7a;p=erp24_rep%2Fyii-erp24%2F.git ERP-199 Доработка меню (вёрстка+) --- diff --git a/erp24/controllers/AuthController.php b/erp24/controllers/AuthController.php index bd5f70ca..279ec544 100644 --- a/erp24/controllers/AuthController.php +++ b/erp24/controllers/AuthController.php @@ -7,6 +7,7 @@ use yii\helpers\ArrayHelper; use yii_app\records\Admin; use yii_app\records\AdminGroup; use yii_app\records\AdminGroupRbacConfig; +use yii_app\records\AdminMenuPositions; use yii_app\records\CrmMenu; use yii_app\records\CrmMenuPermission; use yii_app\records\UniversalCatalog; @@ -252,4 +253,59 @@ class AuthController extends BaseController CrmMenu::deleteAll(['parent_id' => $id]); return $this->redirect('menu'); } + + public function actionMenuByRoles() + { + $roles = ArrayHelper::map(AdminGroup::find()->all(), 'id', 'name'); + $menuItems = []; + + if (Yii::$app->request->isPost) { + // Сохранение порядка + $data = Yii::$app->request->post('order'); + foreach ($data as $position => $id) { + AdminMenuPositions::updateAll(['position' => $position], ['id' => $id]); + } + Yii::$app->session->setFlash('success', 'Порядок сохранён'); + return $this->refresh(); + } + + $selectedRole = Yii::$app->request->get('role'); + if ($selectedRole) { + // Attempt to get menu items for the selected role + $menuItems = AdminMenuPositions::find()->where(['role' => $selectedRole])->orderBy('position')->all(); + + // If no items found, fetch from CrmMenu + if (empty($menuItems)) { + $crmMenuItems = CrmMenu::find()->all(); // Fetch all items from CrmMenu + foreach ($crmMenuItems as $crmItem) { + // Check if the item already exists in AdminMenuPositions + if (!AdminMenuPositions::find()->where(['id' => $crmItem->id, 'role' => $selectedRole])->exists()) { + // Create a new AdminMenuPosition record + $adminMenuPosition = new AdminMenuPositions(); + $adminMenuPosition->id = $crmItem->id; // Assuming 'id' is the same + $adminMenuPosition->title = $crmItem->title; // Assuming 'title' exists in CrmMenu + $adminMenuPosition->role = $selectedRole; // Assign the selected role + $adminMenuPosition->position = 0; // Set default position (you can modify this logic) + $adminMenuPosition->save(); + } + } + + // After saving, get the menu items again + $menuItems = AdminMenuPositions::find()->where(['role' => $selectedRole])->orderBy('position')->all(); + } + } + + if (Yii::$app->request->isAjax) { + if ($selectedRole) { + return $this->asJson($menuItems); + } + } + + return $this->render('menu-by-roles', [ + 'roles' => $roles, + 'menuItems' => $menuItems, + 'selectedRole' => $selectedRole, + ]); + } + } \ No newline at end of file diff --git a/erp24/records/AdminMenuPositions.php b/erp24/records/AdminMenuPositions.php new file mode 100644 index 00000000..8ca1a188 --- /dev/null +++ b/erp24/records/AdminMenuPositions.php @@ -0,0 +1,55 @@ + 'ID', + 'admin_group_id' => 'Admin Group ID', + 'crm_menu_id' => 'Crm Menu ID', + 'is_parent' => 'Is Parent', + 'position' => 'Position' + ]; + } +} \ No newline at end of file diff --git a/erp24/templates/top.php b/erp24/templates/top.php index 22dd4f1f..8361ea09 100644 --- a/erp24/templates/top.php +++ b/erp24/templates/top.php @@ -95,11 +95,14 @@ elseif(!is_mobile()) echo" [MENU_COLLAPSE]"; - - +