e791cad89d6bc4934070ed86b367cd81a8bfd59d.svn-base 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | menu.php 后台管理 左侧菜单管理
  5. |--------------------------------------------------------------------------
  6. */
  7. namespace MazeApp\Manage;
  8. use Maze;
  9. Maze::load('manage/auth.init');
  10. class Menu
  11. {
  12. /**
  13. * fast
  14. *
  15. * @var array
  16. */
  17. private $fast = array();
  18. /**
  19. * data
  20. *
  21. * @var array
  22. */
  23. private $data = array();
  24. /**
  25. * __construct
  26. *
  27. * @return mixed
  28. */
  29. public function __construct()
  30. {
  31. }
  32. public function checkKey($id = 0)
  33. {
  34. $id = Maze::input('where_id');
  35. $key = Maze::input('update_key');
  36. if($key && !$id)
  37. {
  38. $info = Maze::load('manage/menu-key', array('where_key' => $key));
  39. if($info)
  40. {
  41. Maze::abert('该菜单唯一key已被使用,请换一个吧');
  42. }
  43. }
  44. elseif($id > 0)
  45. {
  46. $info = Maze::load('manage/menu-keyId', array('where_key' => $key, 'where_id' => $id));
  47. if($info)
  48. {
  49. Maze::abert('该菜单唯一key已被使用,请换一个吧');
  50. }
  51. }
  52. }
  53. /**
  54. * menu 从数据库中得到所有菜单,如果为空,则从项目中读取一次
  55. *
  56. * @return array
  57. */
  58. private function get($auth = '')
  59. {
  60. $menu = Maze::load('manage/menu-main');
  61. if($menu)
  62. {
  63. $this->fast = array();
  64. $child = Maze::load('manage/menu-child');
  65. if($auth && is_array($auth))
  66. {
  67. $auth = implode(',', $auth);
  68. }
  69. foreach($menu as $k => $v)
  70. {
  71. if($auth && strpos($auth, $k . '.') === false)
  72. {
  73. unset($menu[$k]);
  74. continue;
  75. }
  76. if(isset($child[$v['id']]))
  77. {
  78. foreach($child[$v['id']] as $i => $j)
  79. {
  80. if($auth && strpos($auth, $i) === false)
  81. {
  82. //unset($child[$parent][$i]);
  83. //continue;
  84. }
  85. else
  86. {
  87. if($j['link'])
  88. {
  89. $j['link'] = Maze::url($j['link'] . '&menu_id=' . $j['id']);
  90. }
  91. $menu[$k]['child'][$i] = $j;
  92. if($j['fast'] == 1)
  93. {
  94. $this->fast[$i] = $j;
  95. }
  96. }
  97. }
  98. if(!isset($menu[$k]['child']))
  99. {
  100. unset($menu[$k]);
  101. }
  102. }
  103. }
  104. }
  105. else
  106. {
  107. # 数据为空,读取一次,后续就得手动读取了
  108. $this->load();
  109. $menu = $this->get();
  110. }
  111. return $menu;
  112. }
  113. /**
  114. * 清空所有菜单
  115. *
  116. * @return array
  117. */
  118. public function delete()
  119. {
  120. $data = $menu = Maze::load('manage/menu-all');
  121. foreach($data as $k => $v)
  122. {
  123. }
  124. Maze::out('yes');
  125. }
  126. /**
  127. * 更新菜单
  128. *
  129. * @return array
  130. */
  131. public function update()
  132. {
  133. $this->load();
  134. Maze::out('yes');
  135. }
  136. /**
  137. * 载入从项目中定义好的菜单
  138. *
  139. * @return array
  140. */
  141. public function load()
  142. {
  143. $project = Maze::load('manage/project.get');
  144. # 这样写是直接调用datbase类 或者 new MazeApp\Manage\Database
  145. $database = Maze::load('manage/database.');
  146. $top = Maze::load('manage/top.');
  147. foreach($project as $k => $v)
  148. {
  149. $project[$k]['name'] = $v['lang'];
  150. $config = $database->config($k, $v['path']);
  151. if($config)
  152. {
  153. foreach($config as $i => $j)
  154. {
  155. if(isset($j['auth']) && is_array($j['auth']))
  156. {
  157. # 加入精细控制权限的菜单
  158. $j['auth']['name'] = $project[$k]['name'] . '-' . $j['auth']['name'];
  159. $top_id = $top->update($j['auth']);
  160. $auth = Maze::load($k . '/' . $i . '-' . $j['auth']['data']);
  161. if($top_id && $auth)
  162. {
  163. foreach($auth as $m => $n)
  164. {
  165. $p['key'] = $j['auth']['key'] . '_' . $n['id'];
  166. $p['name'] = $n[$j['auth']['col']];
  167. $p['top'] = $top_id;
  168. $p['value'] = $n['id'];
  169. $top->update($p);
  170. }
  171. }
  172. }
  173. if(!isset($j['menu']))
  174. {
  175. $j['menu'] = true;
  176. }
  177. if(isset($j['manage']) && $j['menu'] != false)
  178. {
  179. if(is_string($j['menu']))
  180. {
  181. $m = $j['menu'];
  182. }
  183. else
  184. {
  185. $m = $k;
  186. }
  187. if($m == $k)
  188. {
  189. $key = $k . '.' . $i;
  190. }
  191. else
  192. {
  193. $key = $m . '.' . $k . '.' . $i;
  194. }
  195. $project[$m]['child'][$i]['key'] = $key;
  196. $project[$m]['child'][$i]['name'] = $j['lang'];
  197. $project[$m]['child'][$i]['link'] = 'project/database/list?key=' . $k . '&table=' . $i . '&menu=' . $m;
  198. $project[$m]['child'][$i]['order'] = isset($j['order']) ? $j['order'] : 1;
  199. $project[$m]['child'][$i]['fast'] = isset($j['fast']) ? $j['fast'] : 2;
  200. }
  201. }
  202. }
  203. else
  204. {
  205. unset($project[$k]);
  206. }
  207. }
  208. //print_r($project);die;
  209. # 将所有数据都记录到菜单中
  210. if($project)
  211. {
  212. foreach($project as $k => $v)
  213. {
  214. if(!isset($v['child']))
  215. {
  216. unset($project[$k]);
  217. continue;
  218. }
  219. if($v && isset($v['lang']) && $v['child'])
  220. {
  221. $id = $this->updateMenu($k, $v['lang'], $v['path'] . ':' . $v['lib'], -1, $v['order'], 2);
  222. if($id > 0)
  223. {
  224. foreach($v['child'] as $i => $j)
  225. {
  226. $this->updateMenu($j['key'], $j['name'], $j['link'], $id, $j['order'], $j['fast']);
  227. }
  228. }
  229. }
  230. }
  231. }
  232. return $project;
  233. }
  234. /**
  235. * 更新菜单到数据库
  236. *
  237. * @return array
  238. */
  239. public function updateMenu($key, $name, $link, $menu, $order, $fast)
  240. {
  241. $info = Maze::load('manage/menu-key', array('where_key' => $key));
  242. if(!$info)
  243. {
  244. $update['add_key'] = $key;
  245. $update['add_name'] = $name;
  246. $update['add_link'] = $link;
  247. $update['add_menu_id'] = $menu;
  248. $update['add_reorder'] = $order;
  249. $update['add_fast'] = $fast;
  250. $update['add_state'] = 1;
  251. $update['add_type'] = 2;
  252. $info['id'] = Maze::load('manage/menu-insert', $update);
  253. }
  254. /*
  255. elseif($link != $info['link'])
  256. {
  257. $update['set_link'] = $link;
  258. $id = $update['where_id'] = $info['id'];
  259. Maze::load('manage/menu-update', $update);
  260. }
  261. */
  262. return $info['id'];
  263. }
  264. /**
  265. * menu 生成左侧的菜单 此处应加上权限来显示是否管理员有该菜单的权限
  266. *
  267. * @return array
  268. */
  269. public function left($state = false)
  270. {
  271. if($this->data)
  272. {
  273. return $this->data;
  274. }
  275. $auth = '';
  276. if($state == false)
  277. {
  278. # 需要验证权限
  279. $auth = Maze::load('manage/auth.admin');
  280. }
  281. //$auth = 'manage_admin';
  282. $menu = $this->get($auth);
  283. $this->data = $menu;
  284. return $menu;
  285. /*
  286. //$auth = 'manage_admin,image_thumb';
  287. foreach($project as $k => $v)
  288. {
  289. if($auth && strpos($auth, $k . '_') === false)
  290. {
  291. unset($project[$k]);
  292. continue;
  293. }
  294. if($auth && strpos($auth, $k . '_' . $i) === false)
  295. {
  296. //unset($project[$k]['child'][$i]);
  297. continue;
  298. }
  299. if(!isset($v['child']))
  300. {
  301. unset($project[$k]);
  302. continue;
  303. }
  304. if($k == 'manage')
  305. {
  306. $project[$k]['child']['manage_project'] = array
  307. (
  308. 'name' => '管理菜单设置',
  309. 'link' => Maze::load('manage/menu.link#project'),
  310. 'order' => 1,
  311. );
  312. $project[$k]['child']['manage_log'] = array
  313. (
  314. 'name' => '管理操作日志',
  315. 'link' => Maze::load('manage/menu.link#log'),
  316. 'order' => 1,
  317. );
  318. //print_r($project[$k]);die;
  319. }
  320. //array_multisort($v['child_order'], SORT_DESC, $v['child']);
  321. $order = array();
  322. foreach($project[$k]['child'] as $i => $j)
  323. {
  324. $order[] = $j['order'];
  325. }
  326. array_multisort($order, SORT_DESC, $project[$k]['child']);
  327. }
  328. //此处加缓存吧,不过要针对用户,加到session中也可以
  329. return $project;
  330. */
  331. }
  332. /**
  333. * menu 获取快捷管理菜单
  334. *
  335. * @return array
  336. */
  337. public function fast()
  338. {
  339. $this->left();
  340. return $this->fast;
  341. }
  342. /**
  343. * menu 对一些固定的菜单,生成其链接
  344. *
  345. * @return array
  346. */
  347. public function link()
  348. {
  349. return array
  350. (
  351. 'project' => Maze::url('project/list?menu=manage&table=manage_project'),
  352. 'log' => Maze::url('log/list?menu=manage&table=manage_log'),
  353. 'logout' => Maze::url('auth.quit'),
  354. 'index' => Maze::url(''),
  355. );
  356. }
  357. /**
  358. * database
  359. *
  360. * @return array
  361. */
  362. public function database()
  363. {
  364. $this->config();
  365. $result = array();
  366. $method = $this->project . '/' . $this->table . '-list';
  367. $data = Maze::load($method);
  368. //print_r($data);die;
  369. //print_r(Helper::page("current"));die;
  370. if($data)
  371. {
  372. foreach($data as $k => $v)
  373. {
  374. $result[$k] = '<tr>';
  375. foreach($this->config['manage']['list'] as $ki => $vi)
  376. {
  377. if(isset($v[$vi['col']]) && $v[$vi['col']])
  378. {
  379. if(isset($vi['value']))
  380. {
  381. $eval = '$value = ' . str_replace('{col}', $v[$vi['col']], $vi['value']) . ';';
  382. eval($eval);
  383. if($value) $v[$vi['col']] = $value;
  384. }
  385. elseif(isset($vi['modal']))
  386. {
  387. $v[$vi['col']] = '<a href="#" data-toggle="modal" data-target="#maze_modal" onclick="$(\'#maze_modal_body\').html($(this).next().html())">' . $vi['modal'] . '</a><div style="display:none;">' . $v[$vi['col']] . '</div>';
  388. }
  389. elseif(isset($vi['option']))
  390. {
  391. $v[$vi['col']] = $vi['option'][$v[$vi['col']]];
  392. }
  393. $result[$k] .= '<td>' . $v[$vi['col']] . '</td>';
  394. }
  395. elseif($vi['col'] == 'manage' && isset($vi['value']) && is_array($vi['value']))
  396. {
  397. $result[$k] .= '<td>';
  398. foreach($vi['value'] as $kj => $vj)
  399. {
  400. $result[$k] .= '<a href="' . $this->url($kj, $v['id']) . '" class="oper_' . $kj . '">' . $vj . '</a>';
  401. }
  402. $result[$k] .= '</td>';
  403. }
  404. }
  405. $result[$k] .= '</tr>';
  406. }
  407. }
  408. return $result;
  409. }
  410. }