DiscussionController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Discussion;
  4. use App\Models\Wbw;
  5. use App\Models\WbwBlock;
  6. use App\Models\PaliSentence;
  7. use App\Models\Sentence;
  8. use Illuminate\Http\Request;
  9. use App\Http\Resources\DiscussionResource;
  10. use App\Http\Api\MdRender;
  11. class DiscussionController extends Controller
  12. {
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return \Illuminate\Http\Response
  17. */
  18. public function index(Request $request)
  19. {
  20. //
  21. switch ($request->get('view')) {
  22. case 'question-by-topic':
  23. $topic = Discussion::where('id',$request->get('id'))->select('res_id')->first();
  24. if(!$topic){
  25. return $this->error("无效的id");
  26. }
  27. $table = Discussion::where('res_id',$topic->res_id)->where('parent',null);
  28. break;
  29. case 'question':
  30. $table = Discussion::where('res_id',$request->get('id'))->where('parent',null);
  31. break;
  32. case 'answer':
  33. $table = Discussion::where('parent',$request->get('id'));
  34. break;
  35. case 'all':
  36. $table = Discussion::where('parent',null);
  37. break;
  38. }
  39. if(!empty($search)){
  40. $table->where('title', 'like', $search."%");
  41. }
  42. $table->orderBy($request->get('order','updated_at'),$request->get('dir','desc'));
  43. $count = $table->count();
  44. $table->skip($request->get("offset",0))
  45. ->take($request->get('limit',1000));
  46. $result = $table->get();
  47. if($result){
  48. return $this->ok(["rows"=>DiscussionResource::collection($result),"count"=>$count]);
  49. }else{
  50. return $this->error("没有查询到数据");
  51. }
  52. }
  53. public function discussion_tree(Request $request){
  54. $output = [];
  55. $sentences = $request->get("data");
  56. foreach ($sentences as $key => $sentence) {
  57. # 先查句子信息
  58. $sentInfo = Sentence::where('book_id',$sentence['book'])
  59. ->where('paragraph',$sentence['paragraph'])
  60. ->where('word_start',$sentence['word_start'])
  61. ->where('word_end',$sentence['word_end'])
  62. ->where('channel_uid',$sentence['channel_id'])
  63. ->first();
  64. if($sentInfo){
  65. $sentPr = Discussion::where('res_id',$sentInfo['uid'])
  66. ->whereNull('parent')
  67. ->select('title','children_count','editor_uid')
  68. ->orderBy('created_at','desc')->get();
  69. $output[] = [
  70. 'sentence' => [
  71. 'book' => $sentInfo->book_id,
  72. 'paragraph' => $sentInfo->paragraph,
  73. 'word_start' => $sentInfo->word_start,
  74. 'word_end' => $sentInfo->word_end,
  75. 'channel_id' => $sentInfo->channel_uid,
  76. 'content' => $sentInfo->content,
  77. 'pr_count' => count($sentPr),
  78. ],
  79. 'pr' => $sentPr,
  80. ];
  81. }
  82. }
  83. return $this->ok(['rows'=>$output,'count'=>count($output)]);
  84. }
  85. /**
  86. * Store a newly created resource in storage.
  87. *
  88. * @param \Illuminate\Http\Request $request
  89. * @return \Illuminate\Http\Response
  90. */
  91. public function store(Request $request)
  92. {
  93. $user = \App\Http\Api\AuthApi::current($request);
  94. if(!$user){
  95. return $this->error(__('auth.failed'));
  96. }
  97. //
  98. // validate
  99. // read more on validation at http://laravel.com/docs/validation
  100. if($request->has('parent')){
  101. $rules = [];
  102. $parentInfo = Discussion::find($request->get('parent'));
  103. if(!$parentInfo){
  104. return $this->error('no record');
  105. }
  106. }else{
  107. $rules = array(
  108. 'res_id' => 'required',
  109. 'res_type' => 'required',
  110. 'title' => 'required',
  111. );
  112. }
  113. $validated = $request->validate($rules);
  114. $discussion = new Discussion;
  115. if($request->has('parent')){
  116. $discussion->res_id = $parentInfo->res_id;
  117. $discussion->res_type = $parentInfo->res_type;
  118. }else{
  119. $discussion->res_id = $request->get('res_id');
  120. $discussion->res_type = $request->get('res_type');
  121. }
  122. $discussion->title = $request->get('title',null);
  123. $discussion->content = $request->get('content',null);
  124. $discussion->content_type = $request->get('content_type',"markdown");
  125. $discussion->parent = $request->get('parent',null);
  126. $discussion->editor_uid = $user['user_uid'];
  127. $discussion->save();
  128. //更新parent children_count
  129. if($request->has('parent')){
  130. $parentInfo->increment('children_count',1);
  131. $parentInfo->save();
  132. }
  133. return $this->ok(new DiscussionResource($discussion));
  134. }
  135. /**
  136. * Display the specified resource.
  137. *
  138. * @param \App\Models\Discussion $discussion
  139. * @return \Illuminate\Http\Response
  140. */
  141. public function show(Discussion $discussion)
  142. {
  143. //
  144. return $this->ok(new DiscussionResource($discussion));
  145. }
  146. /**
  147. * 获取discussion 锚点的数据。以句子为最小单位,逐词解析也要显示单词所在的句子
  148. *
  149. * @param string $id
  150. * @return \Illuminate\Http\Response
  151. */
  152. public function anchor($id)
  153. {
  154. //
  155. $discussion = Discussion::find($id);
  156. switch ($discussion->res_type) {
  157. case 'wbw':
  158. # 从逐词解析表获取逐词解析数据
  159. $wbw = Wbw::where('uid',$discussion->res_id)->first();
  160. if(!$wbw){
  161. return $this->error('no wbw data');
  162. }
  163. $wbwBlock = WbwBlock::where('uid',$wbw->block_uid)->first();
  164. if(!$wbwBlock){
  165. return $this->error('no wbwBlock data');
  166. }
  167. $sent = PaliSentence::where('book',$wbw->book_id)
  168. ->where('paragraph',$wbw->paragraph)
  169. ->where('word_begin','<=',$wbw->wid)
  170. ->where('word_end','>=',$wbw->wid)
  171. ->first();
  172. if(!$sent){
  173. return $this->error('no sent data');
  174. }
  175. $sentId = "{$sent['book']}-{$sent['paragraph']}-{$sent['word_begin']}-{$sent['word_end']}";
  176. $channel = $wbwBlock->channel_uid;
  177. $content = MdRender::render("{{".$sentId."}}",$channel);
  178. return $this->ok($content);
  179. break;
  180. default:
  181. # code...
  182. break;
  183. }
  184. return $this->ok();
  185. }
  186. /**
  187. * Update the specified resource in storage.
  188. *
  189. * @param \Illuminate\Http\Request $request
  190. * @param \App\Models\Discussion $discussion
  191. * @return \Illuminate\Http\Response
  192. */
  193. public function update(Request $request, Discussion $discussion)
  194. {
  195. //
  196. $user = \App\Http\Api\AuthApi::current($request);
  197. if(!$user){
  198. return $this->error(__('auth.failed'));
  199. }
  200. //
  201. if($discussion->editor !== $user['user_uid']){
  202. return $this->error(__('auth.failed'));
  203. }
  204. $discussion->title = $request->get('title',null);
  205. $discussion->content = $request->get('content',null);
  206. $discussion->editor_uid = $user['user_uid'];
  207. $discussion->save();
  208. return $this->ok($discussion);
  209. }
  210. /**
  211. * Remove the specified resource from storage.
  212. *
  213. * @param \App\Models\Discussion $discussion
  214. * @return \Illuminate\Http\Response
  215. */
  216. public function destroy(Discussion $discussion)
  217. {
  218. //
  219. $user = \App\Http\Api\AuthApi::current($request);
  220. if(!$user){
  221. return $this->error(__('auth.failed'));
  222. }
  223. //TODO 其他有权限的人也可以删除
  224. if($discussion->editor !== $user['user_uid']){
  225. return $this->error(__('auth.failed'));
  226. }
  227. $delete = $discussion->delete();
  228. return $this->ok($delete);
  229. }
  230. }