comments.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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,$comment_subcounts)
  18. {
  19. $this->counter = [];
  20. $this->users = [];
  21. $this->comments = [];
  22. $this->special_id = intval($special_id);
  23. $this->parse($comments,$comment_subcounts);
  24. }
  25. public function users() {
  26. return $this->users;
  27. }
  28. public function comments() {
  29. return $this->comments;
  30. }
  31. private function parse($comments,$comment_subcounts)
  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. $comment_id = intval($comment['comment_id']);
  69. if(array_key_exists($comment_id,$comment_subcounts)) {
  70. $sub_count = $comment_subcounts[$comment_id];
  71. } else {
  72. $sub_count = 0;
  73. }
  74. $this->counter[$top_id] += 1;
  75. $this->comments[] = $this->format_comment($comment,$to_user,$sub_count);
  76. $this->add_user($comment);
  77. }
  78. }
  79. }
  80. private function add_user($comment)
  81. {
  82. $user_id = intval($comment['user_id']);
  83. if(!algorithm::binary_search($this->users,$user_id)) {
  84. $pos = algorithm::lower_bonud($this->users,$user_id);
  85. algorithm::array_insert($this->users,$pos,$user_id);
  86. }
  87. }
  88. private function format_comment($comment,$to_user,$sub_count)
  89. {
  90. $result['comment_id'] = $comment['comment_id'];
  91. $result['to_user'] = $to_user;
  92. $result['content'] = $comment['content'];
  93. $result['sub_comments'] = $comment['sub_comments'];
  94. $result['likes'] = $comment['likes'];
  95. $result['addtime'] = $comment['addtime'];
  96. $result['user_id'] = $comment['user_id'];
  97. $result['response_id'] = $comment['response_id'];
  98. $result['sub_count'] = $sub_count;
  99. $comment_id = intval($comment['comment_id']);
  100. $supporter = new special_support($this->special_id,$comment_id);
  101. $result['supported'] = $supporter->supported();
  102. return $result;
  103. }
  104. }