table_home_comment.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_comment.php 36284 2016-12-12 00:47:50Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class table_home_comment extends discuz_table
  12. {
  13. public function __construct() {
  14. $this->_table = 'home_comment';
  15. $this->_pk = 'cid';
  16. parent::__construct();
  17. }
  18. public function fetch_all_by_uid($uids, $start = 0, $limit = 5) {
  19. if(!$uids) {
  20. return null;
  21. }
  22. return DB::fetch_all('SELECT * FROM %t WHERE uid IN (%n) OR authorid IN (%n) OR (id IN (%n) AND idtype=%s) %i', array($this->_table, $uids, $uids, $uids, 'uid', DB::limit($start, $limit)));
  23. }
  24. public function delete_by_uid_idtype($uid) {
  25. if(!$uid){
  26. return null;
  27. }
  28. DB::delete($this->_table, DB::field('uid', $uid).' OR '.DB::field('authorid', $uid).' OR ('.DB::field('id', $uid).' AND idtype=\'uid\')');
  29. }
  30. public function delete_by_uid($uids) {
  31. if(!$uids){
  32. return null;
  33. }
  34. DB::delete($this->_table, DB::field('uid', $uids).' OR ('.DB::field('id', $uids).' AND idtype=\'uid\')');
  35. }
  36. public function delete($cid = '', $id = '', $idtype = '') {
  37. $condition = array();
  38. if($cid) {
  39. $condition[] = DB::field('cid', $cid);
  40. }
  41. if($id) {
  42. $condition[] = DB::field('id', $id);
  43. $condition[] = DB::field('idtype', $idtype);
  44. }
  45. if(!count($condition)) {
  46. return null;
  47. }
  48. DB::delete($this->_table, implode(' AND ', $condition));
  49. }
  50. public function update($cids, $data, $authorid = '') {
  51. $condition = array();
  52. if($cids) {
  53. $condition[] = DB::field('cid', $cids);
  54. }
  55. if($authorid) {
  56. $condition[] = DB::field('authorid', $authorid);
  57. }
  58. if(empty($data) || !is_array($data) || !count($condition)) {
  59. return null;
  60. }
  61. return DB::update($this->_table, $data, implode(' AND ', $condition));
  62. }
  63. public function count_by_id_idtype($id, $idtype, $cid = '') {
  64. if($cid) {
  65. $cidsql = DB::field('cid', $cid). ' AND ';
  66. }
  67. return DB::result_first('SELECT COUNT(*) FROM %t WHERE '.$cidsql.' id=%d AND idtype=%s', array($this->_table, $id, $idtype));
  68. }
  69. public function fetch_all_by_id_idtype($id, $idtype, $start, $limit, $cid = '', $order = '') {
  70. if($cid) {
  71. $cidsql = DB::field('cid', $cid). ' AND ';
  72. }
  73. return DB::fetch_all('SELECT * FROM %t WHERE '.$cidsql.' id=%d AND idtype=%s ORDER BY '.DB::order('dateline', $order).' %i', array($this->_table, $id, $idtype, DB::limit($start, $limit)));
  74. }
  75. public function fetch_latest_by_authorid($uid, $cid) {
  76. if($cid) {
  77. $cidsql = DB::field('cid', $cid). ' AND ';
  78. }
  79. return DB::fetch_first('SELECT * FROM %t WHERE '.$cidsql.' authorid=%d ORDER BY dateline DESC LIMIT 0,1', array($this->_table, $uid));
  80. }
  81. public function fetch_by_id_idtype($id, $idtype, $cid = '') {
  82. if($cid) {
  83. $cidsql = DB::field('cid', $cid). ' AND ';
  84. }
  85. return DB::fetch_first('SELECT * FROM %t WHERE '.$cidsql.' id=%d AND idtype=%s', array($this->_table, $id, $idtype));
  86. }
  87. public function fetch($cid, $authorid = '') {
  88. if(!$cid) {
  89. return null;
  90. }
  91. $wherearr = array();
  92. $wherearr[] = DB::field('cid', $cid);
  93. if($authorid) {
  94. $wherearr[] = DB::field('authorid', $authorid);
  95. }
  96. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  97. return DB::fetch_first('SELECT * FROM '.DB::table($this->_table).' '.$wheresql);
  98. }
  99. public function fetch_all_search($fetchtype, $ids, $authorid, $uids, $useip, $keywords, $idtype, $starttime, $endtime, $start = 0, $limit = 0, $basickeywords = 0) {
  100. $parameter = array($this->_table);
  101. $wherearr = array();
  102. if($ids) {
  103. $parameter[] = $ids;
  104. $wherearr[] = 'id IN(%n)';
  105. }
  106. if(is_array($authorid) && count($authorid)) {
  107. $parameter[] = $authorid;
  108. $wherearr[] = 'authorid IN(%n)';
  109. }
  110. if($idtype) {
  111. $parameter[] = $idtype;
  112. $wherearr[] = 'idtype=%s';
  113. }
  114. if($starttime) {
  115. $parameter[] = is_numeric($starttime) ? $starttime : strtotime($starttime);
  116. $wherearr[] = 'dateline>%d';
  117. }
  118. if($endtime) {
  119. $parameter[] = is_numeric($endtime) ? $endtime : strtotime($endtime);
  120. $wherearr[] = 'dateline<%d';
  121. }
  122. if($uids) {
  123. $parameter[] = $uids;
  124. $wherearr[] = 'uid IN(%n)';
  125. }
  126. if($keywords) {
  127. if(!$basickeywords) {
  128. $sqlkeywords = '';
  129. $or = '';
  130. $keywords = explode(',', str_replace(' ', '', $keywords));
  131. for($i = 0; $i < count($keywords); $i++) {
  132. if(preg_match("/\{(\d+)\}/", $keywords[$i])) {
  133. $keywords[$i] = preg_replace("/\\\{(\d+)\\\}/", ".{0,\\1}", preg_quote($keywords[$i], '/'));
  134. $sqlkeywords .= " $or message REGEXP '".addslashes(stripsearchkey($keywords[$i]))."'";
  135. } else {
  136. $sqlkeywords .= " $or message LIKE '%".addslashes(stripsearchkey($keywords[$i]))."%'";
  137. }
  138. $or = 'OR';
  139. }
  140. $parameter[] = $sqlkeywords;
  141. $wherearr[] = '%i';
  142. } else {
  143. $parameter[] = '%'.$basickeywords.'%';
  144. $wherearr[] = 'message LIKE %s';
  145. }
  146. }
  147. if($useip) {
  148. $parameter[] = str_replace('*', '%', $useip);
  149. $wherearr[] = 'ip LIKE %s';
  150. }
  151. if($fetchtype == 3) {
  152. $selectfield = "count(*)";
  153. } elseif ($fetchtype == 2) {
  154. $selectfield = "cid";
  155. } else {
  156. $selectfield = "*";
  157. $parameter[] = DB::limit($start, $limit);
  158. $ordersql = ' ORDER BY dateline DESC %i';
  159. }
  160. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  161. if(empty($wheresql)) {
  162. return null;
  163. }
  164. if($fetchtype == 3) {
  165. return DB::result_first("SELECT $selectfield FROM %t $wheresql", $parameter);
  166. } else {
  167. return DB::fetch_all("SELECT $selectfield FROM %t $wheresql $ordersql", $parameter);
  168. }
  169. }
  170. }
  171. ?>