block_article.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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: block_article.php 31313 2012-08-10 03:51:03Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class block_article extends discuz_block {
  12. var $setting = array();
  13. function block_article() {
  14. global $_G;
  15. $this->setting = array(
  16. 'aids' => array(
  17. 'title' => 'articlelist_aids',
  18. 'type' => 'text',
  19. 'value' => ''
  20. ),
  21. 'uids' => array(
  22. 'title' => 'articlelist_uids',
  23. 'type' => 'text',
  24. 'value' => ''
  25. ),
  26. 'keyword' => array(
  27. 'title' => 'articlelist_keyword',
  28. 'type' => 'text'
  29. ),
  30. 'catid' => array(
  31. 'title' => 'articlelist_catid',
  32. 'type' => 'mselect',
  33. 'value' => array(
  34. ),
  35. ),
  36. 'tag' => array(
  37. 'title' => 'articlelist_tag',
  38. 'type' => 'mcheckbox',
  39. 'value' => array(
  40. ),
  41. ),
  42. 'picrequired' => array(
  43. 'title' => 'articlelist_picrequired',
  44. 'type' => 'radio',
  45. 'default' => '0'
  46. ),
  47. 'starttime' => array(
  48. 'title' => 'articlelist_starttime',
  49. 'type' => 'calendar',
  50. 'default' => ''
  51. ),
  52. 'endtime' => array(
  53. 'title' => 'articlelist_endtime',
  54. 'type' => 'calendar',
  55. 'default' => ''
  56. ),
  57. 'picrequired' => array(
  58. 'title' => 'articlelist_picrequired',
  59. 'type' => 'radio',
  60. 'default' => '0'
  61. ),
  62. 'orderby' => array(
  63. 'title' => 'articlelist_orderby',
  64. 'type' => 'mradio',
  65. 'value' => array(
  66. array('dateline', 'articlelist_orderby_dateline'),
  67. array('viewnum', 'articlelist_orderby_viewnum'),
  68. array('commentnum', 'articlelist_orderby_commentnum'),
  69. ),
  70. 'default' => 'dateline'
  71. ),
  72. 'publishdateline' => array(
  73. 'title' => 'articlelist_publishdateline',
  74. 'type'=> 'mradio',
  75. 'value' => array(
  76. array('0', 'articlelist_publishdateline_nolimit'),
  77. array('3600', 'articlelist_publishdateline_hour'),
  78. array('86400', 'articlelist_publishdateline_day'),
  79. array('604800', 'articlelist_publishdateline_week'),
  80. array('2592000', 'articlelist_publishdateline_month'),
  81. ),
  82. 'default' => '0'
  83. ),
  84. 'titlelength' => array(
  85. 'title' => 'articlelist_titlelength',
  86. 'type' => 'text',
  87. 'default' => 40
  88. ),
  89. 'summarylength' => array(
  90. 'title' => 'articlelist_summarylength',
  91. 'type' => 'text',
  92. 'default' => 80
  93. ),
  94. 'startrow' => array(
  95. 'title' => 'articlelist_startrow',
  96. 'type' => 'text',
  97. 'default' => 0
  98. ),
  99. );
  100. loadcache('click');
  101. $clicks = !empty($_G['cache']['click']['aid']) ? $_G['cache']['click']['aid'] : array();
  102. if(!empty($clicks)){
  103. foreach($clicks as $key => $value) {
  104. if($value['available']) {
  105. $this->setting['orderby']['value'][] = array('click'.$key, lang('block/articlelist', 'articlelist_orderby_click', array('clickname'=>$value['name'])));
  106. }
  107. }
  108. }
  109. }
  110. function name() {
  111. return lang('blockclass', 'blockclass_article_script_article');
  112. }
  113. function blockclass() {
  114. return array('article', lang('blockclass', 'blockclass_portal_article'));
  115. }
  116. function fields() {
  117. return array(
  118. 'id' => array('name' => lang('blockclass', 'blockclass_field_id'), 'formtype' => 'text', 'datatype' => 'int'),
  119. 'uid' => array('name' => lang('blockclass', 'blockclass_article_field_uid'), 'formtype' => 'text', 'datatype' => 'int'),
  120. 'username' => array('name' => lang('blockclass', 'blockclass_article_field_username'), 'formtype' => 'text', 'datatype' => 'string'),
  121. 'avatar' => array('name' => lang('blockclass', 'blockclass_article_field_avatar'), 'formtype' => 'text', 'datatype' => 'string'),
  122. 'avatar_middle' => array('name' => lang('blockclass', 'blockclass_article_field_avatar_middle'), 'formtype' => 'text', 'datatype' => 'string'),
  123. 'avatar_big' => array('name' => lang('blockclass', 'blockclass_article_field_avatar_big'), 'formtype' => 'text', 'datatype' => 'string'),
  124. 'url' => array('name' => lang('blockclass', 'blockclass_article_field_url'), 'formtype' => 'text', 'datatype' => 'string'),
  125. 'title' => array('name' => lang('blockclass', 'blockclass_article_field_title'), 'formtype' => 'title', 'datatype' => 'title'),
  126. 'pic' => array('name' => lang('blockclass', 'blockclass_article_field_pic'), 'formtype' => 'pic', 'datatype' => 'pic'),
  127. 'summary' => array('name' => lang('blockclass', 'blockclass_article_field_summary'), 'formtype' => 'summary', 'datatype' => 'summary'),
  128. 'dateline' => array('name' => lang('blockclass', 'blockclass_article_field_dateline'), 'formtype' => 'date', 'datatype' => 'date'),
  129. 'caturl' => array('name' => lang('blockclass', 'blockclass_article_field_caturl'), 'formtype' => 'text', 'datatype' => 'string'),
  130. 'catname' => array('name' => lang('blockclass', 'blockclass_article_field_catname'), 'formtype' => 'text', 'datatype' => 'string'),
  131. 'articles' => array('name' => lang('blockclass', 'blockclass_article_field_articles'), 'formtype' => 'text', 'datatype' => 'int'),
  132. 'viewnum' => array('name' => lang('blockclass', 'blockclass_article_field_viewnum'), 'formtype' => 'text', 'datatype' => 'int'),
  133. 'commentnum' => array('name' => lang('blockclass', 'blockclass_article_field_commentnum'), 'formtype' => 'text', 'datatype' => 'int'),
  134. );
  135. }
  136. function fieldsconvert() {
  137. return array(
  138. 'forum_thread' => array(
  139. 'name' => lang('blockclass', 'blockclass_forum_thread'),
  140. 'script' => 'thread',
  141. 'searchkeys' => array('username', 'uid', 'caturl', 'catname', 'articles', 'viewnum', 'commentnum'),
  142. 'replacekeys' => array('author', 'authorid', 'forumurl', 'forumname', 'posts', 'views', 'replies'),
  143. ),
  144. 'group_thread' => array(
  145. 'name' => lang('blockclass', 'blockclass_group_thread'),
  146. 'script' => 'groupthread',
  147. 'searchkeys' => array('username', 'uid', 'caturl', 'catname', 'articles', 'viewnum', 'commentnum'),
  148. 'replacekeys' => array('author', 'authorid', 'groupurl', 'groupname', 'posts', 'views', 'replies'),
  149. ),
  150. 'space_blog' => array(
  151. 'name' => lang('blockclass', 'blockclass_space_blog'),
  152. 'script' => 'blog',
  153. 'searchkeys' => array('commentnum'),
  154. 'replacekeys' => array('replynum'),
  155. ),
  156. );
  157. }
  158. function getsetting() {
  159. global $_G;
  160. $settings = $this->setting;
  161. if($settings['catid']) {
  162. $settings['catid']['value'][] = array(0, lang('portalcp', 'block_all_category'));
  163. loadcache('portalcategory');
  164. foreach($_G['cache']['portalcategory'] as $value) {
  165. if($value['level'] == 0) {
  166. $settings['catid']['value'][] = array($value['catid'], $value['catname']);
  167. if($value['children']) {
  168. foreach($value['children'] as $catid2) {
  169. $value2 = $_G['cache']['portalcategory'][$catid2];
  170. $settings['catid']['value'][] = array($value2['catid'], '-- '.$value2['catname']);
  171. if($value2['children']) {
  172. foreach($value2['children'] as $catid3) {
  173. $value3 = $_G['cache']['portalcategory'][$catid3];
  174. $settings['catid']['value'][] = array($value3['catid'], '---- '.$value3['catname']);
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. if($settings['tag']) {
  183. $tagnames = article_tagnames();
  184. foreach($tagnames as $k=>$v) {
  185. $settings['tag']['value'][] = array($k, $v);
  186. }
  187. }
  188. return $settings;
  189. }
  190. function getdata($style, $parameter) {
  191. global $_G;
  192. require_once libfile('function/portal');
  193. $parameter = $this->cookparameter($parameter);
  194. $aids = !empty($parameter['aids']) ? explode(',', $parameter['aids']) : array();
  195. $uids = !empty($parameter['uids']) ? explode(',', $parameter['uids']) : array();
  196. $keyword = !empty($parameter['keyword']) ? $parameter['keyword'] : '';
  197. $tag = !empty($parameter['tag']) ? $parameter['tag'] : array();
  198. $starttime = !empty($parameter['starttime']) ? strtotime($parameter['starttime']) : 0;
  199. $endtime = !empty($parameter['endtime']) ? strtotime($parameter['endtime']) : 0;
  200. $publishdateline = isset($parameter['publishdateline']) ? intval($parameter['publishdateline']) : 0;
  201. $startrow = isset($parameter['startrow']) ? intval($parameter['startrow']) : 0;
  202. $items = isset($parameter['items']) ? intval($parameter['items']) : 10;
  203. $titlelength = isset($parameter['titlelength']) ? intval($parameter['titlelength']) : 40;
  204. $summarylength = isset($parameter['summarylength']) ? intval($parameter['summarylength']) : 80;
  205. $clickarr = array('click1', 'click2', 'click3', 'click4', 'click5', 'click6', 'click7', 'click8');
  206. $orderby = in_array($parameter['orderby'], array_merge(array('dateline', 'viewnum', 'commentnum'), $clickarr)) ? $parameter['orderby'] : 'dateline';
  207. $catid = array();
  208. if(!empty($parameter['catid'])) {
  209. if($parameter['catid'][0] == '0') {
  210. unset($parameter['catid'][0]);
  211. }
  212. $catid = $parameter['catid'];
  213. }
  214. $picrequired = !empty($parameter['picrequired']) ? 1 : 0;
  215. $bannedids = !empty($parameter['bannedids']) ? explode(',', $parameter['bannedids']) : array();
  216. loadcache('portalcategory');
  217. $list = array();
  218. $wheres = array();
  219. if($aids) {
  220. $wheres[] = 'at.aid IN ('.dimplode($aids).')';
  221. }
  222. if($uids) {
  223. $wheres[] = 'at.uid IN ('.dimplode($uids).')';
  224. }
  225. if($catid) {
  226. include_once libfile('function/portalcp');
  227. $childids = array();
  228. foreach($catid as $id) {
  229. if($_G['cache']['portalcategory'][$id]['disallowpublish']) {
  230. $childids = array_merge($childids, category_get_childids('portal', $id));
  231. }
  232. }
  233. $catid = array_merge($catid, $childids);
  234. $catid = array_unique($catid);
  235. $wheres[] = 'at.catid IN ('.dimplode($catid).')';
  236. }
  237. if(!$aids && !$catid && $_G['setting']['blockmaxaggregationitem']) {
  238. if(($maxid = $this->getmaxid() - $_G['setting']['blockmaxaggregationitem'] ) > 0) {
  239. $wheres[] = 'at.aid > '.$maxid;
  240. }
  241. }
  242. if(empty($aids) && ($picrequired)) {
  243. $wheres[] = "at.pic != ''";
  244. }
  245. if($publishdateline) {
  246. $time = TIMESTAMP - $publishdateline;
  247. $wheres[] = "at.dateline >= '$time'";
  248. }
  249. if($starttime) {
  250. $wheres[] = "at.dateline >= '$starttime'";
  251. }
  252. if($endtime) {
  253. $wheres[] = "at.dateline <= '$endtime'";
  254. }
  255. if($bannedids) {
  256. $wheres[] = 'at.aid NOT IN ('.dimplode($bannedids).')';
  257. }
  258. $wheres[] = "at.status='0'";
  259. if(is_array($tag)) {
  260. $article_tags = array();
  261. foreach($tag as $k) {
  262. $article_tags[$k] = 1;
  263. }
  264. include_once libfile('function/portalcp');
  265. $v=article_make_tag($article_tags);
  266. if($v > 0) {
  267. $wheres[] = "(at.tag & $v) = $v";
  268. }
  269. }
  270. if($keyword) {
  271. require_once libfile('function/search');
  272. $keyword = searchkey($keyword, "at.title LIKE '%{text}%'");
  273. }
  274. $wheresql = $wheres ? implode(' AND ', $wheres) : '1';
  275. if(in_array($orderby, $clickarr)) {
  276. $orderby = "at.$orderby DESC,at.dateline DESC";
  277. } else {
  278. $orderby = ($orderby == 'dateline') ? 'at.dateline DESC ' : "ac.$orderby DESC";
  279. }
  280. $query = DB::query("SELECT at.*, ac.viewnum, ac.commentnum FROM ".DB::table('portal_article_title')." at LEFT JOIN ".DB::table('portal_article_count')." ac ON at.aid=ac.aid WHERE $wheresql$keyword ORDER BY $orderby LIMIT $startrow, $items");
  281. while($data = DB::fetch($query)) {
  282. if(empty($data['pic'])) {
  283. $data['pic'] = STATICURL.'image/common/nophoto.gif';
  284. $data['picflag'] = '0';
  285. } else {
  286. $data['pic'] = $data['pic'];
  287. $data['picflag'] = $data['remote'] == '1' ? '2' : '1';
  288. }
  289. $list[] = array(
  290. 'id' => $data['aid'],
  291. 'idtype' => 'aid',
  292. 'title' => cutstr($data['title'], $titlelength, ''),
  293. 'url' => fetch_article_url($data),
  294. 'pic' => $data['pic'],
  295. 'picflag' => $data['picflag'],
  296. 'summary' => cutstr(strip_tags($data['summary']), $summarylength, ''),
  297. 'fields' => array(
  298. 'uid'=>$data['uid'],
  299. 'username'=>$data['username'],
  300. 'avatar' => avatar($data['uid'], 'small', true, false, false, $_G['setting']['ucenterurl']),
  301. 'avatar_middle' => avatar($data['uid'], 'middle', true, false, false, $_G['setting']['ucenterurl']),
  302. 'avatar_big' => avatar($data['uid'], 'big', true, false, false, $_G['setting']['ucenterurl']),
  303. 'fulltitle' => $data['title'],
  304. 'dateline'=>$data['dateline'],
  305. 'caturl'=> $_G['cache']['portalcategory'][$data['catid']]['caturl'],
  306. 'catname' => $_G['cache']['portalcategory'][$data['catid']]['catname'],
  307. 'articles' => $_G['cache']['portalcategory'][$data['catid']]['articles'],
  308. 'viewnum' => intval($data['viewnum']),
  309. 'commentnum' => intval($data['commentnum'])
  310. )
  311. );
  312. }
  313. return array('html' => '', 'data' => $list);
  314. }
  315. function getmaxid() {
  316. loadcache('databasemaxid');
  317. $data = getglobal('cache/databasemaxid');
  318. if(!isset($data['article']) || TIMESTAMP - $data['article']['dateline'] >= 86400) {
  319. $data['article']['dateline'] = TIMESTAMP;
  320. $data['article']['id'] = DB::result_first('SELECT MAX(aid) FROM '.DB::table('portal_article_title'));
  321. savecache('databasemaxid', $data);
  322. }
  323. return $data['article']['id'];
  324. }
  325. }
  326. ?>