DiscussionController.php 8.7 KB

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