BubbleController.class.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 冒泡管理
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2017-03-02T22:48:35+0800
  9. */
  10. class BubbleController extends CommonController
  11. {
  12. /**
  13. * [_initialize 前置操作-继承公共前置方法]
  14. * @author Devil
  15. * @blog http://gong.gg/
  16. * @version 0.0.1
  17. * @datetime 2017-03-02T22:48:35+0800
  18. */
  19. public function _initialize()
  20. {
  21. // 调用父类前置方法
  22. parent::_initialize();
  23. // 登录校验
  24. $this->Is_Login();
  25. // 权限校验
  26. $this->Is_Power();
  27. }
  28. /**
  29. * [Index 冒泡]
  30. * @author Devil
  31. * @blog http://gong.gg/
  32. * @version 0.0.1
  33. * @datetime 2017-03-02T22:48:35+0800
  34. */
  35. public function Index()
  36. {
  37. // 参数
  38. $param = array_merge($_POST, $_GET);
  39. // 条件
  40. $where = $this->GetIndexWhere();
  41. // 模型
  42. $m = M('Mood');
  43. // 分页
  44. $number = MyC('admin_page_number');
  45. $page_param = array(
  46. 'number' => $number,
  47. 'total' => $m->alias('m')->join('__USER__ AS u ON m.user_id=u.id')->where($where)->count(),
  48. 'where' => $param,
  49. 'url' => U('Admin/Bubble/Index'),
  50. );
  51. $page = new \My\Page($page_param);
  52. // 查询字段
  53. $field = array('m.id', 'm.user_id', 'm.content', 'm.visible', 'm.add_time', 'u.nickname');
  54. // 数据处理
  55. $data = $this->MoodDataHandle($m->alias('m')->join('__USER__ AS u ON m.user_id=u.id')->field($field)->where($where)->limit($page->GetPageStarNumber(), $number)->order('m.id desc')->select());
  56. $this->assign('data', $data);
  57. // 分页
  58. $this->assign('page_html', $page->GetPageHtml());
  59. // 参数
  60. $this->assign('param', $param);
  61. // 基础数据
  62. $this->assign('common_user_visible_list', L('common_user_visible_list'));
  63. $this->assign('bubble_nav_list', L('bubble_nav_list'));
  64. $this->display('Index');
  65. }
  66. /**
  67. * [GetIndexWhere 条件]
  68. * @author Devil
  69. * @blog http://gong.gg/
  70. * @version 0.0.1
  71. * @datetime 2016-12-10T22:16:29+0800
  72. */
  73. private function GetIndexWhere()
  74. {
  75. $where = array();
  76. // 模糊
  77. if(!empty($_REQUEST['keyword']))
  78. {
  79. $like_keyword = array('like', '%'.I('keyword').'%');
  80. $where[] = array(
  81. 'u.nickname' => $like_keyword,
  82. 'u.mobile' => $like_keyword,
  83. 'u.email' => $like_keyword,
  84. 'm.content' => $like_keyword,
  85. '_logic' => 'or',
  86. );
  87. }
  88. // 是否更多条件
  89. if(I('is_more', 0) == 1)
  90. {
  91. // 表达式
  92. if(!empty($_REQUEST['time_start']))
  93. {
  94. $where['m.add_time'][] = array('gt', strtotime(I('time_start')));
  95. }
  96. if(!empty($_REQUEST['time_end']))
  97. {
  98. $where['m.add_time'][] = array('lt', strtotime(I('time_end')));
  99. }
  100. }
  101. return $where;
  102. }
  103. /**
  104. * [MoodDataHandle 说说数据处理]
  105. * @author Devil
  106. * @blog http://gong.gg/
  107. * @version 0.0.1
  108. * @datetime 2017-04-08T20:14:19+0800
  109. * @param [array] $data [需要处理的数据]
  110. * @return [array] [处理好的说说数据]
  111. */
  112. private function MoodDataHandle($data)
  113. {
  114. if(!empty($data) && is_array($data))
  115. {
  116. $mp = M('MoodPraise');
  117. $mc = M('MoodComments');
  118. foreach($data as $k=>&$v)
  119. {
  120. // 昵称
  121. if(empty($v['nickname']))
  122. {
  123. $v['nickname'] = L('common_bubble_mood_nickname');
  124. }
  125. // 发表时间
  126. $v['add_time'] = date('m-d H:i', $v['add_time']);
  127. // 点赞
  128. $v['praise_count'] = $mp->where(array('mood_id'=>$v['id']))->count();
  129. $v['praise_list'] = $mp->alias('mp')->join('__USER__ AS u ON u.id=mp.user_id')->field(array('mp.id', 'mp.add_time', 'u.nickname'))->where(array('mp.mood_id'=>$v['id']))->order('id desc')->select();
  130. // 评论总数
  131. $v['comments_count'] = $mc->where(array('mood_id'=>$v['id']))->count();
  132. // 评论列表
  133. $v['comments'] = $this->GetMoodComments($v['id']);
  134. }
  135. }
  136. return $data;
  137. }
  138. /**
  139. * [GetMoodComments 获取说说评论]
  140. * @author Devil
  141. * @blog http://gong.gg/
  142. * @version 0.0.1
  143. * @datetime 2017-04-10T14:38:00+0800
  144. * @param [int] $mood_id [说说id]
  145. * @return [array] [评论列表]
  146. */
  147. private function GetMoodComments($mood_id)
  148. {
  149. if(empty($mood_id))
  150. {
  151. return array();
  152. }
  153. // 评论列表
  154. $m = M('MoodComments');
  155. $field = array('mc.id', 'mc.user_id', 'mc.content', 'mc.reply_id', 'mc.add_time', 'u.nickname');
  156. $where = array('m.id'=>$mood_id, 'mc.reply_id'=>0);
  157. $data = $m->alias('mc')->join('__MOOD__ AS m ON mc.mood_id=m.id')->join('__USER__ AS u ON mc.user_id=u.id')->field($field)->where($where)->order('mc.id asc')->select();
  158. // 回复列表
  159. if(!empty($data))
  160. {
  161. $u = M('User');
  162. foreach($data as &$v)
  163. {
  164. // 评论时间
  165. $v['add_time'] = date('m-d H:i', $v['add_time']);
  166. // 评论内容
  167. $v['content'] = str_replace("\n", "<br />", $v['content']);
  168. $item_where = array('m.id'=>$mood_id, 'mc.parent_id'=>$v['id'], 'reply_id'=>array('gt', 0));
  169. $item = $m->alias('mc')->join('__MOOD__ AS m ON mc.mood_id=m.id')->join('__USER__ AS u ON mc.user_id=u.id')->field($field)->where($item_where)->order('mc.id asc')->select();
  170. if(!empty($item))
  171. {
  172. foreach($item as &$vs)
  173. {
  174. // 评论时间
  175. $vs['add_time'] = date('m-d H:i', $vs['add_time']);
  176. // 评论内容
  177. $vs['content'] = str_replace("\n", "<br />", $vs['content']);
  178. // 被回复的用户
  179. if($vs['reply_id'] > 0)
  180. {
  181. $uid = $m->where(array('id'=>$vs['reply_id']))->getField('user_id');
  182. if(!empty($uid))
  183. {
  184. $user = $u->field(array('id AS reply_user_id', 'nickname AS reply_nickname'))->find($uid);
  185. if(empty($user['reply_nickname']))
  186. {
  187. $user['reply_nickname'] = L('common_bubble_mood_nickname');
  188. }
  189. $vs = array_merge($vs, $user);
  190. }
  191. }
  192. }
  193. $v['item'] = $item;
  194. }
  195. }
  196. }
  197. return $data;
  198. }
  199. /**
  200. * [MoodDelete 说说删除]
  201. * @author Devil
  202. * @blog http://gong.gg/
  203. * @version 0.0.1
  204. * @datetime 2016-12-25T22:36:12+0800
  205. */
  206. public function MoodDelete()
  207. {
  208. // 是否ajax请求
  209. if(!IS_AJAX)
  210. {
  211. $this->error(L('common_unauthorized_access'));
  212. }
  213. // 参数
  214. $m = M('Mood');
  215. $id = I('id');
  216. // 开启事务
  217. $m->startTrans();
  218. // 数据删除[说说,点赞,评论]
  219. $mood_state = $m->where(array('id'=>$id))->delete();
  220. $praise_state = M('MoodPraise')->where(array('mood_id'=>$id))->delete();
  221. $comments_state = M('MoodComments')->where(array('mood_id'=>$id))->delete();
  222. if($mood_state !== false && $praise_state !== false && $comments_state !== false)
  223. {
  224. // 提交事务
  225. $m->commit();
  226. $this->ajaxReturn(L('common_operation_delete_success'));
  227. } else {
  228. // 回滚事务
  229. $m->rollback();
  230. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  231. }
  232. }
  233. /**
  234. * [MoodPraiseDelete 说说点赞删除]
  235. * @author Devil
  236. * @blog http://gong.gg/
  237. * @version 0.0.1
  238. * @datetime 2016-12-25T22:36:12+0800
  239. */
  240. public function MoodPraiseDelete()
  241. {
  242. // 是否ajax请求
  243. if(!IS_AJAX)
  244. {
  245. $this->error(L('common_unauthorized_access'));
  246. }
  247. // 数据删除
  248. if(M('MoodPraise')->delete(I('id')))
  249. {
  250. $this->ajaxReturn(L('common_operation_delete_success'));
  251. } else {
  252. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  253. }
  254. }
  255. /**
  256. * [MoodCommentsDelete 说说评论删除]
  257. * @author Devil
  258. * @blog http://gong.gg/
  259. * @version 0.0.1
  260. * @datetime 2016-12-25T22:36:12+0800
  261. */
  262. public function MoodCommentsDelete()
  263. {
  264. // 是否ajax请求
  265. if(!IS_AJAX)
  266. {
  267. $this->error(L('common_unauthorized_access'));
  268. }
  269. // 参数
  270. $id = I('id');
  271. // 模型
  272. $m = M('MoodComments');
  273. // 开启事务
  274. $m->startTrans();
  275. // 数据删除
  276. $state = $m->where(array('id'=>$id))->delete();
  277. $item_state = $m->where(array('parent_id'=>$id))->delete();
  278. $reply_state = $m->where(array('reply_id'=>$id))->delete();
  279. if($state !== false && $item_state !== false && $reply_state !== false)
  280. {
  281. // 提交事务
  282. $m->commit();
  283. $this->ajaxReturn(L('common_operation_delete_success'));
  284. } else {
  285. // 回滚事务
  286. $m->rollback();
  287. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  288. }
  289. }
  290. }
  291. ?>