144910092dfae16a5d1663e2cfcd66fe28dae4ef.svn-base 3.9 KB

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