123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php namespace Manage\Lib;
- use Dever;
- use Dever\Project;
- class Menu
- {
- # 初始化菜单
- public function init($name = '')
- {
- $config = Dever::config('manage');
- if ($config) {
- $this->add('manage', $config);
- }
- if ($name) {
- $app = array($name => Project::load($name));
- } else {
- $app = Project::read();
- }
- foreach ($app as $k => $v) {
- $base = $v['path'] . 'manage/core.php';
- if (is_file($base)) {
- $core = include $base;
- if ($core) {
- $this->add($k, $core);
- }
- }
- }
- return 'ok';
- }
- private function add($app, $core)
- {
- if (isset($core['system'])) {
- foreach ($core['system'] as $k => $v) {
- $where = array();
- $where['key'] = $k;
- $data = $where;
- $data['name'] = $v['name'];
- $data['sort'] = $v['sort'];
- $data['partition'] = $v['partition'] ?? 'no';
- $data['info_table'] = $v['info_table'];
- $data['user_table'] = $v['user_table'];
- $data['role_table'] = $v['role_table'];
- Dever::db('system', 'manage')->up($where, $data);
- }
- }
- if (isset($core['module'])) {
- foreach ($core['module'] as $k => $v) {
- $where = array();
- $where['key'] = $k;
- $data = $where;
- $data['name'] = $v['name'];
- $data['sort'] = $v['sort'];
- $data['system'] = $v['system'];
- $data['data_table'] = $v['data_table'];
- if (isset($v['data_where']) && $v['data_where']) {
- $data['data_where'] = Dever::json_encode($v['data_where']);
- }
- Dever::db('system_module', 'manage')->up($where, $data);
- }
- }
- if (isset($core['menu'])) {
- foreach ($core['menu'] as $k => $v) {
- $where = array();
- if (isset($v['app'])) {
- $app = $v['app'];
- }
- $where['app'] = $app;
- $where['key'] = $k;
- if (isset($v['parent'])) {
- $parent = Dever::db('menu', 'manage')->find(array('key' => $v['parent']));
- if ($parent) {
- $where['parent_id'] = $parent['id'];
- $where['module_id'] = $parent['module_id'];
- $where['level'] = 2;
- if ($parent['parent_id']) {
- $where['level'] = 3;
- }
- }
- } else {
- $where['level'] = 1;
- }
- if (isset($v['module'])) {
- $module = Dever::db('system_module', 'manage')->find(array('key' => $v['module']));
- if ($module) {
- $where['module_id'] = $module['id'];
- }
- }
- $data = $where;
- $data['name'] = $v['name'];
- $data['icon'] = $v['icon'];
- $data['sort'] = $v['sort'];
- if (isset($v['show'])) {
- $data['show'] = $v['show'];
- }
- if (isset($v['badge'])) {
- $data['badge'] = $v['badge'];
- }
- if (isset($v['path'])) {
- $data['path'] = $v['path'];
- } else {
- $data['path'] = 'main';
- }
- if (isset($v['link'])) {
- $data['link'] = $v['link'];
- }
- Dever::db('menu', 'manage')->up($where, $data);
- }
- }
- }
- public function getAll()
- {
- $data = Dever::db('menu', 'manage')->select(array('parent_id' => '0'));
- return $data;
- }
- }
|