PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/src/applications/bbs/controller/ForumController.php

https://gitlab.com/wuhang2003/phpwind
PHP | 350 lines | 252 code | 34 blank | 64 comment | 39 complexity | 9ba5d997bc12697a02452a8a54e46fb4 MD5 | raw file
  1. <?php
  2. Wind::import('SRV:forum.srv.PwThreadList');
  3. /**
  4. * 版块相关页面
  5. *
  6. * @author Jianmin Chen <sky_hold@163.com>
  7. * @license http://www.phpwind.com
  8. * @version $Id: ForumController.php 28803 2013-05-24 07:58:21Z jieyin $
  9. * @package forum
  10. */
  11. class ForumController extends PwBaseController {
  12. public function run() {
  13. $order = $this->getInput('order', 'get');
  14. $page = intval($this->getInput('page', 'get'));
  15. $threadList = new PwThreadList();
  16. // $this->runHook('c_thread_run', $forumDisplay);
  17. $threadList->setPage($page)->setPerpage(Wekit::C('bbs', 'thread.perpage'));
  18. Wind::import('SRV:forum.srv.threadList.PwNewThread');
  19. $forbidFids = Wekit::load('forum.srv.PwForumService')->getForbidVisitForum($this->loginUser);
  20. $dataSource = new PwNewThread($forbidFids);
  21. if ($order == 'postdate') {
  22. $dataSource->setOrderBy($order);
  23. } else {
  24. $dataSource->setOrderBy('lastpost');
  25. }
  26. $threadList->execute($dataSource);
  27. if ($threadList->total > 12000) {
  28. Wekit::load('forum.PwThreadIndex')->deleteOver($threadList->total - 10000);
  29. }
  30. $threaddb = $threadList->getList();
  31. $fids = array();
  32. foreach ($threaddb as $key => $value) {
  33. $fids[] = $value['fid'];
  34. }
  35. $forums = Wekit::load('forum.srv.PwForumService')->fetchForum($fids);
  36. if ($operateThread = $this->loginUser->getPermission('operate_thread', false, array())) {
  37. $operateThread = Pw::subArray($operateThread, array('delete'));
  38. }
  39. $this->setOutput($threaddb, 'threadList');
  40. $this->setOutput($forums, 'forums');
  41. $this->setOutput($threadList->icon, 'icon');
  42. $this->setOutput($threadList->uploadIcon, 'uploadIcon');
  43. $this->setOutput(26, 'numofthreadtitle');
  44. $this->setOutput($order, 'order');
  45. $this->setOutput($operateThread, 'operateThread');
  46. $this->setOutput($threadList->page, 'page');
  47. $this->setOutput($threadList->perpage, 'perpage');
  48. $this->setOutput($threadList->total, 'count');
  49. $this->setOutput($threadList->maxPage, 'totalpage');
  50. $this->setOutput($threadList->getUrlArgs(), 'urlargs');
  51. // seo设置
  52. Wind::import('SRV:seo.bo.PwSeoBo');
  53. $seoBo = PwSeoBo::getInstance();
  54. $lang = Wind::getComponent('i18n');
  55. $threadList->page <=1 && $seoBo->setDefaultSeo($lang->getMessage('SEO:bbs.forum.run.title'), '', $lang->getMessage('SEO:bbs.forum.run.description'));
  56. $seoBo->init('bbs', 'new');
  57. $seoBo->set('{page}', $threadList->page);
  58. Wekit::setV('seo', $seoBo);
  59. }
  60. /**
  61. * 我的版块
  62. */
  63. public function myAction() {
  64. if (!$this->loginUser->isExists()) {
  65. $this->forwardAction('u/login/run', array('backurl' => WindUrlHelper::createUrl('bbs/forum/my')));
  66. }
  67. $order = $this->getInput('order', 'get');
  68. $page = intval($this->getInput('page', 'get'));
  69. $threadList = new PwThreadList();
  70. // $this->runHook('c_thread_run', $forumDisplay);
  71. $threadList->setPage($page)->setPerpage(Wekit::C('bbs', 'thread.perpage'));
  72. Wind::import('SRV:forum.srv.threadList.PwMyForumThread');
  73. $dataSource = new PwMyForumThread($this->loginUser);
  74. if ($order == 'postdate') {
  75. $dataSource->setOrderBy($order);
  76. } else {
  77. $dataSource->setOrderBy('lastpost');
  78. }
  79. $threadList->execute($dataSource);
  80. $threaddb = $threadList->getList();
  81. $fids = array();
  82. foreach ($threaddb as $key => $value) {
  83. $fids[] = $value['fid'];
  84. }
  85. $forums = Wekit::load('forum.PwForum')->fetchForum($fids);
  86. $this->setOutput($threaddb, 'threadList');
  87. $this->setOutput($forums, 'forums');
  88. $this->setOutput($threadList->icon, 'icon');
  89. $this->setOutput($threadList->uploadIcon, 'uploadIcon');
  90. $this->setOutput($order, 'order');
  91. $this->setOutput($threadList->page, 'page');
  92. $this->setOutput($threadList->perpage, 'perpage');
  93. $this->setOutput($threadList->total, 'count');
  94. $this->setOutput($threadList->maxPage, 'totalpage');
  95. $this->setOutput($threadList->getUrlArgs(), 'urlargs');
  96. // seo设置
  97. Wind::import('SRV:seo.bo.PwSeoBo');
  98. $seoBo = PwSeoBo::getInstance();
  99. $lang = Wind::getComponent('i18n');
  100. $seoBo->setCustomSeo($lang->getMessage('SEO:bbs.forum.my.title'), '', '');
  101. Wekit::setV('seo', $seoBo);
  102. }
  103. /**
  104. * 版块列表 弹窗
  105. */
  106. public function listAction() {
  107. $withMyforum = $this->getInput('withMyforum');
  108. $service = Wekit::load('forum.srv.PwForumService');
  109. $forums = $service->getForumList();
  110. $map = $service->getForumMap();
  111. $cate = array();
  112. $forum = array();
  113. foreach ($map[0] as $key => $value) {
  114. if (!$value['isshow']) continue;
  115. $array = $service->findOptionInMap($value['fid'], $map,
  116. array('sub' => '--', 'sub2' => '----'));
  117. $tmp = array();
  118. foreach ($array as $k => $v) {
  119. if ($forums[$k]['isshow'] && (!$forums[$k]['allow_post'] || $this->loginUser->inGroup(
  120. explode(',', $forums[$k]['allow_post'])))) {
  121. $tmp[] = array($k, strip_tags($v));
  122. }
  123. }
  124. if ($tmp) {
  125. $cate[] = array($value['fid'], strip_tags($value['name']));
  126. $forum[$value['fid']] = $tmp;
  127. }
  128. }
  129. if ($withMyforum && $this->loginUser->isExists()
  130. && ($joinForum = Wekit::load('forum.PwForumUser')->getFroumByUid($this->loginUser->uid))) {
  131. $tmp = array();
  132. foreach ($joinForum as $key => $value) {
  133. if (!$key) continue;
  134. $tmp[] = array($key, strip_tags($forums[$key]['name']));
  135. }
  136. array_unshift($cate, array('my', '我的版块'));
  137. $forum['my'] = $tmp;
  138. }
  139. $response = array('cate' => $cate, 'forum' => $forum);
  140. $this->setOutput($response, 'data');
  141. $this->showMessage('success');
  142. }
  143. /**
  144. * 加入版块
  145. */
  146. public function joinAction() {
  147. $fid = $this->getInput('fid', 'post');
  148. if (!$fid) {
  149. $this->showError('operate.fail');
  150. }
  151. Wind::import('SRV:forum.bo.PwForumBo');
  152. $forum = new PwForumBo($fid);
  153. if (!$forum->isForum()) {
  154. $this->showError('BBS:forum.exists.not');
  155. }
  156. if (!$this->loginUser->isExists()) {
  157. $this->showError('login.not');
  158. }
  159. if (Wekit::load('forum.PwForumUser')->get($this->loginUser->uid, $fid)) {
  160. $this->showError('BBS:forum.join.already');
  161. }
  162. Wekit::load('forum.PwForumUser')->join($this->loginUser->uid, $fid);
  163. $this->_addJoionForum($this->loginUser->info, $forum->foruminfo);
  164. $this->showMessage('success');
  165. }
  166. /**
  167. * 退出版块
  168. */
  169. public function quitAction() {
  170. $fid = $this->getInput('fid', 'post');
  171. if (!$fid) {
  172. $this->showError('operate.fail');
  173. }
  174. Wind::import('SRV:forum.bo.PwForumBo');
  175. $forum = new PwForumBo($fid);
  176. if (!$forum->isForum()) {
  177. $this->showError('BBS:forum.exists.not');
  178. }
  179. if (!$this->loginUser->isExists()) {
  180. $this->showError('login.not');
  181. }
  182. if (!Wekit::load('forum.PwForumUser')->get($this->loginUser->uid, $fid)) {
  183. $this->showError('BBS:forum.join.not');
  184. }
  185. Wekit::load('forum.PwForumUser')->quit($this->loginUser->uid, $fid);
  186. $this->_removeJoionForum($this->loginUser->info, $fid);
  187. $this->showMessage('success');
  188. }
  189. public function topictypeAction() {
  190. $fid = $this->getInput('fid');
  191. $topictypes = Wekit::load('forum.PwTopicType')->getTopicTypesByFid($fid, !$this->loginUser->getPermission('operate_thread.type'));
  192. $data = array();
  193. foreach ($topictypes['topic_types'] as $key => $value) {
  194. $tmp = array('title' => strip_tags($value['name']), 'val' => $value['id']);
  195. if (isset($topictypes['sub_topic_types'][$value['id']])) {
  196. $sub = array();
  197. foreach ($topictypes['sub_topic_types'][$value['id']] as $k => $v) {
  198. $sub[] = array('title' => strip_tags($v['name']), 'val' => $v['id']);
  199. }
  200. $tmp['items'] = $sub;
  201. }
  202. $data[] = $tmp;
  203. }
  204. $this->setOutput($data, 'data');
  205. $this->showMessage('success');
  206. }
  207. /**
  208. * 进入版块的密码
  209. */
  210. public function passwordAction() {
  211. $fid = $this->getInput('fid');
  212. $this->setOutput($fid, 'fid');
  213. $this->setLayout('TPL:common.layout_error');
  214. }
  215. /**
  216. * 验证版块密码
  217. */
  218. public function verifyAction() {
  219. $fid = $this->getInput('fid');
  220. $password = $this->getInput('password', 'post');
  221. Wind::import('SRV:forum.bo.PwForumBo');
  222. $forum = new PwForumBo($fid);
  223. if (!$forum->isForum(true)) {
  224. $this->showError('BBS:forum.exists.not');
  225. }
  226. if (md5($password) != $forum->foruminfo['password']) {
  227. $this->showError('BBS:forum.password.error');
  228. }
  229. Pw::setCookie('fp_' . $fid, Pw::getPwdCode(md5($password)), 86400);
  230. $this->showMessage('success');
  231. }
  232. /**
  233. * 格式化数据 把字符串"1,版块1,2,版块2"格式化为数组
  234. *
  235. * @param string $string
  236. * @return array
  237. */
  238. public static function splitStringToArray($string) {
  239. $a = explode(',', $string);
  240. $l = count($a);
  241. $l % 2 == 1 && $l--;
  242. $r = array();
  243. for ($i = 0; $i < $l; $i+=2) {
  244. $r[$a[$i]] = $a[$i+1];
  245. }
  246. return $r;
  247. }
  248. /**
  249. * 加入版块 - 更新我的版块缓存数据
  250. *
  251. * @param array $userInfo
  252. * @param array $foruminfo
  253. * @return boolean
  254. */
  255. private function _addJoionForum($userInfo,$foruminfo) {
  256. // 更新用户data表信息
  257. $array = array();
  258. $userInfo['join_forum'] && $array = self::splitStringToArray($userInfo['join_forum']);
  259. $array = array($foruminfo['fid'] => $foruminfo['name']) + $array;
  260. count($array) > 20 && $array = array_slice($array, 0, 20, true);
  261. $this->_updateMyForumCache($userInfo['uid'], $array);
  262. return true;
  263. }
  264. /**
  265. * 推出版块 - 更新我的版块缓存数据
  266. *
  267. * @param array $userInfo
  268. * @param int $fid
  269. * @return boolean
  270. */
  271. private function _removeJoionForum($userInfo,$fid) {
  272. // 更新用户data表信息
  273. $userInfo['join_forum'] && $array = self::splitStringToArray($userInfo['join_forum']);
  274. unset($array[$fid]);
  275. $this->_updateMyForumCache($userInfo['uid'], $array);
  276. return true;
  277. }
  278. private function _updateMyForumCache($uid, $array) {
  279. $joinForums = Wekit::load('forum.srv.PwForumService')->getJoinForum($uid);
  280. $_tmpArray = array();
  281. foreach ($array as $k => $v) {
  282. if (!isset($joinForums[$k])) continue;
  283. $_tmpArray[$k] = strip_tags($joinForums[$k]);
  284. }
  285. Wind::import('SRV:user.dm.PwUserInfoDm');
  286. $dm = new PwUserInfoDm($uid);
  287. $dm->setJoinForum(self::_formatJoinForum($_tmpArray));
  288. return $this->_getUserDs()->editUser($dm, PwUser::FETCH_DATA);
  289. }
  290. /**
  291. * 格式化我的版块缓存数据结构
  292. *
  293. * @param array $array 格式化成"1,版块1,2,版块2"
  294. * @return string
  295. */
  296. private static function _formatJoinForum($array) {
  297. if (!$array) return false;
  298. $user = '';
  299. foreach ($array as $fid => $name) {
  300. $myForum .= $fid . ',' . $name . ',';
  301. }
  302. return rtrim($myForum,',');
  303. }
  304. /**
  305. * @return PwUser
  306. */
  307. private function _getUserDs(){
  308. return Wekit::load('user.PwUser');
  309. }
  310. /**
  311. * @return PwForum
  312. */
  313. private function _getForumService() {
  314. return Wekit::load('forum.PwForum');
  315. }
  316. }