comments.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/7/19
  6. * Time: 下午3:16
  7. */
  8. namespace ugc;
  9. use algorithm;
  10. class comments
  11. {
  12. const max_response = 11;
  13. private $counter;
  14. private $users;
  15. private $comments;
  16. private $special_id;
  17. public function __construct($comments,$special_id)
  18. {
  19. $this->counter = [];
  20. $this->users = [];
  21. $this->comments = [];
  22. $this->special_id = intval($special_id);
  23. $this->parse($comments);
  24. }
  25. public function users() {
  26. return $this->users;
  27. }
  28. public function comments() {
  29. return $this->comments;
  30. }
  31. private function parse($comments)
  32. {
  33. $comment_map = [];
  34. foreach ($comments as $comment) {
  35. $comment_id = intval($comment['comment_id']);
  36. $comment_map[$comment_id] = $comment;
  37. }
  38. foreach ($comments as $comment)
  39. {
  40. $response_id = intval($comment['response_id']);
  41. $top_id = intval($comment['top_id']);
  42. if (!array_key_exists($top_id,$this->counter)) {
  43. $this->counter[$top_id] = 0;
  44. }
  45. if($this->counter[$top_id] < self::max_response)
  46. {
  47. if($response_id != 0)
  48. {
  49. if(array_key_exists($response_id,$comment_map)) {
  50. $resp_comment = $comment_map[$response_id];
  51. $to_user = intval($resp_comment['user_id']);
  52. }
  53. else
  54. {
  55. $mod_comment = Model('ugc_comment');
  56. $resp_comment = $mod_comment->getCommentByID($response_id);
  57. if(!empty($resp_comment)) {
  58. $this->add_user($resp_comment);
  59. $to_user = intval($resp_comment['user_id']);
  60. } else {
  61. $to_user = 0;
  62. }
  63. }
  64. }
  65. else {
  66. $to_user = 0;
  67. }
  68. $this->counter[$top_id] += 1;
  69. $this->comments[] = $this->format_comment($comment,$to_user);
  70. $this->add_user($comment);
  71. }
  72. }
  73. }
  74. private function add_user($comment)
  75. {
  76. $user_id = intval($comment['user_id']);
  77. if(!algorithm::binary_search($this->users,$user_id)) {
  78. $pos = algorithm::lower_bonud($this->users,$user_id);
  79. algorithm::array_insert($this->users,$pos,$user_id);
  80. }
  81. }
  82. private function format_comment($comment,$to_user)
  83. {
  84. $result['comment_id'] = $comment['comment_id'];
  85. $result['to_user'] = $to_user;
  86. $result['content'] = $comment['content'];
  87. $result['sub_comments'] = $comment['sub_comments'];
  88. $result['likes'] = $comment['likes'];
  89. $result['addtime'] = $comment['addtime'];
  90. $result['user_id'] = $comment['user_id'];
  91. $result['response_id'] = $comment['response_id'];
  92. $comment_id = intval($comment['comment_id']);
  93. $supporter = new special_support($this->special_id,$comment_id);
  94. $result['supported'] = $supporter->supported();
  95. return $result;
  96. }
  97. }