table_common_block_permission.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: table_common_block_permission.php 27846 2012-02-15 09:04:33Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class table_common_block_permission extends discuz_table
  12. {
  13. public function __construct() {
  14. $this->_table = 'common_block_permission';
  15. $this->_pk = '';
  16. parent::__construct();
  17. }
  18. public function fetch($bid, $uid){
  19. return ($bid = dintval($bid)) && ($uid = dintval($uid)) ? DB::fetch_first('SELECT * FROM %t WHERE bid=%d AND uid=%d', array($this->_table, $bid, $uid)) : array();
  20. }
  21. public function fetch_all_by_bid($bid, $uid = 0) {
  22. return ($bid = dintval($bid, true)) ? DB::fetch_all('SELECT * FROM %t WHERE bid=%d'.($uid ? ' AND '.DB::field('uid', $uid) : '').' ORDER BY inheritedtplname', array($this->_table, $bid), 'uid') : array();
  23. }
  24. public function fetch_all_by_uid($uids, $flag = true, $sort = 'ASC', $start = 0, $limit = 0) {
  25. $wherearr = array();
  26. $sort = $sort === 'ASC' ? 'ASC' : 'DESC';
  27. if(($uids = dintval($uids))) {
  28. $wherearr[] = DB::field('uid', $uids);
  29. }
  30. if(!$flag) {
  31. $wherearr[] = 'inheritedtplname = \'\'';
  32. }
  33. $where = $wherearr ? ' WHERE '.implode(' AND ', $wherearr) : '';
  34. return DB::fetch_all('SELECT * FROM '.DB::table($this->_table).$where.' ORDER BY uid '.$sort.', inheritedtplname'.DB::limit($start, $limit), NULL, ($uids && !is_array($uids)) ? 'bid' : '');
  35. }
  36. public function count_by_uids($uids, $flag) {
  37. $wherearr = array();
  38. if(($uids = dintval($uids, true))) {
  39. $wherearr[] = DB::field('uid', $uids);
  40. }
  41. if(!$flag) {
  42. $wherearr[] = 'inheritedtplname = \'\'';
  43. }
  44. $where = $wherearr ? ' WHERE '.implode(' AND ', $wherearr) : '';
  45. return DB::result_first('SELECT COUNT(*) FROM '.DB::table($this->_table).$where);
  46. }
  47. public function fetch_permission_by_uid($uids) {
  48. return ($uids = dintval($uids, true)) ? DB::fetch_all('SELECT uid, sum(allowmanage) as allowmanage, sum(allowrecommend) as allowrecommend, sum(needverify) as needverify FROM '.DB::table($this->_table)." WHERE ".DB::field('uid', $uids)." GROUP BY uid", null, 'uid') : array();
  49. }
  50. public function delete_by_bid_uid_inheritedtplname($bid = false, $uids = false, $inheritedtplname = false) {
  51. $wherearr = array();
  52. if(($bid = dintval($bid, true))) {
  53. $wherearr[] = DB::field('bid', $bid);
  54. }
  55. if(($uids = dintval($uids, true))) {
  56. $wherearr[] = DB::field('uid', $uids);
  57. }
  58. if($inheritedtplname === true) {
  59. $wherearr[] = "inheritedtplname!=''";
  60. } elseif($inheritedtplname !== false && is_string($inheritedtplname)) {
  61. $wherearr[] = DB::field('inheritedtplname', $inheritedtplname);
  62. }
  63. return $wherearr ? DB::delete($this->_table, implode(' AND ', $wherearr)) : false;
  64. }
  65. public function insert_batch($users, $bids, $tplname = '') {
  66. $blockperms = array();
  67. if(!empty($users) && $bids = dintval($bids, true)){
  68. $uids = $notinherit = array();
  69. foreach($users as &$user) {
  70. if(($user['uid'] = dintval($user['uid']))) {
  71. $uids[] = $user['uid'];
  72. }
  73. }
  74. if(!empty($uids)) {
  75. foreach($this->fetch_all_by_uid($uids, false) as $value) {
  76. if(in_array($value['bid'], $bids)) {
  77. $notinherit[$value['bid']][$value['uid']] = true;
  78. }
  79. }
  80. }
  81. foreach($users as $user) {
  82. if($user['uid']) {
  83. $tplname = !empty($user['inheritedtplname']) ? $user['inheritedtplname'] : $tplname;
  84. foreach ($bids as $bid) {
  85. if(empty($notinherit[$bid][$user['uid']])) {
  86. $blockperms[] = "('$bid','$user[uid]','$user[allowmanage]','$user[allowrecommend]','$user[needverify]','$tplname')";
  87. }
  88. }
  89. }
  90. }
  91. if($blockperms) {
  92. DB::query('REPLACE INTO '.DB::table($this->_table).' (bid,uid,allowmanage,allowrecommend,needverify,inheritedtplname) VALUES '.implode(',', $blockperms));
  93. return $uids;
  94. } else {
  95. return FALSE;
  96. }
  97. }
  98. return false;
  99. }
  100. public function insert_by_bid($bid, $users) {
  101. $sqlarr = $uids = array();
  102. $bid = intval($bid);
  103. if(!empty($bid) && !empty($users)) {
  104. foreach ($users as $v) {
  105. if(($v['uid'] = dintval($v['uid']))) {
  106. $sqlarr[] = "('$bid','$v[uid]','$v[allowmanage]','$v[allowrecommend]','$v[needverify]','')";
  107. $uids[] = $v['uid'];
  108. }
  109. }
  110. if(!empty($sqlarr)) {
  111. DB::query('REPLACE INTO '.DB::table($this->_table).' (bid,uid,allowmanage,allowrecommend,needverify,inheritedtplname) VALUES '.implode(',', $sqlarr));
  112. }
  113. }
  114. return $uids;
  115. }
  116. }
  117. ?>