CommentHistory.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use App\Models\DTO\SearchData;
  6. class CommentHistory extends Model
  7. {
  8. protected $table = 'tb_comment_history';
  9. protected $primaryKey = 'id';
  10. public $keyType = 'int';
  11. public $incrementing = true;
  12. public $timestamps = true;
  13. const CREATED_AT = 'created_at';
  14. const UPDATED_AT = null;
  15. protected $guarded = [];
  16. /**
  17. * 댓글 변경 이력 조회
  18. */
  19. public function data(SearchData $params): object
  20. {
  21. $query = $this->query();
  22. $query->select(
  23. 'tb_board.name AS boardName',
  24. 'tb_comment_history.id',
  25. 'tb_comment_history.board_id',
  26. 'tb_comment_history.before_content',
  27. 'tb_comment_history.after_content',
  28. 'tb_comment_history.ip_address',
  29. 'tb_comment_history.user_agent',
  30. 'tb_comment_history.created_at',
  31. 'user_1.sid AS sid_1',
  32. 'user_1.name AS name_1',
  33. 'user_2.sid AS sid_2',
  34. 'user_2.name AS name_2'
  35. );
  36. if($params->keyword) {
  37. switch ($params->field) {
  38. case 'tb_comment_history.before_content' :
  39. case 'tb_comment_history.after_content' :
  40. case 'tb_comment_history.ip_address' :
  41. case 'user_1.name' :
  42. case 'user_1.email' :
  43. case 'user_2.name' :
  44. case 'user_2.email' :
  45. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  46. break;
  47. case 'user_1.id' :
  48. case 'user_1.sid' :
  49. case 'user_2.id' :
  50. case 'user_2.sid' :
  51. $query->where($params->field, '=', $params->keyword);
  52. break;
  53. }
  54. }
  55. if($params->startDate || $params->endDate) {
  56. if($params->startDate) {
  57. $query->where('tb_post_history.created_at', '>=', $params->startDate . ' 00:00:00');
  58. }
  59. if($params->endDate) {
  60. $query->where('tb_post_history.created_at', '<=', $params->endDate . ' 23:59:59');
  61. }
  62. }
  63. if($params->boardID) {
  64. $query->where('tb_comment_history.board_id', '=', $params->boardID);
  65. }
  66. $query->join('tb_board', 'tb_board.id', '=', 'tb_comment_history.board_id');
  67. $query->join('tb_post', 'tb_post.id', '=', 'tb_comment_history.post_id');
  68. $query->join('tb_comment', 'tb_comment.id', '=', 'tb_comment_history.comment_id');
  69. $query->leftJoin('users AS user_1', 'user_1.id', '=', 'tb_comment_history.user_id'); // 수정
  70. $query->leftJoin('users AS user_2', 'user_2.id', '=', 'tb_comment.user_id'); // 작성
  71. $query->orderByDesc('tb_comment_history.id');
  72. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  73. $total = $this->count();
  74. $rows = $list->count();
  75. return (object)[
  76. 'total' => $total,
  77. 'rows' => $rows,
  78. 'list' => $list
  79. ];
  80. }
  81. /**
  82. * 댓글 최초 기록
  83. */
  84. public function register(Comment $comment, string $memo): int
  85. {
  86. return $this->insert([
  87. 'board_id' => $comment->board_id,
  88. 'post_id' => $comment->post_id,
  89. 'comment_id' => $comment->id,
  90. 'user_id' => $comment->user_id,
  91. 'before_content' => $comment->content,
  92. 'after_content' => null,
  93. 'memo' => $memo,
  94. 'ip_address' => IP_ADDRESS,
  95. 'user_agent' => USER_AGENT,
  96. 'created_at' => now()
  97. ]);
  98. }
  99. /**
  100. * 댓글 수정/삭제 등 기록
  101. */
  102. public function updater(Comment $comment, string $memo): bool
  103. {
  104. $sql = '
  105. INSERT INTO tb_comment_history (board_id, post_id, comment_id, user_id, before_content, after_content, memo, ip_address, user_agent, created_at)
  106. SELECT board_id, post_id, id, :user_id, :content, content, :memo, :ip_address, :user_agent, NOW()
  107. FROM tb_comment WHERE id = :id;
  108. ';
  109. return DB::insert($sql, [
  110. 'id' => $comment->id,
  111. 'user_id' => $comment->user_id,
  112. 'content' => $comment->content,
  113. 'memo' => $memo,
  114. 'ip_address' => IP_ADDRESS,
  115. 'user_agent' => USER_AGENT
  116. ]);
  117. }
  118. }