table_forum_postcomment.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_postcomment.php 36284 2016-12-12 00:47:50Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class table_forum_postcomment extends discuz_table
  12. {
  13. private $cic_for_fetch_postcomment_by_pid;
  14. public function __construct() {
  15. $this->_table = 'forum_postcomment';
  16. $this->_pk = 'id';
  17. parent::__construct();
  18. }
  19. public function count_by_authorid($authorid) {
  20. return DB::result_first('SELECT COUNT(*) FROM %t WHERE authorid=%d', array($this->_table, $authorid));
  21. }
  22. public function count_by_pid($pid, $authorid = null, $score = null) {
  23. return DB::result_first('SELECT COUNT(*) FROM %t WHERE pid=%d '.($authorid ? ' AND '.DB::field('authorid', $authorid) : null).($score ? ' AND '.DB::field('score', $score) : null), array($this->_table, $pid, $authorid, $score));
  24. }
  25. public function count_by_tid($tid, $authorid = null, $score = null) {
  26. return DB::result_first('SELECT COUNT(*) FROM %t WHERE tid=%d '.($authorid ? ' AND '.DB::field('authorid', $authorid) : null).($score ? ' AND '.DB::field('score', $score) : null), array($this->_table, $tid, $authorid, $score));
  27. }
  28. public function count_by_search($tid = null, $pid = null, $authorid = null, $starttime = null, $endtime = null, $ip = null, $message = null) {
  29. $sql = '';
  30. $tid && $sql .= ' AND '.DB::field('tid', $tid);
  31. $pid && $sql .= ' AND '.DB::field('pid', $pid);
  32. $authorid && $sql .= ' AND '.DB::field('authorid', $authorid);
  33. $starttime && $sql .= ' AND '.DB::field('dateline', $starttime, '>=');
  34. $endtime && $sql .= ' AND '.DB::field('dateline', $endtime, '<');
  35. $ip && $sql .= ' AND '.DB::field('useip', str_replace('*', '%', $ip), 'like');
  36. if($message) {
  37. $sqlmessage = '';
  38. $or = '';
  39. $message = explode(',', str_replace(' ', '', $message));
  40. for($i = 0; $i < count($message); $i++) {
  41. if(preg_match("/\{(\d+)\}/", $message[$i])) {
  42. $message[$i] = preg_replace("/\\\{(\d+)\\\}/", ".{0,\\1}", preg_quote($message[$i], '/'));
  43. $sqlmessage .= " $or comment REGEXP '".$message[$i]."'";
  44. } else {
  45. $sqlmessage .= " $or ".DB::field('comment', '%'.$message[$i].'%', 'like');
  46. }
  47. $or = 'OR';
  48. }
  49. $sql .= " AND ($sqlmessage)";
  50. }
  51. return DB::result_first('SELECT COUNT(*) FROM %t WHERE authorid>-1 %i', array($this->_table, $sql));
  52. }
  53. public function fetch_all_by_search($tid = null, $pid = null, $authorid = null, $starttime = null, $endtime = null, $ip = null, $message = null, $start = null, $limit = null) {
  54. $sql = '';
  55. $tid && $sql .= ' AND '.DB::field('tid', $tid);
  56. $pid && $sql .= ' AND '.DB::field('pid', $pid);
  57. $authorid && $sql .= ' AND '.DB::field('authorid', $authorid);
  58. $starttime && $sql .= ' AND '.DB::field('dateline', $starttime, '>=');
  59. $endtime && $sql .= ' AND '.DB::field('dateline', $endtime, '<');
  60. $ip && $sql .= ' AND '.DB::field('useip', str_replace('*', '%', $ip), 'like');
  61. if($message) {
  62. $sqlmessage = '';
  63. $or = '';
  64. $message = explode(',', str_replace(' ', '', $message));
  65. for($i = 0; $i < count($message); $i++) {
  66. if(preg_match("/\{(\d+)\}/", $message[$i])) {
  67. $message[$i] = preg_replace("/\\\{(\d+)\\\}/", ".{0,\\1}", preg_quote($message[$i], '/'));
  68. $sqlmessage .= " $or comment REGEXP '".$message[$i]."'";
  69. } else {
  70. $sqlmessage .= " $or ".DB::field('comment', '%'.$message[$i].'%', 'like');
  71. }
  72. $or = 'OR';
  73. }
  74. $sql .= " AND ($sqlmessage)";
  75. }
  76. return DB::fetch_all('SELECT * FROM %t WHERE authorid>-1 %i ORDER BY dateline DESC '.DB::limit($start, $limit), array($this->_table, $sql));
  77. }
  78. public function fetch_all_by_authorid($authorid, $start = 0, $limit = 0) {
  79. return DB::fetch_all('SELECT * FROM %t WHERE authorid=%d ORDER BY dateline DESC '.DB::limit($start, $limit), array($this->_table, $authorid));
  80. }
  81. public function fetch_all_by_pid($pids) {
  82. if(empty($pids)) {
  83. return array();
  84. }
  85. return DB::fetch_all('SELECT * FROM %t WHERE '.DB::field('pid', $pids).' ORDER BY dateline DESC', array($this->_table));
  86. }
  87. public function fetch_all_by_pid_score($pid, $score) {
  88. return DB::fetch_all('SELECT * FROM %t WHERE pid=%d AND score=%d', array($this->_table, $pid, $score));
  89. }
  90. public function fetch_standpoint_by_pid($pid) {
  91. return DB::fetch_first('SELECT * FROM %t WHERE pid=%d AND authorid=-1', array($this->_table, $pid));
  92. }
  93. public function update_by_pid($pids, $data, $unbuffered = false, $low_priority = false, $authorid = null) {
  94. if(empty($data)) {
  95. return false;
  96. }
  97. $where = array();
  98. $where[] = DB::field('pid', $pids);
  99. $authorid !== null && $where[] = DB::field('authorid', $authorid);
  100. return DB::update($this->_table, $data, implode(' AND ', $where), $unbuffered, $low_priority);
  101. }
  102. public function delete_by_authorid($authorids, $unbuffered = false, $rpid = false) {
  103. if(empty($authorids)) {
  104. return false;
  105. }
  106. $where = array();
  107. $where[] = DB::field('authorid', $authorids);
  108. $rpid && $where[] = DB::field('rpid', 0, '>');
  109. return DB::delete($this->_table, implode(' AND ', $where), null, $unbuffered);
  110. }
  111. public function delete_by_tid($tids, $unbuffered = false, $authorids = null) {
  112. $where = array();
  113. $where[] = DB::field('tid', $tids);
  114. $authorids !== null && !(is_array($authorids) && empty($authorids)) && $where[] = DB::field('authorid', $authorids);
  115. return DB::delete($this->_table, implode(' AND ', $where), null, $unbuffered);
  116. }
  117. public function delete_by_pid($pids, $unbuffered = false, $authorid = null) {
  118. $where = array();
  119. $where[] = DB::field('pid', $pids);
  120. $authorid !== null && !(is_array($authorid) && empty($authorid)) && $where[] = DB::field('authorid', $authorid);
  121. return DB::delete($this->_table, implode(' AND ', $where), null, $unbuffered);
  122. }
  123. public function delete_by_rpid($rpids, $unbuffered = false) {
  124. if(empty($rpids)) {
  125. return false;
  126. }
  127. return DB::delete($this->_table, DB::field('rpid', $rpids), null, $unbuffered);
  128. }
  129. public function fetch_postcomment_by_pid($pids, $postcache, $commentcount, $totalcomment, $commentnumber) {
  130. $query = DB::query("SELECT * FROM ".DB::table('forum_postcomment')." WHERE pid IN (".dimplode($pids).') ORDER BY dateline DESC');
  131. $commentcount = $comments = array();
  132. while($comment = DB::fetch($query)) {
  133. if($comment['authorid'] > '-1') {
  134. $commentcount[$comment['pid']]++;
  135. }
  136. if(count($comments[$comment['pid']]) < $commentnumber && $comment['authorid'] > '-1') {
  137. $comment['avatar'] = avatar($comment['authorid'], 'small');
  138. $comment['comment'] = str_replace(array('[b]', '[/b]', '[/color]'), array('<b>', '</b>', '</font>'), preg_replace("/\[color=([#\w]+?)\]/i", "<font color=\"\\1\">", $comment['comment']));
  139. $comments[$comment['pid']][] = $comment;
  140. }
  141. if($comment['authorid'] == '-1') {
  142. $this->cic_for_fetch_postcomment_by_pid = 0;
  143. $totalcomment[$comment['pid']] = preg_replace_callback('/<i>([\.\d]+)<\/i>/', array($this, 'fetch_postcomment_by_pid_callback_1'), $comment['comment']);
  144. }
  145. $postcache[$comment['pid']]['comment']['count'] = $commentcount[$comment['pid']];
  146. $postcache[$comment['pid']]['comment']['data'] = $comments[$comment['pid']];
  147. $postcache[$comment['pid']]['comment']['totalcomment'] = $totalcomment[$comment['pid']];
  148. }
  149. return array($comments, $postcache, $commentcount, $totalcomment);
  150. }
  151. public function fetch_postcomment_by_pid_callback_1($matches) {
  152. return '<i class="cmstarv" style="background-position:20px -'.(intval($matches[1]) * 16).'px">'.sprintf('%1.1f', $matches[1]).'</i>'.($this->cic_for_fetch_postcomment_by_pid++ % 2 ? '<br />' : '');
  153. }
  154. }
  155. ?>