table_home_follow.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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_home_follow.php 28321 2012-02-28 03:03:51Z zhengqingpeng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class table_home_follow extends discuz_table
  12. {
  13. public function __construct() {
  14. $this->_table = 'home_follow';
  15. $this->_pk = '';
  16. $this->_pre_cache_key = 'home_follow_';
  17. parent::__construct();
  18. }
  19. public function fetch_all_following_by_uid($uid, $status = 0, $start = 0, $limit = 0) {
  20. $data = array();
  21. $wherearr = array();
  22. $force = !$start && !$limit && !$status ? false : true;
  23. if((!$force && ($data = $this->fetch_cache($uid)) === false) || $force) {
  24. $parameter = array($this->_table, $uid);
  25. $wherearr[] = 'uid=%d';
  26. if($status) {
  27. $wherearr[] = "status=%d";
  28. } else {
  29. $wherearr[] = "status!=%d";
  30. $status = -1;
  31. }
  32. $parameter[] = $status;
  33. $wheresql = !empty($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  34. $data = DB::fetch_all("SELECT * FROM %t $wheresql ORDER BY dateline DESC ".DB::limit($start, $limit), $parameter, 'followuid');
  35. if(!$force) {
  36. $this->store_cache($uid, $data, $this->_cache_ttl);
  37. }
  38. }
  39. return $data;
  40. }
  41. public function fetch_all_follower_by_uid($uids, $start = 0, $limit = 0) {
  42. $uids = dintval($uids, true);
  43. if($uids) {
  44. $parameter = array($this->_table, $uids);
  45. $fsql = is_array($uids) && $uids ? 'followuid IN(%n)' : 'followuid=%d';
  46. return DB::fetch_all("SELECT * FROM %t WHERE $fsql ORDER BY dateline DESC ".DB::limit($start, $limit), $parameter, 'uid');
  47. }
  48. return array();
  49. }
  50. public function fetch_all_by_uid_followuid($uid, $followuids) {
  51. $followuids = dintval($followuids, true);
  52. if($followuids) {
  53. return DB::fetch_all("SELECT * FROM %t WHERE uid=%d AND followuid IN(%n)", array($this->_table, $uid, $followuids), 'followuid');
  54. }
  55. return array();
  56. }
  57. public function fetch_status_by_uid_followuid($uid, $followuid) {
  58. return DB::fetch_all('SELECT * FROM %t WHERE (uid=%d AND followuid=%d) OR (uid=%d AND followuid=%d)', array($this->_table, $uid, $followuid, $followuid, $uid), 'uid');
  59. }
  60. public function fetch_all_by_uid_fusername($uid, $users) {
  61. if(empty($uid) || empty($users)) {
  62. return array();
  63. }
  64. return DB::fetch_all('SELECT * FROM %t WHERE uid=%d AND fusername IN(%n)', array($this->_table, $uid, $users));
  65. }
  66. public function fetch_all_by_uid_username($uid, $username = '', $start = 0, $limit = 0) {
  67. $parameter = array($this->_table, $uid);
  68. $wherearr = array('uid=%d');
  69. if(!empty($username)) {
  70. $parameter[] = $username.'%';
  71. $wherearr[] = "fusername LIKE %s";
  72. }
  73. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  74. return DB::fetch_all("SELECT * FROM %t $wheresql ORDER BY dateline DESC ".DB::limit($start, $limit), $parameter);
  75. }
  76. public function count_follow_user($uid, $type = 0, $dateline = 0) {
  77. $count = 0;
  78. $parameter = array($this->_table, $uid);
  79. $wherearr = array();
  80. $field = $type ? 'followuid' : 'uid';
  81. $wherearr[] = "$field=%d";
  82. $parameter[] = $uid;
  83. if($dateline) {
  84. $wherearr[] = "dateline >%d";
  85. $parameter[] = $dateline;
  86. }
  87. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  88. $count = DB::result_first("SELECT COUNT(*) FROM %t $wheresql", $parameter);
  89. return $count;
  90. }
  91. public function count_by_uid_username($uid, $username = '') {
  92. $parameter = array($this->_table, $uid);
  93. $wherearr = array('uid=%d');
  94. if(!empty($username)) {
  95. $parameter[] = $username.'%';
  96. $wherearr[] = "fusername LIKE %s";
  97. }
  98. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  99. $count = DB::result_first("SELECT COUNT(*) FROM %t $wheresql", $parameter);
  100. return $count;
  101. }
  102. public function insert($data, $return_insert_id = false, $replace = false, $silent = false) {
  103. if($data && is_array($data)) {
  104. $this->clear_cache($data['uid']);
  105. return DB::insert($this->_table, $data, $return_insert_id, $replace, $silent);
  106. }
  107. return 0;
  108. }
  109. public function fetch_by_uid_followuid($uid, $followuid) {
  110. return DB::fetch_first("SELECT * FROM %t WHERE uid=%d AND followuid=%d", array($this->_table, $uid, $followuid));
  111. }
  112. public function update_by_uid_followuid($uid, $followuid, $data) {
  113. $uid = dintval($uid, true);
  114. $followuid = dintval($followuid, true);
  115. if(!empty($data) && is_array($data) && $uid && $followuid) {
  116. $this->clear_cache($uid);
  117. return DB::update($this->_table, $data, DB::field('uid', $uid).' AND '.DB::field('followuid', $followuid));
  118. }
  119. return 0;
  120. }
  121. public function delete_by_uid_followuid($uid, $followuid) {
  122. $this->clear_cache($uid);
  123. return DB::query('DELETE FROM %t WHERE uid=%d AND followuid=%d', array($this->_table, $uid, $followuid));
  124. }
  125. }
  126. ?>