Menu.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php namespace Manage\Lib;
  2. use Dever;
  3. use Dever\Project;
  4. class Menu
  5. {
  6. # 初始化菜单
  7. public function init($name = '')
  8. {
  9. if ($name) {
  10. $app = array($name => Project::load($name));
  11. } else {
  12. $app = Project::read();
  13. }
  14. foreach ($app as $k => $v) {
  15. $base = $v['path'] . 'table/manage/core.php';
  16. if (is_file($base)) {
  17. $core = include $base;
  18. if ($core) {
  19. $this->add($k, $core);
  20. }
  21. }
  22. }
  23. return 'ok';
  24. }
  25. private function add($app, $core)
  26. {
  27. if (isset($core['system'])) {
  28. foreach ($core['system'] as $k => $v) {
  29. $where = array();
  30. $where['key'] = $k;
  31. $data = $where;
  32. $data['name'] = $v['name'];
  33. $data['sort'] = $v['sort'];
  34. $data['partition'] = $v['partition'] ?? 'database';
  35. $data['relation_table'] = $v['relation_table'];
  36. $data['relation_field'] = $v['relation_field'];
  37. $data['relation_user_table'] = $v['relation_user_table'];
  38. Dever::db('system')->up($where, $data);
  39. }
  40. }
  41. if (isset($core['menu'])) {
  42. foreach ($core['menu'] as $k => $v) {
  43. $where = array();
  44. if (isset($v['app'])) {
  45. $app = $v['app'];
  46. }
  47. $where['app'] = $app;
  48. $where['key'] = $k;
  49. if (isset($v['parent'])) {
  50. $parent = Dever::db('menu')->find(array('key' => $v['parent']));
  51. if ($parent) {
  52. $where['parent_id'] = $parent['id'];
  53. $where['system_id'] = $parent['system_id'];
  54. $where['level'] = 2;
  55. if ($parent['parent_id']) {
  56. $where['level'] = 3;
  57. }
  58. }
  59. } else {
  60. $where['level'] = 1;
  61. }
  62. if (isset($v['system'])) {
  63. $system = Dever::db('system')->find(array('key' => $v['system']));
  64. if ($system) {
  65. $where['system_id'] = $system['id'];
  66. }
  67. }
  68. $data = $where;
  69. $data['name'] = $v['name'];
  70. $data['icon'] = $v['icon'];
  71. $data['sort'] = $v['sort'];
  72. if (isset($v['show'])) {
  73. $data['show'] = $v['show'];
  74. }
  75. if (isset($v['badge'])) {
  76. $data['badge'] = $v['badge'];
  77. }
  78. if (isset($v['path'])) {
  79. $data['path'] = $v['path'];
  80. } else {
  81. $data['path'] = 'main';
  82. }
  83. if (isset($v['link'])) {
  84. $data['link'] = $v['link'];
  85. }
  86. Dever::db('menu')->up($where, $data);
  87. }
  88. }
  89. }
  90. public function getAll()
  91. {
  92. $data = Dever::db('menu')->select(array('parent_id' => '0'));
  93. return $data;
  94. }
  95. }