PostService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Http\Request;
  5. use App\Http\Requests\PostRequest;
  6. use App\Http\Traits\CommonTrait;
  7. use App\Http\Traits\BoardTrait;
  8. use App\Models\Board;
  9. use App\Models\BoardMeta;
  10. use App\Models\Post;
  11. use App\Models\PostMeta;
  12. use App\Models\PostLike;
  13. use App\Models\PostTag;
  14. use App\Models\PostLink;
  15. use App\Models\PostFile;
  16. use App\Models\PostHistory;
  17. use App\Models\PostDeleted;
  18. use App\Models\User;
  19. use App\Models\FileLib;
  20. use App\Models\PostBlame;
  21. use App\Models\PostBookmark;
  22. use App\Models\PostFileDownLog;
  23. use App\Models\PostLinkClickLog;
  24. use App\Models\EditorImage;
  25. use App\Models\EmailLib;
  26. use App\Models\Exp;
  27. use App\Models\DTO\ResponseData;
  28. use Exception;
  29. class PostService
  30. {
  31. use CommonTrait;
  32. use BoardTrait;
  33. public Board $boardModel;
  34. public BoardMeta $boardMetaModel;
  35. public User $userModel;
  36. public Post $postModel;
  37. public PostMeta $postMetaModel;
  38. public PostHistory $postHistoryModel;
  39. public PostBookmark $postBookmarkModel;
  40. public PostLike $postLikeModel;
  41. public PostTag $postTagModel;
  42. public PostFile $postFileModel;
  43. public PostFileDownLog $postFileDownLogModel;
  44. public PostBlame $postBlameModel;
  45. public PostLink $postLinkModel;
  46. public PostLinkClickLog $postLinkClickLogModel;
  47. public PostDeleted $postDeletedModel;
  48. public FileLib $fileLib;
  49. public EditorImage $editorImageModel;
  50. public function __construct()
  51. {
  52. $this->boardModel = new Board();
  53. $this->boardMetaModel = new BoardMeta();
  54. $this->postModel = new Post();
  55. $this->postMetaModel = new PostMeta();
  56. $this->postHistoryModel = new PostHistory();
  57. $this->postBookmarkModel = new PostBookmark();
  58. $this->postLikeModel = new PostLike();
  59. $this->postTagModel = new PostTag();
  60. $this->postFileModel = new PostFile();
  61. $this->postFileDownLogModel = new PostFileDownLog();
  62. $this->postBlameModel = new PostBlame();
  63. $this->postLinkModel = new PostLink();
  64. $this->postLinkClickLogModel = new PostLinkClickLog();
  65. $this->postDeletedModel = new PostDeleted();
  66. $this->userModel = new User();
  67. $this->fileLib = new FileLib();
  68. $this->editorImageModel = new EditorImage();
  69. }
  70. public function meta(int $postID): ?PostMeta
  71. {
  72. return $this->postMetaModel->getAllMeta($postID);
  73. }
  74. /**
  75. * 게시글 정보 조회
  76. */
  77. public function find(int $postID): Post
  78. {
  79. $post = $this->postModel->get($postID);
  80. if(!$post->exists) {
  81. return $post;
  82. }
  83. $code = $post->board->code;
  84. $boardMeta = $this->boardMetaModel->getAllMeta($post->board_id);
  85. // 게시글 정보
  86. $post->viewURL = route('board.post.view', [$code, $postID]);
  87. $post->beforeViewURL = ($post->beforePostID ? route('board.post.view', [$code, $post->beforePostID]) : null);
  88. $post->nextViewURL = ($post->nextPostID ? route('board.post.view', [$code, $post->nextPostID]) : null);
  89. $post->createdAt = $post->created_at->format('Y.m.d H:i:s');
  90. $post->ipAddress = null;
  91. $post->isBookmark = false;
  92. $post->isLike = false;
  93. $post->isDislike = false;
  94. // 본문 URL 링크 자동 생성
  95. if ($boardMeta->item('use_auto_url', 0)) {
  96. $post->content = $this->urlAutoLink($post->content, $boardMeta->item('content_target_blank', 0)); // Link 새창 여부
  97. }
  98. // 익명 게시판일 경우
  99. if(
  100. $boardMeta->item('use_anonymous', 0)
  101. && (!$post->user->is_admin || !$boardMeta->item('anonymous_except_admin', 0))
  102. && $post->user->id
  103. ) {
  104. $post->username = $this->createAnonymousName(
  105. $boardMeta->item('anonymous_name'), $post->user_id, $post->id
  106. );
  107. }
  108. if($post->user->exists) {
  109. $post->user->thumb = $this->profileThumbSrc($post->user->thumb);
  110. $post->user->createdAt = $post->user->created_at->format('Y.m.d');
  111. }
  112. // IP 형식 처리
  113. $showPostIP = $boardMeta->item('show_post_ip'); // 노출 방법
  114. if ($showPostIP) {
  115. $ipDisplayStyle = ($showPostIP == 2 || IS_ADMIN ? '1111' : config('ip_display_style')); // IP 형식
  116. $post->ipAddress = $this->ipAddrMasking($post->ip_address, $ipDisplayStyle);
  117. }
  118. // 즐겨찾기
  119. if ($boardMeta->item('use_bookmark', 0)) {
  120. $post->isBookmark = $this->postBookmarkModel->isAlready($post, UID);
  121. }
  122. // 좋아요
  123. if ($boardMeta->item('use_post_like', 0)) {
  124. $post->isLike = $this->postLikeModel->isAlready($post, UID, LIKE);
  125. }
  126. // 싫어요
  127. if ($boardMeta->item('use_post_dislike', 0)) {
  128. $post->isDislike = $this->postLikeModel->isAlready($post, UID, DISLIKE);
  129. }
  130. // 태그
  131. $post->tags = $post->postTag;
  132. // 첨부파일
  133. $post->files = $post->postFile;
  134. // 링크 첨부
  135. $post->links = $post->postLink;
  136. return $post;
  137. }
  138. /**
  139. * 게시글 등록 처리
  140. */
  141. public function register(PostRequest $request, ResponseData $response): ResponseData
  142. {
  143. DB::beginTransaction();
  144. try {
  145. $user = $request->user();
  146. $boardID = intval($request->post('bid'));
  147. $boardMeta = $request->boardMeta;
  148. // 게시글 첨부 사진 저장
  149. $content = $this->getContent($request);
  150. $thumbnail = $this->getThumbnail($content,
  151. ($boardMeta->item('board_layout_type') == BOARD_LAYOUT_TYPE_1 ? NO_IMAGE_NORMAL_SRC : null)
  152. );
  153. $imageRows = $this->getImageRows($content);
  154. $videoRows = $this->getVideoRows($content);
  155. // 게시글 정보 저장
  156. $post = $this->postModel->register([
  157. 'board_id' => $boardID,
  158. 'board_category_id' => $request->post('category'),
  159. 'user_id' => $user?->id,
  160. 'thumbnail' => $thumbnail,
  161. 'subject' => trim($request->post('subject')),
  162. 'content' => $content,
  163. 'sid' => $user?->sid,
  164. 'username' => $request->post('username', $user?->name),
  165. 'email' => $user?->email,
  166. 'password' => $request->post('password'),
  167. 'comment_rows' => 0,
  168. 'comment_updated_at' => null,
  169. 'is_reply' => 0,
  170. 'is_personal' => $boardMeta->item('use_personal', 0),
  171. 'is_secret' => ($boardMeta->item('use_post_secret', 0) == '2' ? 1 : $request->post('is_secret', 0)),
  172. 'is_notice' => $request->post('is_notice', 0),
  173. 'is_speaker' => $request->post('is_speaker', 0),
  174. 'is_html' => $boardMeta->item('use_post_dhtml', 0),
  175. 'use_comment' => $boardMeta->item('use_comment', 0),
  176. 'receive_email' => 0,
  177. 'hit' => 0,
  178. 'like' => 0,
  179. 'dislike' => 0,
  180. 'blame' => 0,
  181. 'device_type' => DEVICE_TYPE,
  182. 'file_rows' => 0,
  183. 'image_rows' => $imageRows,
  184. 'video_rows' => $videoRows,
  185. 'link_rows' => 0,
  186. 'tag_rows' => 0,
  187. 'is_delete' => 0,
  188. 'ip_address' => IP_ADDRESS,
  189. 'user_agent' => USER_AGENT,
  190. 'deleted_at' => null,
  191. 'updated_at' => null,
  192. 'created_at' => now()
  193. ]);
  194. // 게시글 Tag 저장
  195. $this->postTagModel->register($post, $request->post('tags'));
  196. // 게시글 Link 저장
  197. $this->postLinkModel->register($post, $request->post('links'));
  198. // 게시글 File 저장
  199. $this->postFileModel->register($post, $request->file('files'));
  200. // 게시글 기록 저장
  201. if ($boardMeta->use_post_history) {
  202. $this->postHistoryModel->register($post,'게시글 등록');
  203. }
  204. // 게시판 게시글 수 갱신
  205. $this->boardModel->updatePostRows($boardID);
  206. $response->post = $post;
  207. DB::commit();
  208. } catch (Exception $e) {
  209. $response = $response::fromException($e);
  210. DB::rollBack();
  211. }
  212. return $response;
  213. }
  214. /**
  215. * 게시글 수정 처리
  216. */
  217. public function updater(PostRequest $request, ResponseData $response): ResponseData
  218. {
  219. DB::beginTransaction();
  220. try {
  221. $user = $request->user();
  222. $boardMeta = $request->boardMeta;
  223. $boardID = intval($request->post('bid'));
  224. $postID = intval($request->post('pid'));
  225. // 게시글 첨부 사진 저장
  226. $content = $this->getContent($request);
  227. $thumbnail = $this->getThumbnail($content,
  228. ($boardMeta->item('board_layout_type') == BOARD_LAYOUT_TYPE_1 ? NO_IMAGE_NORMAL_SRC : null)
  229. );
  230. $imageRows = $this->getImageRows($content);
  231. $videoRows = $this->getVideoRows($content);
  232. // 게시글 기록 저장
  233. if ($boardMeta->item('use_post_history', 0)) {
  234. $this->postHistoryModel->updater($request->post, '게시글 수정');
  235. }
  236. // 게시글 정보 저장
  237. $post = $this->postModel->updater($postID, [
  238. 'board_id' => $boardID,
  239. 'board_category_id' => $request->post('category'),
  240. 'user_id' => $user?->id,
  241. 'thumbnail' => $thumbnail,
  242. 'subject' => trim($request->post('subject')),
  243. 'content' => $content,
  244. 'sid' => $user?->sid,
  245. 'username' => $request->post('username', $user?->name),
  246. 'email' => $user?->email,
  247. 'password' => $request->post('password'),
  248. 'is_personal' => $boardMeta->item('use_personal', 0),
  249. 'is_secret' => ($boardMeta->item('use_post_secret', 0) == '2' ? 1 : $request->post('is_secret', 0)),
  250. 'is_notice' => $request->post('is_notice', 0),
  251. 'is_speaker' => $request->post('is_speaker', 0),
  252. 'is_html' => $boardMeta->item('use_post_dhtml', 0),
  253. 'use_comment' => $boardMeta->item('use_comment', 0),
  254. 'receive_email' => 0,
  255. 'device_type' => DEVICE_TYPE,
  256. 'file_rows' => 0,
  257. 'image_rows' => $imageRows,
  258. 'video_rows' => $videoRows,
  259. 'link_rows' => 0,
  260. 'tag_rows' => 0,
  261. 'ip_address' => IP_ADDRESS,
  262. 'user_agent' => USER_AGENT,
  263. 'updated_at' => now()
  264. ]);
  265. // 게시글 Tag 저장
  266. $this->postTagModel->updater($post, $request->post('tags'));
  267. // 게시글 Link 저장
  268. $this->postLinkModel->updater($post, $request->post('links'));
  269. // 게시글 File 저장
  270. $this->postFileModel->updater($post, $request->post('file_is_delete', []), $request->file('files'));
  271. DB::commit();
  272. } catch (Exception $e) {
  273. $response = $response::fromException($e);
  274. DB::rollBack();
  275. }
  276. return $response;
  277. }
  278. /**
  279. * 게시글 삭제
  280. */
  281. public function delete(Request $request, ResponseData $response): ResponseData
  282. {
  283. DB::beginTransaction();
  284. try {
  285. $post = $request->post;
  286. // 게시글 존재 확인
  287. if (!$post->exists) {
  288. throw new Exception('게시글이 존재 하지 않습니다.');
  289. }
  290. $user = $request->user();
  291. // 게시글 삭제 여부 승인
  292. $this->postModel->remove($post, $user);
  293. DB::commit();
  294. } catch (Exception $e) {
  295. $response = $response::fromException($e);
  296. DB::rollBack();
  297. }
  298. return $response;
  299. }
  300. /**
  301. * 게시글 즐겨찾기
  302. */
  303. public function bookmark(Request $request, ResponseData $response): ResponseData
  304. {
  305. DB::beginTransaction();
  306. try {
  307. $post = $this->postModel->findOrNew(
  308. $request->input('pid')
  309. );
  310. // 게시글 존재 확인
  311. if (!$post->exists) {
  312. throw new Exception('게시글이 존재 하지 않습니다.');
  313. }
  314. $boardMeta = $this->boardMetaModel->getAllMeta($post->board_id);
  315. if (!$boardMeta->item('use_bookmark', 0)) {
  316. throw new Exception('즐겨찾기 기능은 사용하지 않습니다.');
  317. }
  318. $userID = $request->user()->id;
  319. if ($this->postBookmarkModel->isAlready($post, $userID)) {
  320. $this->postBookmarkModel->remove($post, $userID);
  321. } else {
  322. $this->postBookmarkModel->register([
  323. 'board_id' => $post->board_id,
  324. 'post_id' => $post->id,
  325. 'user_id' => $userID,
  326. 'ip_address' => IP_ADDRESS,
  327. 'user_agent' => USER_AGENT,
  328. 'created_at' => now()
  329. ]);
  330. }
  331. // 즐겨찾기 수 저장
  332. $this->postMetaModel->save($post->id, [
  333. 'bookmark_rows' => $this->postBookmarkModel->rows($post)
  334. ]);
  335. $response->post = $post;
  336. DB::commit();
  337. } catch (Exception $e) {
  338. $response = $response::fromException($e);
  339. DB::rollBack();
  340. }
  341. return $response;
  342. }
  343. /**
  344. * 게시글 신고
  345. */
  346. public function blame(Request $request, ResponseData $response): ResponseData
  347. {
  348. DB::beginTransaction();
  349. try {
  350. $post = $this->postModel->findOrNew(
  351. $request->input('pid')
  352. );
  353. // 게시글 존재 확인
  354. if(!$post->exists) {
  355. throw new Exception('게시글이 존재 하지 않습니다.');
  356. }
  357. $boardMeta = $this->boardMetaModel->getAllMeta($post->board_id);
  358. if (!$boardMeta->item('use_blame', 0)) {
  359. throw new Exception('신고 기능은 사용하지 않습니다.');
  360. }
  361. $userID = $request->user()->id;
  362. // 이미 신고했는지 확인
  363. if ($this->postBlameModel->isAlready($post, $userID)) {
  364. throw new Exception('이미 신고하셨습니다.');
  365. }
  366. $this->postBlameModel->register([
  367. 'board_id' => $post->board_id,
  368. 'post_id' => $post->id,
  369. 'user_id' => $userID,
  370. 'type' => $request->post('type'),
  371. 'reason' => $request->post('reason'),
  372. 'status' => 0,
  373. 'memo' => null,
  374. 'ip_address' => IP_ADDRESS,
  375. 'user_agent' => USER_AGENT,
  376. 'created_at' => now()
  377. ]);
  378. // 게시글 신고 수 증가
  379. $this->postModel->increaseBlame($post->id);
  380. $response->post = $post;
  381. DB::commit();
  382. } catch (Exception $e) {
  383. $response = $response::fromException($e);
  384. DB::rollBack();
  385. }
  386. return $response;
  387. }
  388. /**
  389. * 게시글 좋아요
  390. */
  391. public function like(Request $request, ResponseData $response): ResponseData
  392. {
  393. DB::beginTransaction();
  394. try {
  395. $post = $this->postModel->findOrNew(
  396. $request->input('pid')
  397. );
  398. // 게시글 존재 확인
  399. if(!$post->exists) {
  400. throw new Exception('게시글이 존재 하지 않습니다.');
  401. }
  402. $boardMeta = $this->boardMetaModel->getAllMeta($post->board_id);
  403. if (!$boardMeta->item('use_post_like', 0)) {
  404. throw new Exception('추천 기능은 사용하지 않습니다.');
  405. }
  406. $userID = $request->user()->id;
  407. $saveData = [
  408. 'board_id' => $post->board_id,
  409. 'post_id' => $post->id,
  410. 'user_id' => $userID,
  411. 'type' => LIKE,
  412. 'ip_address' => IP_ADDRESS,
  413. 'user_agent' => USER_AGENT,
  414. 'created_at' => now()
  415. ];
  416. $likeInfo = $this->postLikeModel->info($post, $userID);
  417. if ($likeInfo->exists) { // 이미 추천함
  418. $this->postLikeModel->remove($post, $userID);
  419. if ($likeInfo->type == LIKE) {
  420. $this->postModel->decreaseLike($post->id);
  421. } else if ($likeInfo->type == DISLIKE) {
  422. $this->postLikeModel->register($saveData);
  423. $this->postModel->increaseLike($post->id);
  424. $this->postModel->decreaseDisLike($post->id);
  425. }
  426. } else { // 추천
  427. $this->postLikeModel->register($saveData);
  428. $this->postModel->increaseLike($post->id);
  429. }
  430. $response->post = $post;
  431. DB::commit();
  432. } catch (Exception $e) {
  433. $response = $response::fromException($e);
  434. DB::rollBack();
  435. }
  436. return $response;
  437. }
  438. /**
  439. * 게시글 싫어요
  440. */
  441. public function dislike(Request $request, ResponseData $response): ResponseData
  442. {
  443. DB::beginTransaction();
  444. try {
  445. $post = $this->postModel->findOrNew(
  446. $request->input('pid')
  447. );
  448. // 게시글 존재 확인
  449. if(!$post->exists) {
  450. throw new Exception('게시글이 존재 하지 않습니다.');
  451. }
  452. $boardMeta = $this->boardMetaModel->getAllMeta($post->board_id);
  453. if (!$boardMeta->item('use_post_dislike', 0)) {
  454. throw new Exception('비추천 기능은 사용하지 않습니다.');
  455. }
  456. $userID = $request->user()->id;
  457. $saveData = [
  458. 'board_id' => $post->board_id,
  459. 'post_id' => $post->id,
  460. 'user_id' => $userID,
  461. 'type' => DISLIKE,
  462. 'ip_address' => IP_ADDRESS,
  463. 'user_agent' => USER_AGENT,
  464. 'created_at' => now()
  465. ];
  466. $likeInfo = $this->postLikeModel->info($post, $userID);
  467. if ($likeInfo->exists) { // 이미 추천함
  468. $this->postLikeModel->remove($post, $userID);
  469. if ($likeInfo->type == DISLIKE) {
  470. $this->postModel->decreaseDisLike($post->id);
  471. } else if ($likeInfo->type == LIKE) {
  472. $this->postLikeModel->register($saveData);
  473. $this->postModel->increaseDisLike($post->id);
  474. $this->postModel->decreaseLike($post->id);
  475. }
  476. } else {
  477. $this->postLikeModel->register($saveData);
  478. $this->postModel->increaseDisLike($post->id);
  479. }
  480. $response->post = $post;
  481. DB::commit();
  482. } catch (Exception $e) {
  483. $response = $response::fromException($e);
  484. DB::rollBack();
  485. }
  486. return $response;
  487. }
  488. /**
  489. * 게시글 첨부파일 다운로드
  490. */
  491. public function download(Request $request): object
  492. {
  493. $ret = (object)[
  494. 'path' => null,
  495. 'name' => null
  496. ];
  497. $file = $this->postFileModel->info(
  498. $request->post('fid')
  499. );
  500. if (!$file->exists) {
  501. return $ret;
  502. }
  503. // 다운로드 횟수 증가
  504. $file->increaseDownload($file->id);
  505. // 다운로드 기록
  506. $boardMeta = $this->boardMetaModel->getAllMeta($file->board_id);
  507. if ($boardMeta->item('use_download_log', 0)) {
  508. $this->postFileDownLogModel->register([
  509. 'post_file_id' => $file->id,
  510. 'board_id' => $file->board_id,
  511. 'post_id' => $file->post_id,
  512. 'user_id' => $file->user_id,
  513. 'ip_address' => IP_ADDRESS,
  514. 'user_agent' => USER_AGENT,
  515. 'created_at' => now()
  516. ]);
  517. }
  518. $ret->path = $file->file_path;
  519. $ret->name = $file->origin_name;
  520. return $ret;
  521. }
  522. /**
  523. * 게시글 Link 클릭
  524. */
  525. public function linked(Request $request): string|null
  526. {
  527. $link = $this->postLinkModel->info(
  528. $request->post('lid')
  529. );
  530. if (!$link->exists) {
  531. return null;
  532. }
  533. // Link 클릭 횟수 증가
  534. $link->increaseHit($link->id);
  535. // Link 클릭 기록
  536. $boardMeta = $this->boardMetaModel->getAllMeta($link->board_id);
  537. if ($boardMeta->item('use_link_click_log', 0)) {
  538. $this->postLinkClickLogModel->register([
  539. 'post_link_id' => $link->id,
  540. 'board_id' => $link->board_id,
  541. 'post_id' => $link->post_id,
  542. 'user_id' => $link->user_id,
  543. 'ip_address' => IP_ADDRESS,
  544. 'user_agent' => USER_AGENT,
  545. 'created_at' => now()
  546. ]);
  547. }
  548. return ($link->url ?? null);
  549. }
  550. /**
  551. * 게시글 보기 조회 수 증가
  552. */
  553. public function addHit(Post $post): void
  554. {
  555. if($this->postModel->addHit($post)) {
  556. $this->postModel->increaseHit($post->id);
  557. }
  558. }
  559. /**
  560. * 게시글 작성 개수
  561. */
  562. public function userPostRows(Board $board, ?User $user): int
  563. {
  564. return $this->postModel->where([
  565. ['board_id', $board->id],
  566. ['user_id', $user?->id],
  567. ['ip_address', IP_ADDRESS]
  568. ])->whereRaw('DATE(created_at) = CURDATE()')->count();
  569. }
  570. /**
  571. * 게시글 추천 여부
  572. */
  573. public function userPostIsLike(Post $post, User $user): int
  574. {
  575. return $this->postLikeModel->where([
  576. ['board_id', $post->board_id],
  577. ['post_id', $post->id],
  578. ['user_id', $user->id]
  579. ])->orWhere('ip_address', IP_ADDRESS)->exists();
  580. }
  581. /**
  582. * 첨부 사진 저장
  583. */
  584. private function getContent(PostRequest $request): string
  585. {
  586. // 게시글 이미지 저장
  587. $boardMeta = $request->boardMeta;
  588. $postID = $this->postModel->lastedKey();
  589. $content = trim($request->post('content'));
  590. if ($boardMeta->item('save_external_image', 0)) {
  591. // link
  592. $content = $this->fileLib->saveImageFromUrl($content, UPLOAD_PATH_POST, $postID);
  593. }
  594. // blob
  595. $content = $this->fileLib->saveImageFromBlob($content, UPLOAD_PATH_POST, $postID);
  596. // 이미지 DB 기록
  597. $images = $this->fileLib->getImageFromContent($content);
  598. if(count($images) > 0) {
  599. foreach($images as $img) {
  600. $this->editorImageModel->register([
  601. 'target_type' => EDITOR_IMG_TYPE_1,
  602. 'target_id' => $postID,
  603. 'user_id' => UID,
  604. 'origin_name' => $img->origin,
  605. 'file_name' => $img->name,
  606. 'file_path' => $img->path,
  607. 'file_url' => $img->url,
  608. 'file_size' => $img->size,
  609. 'file_type' => $img->type,
  610. 'width' => $img->width,
  611. 'height' => $img->height,
  612. 'ip_address' => IP_ADDRESS,
  613. 'user_agent' => USER_AGENT
  614. ]);
  615. }
  616. }
  617. return $content;
  618. }
  619. /**
  620. * 게시글 경험치 지급
  621. */
  622. public function setUserExp(Post $post, User $user, int $type): bool
  623. {
  624. if(!$post->exists) {
  625. return false;
  626. }
  627. $boardMeta = $this->boardMetaModel->getAllMeta($post->board_id);
  628. if (!$boardMeta->item('use_exp', 0)) {
  629. return false;
  630. }
  631. $column = MAP_EXP_TYPE[$type];
  632. $content = MAP_EXP_CONTENT[$type];
  633. $value = intval($boardMeta->{$column});
  634. $relatedID = sprintf("%d-%d", $post->board_id, $post->id);
  635. // 경험치 지급 구분
  636. return (new Exp)->register([
  637. 'user_id' => $user->id,
  638. 'content' => $content,
  639. 'value' => $value,
  640. 'type' => $type,
  641. 'related_id' => $relatedID,
  642. 'method' => request()->getMethod(),
  643. 'created_at' => now()
  644. ]);
  645. }
  646. /**
  647. * 게시글 관련 메일 발송
  648. */
  649. public function sendEmailNotify(Post $post, string $sendMailFormType): bool
  650. {
  651. if (!in_array($sendMailFormType, MAP_SEND_MAIL_TYPE)) {
  652. return false;
  653. }
  654. $boardMeta = $this->boardMetaModel->getAllMeta($post->board_id);
  655. $sendMailType = str_replace('_form', '', $sendMailFormType);
  656. // 최고 관리자에게 발송
  657. $admin = ($boardMeta->item($sendMailType . '_super_admin', 0) ? $this->userModel->getMaster() : null);
  658. // 게시글 작성자에게 발송
  659. $postWriter = ($boardMeta->item($sendMailType . '_post_writer', 0) ? $post->user : null);
  660. if (!$admin && !$postWriter) {
  661. return false;
  662. }
  663. return (new EmailLib)->send($sendMailFormType, $admin, $postWriter);
  664. }
  665. }