人人商城

Comment.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. namespace We7\Table\Article;
  7. class Comment extends \We7Table {
  8. protected $tableName = 'article_comment';
  9. protected $primaryKey = 'id';
  10. protected $field = array(
  11. 'articleid',
  12. 'parentid',
  13. 'uid',
  14. 'content',
  15. 'is_like',
  16. 'is_reply',
  17. 'like_num',
  18. 'createtime',
  19. );
  20. protected $default = array(
  21. 'articleid' => '',
  22. 'parentid' => 0,
  23. 'uid' => '',
  24. 'content' => '',
  25. 'is_like' => 2,
  26. 'is_reply' => 2,
  27. 'like_num' => 0,
  28. 'createtime' => '',
  29. );
  30. public function getById($comment_id) {
  31. return $this->where('id' ,$comment_id)->get();
  32. }
  33. public function addComment($comment) {
  34. if (!empty($comment['parentid'])) {
  35. $result = $this->where('id', $comment['parentid'])->fill('is_reply', 1)->save();
  36. if ($result === false) {
  37. return false;
  38. }
  39. }
  40. $comment['createtime'] = TIMESTAMP;
  41. $comment['is_like'] = 2;
  42. return $this->fill($comment)->save();
  43. }
  44. public function hasLiked($articleid, $comment_id) {
  45. global $_W;
  46. $liked = $this->where(array('articleid' => $articleid, 'parentid' => $comment_id, 'is_like' => 1, 'uid' => $_W['uid']))->getcolumn('id');
  47. return boolval($liked);
  48. }
  49. public function likeComment($uid, $articleid, $comment_id) {
  50. $like_num = $this->where('id', $comment_id)->getcolumn('like_num');
  51. $result = $this->where('id', $comment_id)->fill('like_num', $like_num + 1)->save();
  52. if ($result === false) {
  53. return false;
  54. }
  55. $this->fill(array(
  56. 'uid' => $uid,
  57. 'articleid' => $articleid,
  58. 'parentid' => $comment_id,
  59. 'is_like' => 1,
  60. 'is_reply' => 1,
  61. 'like_num' => 0,
  62. 'content' => '',
  63. 'createtime' => TIMESTAMP,
  64. ));
  65. return $this->save();
  66. }
  67. public function getComments($articleid, $pageindex, $pagesize = 15, $order = 'id') {
  68. $comments = $this->where('articleid', $articleid)
  69. ->where('parentid', 0)
  70. ->where('is_like', 2)
  71. ->orderby($order, 'DESC')
  72. ->page($pageindex, $pagesize)
  73. ->getall('id');
  74. $total = $this->getLastQueryTotal();
  75. if (!empty($comments)) {
  76. $this->extendUserinfo($comments);
  77. foreach ($comments as $k => &$comment) {
  78. $comment['createtime'] = date('Y-m-d H:i', $comment['createtime']);
  79. }
  80. }
  81. return array('list' => $comments, 'total' => $total);
  82. }
  83. public function extendUserinfo(&$comments) {
  84. if (empty($comments)) {
  85. return true;
  86. }
  87. $uids = array();
  88. foreach ($comments as $comment) {
  89. $uids[$comment['uid']] = $comment['uid'];
  90. }
  91. if (!empty($uids)) {
  92. $users = $this->getQuery()
  93. ->select('u.uid, u.username, p.realname, p.nickname, p.avatar, p.mobile')
  94. ->from('users', 'u')
  95. ->leftjoin('users_profile', 'p')
  96. ->on(array('u.uid' => 'p.uid'))
  97. ->where('u.uid', $uids)
  98. ->getall('uid');
  99. foreach ($comments as &$comment) {
  100. if (!empty($users[$comment['uid']])) {
  101. $comment = array_merge($comment, $users[$comment['uid']]);
  102. }
  103. }
  104. }
  105. return true;
  106. }
  107. }