/admin/model/duty.php

https://github.com/pennsong/ShiyishiWeb · PHP · 161 lines · 138 code · 17 blank · 6 comment · 21 complexity · 469d5e5e8ac544b9a92d947cc707c368 MD5 · raw file

  1. <?php
  2. class duty_Model extends Model{
  3. function init(){
  4. $this->duty = Load::table('admin_duty');
  5. $this->cache = Load::lib('cache_file');
  6. $this->key = 'duty';
  7. }
  8. /*
  9. $type=1 一维数组, $type=2 多维数组
  10. */
  11. function children($parentid, $maxdepth = 0, $type = 1, $depth = 1, $all = array()){
  12. if($maxdepth > 0 && $depth > $maxdepth){
  13. return $all;
  14. }
  15. $pre_name = '';
  16. for($i=1;$i < $depth;$i++){
  17. if($i == 1){
  18. $pre_name = '&nbsp; |--';
  19. continue;
  20. }
  21. $pre_name = '&nbsp; | &nbsp;'.$pre_name;
  22. }
  23. $depth++;
  24. $rows = $this->duty->findBy($parentid, 'parent_id');
  25. foreach($rows as $row){
  26. $row['prename'] = $pre_name.$row['duty_name'];
  27. if($type == 1){
  28. $all[$row['id']] = $row;
  29. $all = $this->children($row['id'], $maxdepth, $type, $depth, $all);
  30. }else{
  31. $row['children'] = $this->children($row['id'], $maxdepth, $type, $depth);
  32. $all[$row['id']] = $row;
  33. }
  34. }
  35. return $all;
  36. }
  37. function all(){
  38. $rows = $this->children(0);
  39. return $rows;
  40. }
  41. function fetchAll($where,$order='order_id ASC'){
  42. return $this->duty->fetchAll($where, $order);
  43. }
  44. function find($id){
  45. return $this->duty->find($id);
  46. }
  47. function remove($id){
  48. if($this->children($id)){
  49. $this->setError('请先删除子菜单');
  50. return false;
  51. }
  52. $this->cache->remove($this->key);
  53. return $this->duty->delete($id);
  54. }
  55. function order($order){
  56. foreach($order as $id => $orderid){
  57. $this->duty->update(array('id' => $id, 'order_id' => $orderid));
  58. }
  59. $this->cache->remove($this->key);
  60. return true;
  61. }
  62. function save($info){
  63. if($_POST['formhash'] != $_COOKIE['formhash']){
  64. $this->setError('对不起,您提交的表单已经过期,请重新提交!');
  65. return false;
  66. }
  67. if(isset($info['duty_name'])){
  68. if(!$info['duty_name']){
  69. $this->setError('分组名字不能为空');
  70. }
  71. }
  72. if($this->isError())
  73. return false;
  74. $this->cache->remove($this->key);
  75. setcookie('formhash', '', -86400, '/');
  76. return $this->duty->save($info);
  77. }
  78. //获取组权限
  79. function getDutyAuth($group = '')
  80. {
  81. // get auth purview
  82. if(empty($group))return array();
  83. $authGroup = $this->duty->fetchRow("duty_link = '{$group}'");
  84. $authGroup['purview'] = unserialize($authGroup['purview']);
  85. return $this->getDutypurview($authGroup['purview']);
  86. }
  87. function getDutypurview($auth)
  88. {
  89. $authGroup['purview'] = $auth;
  90. try {
  91. $ext = array();
  92. if (is_array($authGroup['purview'])) {
  93. foreach ($authGroup['purview'] as $k=>$v)
  94. {
  95. foreach ($v as $vk=>$val){
  96. $val = str_ireplace(',',',',$val);
  97. if (strpos($val,',')) {
  98. $ext = explode(',',$val);
  99. unset($authGroup['purview'][$k][$vk]);
  100. $authGroup['purview'][$k] = F::array_values_merge($ext,$authGroup['purview'][$k]);
  101. }
  102. }
  103. }
  104. }
  105. }catch (Exception $e){};
  106. return $authGroup;
  107. }
  108. //获取组信息
  109. function getDuty($id = 0)
  110. {
  111. $rs = $this->duty->fetchRow("id = $id");
  112. if ($rs) {
  113. $auth = $this->getDutyAuth($rs['duty_link']);
  114. $rs['purview'] = $auth['purview'];
  115. return $rs;
  116. }else {
  117. return $this->getDutyAuth('guest');
  118. }
  119. }
  120. function getcache($key=null){
  121. if(!$key)$key = $this->key;
  122. if(!$row = $this->cache->get($key)){
  123. $row = $this->flushcache($key,true);
  124. }
  125. return $row;
  126. }
  127. function flushcache($key='',$return=false){
  128. if(!$key)$key = $this->key;
  129. $rows = $this->fetchAll(null,"parent_id asc,order_id ASC");
  130. $newdata = array();
  131. foreach($rows as $k=>$v){
  132. $newdata[$this->key][$v['id']] = $v['duty_name'];
  133. $newdata['duty_one'][$v['id']] = $v['duty_name'];
  134. }
  135. $this->cache->set($this->key,$newdata[$this->key]);
  136. $this->cache->set('duty_one',$newdata['duty_one']);
  137. if($return) return $newdata[$key];
  138. }
  139. function getName($id){
  140. $info = $this->find($id);
  141. return $info['duty_name'];
  142. }
  143. }
  144. ?>