PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/src/applications/tag/controller/IndexController.php

https://gitlab.com/wuhang2003/phpwind
PHP | 419 lines | 296 code | 27 blank | 96 comment | 45 complexity | b4ee6fb949d23d21edb1166d71fea276 MD5 | raw file
  1. <?php
  2. Wind::import('LIB:base.PwBaseController');
  3. /**
  4. * 话题前台
  5. *
  6. */
  7. class IndexController extends PwBaseController {
  8. private $hotTag = 4;
  9. private $hotContents = 3;
  10. private $perpage = 10;
  11. private $defaultType = 'threads';
  12. private $attentionTagList = 10;
  13. private $hotTagList = 10; //热门话题显示数
  14. /* (non-PHPdoc)
  15. * @see WindController::run()
  16. */
  17. public function run() {
  18. $typeName = $this->defaultType;
  19. $categoryId = intval($this->getInput('categoryid','get'));
  20. $alias = $this->getInput('alias','get');
  21. $tagServicer = $this->_getTagService();
  22. // $hotTags = $tagServicer->getHotTags($categoryId,20);
  23. $hotTags = $tagServicer->getHotTagsNoCache($categoryId,20);
  24. $tagIds = array();
  25. foreach ($hotTags as $k => $v) {
  26. $attentions = $this->_getTagAttentionDs()->getAttentionUids($k,0,5);
  27. $hotTags[$k]['weight'] = 0.7 * $v['content_count'] + 0.3 * $v['attention_count'];
  28. $hotTags[$k]['attentions'] = array_keys($attentions);
  29. $tagIds[] = $k;
  30. }
  31. usort($hotTags, array($this, 'cmp'));
  32. $myTags = $this->_getTagAttentionDs()->getAttentionByUidAndTagsIds($this->loginUser->uid,$tagIds);
  33. $this->setOutput($myTags, 'myTags');
  34. $this->setOutput($hotTags, 'hotTags');
  35. $this->setOutput($categoryId, 'categoryId');
  36. //seo设置
  37. Wind::import('SRV:seo.bo.PwSeoBo');
  38. $seoBo = PwSeoBo::getInstance();
  39. $seoBo->init('topic', 'hot');
  40. Wekit::setV('seo', $seoBo);
  41. }
  42. /**
  43. * 我的话题
  44. */
  45. public function myAction(){
  46. if ($this->loginUser->uid < 1) {
  47. $this->forwardRedirect(WindUrlHelper::createUrl('u/login/run', array('backurl' => WindUrlHelper::createUrl('tag/index/my'))));
  48. }
  49. $typeName = $this->defaultType;
  50. /* list($page) = $this->getInput(array('page'));
  51. $page = $page ? $page : 1;
  52. list($start, $limit) = Pw::page2limit($page, $this->attentionTagList);*/
  53. $tagServicer = $this->_getTagService();
  54. //获取我关注的话题列表
  55. $myTagsCount = $this->_getTagAttentionDs()->countAttentionByUid($this->loginUser->uid);
  56. if ($myTagsCount) {
  57. $relations = $this->_getTagDs()->getAttentionByUid($this->loginUser->uid,0,50);
  58. $relationTagIds = array_keys($relations);
  59. $myTagList = array_slice($relationTagIds,0,10);
  60. $myTagsList = $this->_getTagDs()->fetchTag($relationTagIds);
  61. $tmpArray = array();
  62. foreach ($myTagList as $v) {
  63. $tmpArray[$v] = $myTagsList[$v];
  64. }
  65. $myTags['tags'] = $tmpArray;
  66. $myTags['step'] = $myTagsCount > $this->attentionTagList ? 2 : '';
  67. $ifcheck = !$this->_checkAllowManage() ? 1 : '';
  68. $tagContents = $params = $relatedTags = array();
  69. $tmpTagContent = $myTags['tags'] ? array_slice($myTags['tags'], 0, 5, true) : array();
  70. foreach($tmpTagContent as $k=>$v) {
  71. $contents = $tagServicer->getContentsByTypeName($k,$typeName,$ifcheck,0,$this->hotContents);
  72. if ($contents) {
  73. $tagContents[$k] = $contents;
  74. foreach ($contents as $k2=>$v2) {
  75. $params[] = $k2;
  76. }
  77. }
  78. }
  79. $moreTags = array_diff_key($myTagsList, $tagContents);
  80. $params and $relatedTags = $tagServicer->getRelatedTags($typeName,$params);
  81. }
  82. //热门话题
  83. $this->_setHotTagList($tagServicer->getHotTags(0,20));
  84. $this->setOutput($tagContents, 'tagContents');
  85. $this->setOutput($relatedTags, 'relatedTags');
  86. $this->setOutput($myTags, 'myTags');
  87. $this->setOutput($moreTags, 'moreTags');
  88. $this->setOutput($myTagsList, 'myTagsList');
  89. $this->setOutput($myTagsCount, 'myTagsCount');
  90. //$this->setOutput($page, 'page');
  91. //$this->setOutput($this->perpage, 'perpage');
  92. // seo设置
  93. Wind::import('SRV:seo.bo.PwSeoBo');
  94. $seoBo = PwSeoBo::getInstance();
  95. $lang = Wind::getComponent('i18n');
  96. $seoBo->setCustomSeo($lang->getMessage('SEO:tag.index.my.title'), '', '');
  97. Wekit::setV('seo', $seoBo);
  98. }
  99. /**
  100. *
  101. * 关注话题
  102. */
  103. public function attentionAction(){
  104. if ($this->loginUser->uid < 1) {
  105. $this->showError('USER:user.not.login');
  106. }
  107. $tagId = intval($this->getInput('id'));
  108. $type = $this->getInput('type');
  109. $uid = $this->loginUser->uid;
  110. if ($type == 'add') {
  111. $result = $this->_getTagService()->addAttention($uid,$tagId);
  112. if ($result instanceof PwError) $this->showError($result->getError());
  113. $this->showMessage('TAG:add.success');
  114. } else {
  115. $this->_getTagService()->deleteAttention($uid, $tagId);
  116. $this->showMessage('TAG:del.success');
  117. }
  118. }
  119. /**
  120. * 话题聚合页 - 内容
  121. *
  122. * @return void
  123. */
  124. public function viewAction(){
  125. list($id,$page,$perpage,$type,$tagName) = $this->getInput(array('id', 'page', 'perpage', 'type', 'name'));
  126. $page = $page ? $page : 1;
  127. if (!$id && $tagName) {
  128. $tagName = rawurldecode($tagName);
  129. $tag = $this->_getTagDs()->getTagByName($tagName);
  130. $id = $tag['tag_id'];
  131. } else {
  132. $tag = $this->_getTagDs()->getTag($id);
  133. }
  134. if (!$tag) $this->showError("TAG:id.empty", "tag/index/run");
  135. if ($tag['parent_tag_id']) {
  136. $tag = $this->_getTagDs()->getTag($tag['parent_tag_id']);
  137. $id = $tag['tag_id'];
  138. }
  139. // 是否关注
  140. $tag['attention'] = $this->_getTagAttentionDs()->isAttentioned($this->loginUser->uid,$id);
  141. //获取我关注的话题列表
  142. list($myTagsCount,$myTags['tags']) = $this->_getTagService()->getAttentionTags($this->loginUser->uid,0,$this->attentionTagList);
  143. $myTags['step'] = $myTagsCount > $this->attentionTagList ? 2 : '';
  144. //热门话题
  145. $this->_setHotTagList($this->_getTagService()->getHotTags(0,20));
  146. if ($type == 'users') {
  147. $perpage = 50;
  148. list($start, $limit) = Pw::page2limit($page, $perpage);
  149. list($count, $users) = $this->_getTagService()->getTagMembers($id,$start,$limit);
  150. $this->setOutput($users, 'users');
  151. } else {
  152. $perpage = $perpage ? $perpage : $this->perpage;
  153. list($start, $limit) = Pw::page2limit($page, $perpage);
  154. // to du $ifcheck
  155. $ifcheck = !$this->_checkAllowManage() ? 1 : '';
  156. $typeId = $this->_getTagService()->getTypeIdByTypeName($this->defaultType);
  157. $count = $this->_getTagDs()->countRelationsByTagId($id,$typeId,$ifcheck);
  158. // $tag['content_count'] = $count;
  159. if ($count > 0) {
  160. $tmpTags = $this->_getTagService()->getContentsByTypeName($id,$this->defaultType,$ifcheck,$start,$limit);
  161. foreach ($tmpTags as $k=>$v) {
  162. $params[] = $k;
  163. $v['type_id'] = $typeId;
  164. $v['param_id'] = $v['tid'];
  165. $contents[$k] = $v;
  166. }
  167. $params and $relatedTags = $this->_getTagService()->getRelatedTags($this->defaultType,$params);
  168. $this->setOutput($relatedTags, 'relatedTags');
  169. $this->setOutput($contents, 'contents');
  170. }
  171. }
  172. $args = array(
  173. 'id' => $tag['tag_id'],
  174. 'type' => $type,
  175. );
  176. $this->setOutput($this->_checkAllowManage(), 'allowManage');
  177. $this->setOutput($myTags, 'myTags');
  178. $this->setOutput($count, 'count');
  179. $this->setOutput($page, 'page');
  180. $this->setOutput($perpage, 'perpage');
  181. $this->setOutput($tag, 'tag');
  182. $this->setOutput($args, 'args');
  183. // seo设置
  184. Wind::import('SRV:seo.bo.PwSeoBo');
  185. $seoBo = PwSeoBo::getInstance();
  186. if ($type == 'users') {
  187. $lang = Wind::getComponent('i18n');
  188. $seoBo->setCustomSeo($lang->getMessage('SEO:tag.index.view.users.title', array($tag['tag_name'])), '', '');
  189. } else {
  190. if ($tag['seo_title'] || $tag['seo_keywords'] || $tag['seo_description']) {
  191. $seoBo->setCustomSeo($tag['seo_title'],$tag['seo_keywords'],$tag['seo_description']);
  192. } else {
  193. $lang = Wind::getComponent('i18n');
  194. $seoBo->setCustomSeo($lang->getMessage('SEO:tag.index.view.title', array($tag['tag_name'])), '', '');
  195. }
  196. }
  197. Wekit::setV('seo', $seoBo);
  198. }
  199. /**
  200. * 屏蔽操作
  201. *
  202. * @return void
  203. */
  204. public function doshieldAction(){
  205. // 是否登录
  206. if ($this->loginUser->uid < 1) {
  207. $this->showError('USER:user.not.login');
  208. }
  209. // 是否有权限
  210. if ($this->_checkAllowManage() !== true) {
  211. $this->showError('TAG:right.tag_allow_manage.error');
  212. }
  213. list($id,$typeId,$paramId,$ifcheck) = $this->getInput(array('id','type_id','param_id','ifcheck'));
  214. $increseCount = $ifcheck ? 1 : -1;
  215. Wind::import('SRV:tag.dm.PwTagDm');
  216. $dm = new PwTagDm($id);
  217. $dm->setIfCheck($ifcheck)
  218. ->addContentCount($increseCount);
  219. $result = $this->_getTagDs()->updateRelation($typeId,$paramId,$id,$dm);
  220. $this->_getTagDs()->updateTag($dm);
  221. Wind::import('SRV:log.srv.operator.PwAddTagShieldLog');
  222. $log = new PwAddTagShieldLog($id, $typeId, $paramId, $this->loginUser);
  223. $log->setIfShield($ifcheck)
  224. ->execute();
  225. !$result && $this->showError('fail');
  226. $this->showMessage('success');
  227. }
  228. /**
  229. * 关注话题榜单
  230. *
  231. * @return void
  232. */
  233. public function attentionlistAction(){
  234. $step = (int)$this->getInput('step');
  235. $step < 1 && $this->showError('data.error');
  236. list($start, $limit) = Pw::page2limit($step, $this->attentionTagList);
  237. list($myTagsCount,$myTags['tags']) = $this->_getTagService()->getAttentionTags($this->loginUser->uid,$start,$limit);
  238. $countStep = ceil($myTagsCount/$this->attentionTagList);
  239. $step < $countStep && $myTags['step'] = $step+1;
  240. Pw::echoJson($myTags);exit;
  241. }
  242. /**
  243. * 编辑帖子阅读页话题
  244. *
  245. * @return void
  246. */
  247. public function editReadTagAction(){
  248. // 是否登录
  249. if ($this->loginUser->uid < 1) {
  250. $this->showError('USER:user.not.login');
  251. }
  252. list($tid,$tagnames) = $this->getInput(array('tid','tagnames'));
  253. $tagnames = $tagnames ? $tagnames : array();
  254. // 是否有权限
  255. if ($this->_checkAllowEdit($tid) !== true) {
  256. $this->showError('TAG:right.tag_allow_edit.error');
  257. }
  258. $count = count($tagnames);
  259. $count > 5 && $this->showError("Tag:tagnum.exceed");
  260. Wind::import('SRV:tag.dm.PwTagDm');
  261. if ($count == 1) {
  262. $dm = new PwTagDm();
  263. $dm->setName($tagnames['0']);
  264. if(($result = $dm->beforeUpdate()) instanceof PwError) {
  265. $this->showError($result->getError());
  266. }
  267. }
  268. // 敏感词
  269. $content = implode(' ', $tagnames);
  270. $wordFilter = Wekit::load('SRV:word.srv.PwWordFilter');
  271. list($type, $words) = $wordFilter->filterWord($content);
  272. if ($type) {
  273. $this->showError("WORD:content.error");
  274. }
  275. $typeId = $this->_getTagService()->getTypeIdByTypeName($this->defaultType);
  276. $dmArray = array();
  277. foreach ((array)$tagnames as $value) {
  278. $value = trim($value);
  279. if(Pw::strlen($value) > 15) {
  280. continue;
  281. }
  282. $dm = new PwTagDm();
  283. if (($result = $dm->checkTagName($value)) instanceof PwError) {
  284. $this->showError($result->getError());
  285. }
  286. $dmArray[$value] =
  287. $dm->setName($value)
  288. ->setTypeId($typeId)
  289. ->setParamId($tid)
  290. ->setIfHot(1)
  291. ->setCreatedTime(Pw::getTime())
  292. ->setCreateUid($this->loginUser->uid)
  293. ;
  294. }
  295. $result = $this->_getTagService()->updateTags($typeId,$tid,$dmArray);
  296. if ($result instanceof PwError) {
  297. $this->showError($result->getError());
  298. }
  299. $this->showMessage('success');
  300. }
  301. /**
  302. * 获取热门话题
  303. *
  304. * @return void
  305. */
  306. public function getHotTagsAction(){
  307. $hotTags = $this->_getTagService()->getHotTags('',$this->hotTagList);
  308. Pw::echoJson($hotTags);exit;
  309. }
  310. /**
  311. * 话题小名片
  312. *
  313. * @return void
  314. */
  315. public function cardAction(){
  316. $name = $this->getInput('name');
  317. $tag = $this->_getTagService()->getTagCard($name,$this->loginUser->uid);
  318. $this->setOutput($tag, 'tag');
  319. }
  320. protected function _formatTags($tags) {
  321. if (!$tags) return false;
  322. $tagname = array();
  323. foreach ($tags as $v) {
  324. $tagname[] = $v['tag_name'];
  325. }
  326. return implode(',',$tagname);
  327. }
  328. /**
  329. * 检测屏蔽权限
  330. *
  331. * @return void
  332. */
  333. private function _checkAllowManage() {
  334. if ($this->loginUser->getPermission('tag_allow_manage') < 1) {
  335. return false;
  336. }
  337. return true;
  338. }
  339. /**
  340. * 检测编辑权限
  341. *
  342. * @return void
  343. */
  344. private function _checkAllowEdit($tid) {
  345. $thread = Wekit::load('forum.PwThread')->getThread($tid);
  346. if (!($thread['created_userid'] == $this->loginUser->uid && $this->loginUser->getPermission('tag_allow_add')) && $this->loginUser->getPermission('tag_allow_edit') < 1) {
  347. return false;
  348. }
  349. return true;
  350. }
  351. /**
  352. * 设置热门话题
  353. *
  354. * @return void
  355. */
  356. private function _setHotTagList($hotTags){
  357. $hotTags = array_slice($hotTags,0, $this->hotTagList);
  358. $this->setOutput($hotTags,'hotTagList');
  359. }
  360. private function cmp($a, $b) {
  361. return strcmp($b["weight"], $a["weight"]);
  362. }
  363. /**
  364. * @return PwTag
  365. */
  366. private function _getTagDs() {
  367. return Wekit::load('tag.PwTag');
  368. }
  369. /**
  370. * @return PwTagService
  371. */
  372. private function _getTagService() {
  373. return Wekit::load('tag.srv.PwTagService');
  374. }
  375. /**
  376. * 分类DS
  377. *
  378. * @return PwTagCateGory
  379. */
  380. private function _getTagCateGoryDs(){
  381. return Wekit::load('tag.PwTagCateGory');
  382. }
  383. /**
  384. * 关注DS
  385. *
  386. * @return PwTagAttention
  387. */
  388. private function _getTagAttentionDs(){
  389. return Wekit::load('tag.PwTagAttention');
  390. }
  391. }
  392. ?>