Top.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace Manage\Src;
  3. use Dever;
  4. class Top
  5. {
  6. /**
  7. * 获取所有的头部菜单权限
  8. *
  9. * @return array
  10. */
  11. public function all()
  12. {
  13. $top = Dever::db('manage/top')->main();
  14. $result = array();
  15. if ($top) {
  16. $child = Dever::db('manage/top')->child();
  17. foreach ($top as $k => $v) {
  18. $k = str_replace('/', '-', $k);
  19. $result[$k] = $v;
  20. if (isset($child[$v['id']])) {
  21. $c = 0;
  22. foreach ($child[$v['id']] as $i => $j) {
  23. $i = str_replace('/', '-', $i);
  24. $result[$k]['child'][$i] = $j;
  25. $c++;
  26. }
  27. if (!isset($result[$k]['child'])) {
  28. unset($result[$k]);
  29. }
  30. }
  31. }
  32. }
  33. return $result;
  34. }
  35. /**
  36. * menu 从数据库中得到所有权限
  37. *
  38. * @return array
  39. */
  40. public function get($auth = true)
  41. {
  42. # 得到当前的项目和表
  43. $config = Dever::load('manage/database.config');
  44. if (empty($config['top']) || is_array($config['top'])) {
  45. return array();
  46. }
  47. $auth = true;
  48. if ($auth) {
  49. # 需要验证权限
  50. $auth = Dever::load('manage/auth.top');
  51. }
  52. $top = Dever::db('manage/top')->main();
  53. if ($top) {
  54. $child = Dever::db('manage/top')->child();
  55. foreach ($top as $k => $v) {
  56. /*
  57. if ($auth && strpos($auth, $k . '_') === false) {
  58. unset($top[$k]);
  59. continue;
  60. }
  61. */
  62. if (isset($child[$v['id']])) {
  63. # 如果已经保存了,就直接取出来吧
  64. //$info = Dever::load('manage/auth.getTop', $v['key']);
  65. $info = Dever::load('manage/auth.getTop', $config['top']);
  66. if ($info) {
  67. $top[$k]['name'] = $info['name'];
  68. }
  69. $c = 0;
  70. foreach ($child[$v['id']] as $i => $j) {
  71. if (strstr($i, $config['top'])) {
  72. $i = str_replace('/', '-', $i);
  73. if (($auth && strpos($auth . ',', $i . ',') === false) || ($info && $info['id'] == $j['id'])) {
  74. } elseif (!$info && $c == 0) {
  75. $top[$k]['name'] = $j['name'];
  76. Dever::load('manage/auth._setTop', $j);
  77. $c++;
  78. } else {
  79. $top[$k]['child'][$i] = $j;
  80. $c++;
  81. }
  82. }
  83. }
  84. if (!isset($top[$k]['child'])) {
  85. //unset($top[$k]);
  86. }
  87. }
  88. }
  89. }
  90. if (isset($_GET['test']) && $_GET['test'] == 1) {
  91. print_r($config);
  92. print_r($child);
  93. print_r($top);die;
  94. }
  95. //print_r($top);die;
  96. return $top;
  97. }
  98. /**
  99. * 更新数据到数据库
  100. *
  101. * @return array
  102. */
  103. public function update($param = array())
  104. {
  105. if (isset($param['key'])) {
  106. $info = Dever::db('manage/top')->key(array('where_key' => $param['key']));
  107. //print_r($info);die;
  108. if (!$info) {
  109. $update['add_key'] = $param['key'];
  110. $update['add_name'] = $param['name'];
  111. $update['add_top_id'] = isset($param['top']) ? $param['top'] : -1;
  112. $update['add_value'] = $param['value'];
  113. $update['add_state'] = isset($param['state']) ? $param['state'] : 1;
  114. $info['id'] = Dever::db('manage/top')->insert($update);
  115. } else {
  116. $update['set_name'] = $param['name'];
  117. $update['set_value'] = $param['value'];
  118. $update['set_state'] = isset($param['state']) ? $param['state'] : 1;
  119. $update['where_id'] = $info['id'];
  120. Dever::db('manage/top')->update($update);
  121. }
  122. return $info['id'];
  123. }
  124. return false;
  125. }
  126. /**
  127. * 同步子权限更新到数据库
  128. *
  129. * @return array
  130. */
  131. public function sync($id, $name, $param = array())
  132. {
  133. if ($id && isset($param['top'])) {
  134. $key = $param['top']['key'];
  135. $info = Dever::db('manage/top')->key(array('where_key' => $key));
  136. if ($info) {
  137. $update['value'] = Dever::input('where_id', $id);
  138. $update['name'] = Dever::param($param['top']['col'], $param);
  139. $update['top'] = $info['id'];
  140. if (isset($param['top']['type']) && $param['top']['type']) {
  141. $type = Dever::param($param['top']['type'], $param);
  142. $update['key'] = $key . '-' . $type . '_' . $update['value'];
  143. } else {
  144. $update['key'] = $key . '_' . $update['value'];
  145. }
  146. $update['state'] = Dever::param('state', $param);
  147. $this->update($update);
  148. }
  149. }
  150. }
  151. /**
  152. * 更新当前使用的top菜单
  153. *
  154. * @return array
  155. */
  156. public function update_action_api($id = false)
  157. {
  158. $id = $id ? $id : Dever::input('id');
  159. $id = intval($id);
  160. if ($id > 0) {
  161. # 需要验证权限
  162. $auth = Dever::load('manage/auth.top');
  163. $info = Dever::db('manage/top')->one($id);
  164. $info['key'] = str_replace('/', '-', $info['key']);
  165. if ($info && $info['top_id'] > 0 && (!$auth || strpos($auth . ',', $info['key'] . ',') !== false)) {
  166. Dever::load('manage/auth._setTop', $info);
  167. }
  168. return $info['value'];
  169. }
  170. }
  171. }