Menu.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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::read($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. }
  55. }
  56. if (isset($v['system'])) {
  57. $system = Dever::db('system')->find(array('key' => $v['system']));
  58. if ($system) {
  59. $where['system_id'] = $system['id'];
  60. }
  61. }
  62. $data = $where;
  63. $data['name'] = $v['name'];
  64. $data['icon'] = $v['icon'];
  65. $data['sort'] = $v['sort'];
  66. if (isset($v['show'])) {
  67. $data['show'] = $v['show'];
  68. }
  69. if (isset($v['badge'])) {
  70. $data['badge'] = $v['badge'];
  71. }
  72. if (isset($v['path'])) {
  73. $data['path'] = $v['path'];
  74. } else {
  75. $data['path'] = 'main';
  76. }
  77. if (isset($v['link'])) {
  78. $data['link'] = $v['link'];
  79. }
  80. Dever::db('menu')->up($where, $data);
  81. }
  82. }
  83. }
  84. public function getAll()
  85. {
  86. $data = Dever::db('menu')->select(array('parent_id' => '0'));
  87. return $data;
  88. }
  89. }