table_forum_collection.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_forum_collection.php 31438 2012-08-28 06:03:08Z chenmengshu $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class table_forum_collection extends discuz_table
  12. {
  13. public function __construct() {
  14. $this->_table = 'forum_collection';
  15. $this->_pk = 'ctid';
  16. $this->_pre_cache_key = 'forum_collection_';
  17. parent::__construct();
  18. }
  19. public function count_by_uid($uid) {
  20. return DB::result_first('SELECT COUNT(*) FROM %t WHERE uid=%d', array($this->_table, $uid), $this->_pk);
  21. }
  22. public function fetch_all_by_uid($uid, $start = 0, $limit = 0, $exceptctid = null) {
  23. if($exceptctid) {
  24. $sql = ' AND ctid!='.intval($exceptctid);
  25. } else {
  26. $sql = '';
  27. }
  28. return DB::fetch_all('SELECT * FROM %t WHERE uid=%d %i'.DB::limit($start, $limit), array($this->_table, $uid, $sql), $this->_pk);
  29. }
  30. public function range($start = 0, $limit = 0, $reqthread = 0, $pK = true) {
  31. return DB::fetch_all('SELECT * FROM %t WHERE threadnum>=%d ORDER BY lastupdate DESC '.DB::limit($start, $limit), array($this->_table, $reqthread), $pK ? $this->_pk : '');
  32. }
  33. public function fetch_all($ctid = '', $orderby = '', $ordersc = '', $start = 0, $limit = 0, $title = '', $cachetid = '') {
  34. if($this->_allowmem && $cachetid) {
  35. $data = $this->fetch_cache($cachetid, $this->_pre_cache_key.'tid_');
  36. if($data) {
  37. return $data;
  38. }
  39. }
  40. $sql = '';
  41. if($ctid) {
  42. $sql .= 'WHERE '.DB::field('ctid', $ctid);
  43. }
  44. if($title && str_replace('%', '', $title)) {
  45. $sql .= ($sql ? ' AND ' : 'WHERE ').DB::field('name', '%'.$title.'%', 'like');
  46. }
  47. $sql .= ($orderby = DB::order($orderby, $ordersc)) ? ' ORDER BY '.$orderby : '';
  48. $sql .= ' '.DB::limit($start, $limit);
  49. if(!$sql) {
  50. return null;
  51. }
  52. $data = DB::fetch_all('SELECT * FROM %t %i', array($this->_table, $sql), $this->_pk);
  53. if($this->_allowmem && $cachetid) {
  54. $this->store_cache($cachetid, $data, $this->_cache_ttl, $this->_pre_cache_key.'tid_');
  55. }
  56. return $data;
  57. }
  58. public function count_by_title($title) {
  59. if(!$title || !str_replace('%', '', $title)) {
  60. return null;
  61. }
  62. $sql = DB::field('name', '%'.$title.'%', 'like');
  63. return DB::result_first('SELECT count(*) FROM %t WHERE %i', array($this->_table, $sql));
  64. }
  65. public function count_all_by_uid($uid) {
  66. return DB::result_first('SELECT COUNT(*) FROM %t WHERE uid=%d', array($this->_table, $uid));
  67. }
  68. public function update_by_ctid($ctid, $incthreadnum = 0, $incfollownum = 0, $inccommentnum = 0, $lastupdate = 0, $incratenum = 0, $totalratenum = 0, $lastpost = '') {
  69. if(!$ctid) {
  70. return false;
  71. }
  72. $sql = array();
  73. $para = array($this->_table);
  74. if($incthreadnum) {
  75. $sql[] = 'threadnum=threadnum+\'%d\'';
  76. $para[] = $incthreadnum;
  77. }
  78. if($incfollownum) {
  79. $sql[] = 'follownum=follownum+\'%d\'';
  80. $para[] = $incfollownum;
  81. }
  82. if($inccommentnum) {
  83. $sql[] = 'commentnum=commentnum+\'%d\'';
  84. $para[] = $inccommentnum;
  85. }
  86. if($lastupdate != 0) {
  87. $sql[] = 'lastupdate=%d';
  88. $para[] = $lastupdate;
  89. }
  90. if($incratenum > 0) {
  91. if($totalratenum > 0) {
  92. $sql[] = 'rate=((rate*ratenum)+\'%d\')/(ratenum+1),ratenum=ratenum+1';
  93. } else {
  94. $sql[] = 'ratenum=ratenum+1,rate=%d';
  95. }
  96. $para[] = $incratenum;
  97. }
  98. if(count($lastpost) == 4) {
  99. $sql[] = 'lastpost=%d,lastsubject=%s,lastposttime=%d,lastposter=%s';
  100. $para = array_merge($para, array($lastpost['lastpost'], $lastpost['lastsubject'], $lastpost['lastposttime'], $lastpost['lastposter']));
  101. }
  102. if(!count($sql)) {
  103. return null;
  104. }
  105. $sqlupdate = implode(',', $sql);
  106. $result = DB::query('UPDATE %t SET '.$sqlupdate.' WHERE '.DB::field($this->_pk, $ctid), $para, false, true);
  107. return $result;
  108. }
  109. public function fetch_all_for_search($name, $ctid, $username, $uid, $start = 0, $limit = 20) {
  110. $where = '1';
  111. $where .= $name ? ' AND '.DB::field('name', '%'.stripsearchkey($name).'%', 'like') : '';
  112. $where .= $ctid ? ' AND '.DB::field('ctid', $ctid) : '';
  113. $where .= $username ? ' AND '.DB::field('username', '%'.stripsearchkey($username).'%', 'like') : '';
  114. $where .= $uid ? ' AND '.DB::field('uid', $uid) : '';
  115. if($start == -1) {
  116. return DB::result_first("SELECT count(*) FROM %t WHERE %i", array($this->_table, $where));
  117. }
  118. return DB::fetch_all("SELECT * FROM %t WHERE %i ORDER BY dateline DESC %i", array($this->_table, $where, DB::limit($start, $limit)));
  119. }
  120. public function update($val, $data, $unbuffered = false, $low_priority = false) {
  121. if(!empty($data) && is_array($data) && $val) {
  122. $this->checkpk();
  123. return DB::update($this->_table, $data, DB::field($this->_pk, $val), $unbuffered, $low_priority);
  124. }
  125. return !$unbuffered ? 0 : false;
  126. }
  127. public function fetch_ctid_by_searchkey($searchkey, $limit) {
  128. return DB::fetch_all('SELECT ctid FROM %t WHERE 1 %i ORDER BY ctid DESC %i', array($this->_table, $searchkey, DB::limit(0, $limit)));
  129. }
  130. public function delete($val, $unbuffered = false) {
  131. if(!$val) {
  132. return false;
  133. }
  134. $this->checkpk();
  135. $ret = DB::delete($this->_table, DB::field($this->_pk, $val), null, $unbuffered);
  136. return $ret;
  137. }
  138. public function fetch($id, $force_from_db = true){
  139. return parent::fetch($id, true);
  140. }
  141. }
  142. ?>