table_home_album.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_album.php 28041 2012-02-21 07:33:55Z chenmengshu $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class table_home_album extends discuz_table
  12. {
  13. public function __construct() {
  14. $this->_table = 'home_album';
  15. $this->_pk = 'albumid';
  16. parent::__construct();
  17. }
  18. public function count_by_catid($catid) {
  19. return DB::result_first('SELECT COUNT(*) FROM %t WHERE catid = %d', array($this->_table, $catid));
  20. }
  21. public function count_by_uid($uid) {
  22. return DB::result_first('SELECT COUNT(*) FROM %t WHERE uid = %d', array($this->_table, $uid));
  23. }
  24. public function update_num_by_albumid($albumid, $inc, $field = 'picnum', $uid = '') {
  25. if(!in_array($field, array('picnum', 'favtimes', 'sharetimes'))) {
  26. return null;
  27. }
  28. $parameter = array($this->_table, $inc, $albumid);
  29. if($uid) {
  30. $parameter[] = $uid;
  31. $uidsql = ' AND uid = %d';
  32. }
  33. return DB::query('UPDATE %t SET '.$field.'='.$field.'+\'%d\' WHERE albumid=%d '.$uidsql, $parameter);
  34. }
  35. public function delete_by_uid($uid) {
  36. if(!$uid) {
  37. return null;
  38. }
  39. return DB::delete($this->_table, DB::field('uid', $uid));
  40. }
  41. public function update_by_catid($catid, $data) {
  42. if(!is_array($data) || empty($data)) {
  43. return null;
  44. }
  45. return DB::update($this->_table, $data, DB::field('catid', $catid));
  46. }
  47. public function fetch_uid_by_username($users) {
  48. if(!$users) {
  49. return null;
  50. }
  51. return DB::fetch_all('SELECT uid FROM %t WHERE username IN (%n)', array($this->_table, $users), 'uid');
  52. }
  53. public function fetch_albumid_by_albumname_uid($albumname, $uid) {
  54. return DB::result_first('SELECT albumid FROM %t WHERE albumname=%s AND uid=%d', array($this->_table, $albumname, $uid));
  55. }
  56. public function fetch_albumid_by_searchkey($searchkey, $limit) {
  57. return DB::fetch_all('SELECT albumid FROM %t WHERE 1 %i ORDER BY albumid DESC %i', array($this->_table, $searchkey, DB::limit(0, $limit)));
  58. }
  59. public function fetch_uid_by_uid($uid) {
  60. if(!is_array($uid)) {
  61. $uid = explode(',', $uid);
  62. }
  63. if(!$uid) {
  64. return null;
  65. }
  66. return DB::fetch_all('SELECT uid FROM %t WHERE uid IN (%n)', array($this->_table, $uid), 'uid');
  67. }
  68. public function fetch($albumid, $uid = '') {
  69. $data = $this->fetch_all_by_uid($uid, false, 0, 0, $albumid);
  70. return $data[0];
  71. }
  72. public function fetch_all($albumids, $order = false, $start = 0, $limit = 0) {
  73. return $this->fetch_all_by_uid('', $order, $start, $limit, $albumids);
  74. }
  75. public function fetch_all_by_uid($uid, $order = false, $start = 0, $limit = 0, $albumid = '') {
  76. $parameter = array($this->_table);
  77. $wherearr = array();
  78. if($albumid) {
  79. $wherearr[] = DB::field('albumid', $albumid);
  80. }
  81. if($uid) {
  82. $wherearr[] = DB::field('uid', $uid);
  83. }
  84. if(is_string($order) && $order = DB::order($order, 'DESC')) {
  85. $ordersql = ' ORDER BY '.$order;
  86. }
  87. if($limit) {
  88. $parameter[] = DB::limit($start, $limit);
  89. $ordersql .= ' %i';
  90. }
  91. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  92. if(empty($wheresql)) {
  93. return null;
  94. }
  95. return DB::fetch_all('SELECT * FROM %t '.$wheresql.$ordersql, $parameter);
  96. }
  97. public function fetch_all_by_block($aids, $bannedids, $uids, $catid, $startrow, $items, $orderby) {
  98. $wheres = array();
  99. if($aids) {
  100. $wheres[] = DB::field('albumid', $aids, 'in');
  101. }
  102. if($bannedids) {
  103. $wheres[] = DB::field('albumid', $bannedids, 'notin');
  104. }
  105. if($uids) {
  106. $wheres[] = DB::field('uid', $uids, 'in');
  107. }
  108. if($catid && !in_array('0', $catid)) {
  109. $wheres[] = DB::field('catid', $catid, 'in');
  110. }
  111. $wheres[] = "friend = '0'";
  112. $wheresql = $wheres ? implode(' AND ', $wheres) : '1';
  113. if(!in_array($orderby, array('dateline', 'picnum', 'updatetime'))) {
  114. $orderby = 'dateline';
  115. }
  116. return DB::fetch_all('SELECT * FROM '.DB::table($this->_table).' WHERE '.$wheresql.' ORDER BY '.DB::order($orderby, 'DESC').DB::limit($startrow, $items));
  117. }
  118. public function fetch_all_by_search($fetchtype, $uids, $albumname, $searchname, $catid, $starttime, $endtime, $albumids, $friend = '', $orderfield = '', $ordersort = 'DESC', $start = 0, $limit = 0, $findex = '') {
  119. $parameter = array($this->_table);
  120. $wherearr = array();
  121. if(is_array($uids) && count($uids)) {
  122. $parameter[] = $uids;
  123. $wherearr[] = 'uid IN(%n)';
  124. }
  125. if($albumname) {
  126. if($searchname == false) {
  127. $parameter[] = $albumname;
  128. $wherearr[] = 'albumname=%s';
  129. } else {
  130. $parameter[] = '%'.$albumname.'%';
  131. $wherearr[] = 'albumname LIKE %s';
  132. }
  133. }
  134. if($catid) {
  135. $parameter[] = $catid;
  136. $wherearr[] = 'catid=%d';
  137. }
  138. if($starttime) {
  139. $parameter[] = is_numeric($starttime) ? $starttime : strtotime($starttime);
  140. $wherearr[] = 'dateline>%d';
  141. }
  142. if($endtime) {
  143. $parameter[] = is_numeric($endtime) ? $endtime : strtotime($endtime);
  144. $wherearr[] = 'dateline<%d';
  145. }
  146. if(is_numeric($friend)) {
  147. $parameter[] = $friend;
  148. $wherearr[] = 'friend=%d';
  149. }
  150. if(is_array($albumids) && count($albumids)) {
  151. $parameter[] = $albumids;
  152. $wherearr[] = 'albumid IN(%n)';
  153. }
  154. if($fetchtype == 3) {
  155. $selectfield = "count(*)";
  156. } elseif ($fetchtype == 2) {
  157. $selectfield = "albumid";
  158. } else {
  159. $selectfield = "*";
  160. if(is_string($orderfield) && $order = DB::order($orderfield, $ordersort)) {
  161. $ordersql = 'ORDER BY '.$order;
  162. }
  163. if($limit) {
  164. $parameter[] = DB::limit($start, $limit);
  165. $ordersql .= ' %i';
  166. }
  167. }
  168. if($findex) {
  169. $findex = 'USE INDEX(updatetime)';
  170. }
  171. $wheresql = !empty($wherearr) && is_array($wherearr) ? ' WHERE '.implode(' AND ', $wherearr) : '';
  172. if($fetchtype == 3) {
  173. return DB::result_first("SELECT $selectfield FROM %t $wheresql", $parameter);
  174. } else {
  175. return DB::fetch_all("SELECT $selectfield FROM %t {$findex} $wheresql $ordersql", $parameter);
  176. }
  177. }
  178. }
  179. ?>