table_forum_attachment.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_attachment.php 36278 2016-12-09 07:52:35Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class table_forum_attachment extends discuz_table
  12. {
  13. private $_tableids = array();
  14. public function __construct() {
  15. $this->_table = 'forum_attachment';
  16. $this->_pk = 'aid';
  17. $this->_pre_cache_key = 'forum_attachment_';
  18. $this->_cache_ttl = 0;
  19. parent::__construct();
  20. }
  21. public function update_download($aid, $count = 1) {
  22. $this->clear_cache($aid);
  23. return DB::query("UPDATE %t SET downloads=downloads+%d WHERE aid IN (%n)", array($this->_table, $count, (array)$aid), false, true);
  24. }
  25. public function fetch_all_by_id($idtype, $ids, $orderby = '') {
  26. $attachments = array();
  27. if($orderby) {
  28. $orderby = 'ORDER BY '.DB::order($orderby, 'DESC');
  29. }
  30. if(in_array($idtype, array('aid', 'tid', 'pid', 'uid')) && $ids) {
  31. $query = DB::query("SELECT * FROM %t WHERE %i IN (%n) %i", array($this->_table, $idtype, (array)$ids, $orderby));
  32. while($value = DB::fetch($query)) {
  33. $attachments[$value['aid']] = $value;
  34. $this->_tableids[$value['tableid']][] = $value['aid'];
  35. }
  36. }
  37. return $attachments;
  38. }
  39. public function delete_by_id($idtype, $ids) {
  40. if(in_array($idtype, array('aid', 'tid', 'pid', 'uid')) && $ids) {
  41. DB::query('DELETE FROM %t WHERE %i IN (%n)', array($this->_table, $idtype, (array)$ids), false, true);
  42. }
  43. }
  44. public function update_by_id($idtype, $ids, $newtid) {
  45. if(in_array($idtype, array('tid', 'pid')) && $ids) {
  46. DB::query("UPDATE %t SET tid=%d,tableid=%d WHERE %i IN (%n)", array($this->_table, $newtid, getattachtableid($newtid), $idtype, (array)$ids), false, true);
  47. }
  48. }
  49. public function count_by_tid($tid) {
  50. return $tid ? DB::result_first("SELECT COUNT(*) FROM %t WHERE tid=%d", array($this->_table, $tid)) : 0;
  51. }
  52. public function fetch_by_aid_uid($aid, $uid) {
  53. $query = DB::query("SELECT * FROM %t WHERE aid=%d AND uid=%d", array($this->_table, $aid, $uid));
  54. return DB::fetch($query);
  55. }
  56. public function fetch_all_unused_attachment($uid, $aids = null, $posttime = null) {
  57. $parameter = array($this->_table);
  58. $wherearr = array();
  59. if($aids !== null) {
  60. $parameter[] = $aids;
  61. $wherearr[] = is_array($aids) ? 'a.aid IN(%n)' : 'a.aid=%d';
  62. }
  63. $parameter[] = $uid;
  64. $wherearr[] = 'af.uid=%d';
  65. $wherearr[] = 'a.tid=0';
  66. if($posttime !== null) {
  67. $parameter[] = $posttime;
  68. $wherearr[] = "af.dateline>%d";
  69. }
  70. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  71. return DB::fetch_all("SELECT a.*, af.* FROM %t a INNER JOIN ".DB::table('forum_attachment_unused')." af USING(aid) $wheresql ORDER BY a.aid DESC", $parameter);
  72. }
  73. public function get_tableids() {
  74. return $this->_tableids;
  75. }
  76. public function fetch_all_for_manage($tableid, $inforum = '', $authorid = 0, $filename = '', $keyword = '', $sizeless = 0, $sizemore = 0, $dlcountless = 0, $dlcountmore = 0, $daysold = 0, $count = 0, $start = 0, $limit = 0) {
  77. $sql = "1";
  78. if(!is_numeric($tableid) || $tableid < 0 || $tableid > 9) {
  79. return;
  80. }
  81. if($inforum) {
  82. $sql .= is_numeric($inforum) ? " AND t.fid=".DB::quote($inforum) : '';
  83. $sql .= $inforum == 'isgroup' ? ' AND t.isgroup=\'1\'' : ' AND t.isgroup=\'0\'';
  84. }
  85. if($authorid) {
  86. $sql .= " AND a.uid=".DB::quote($authorid);
  87. }
  88. if($filename) {
  89. $sql .= " AND a.filename LIKE ".DB::quote('%'.$filename.'%');
  90. }
  91. if($keyword) {
  92. $sqlkeywords = $or = '';
  93. foreach(explode(',', str_replace(' ', '', $keyword)) as $keyword) {
  94. $sqlkeywords .= " $or a.description LIKE ".DB::quote('%'.$keyword.'%');
  95. $or = 'OR';
  96. }
  97. $sql .= " AND ($sqlkeywords)";
  98. }
  99. $sql .= $sizeless ? " AND a.filesize>'$sizeless'" : '';
  100. $sql .= $sizemore ? " AND a.filesize<'$sizemore' " : '';
  101. $sql .= $dlcountless ? " AND ai.downloads<'$dlcountless'" : '';
  102. $sql .= $dlcountmore ? " AND ai.downloads>'$dlcountmore'" : '';
  103. $sql .= $daysold ? " AND a.dateline<'".(TIMESTAMP - intval($daysold) * 86400)."'" : '';
  104. if($count) {
  105. return DB::result_first("SELECT COUNT(*)
  106. FROM ". DB::table('forum_attachment_'.$tableid)." a
  107. INNER JOIN ".DB::table('forum_attachment')." ai USING(aid)
  108. INNER JOIN ".DB::table('forum_thread')." t
  109. INNER JOIN ".DB::table('forum_forum')." f
  110. WHERE t.tid=a.tid AND f.fid=t.fid AND t.displayorder>='0' AND $sql");
  111. }
  112. return DB::fetch_all("SELECT a.*, ai.downloads, t.fid, t.tid, t.subject, f.name AS fname
  113. FROM ". DB::table('forum_attachment_'.$tableid)." a
  114. INNER JOIN ".DB::table('forum_attachment')." ai USING(aid)
  115. INNER JOIN ".DB::table('forum_thread')." t
  116. INNER JOIN ".DB::table('forum_forum')." f
  117. WHERE t.tid=a.tid AND f.fid=t.fid AND t.displayorder>='0' AND $sql ORDER BY a.aid DESC ".DB::limit($start, $limit));
  118. }
  119. }
  120. ?>