PostController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace App\Http\Controllers\Dbrbbs;
  3. use App\Helpers\ResponseConverter;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\CallApiService;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Pagination\LengthAwarePaginator;
  8. use Illuminate\Support\Str;
  9. use Jenssegers\Agent\Agent as Agent;
  10. class PostController extends Controller
  11. {
  12. private $callApiService;
  13. private $Agent;
  14. public function __construct(CallApiService $callApiService)
  15. {
  16. $this->callApiService = $callApiService;
  17. $this->Agent = new Agent();
  18. }
  19. public function postTypePage($postCode)
  20. {
  21. return $this->callApiService->callApi([
  22. 'url' => 'list-type1-page',
  23. 'data' => [
  24. 'QueryVars' => [
  25. 'QueryName' => 'post/post-type-input',
  26. 'SimpleFilter' => "post_code='$postCode'",
  27. 'IsntPagination' => true,
  28. ],
  29. 'PageVars' => [
  30. 'Limit' => 1
  31. ]
  32. ]
  33. ]);
  34. }
  35. public function list($postCode)
  36. {
  37. $postType = $this->postTypePage($postCode)['Page'][0];
  38. $design = $postType['T1'];
  39. $design = json_decode($design, true)['Design'];
  40. $p = request('p');
  41. $page = (int)request('page', 1);
  42. $limit = 12;
  43. if ($design) {
  44. if ($design['PcTitleLeng'] === 0 || $design['MoTitleLeng'] === 0 ||
  45. $design['PcPageLeng'] === 0 || $design['MoPageLeng'] === 0) {
  46. notify()->error('관리자 게시판 구분페이지에서 게시판 디자인 설정을 해주세요', 'Error', 'bottomRight');
  47. return redirect()->to('/');
  48. }
  49. if ($this->Agent->isDesktop()) {
  50. $limit = (int)request('limit', $design['PcPageLeng']);
  51. } else {
  52. $limit = (int)request('limit', $design['MoPageLeng']);
  53. }
  54. }
  55. $simpleFilter = "post_code='$postCode' and mx.status = '1'";
  56. if ($p) {
  57. $simpleFilter = $simpleFilter . "and (post_title LIKE '%$p%' or post_contents LIKE '%$p%')";
  58. }
  59. $listType1Book = $this->callApiService->callApi([
  60. 'url' => 'list-type1-book',
  61. 'data' => [
  62. 'Book' => [
  63. [
  64. 'QueryVars' => [
  65. 'QueryName' => 'pro:my-page/post-list',
  66. 'SimpleFilter' => $simpleFilter,
  67. 'SubSimpleFilter' => "image_type = 'middle'",
  68. 'IsntPagination' => false
  69. ],
  70. 'ListType1Vars' => [
  71. 'OrderBy' => request('sort', 'mx.created_on desc')
  72. ],
  73. 'PageVars' => [
  74. 'Limit' => $limit,
  75. 'Offset' => ($page - 1) * $limit
  76. ]
  77. ],
  78. ]
  79. ]
  80. ]);
  81. if ($this->callApiService->verifyApiError($listType1Book)) {
  82. notify()->error($listType1Book['body'], 'Error', 'bottomRight');
  83. return redirect()->back();
  84. }
  85. $postPage = $listType1Book['Book'][0] ?? [];
  86. if ($design) {
  87. $postPage['Page'] = collect($postPage['Page'])->map(function ($post) use ($design) {
  88. if ($this->Agent->isDesktop()) {
  89. $post['Title'] = Str::limit($post['C6'], $design['PcTitleLeng'], '...');
  90. } else {
  91. $post['Title'] = Str::limit($post['C6'], $design['MoTitleLeng'], '...');
  92. }
  93. $post['PcGalleryClass'] = 'col-md-' . (12 / $design['PcGalleryLeng']);
  94. $post['MoGalleryClass'] = 'col-' . (12 / $design['MoGalleryLeng']);
  95. $post['PcVideoClass'] = 'col-md-' . (12 / $design['PcVideoLeng']);
  96. $post['MoVideoClass'] = 'col-' . (12 / $design['MoVideoLeng']);
  97. return $post;
  98. })->toArray();
  99. }
  100. $postPage['Page'] = new LengthAwarePaginator($postPage['Page'], $postPage['PageVars']['QueryCnt'],
  101. $limit, $page, ['path' => request()->url()]);
  102. return view('dbrbbs.list', compact('postPage', 'postType', 'postCode', 'design'));
  103. }
  104. public function details($postCode, $slug)
  105. {
  106. $limit = (int)request('limit', 3);
  107. $page = (int)request('page', 1);
  108. $post = $this->postPick(['PostSlug' => $slug]);
  109. $postId = $post['Id'];
  110. $postTypeId = $post['PostTypeId'];
  111. // $postType = $this->postTypePick(['Id' => (int)$postTypeId]);
  112. // dump($postCode);
  113. // dd($slug);
  114. $listType1Book = $this->callApiService->callApi([
  115. 'url' => 'list-type1-book',
  116. 'data' => [
  117. 'Book' => [
  118. [
  119. 'QueryVars' => [
  120. 'QueryName' => 'pro:my-page/post-details',
  121. 'SimpleFilter' => "post_code = '$postCode' and mx.id = $postId",
  122. 'SubSimpleFilter' => "image_type = 'big'",
  123. 'IsntPagination' => true,
  124. ],
  125. 'PageVars' => [
  126. 'Limit' => 1
  127. ]
  128. ],
  129. [
  130. 'QueryVars' => [
  131. 'QueryName' => 'pro:my-page/post-details-prenext',
  132. 'SimpleFilter' => "mx.id = (select max(id) from pro_post where id < $postId and post_type_id = $postTypeId)",
  133. 'SubSimpleFilter' => "",
  134. 'IsntPagination' => true,
  135. ],
  136. 'PageVars' => [
  137. 'Limit' => 1
  138. ]
  139. ],
  140. [
  141. 'QueryVars' => [
  142. 'QueryName' => 'pro:my-page/post-details-prenext',
  143. 'SimpleFilter' => "mx.id = (select min(id) from pro_post where id > $postId and post_type_id = $postTypeId)",
  144. 'SubSimpleFilter' => "",
  145. 'IsntPagination' => true,
  146. ],
  147. 'PageVars' => [
  148. 'Limit' => 1
  149. ]
  150. ],
  151. [
  152. 'QueryVars' => [
  153. 'QueryName' => 'post/post-type-input',
  154. 'SimpleFilter' => "mx.id = $postTypeId",
  155. 'IsntPagination' => true,
  156. ],
  157. 'PageVars' => [
  158. 'Limit' => 1
  159. ]
  160. ],
  161. [
  162. 'QueryVars' => [
  163. 'QueryName' => 'post/post-type-input',
  164. 'SimpleFilter' => "post_code='$postCode'",
  165. 'IsntPagination' => true,
  166. ],
  167. 'PageVars' => [
  168. 'Limit' => 1
  169. ]
  170. ],
  171. [
  172. 'QueryVars' => [
  173. 'QueryName' => 'post/blog-bd-list-std',
  174. 'SimpleFilter' => "mx.post_id = {$postId} and mx.parent_id = 0",
  175. ],
  176. 'ListType1Vars' => [
  177. 'OrderBy' => ''
  178. ],
  179. 'PageVars' => [
  180. 'Limit' => $limit,
  181. 'Offset' => ($page - 1) * $limit
  182. ]
  183. ],
  184. [
  185. 'QueryVars' => [
  186. 'QueryName' => 'post/blog-bd-list-std',
  187. 'SimpleFilter' => "mx.post_id = {$postId} and mx.parent_id != 0",
  188. ],
  189. 'ListType1Vars' => [
  190. 'OrderBy' => ''
  191. ],
  192. 'PageVars' => [
  193. 'Limit' => 100,
  194. 'Offset' => 0
  195. ]
  196. ]
  197. ]
  198. ]
  199. ]);
  200. // dd($listType1Book);
  201. if ($this->callApiService->verifyApiError($listType1Book)) {
  202. notify()->error($listType1Book['body'], 'Error', 'bottomRight');
  203. return redirect()->back();
  204. }
  205. $post = $listType1Book['Book'][0]['Page'][0];
  206. $prePost = $listType1Book['Book'][1]['Page'];
  207. $nextPost = $listType1Book['Book'][2]['Page'];
  208. $postType = $listType1Book['Book'][4]['Page'][0];
  209. $comments = $listType1Book['Book'][5]['Page'];
  210. $replays = $listType1Book['Book'][6]['Page'];
  211. $comments = ResponseConverter::joinFor($comments, $replays, 'C5', 'Id', 'ReplyPage');
  212. $comments = new LengthAwarePaginator($comments, $listType1Book['Book'][5]['PageVars']['QueryCnt'],
  213. $limit, $page, ['path' => request()->url()]);
  214. return view('dbrbbs.details', compact('post', 'prePost', 'nextPost', 'postCode', 'postType', 'comments'));
  215. }
  216. public function comment()
  217. {
  218. $response = $this->callApiService->callApi([
  219. 'url' => 'post-bd-act',
  220. 'data' => [
  221. 'Page' => [
  222. [
  223. 'Id' => 0,
  224. 'PostId' => (int)request('post_id'),
  225. 'BdContents' => request('bd_contents'),
  226. 'ParentId' => (int)request('parent_id'),
  227. 'SeqNo' => (int)request('seq_no'),
  228. 'ChildLastSeq' => (int)request('child_last_seq'),
  229. ]
  230. ],
  231. ]
  232. ]);
  233. if ($this->callApiService->verifyApiError($response)) {
  234. notify()->error($response['body'], 'Error', 'bottomRight');
  235. return redirect()->back();
  236. }
  237. notify()->success(_e('Action completed'), 'Success', 'bottomRight');
  238. return redirect()->back();
  239. }
  240. public function postPick($page)
  241. {
  242. $postPick = $this->callApiService->callApi([
  243. 'url' => 'post-pick',
  244. 'data' => [
  245. 'Page' => [
  246. $page
  247. ],
  248. ]
  249. ]);
  250. return $postPick['Page'][0];
  251. }
  252. }