PostController.php 11 KB

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