PostLink.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 PostLink extends Model
  7. {
  8. protected $table = 'tb_post_link';
  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. public function board()
  17. {
  18. return $this->belongsTo(Board::class);
  19. }
  20. public function post()
  21. {
  22. return $this->belongsTo(Post::class);
  23. }
  24. public function postLinkClickLog()
  25. {
  26. return $this->hasMany(PostLinkClickLog::class);
  27. }
  28. /**
  29. * 게시글 URL 링크 조회
  30. */
  31. public function data(SearchData $params): object
  32. {
  33. $query = $this->query();
  34. $query->select(
  35. 'tb_board.name AS boardName',
  36. 'tb_board.code',
  37. 'tb_post_link.*',
  38. 'tb_post.subject',
  39. 'users.sid',
  40. 'users.name'
  41. );
  42. if($params->keyword) {
  43. switch ($params->field) {
  44. case 'tb_post_link.url' :
  45. case 'tb_post.subject' :
  46. case 'users.name' :
  47. case 'users.email' :
  48. $query->where($params->field, 'LIKE', "%{$params->keyword}%");
  49. break;
  50. case 'tb_post_link.id' :
  51. case 'tb_post_link.board_id' :
  52. case 'tb_post.id' :
  53. case 'users.id' :
  54. case 'users.sid' :
  55. $query->where($params->field, '=', $params->keyword);
  56. break;
  57. }
  58. }
  59. if($params->startDate || $params->endDate) {
  60. if($params->startDate) {
  61. $query->where('tb_post_link.created_at', '>=', $params->startDate . ' 00:00:00');
  62. }
  63. if($params->endDate) {
  64. $query->where('tb_post_link.created_at', '<=', $params->endDate . ' 23:59:59');
  65. }
  66. }
  67. if($params->boardID) {
  68. $query->where('tb_post_link.board_id', '=', $params->boardID);
  69. }
  70. $query->join('tb_board', 'tb_board.id', '=', 'tb_post_link.board_id');
  71. $query->join('tb_post', 'tb_post.id', '=', 'tb_post_link.post_id');
  72. $query->leftJoin('users', 'users.id', '=', 'tb_post.user_id');
  73. $query->orderByDesc('tb_post_link.id');
  74. $list = $query->paginate($params->perPage, ['*'], 'page', $params->page);
  75. $total = $this->count();
  76. $rows = $list->count();
  77. return (object)[
  78. 'total' => $total,
  79. 'rows' => $rows,
  80. 'list' => $list
  81. ];
  82. }
  83. /**
  84. * 게시글 URL 정보 조회
  85. */
  86. public function info(int $linkID): PostLink
  87. {
  88. return $this->findOrNew($linkID);
  89. }
  90. /**
  91. * 게시글 URL 입력
  92. */
  93. public function register(Post $post, ?array $links): void
  94. {
  95. if ($links) {
  96. foreach ($links as $url) {
  97. $url = trim($url);
  98. if ($url) {
  99. $this->insert([
  100. 'board_id' => $post->board_id,
  101. 'post_id' => $post->id,
  102. 'url' => $url,
  103. 'hit' => 0,
  104. 'ip_address' => IP_ADDRESS,
  105. 'user_agent' => USER_AGENT,
  106. 'created_at' => now()
  107. ]);
  108. }
  109. }
  110. $post->updateLinkRows($post->id);
  111. }
  112. }
  113. /**
  114. * 게시글 URL 수정
  115. */
  116. public function updater(Post $post, ?array $links): void
  117. {
  118. if ($links) {
  119. foreach ($links as $linkID => $url) {
  120. $url = trim($url);
  121. if ($url) {
  122. DB::statement("
  123. REPLACE INTO tb_post_link
  124. SET id = ?, board_id = ?, post_id = ?, url = ?, hit = ?, ip_address = ?, user_agent = ?, created_at = NOW();
  125. ", [
  126. $linkID,
  127. $post->board_id,
  128. $post->id,
  129. $url,
  130. 0,
  131. IP_ADDRESS,
  132. USER_AGENT
  133. ]);
  134. } else {
  135. $this->where([
  136. ['id', $linkID],
  137. ['board_id', $post->board_id],
  138. ['post_id', $post->id]
  139. ])->delete();
  140. }
  141. }
  142. $post->updateLinkRows($post->id);
  143. }
  144. }
  145. /**
  146. * 게시글 URL 삭제
  147. */
  148. public function remove(int $postLinkID): bool
  149. {
  150. return DB::transaction(function() use ($postLinkID) {
  151. $postLink = $this->info($postLinkID);
  152. // URL 존재 확인
  153. if(!$postLink->exists) {
  154. return false;
  155. }
  156. // Link 기록 삭제
  157. $postLink->postLinkClickLog->delete();
  158. // 게시글 Link 수 갱신
  159. $postLink->post->updateLinkRows($postLink->post_id);
  160. // Link 삭제
  161. $postLink->delete();
  162. return true;
  163. });
  164. }
  165. /**
  166. * 게시글 URL 클릭 수 증가
  167. */
  168. public function increaseHit(int $linkID): int
  169. {
  170. return $this->where('id', $linkID)->increment('hit');
  171. }
  172. }