PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/src/service/forum/srv/PwForumService.php

https://gitlab.com/wuhang2003/phpwind
PHP | 279 lines | 162 code | 20 blank | 97 comment | 32 complexity | d3ee96ad12aa33b2e2004e0adfa45c94 MD5 | raw file
  1. <?php
  2. defined('WEKIT_VERSION') || exit('Forbidden');
  3. Wind::import('SRV:forum.dm.PwForumDm');
  4. Wind::import('SRV:forum.bo.PwForumBo');
  5. Wind::import('SRV:forum.PwForum');
  6. /**
  7. * 版块公共服务
  8. *
  9. * @author Jianmin Chen <sky_hold@163.com>
  10. * @license http://www.phpwind.com
  11. * @version $Id: PwForumService.php 24758 2013-02-20 06:55:42Z jieyin $
  12. * @package forum
  13. */
  14. class PwForumService {
  15. protected static $_forums = null;
  16. protected static $_map = null;
  17. /**
  18. * 获取版块列表
  19. *
  20. * @return array
  21. */
  22. public function getForumList() {
  23. isset(self::$_forums) || self::$_forums = $this->_getForum()->getForumList();
  24. return self::$_forums;
  25. }
  26. /**
  27. * 获取用户所有可以访问的版块列表
  28. *
  29. * @param PwUserBo $user
  30. * @return array
  31. */
  32. public function getAllowVisitForum(PwUserBo $user, $forums = null) {
  33. $forums === null && $forums = $this->getForumList();
  34. $fids = array();
  35. foreach ($forums as $key => $value) {
  36. if (!$value['allow_visit'] || $user->inGroup(explode(',', $value['allow_visit']))) {
  37. $fids[] = $value['fid'];
  38. }
  39. }
  40. return $fids;
  41. }
  42. /**
  43. * 获取用户所有禁止访问的版块列表
  44. *
  45. * @param PwUserBo $user
  46. * @param array $forums 版块列表
  47. * @param bool $includeHide 是否包含隐藏版块
  48. * @return array
  49. */
  50. public function getForbidVisitForum(PwUserBo $user, $forums = null, $includeHide = false) {
  51. $forums === null && $forums = $this->getForumList();
  52. $fids = array();
  53. foreach ($forums as $key => $value) {
  54. if ($value['allow_visit'] && !$user->inGroup(explode(',', $value['allow_visit']))) {
  55. $fids[] = $value['fid'];
  56. } elseif ($includeHide && $value['isshow'] == 0) {
  57. $fids[] = $value['fid'];
  58. }
  59. }
  60. return $fids;
  61. }
  62. /**
  63. * 获取指定版块的信息(使用全部版块缓存,如果没有这个前提,直接使用 forum.PwForum->fetchForum 接口比较合算)
  64. *
  65. * @param array $fids;
  66. * @return array
  67. */
  68. public function fetchForum($fids) {
  69. return Pw::subArray($this->getForumList(), $fids);
  70. }
  71. /**
  72. * 获取版块层级列表
  73. *
  74. * @return array
  75. */
  76. public function getForumMap() {
  77. if (!isset(self::$_map)) {
  78. $forums = $this->getForumList();
  79. foreach ($forums as $key => $value) {
  80. self::$_map[$value['parentid']][] = $value;
  81. }
  82. }
  83. return self::$_map;
  84. }
  85. /**
  86. * 获取分类和版块列表(子版除外)
  87. *
  88. * @return array
  89. */
  90. public function getCommonForumList($fetchmode = PwForum::FETCH_MAIN) {
  91. $forumdb = array(0 => array());
  92. $forumList = $this->_getForum()->getCommonForumList($fetchmode);
  93. foreach ($forumList as $forums) {
  94. if (!$forums['isshow']) continue;
  95. if ($forums['type'] === 'forum') {
  96. $forumdb[$forums['parentid']][$forums['fid']] = $forums;
  97. } elseif ($forums['type'] === 'category') {
  98. $forumdb[0][$forums['fid']] = $forums;
  99. }
  100. }
  101. return $forumdb;
  102. }
  103. /**
  104. * 根据层级列表,递归获取链级列表
  105. *
  106. * @param int $parentid 获取该版的所属
  107. * @param array $map 版块层级列表
  108. * @return array
  109. */
  110. public function getForumsByLevel($parentid, $map) {
  111. if (!isset($map[$parentid])) return array();
  112. $length = count($map[$parentid]);
  113. $array = array();
  114. foreach ($map[$parentid] as $key => $value) {
  115. if ($key == $length-1) $value['isEnd'] = 1;
  116. $array[] = $value;
  117. $array = array_merge($array, $this->getForumsByLevel($value['fid'], $map));
  118. }
  119. return $array;
  120. }
  121. /**
  122. * 根据层级列表,递归获取链级列表
  123. *
  124. * @param int $parentid 获取该版的所属
  125. * @param array $map 版块层级列表
  126. * @return array
  127. */
  128. public function findOptionInMap($parentid, $map, $lang = array()) {
  129. if (!isset($map[$parentid])) return array();
  130. $result = array();
  131. foreach ($map[$parentid] as $key => $value) {
  132. $result[$value['fid']] = $lang[$value['type']] . $value['name'];
  133. $result += $this->findOptionInMap($value['fid'], $map, $lang);
  134. }
  135. return $result;
  136. }
  137. /**
  138. * 获取版块的select/option
  139. *
  140. * @param mixed $selected 选中的fid序列
  141. * @return string option的html
  142. */
  143. public function getForumOption($selected = array()) {
  144. is_array($selected) || $selected = array($selected);
  145. $map = $this->getForumMap();
  146. $option_html = '';
  147. $option_arr = $this->findOptionInMap(0, $map, array(
  148. 'category' => '&gt;&gt; ',
  149. 'forum' => ' &nbsp;|- ',
  150. 'sub' => ' &nbsp; &nbsp;|- ',
  151. 'sub2' => '&nbsp;&nbsp; &nbsp; &nbsp;|- '
  152. ));
  153. foreach ($option_arr as $key => $value) {
  154. $option_html .= '<option value="' . $key . '"' . (in_array($key, $selected) ? ' selected' : '') . '>' . strip_tags($value) . '</option>';
  155. }
  156. return $option_html;
  157. }
  158. /**
  159. * 获取上级版块id序列
  160. *
  161. * @param int $fid 版块id
  162. * @return array
  163. */
  164. public function getParentFids($fid) {
  165. $forums = $this->getForumList();
  166. $upfids = array();
  167. $fid = $forums[$fid]['parentid'];
  168. while (in_array($forums[$fid]['type'], array('sub2','sub','forum'))) {
  169. $upfids[] = $fid;
  170. $fid = $forums[$fid]['parentid'];
  171. }
  172. return $upfids;
  173. }
  174. /**
  175. * 获取分类id
  176. *
  177. * @param int $fid
  178. * @return int
  179. */
  180. public function getCateId($fid) {
  181. $forum = $this->_getForum()->getForum($fid);
  182. if ($forum['type'] == 'category') return $fid;
  183. $array = explode(',', $forum['fup']);
  184. return array_pop($array);
  185. }
  186. /**
  187. * 获取用户加入的版块列表
  188. *
  189. * @param int $uid
  190. * @return array
  191. */
  192. public function getJoinForum($uid) {
  193. if ($result = Wekit::load('forum.PwForumUser')->getFroumByUid($uid)) {
  194. $array = array();
  195. $tmp = Wekit::load('forum.PwForum')->fetchForum(array_keys($result));
  196. foreach ($tmp as $key => $value) {
  197. $array[$value['fid']] = $value['name'];
  198. }
  199. return $array;
  200. }
  201. return array();
  202. }
  203. /**
  204. * 重新统计本版及上级版块的帖子统计数
  205. *
  206. * @param mixed $forum (int fid | object PwForumBo)
  207. * @return void
  208. */
  209. public function updateForumStatistics($forum) {
  210. if (!$forum instanceof PwForumBo) {
  211. $forum = new PwForumBo($forum);
  212. }
  213. if (!$forum->isForum()) return false;
  214. $service = $this->_getForum();
  215. $service->updateForumStatistics($forum->fid);
  216. if ($fids = $forum->getParentFids()) {
  217. foreach ($fids as $fid) {
  218. $service->updateForumStatistics($fid);
  219. }
  220. }
  221. }
  222. /**
  223. * 更新版块帖子统计数
  224. *
  225. * @param mixed $forum int 版块fid | object PwForumBo
  226. * @param int $topic 主题更新数
  227. * @param int $replies 回复更新数
  228. * @param int $tpost 今日发帖更新数
  229. * @param int $lastinfo
  230. * @return void
  231. */
  232. public function updateStatistics($forum, $topic, $replies, $tpost = 0, $lastinfo = array()) {
  233. if (!$forum instanceof PwForumBo) {
  234. $forum = new PwForumBo($forum);
  235. }
  236. if (!$forum->isForum()) return false;
  237. $article = $topic + $replies;
  238. $dm = new PwForumDm($forum->fid);
  239. $dm->addThreads($topic)->addPosts($replies)->addArticle($article)->addTodayPosts($tpost);
  240. if ($lastinfo) {
  241. !isset($lastinfo['time']) && $lastinfo['time'] = Pw::getTime();
  242. $dm->setLastpostInfo($lastinfo['tid'], Pw::substrs($lastinfo['subject'], 26, 0, true), $lastinfo['username'], $lastinfo['time']);
  243. }
  244. $service = $this->_getForum();
  245. $service->updateForum($dm, PwForum::FETCH_STATISTICS);
  246. if ($fids = $forum->getParentFids()) {
  247. $dm = new PwForumDm(true);
  248. $dm->addArticle($article)->addSubThreads($topic)->addTodayPosts($tpost);
  249. if ($lastinfo && $forum->isOpen()) {
  250. $dm->setLastpostInfo($lastinfo['tid'], Pw::substrs($lastinfo['subject'], 26, 0, true), $lastinfo['username'], $lastinfo['time']);
  251. }
  252. $service->batchUpdateForum($fids, $dm, PwForum::FETCH_STATISTICS);
  253. }
  254. return true;
  255. }
  256. protected function _getForum() {
  257. return Wekit::load('forum.PwForum');
  258. }
  259. }