ArticleResource.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\MdRender;
  5. use App\Models\CourseMember;
  6. use App\Models\Course;
  7. use App\Models\Collection;
  8. use App\Models\ArticleCollection;
  9. use App\Models\Channel;
  10. use Illuminate\Support\Facades\Log;
  11. use App\Http\Api\UserApi;
  12. use App\Http\Api\StudioApi;
  13. use App\Http\Api\AuthApi;
  14. use App\Http\Controllers\ArticleController;
  15. use App\Http\Api\ChannelApi;
  16. use Illuminate\Support\Str;
  17. class ArticleResource extends JsonResource
  18. {
  19. /**
  20. * Transform the resource into an array.
  21. *
  22. * @param \Illuminate\Http\Request $request
  23. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  24. */
  25. public function toArray($request)
  26. {
  27. $data = [
  28. "uid" => $this->uid,
  29. "title" => $this->title,
  30. "subtitle" => $this->subtitle,
  31. "summary" => $this->summary,
  32. "studio"=> StudioApi::getById($this->owner),
  33. "editor"=> UserApi::getById($this->editor_id),
  34. "status" => $this->status,
  35. "lang" => $this->lang,
  36. "created_at" => $this->created_at,
  37. "updated_at" => $this->updated_at,
  38. ];
  39. $user = AuthApi::current($request);
  40. if($user){
  41. $canEdit = ArticleController::userCanEdit($user['user_uid'],$this);
  42. if($canEdit){
  43. $data['role'] = 'editor';
  44. }
  45. }
  46. //查询该文章在哪些文集中出现
  47. $collectionCount = ArticleCollection::where('article_id',$this->uid)->count();
  48. if($collectionCount>0){
  49. $data['anthology_count'] = $collectionCount;
  50. $collection = ArticleCollection::where('article_id',$this->uid)->first();
  51. $data['anthology_first'] = Collection::find($collection->collect_id);
  52. }
  53. //path
  54. if($request->has('anthology') && Str::isUuid($request->get('anthology'))){
  55. $data['path'] = array();
  56. //anthology title
  57. $anthology = Collection::where('uid',$request->get('anthology'))->first();
  58. if($anthology){
  59. $data['path'][] = ['key'=>$anthology->uid,
  60. 'title'=>$anthology->title,
  61. 'level'=>0];
  62. }
  63. $currLevel = -1;
  64. $aList = ArticleCollection::where('collect_id',$request->get('anthology'))
  65. ->orderBy('id','desc')
  66. ->select(['article_id','title','level'])->get();
  67. $path = array();
  68. foreach ($aList as $article) {
  69. if($article->article_id === $this->uid ||
  70. ($currLevel >= 0 && $article->level < $currLevel)){
  71. $currLevel = $article->level;
  72. $path[] = ['key'=>$article->article_id,
  73. 'title'=>$article->title,
  74. 'level'=>$article->level];
  75. }
  76. }
  77. for ($i=count($path)-1; $i >=0 ; $i--) {
  78. $data['path'][] = $path[$i];
  79. }
  80. //下级目录
  81. $level = -1;
  82. $subToc = array();
  83. for ($i=count($aList)-1; $i >=0 ; $i--) {
  84. $article = $aList[$i];
  85. if($level>=0){
  86. if($article->level>$level){
  87. $subToc[] =[
  88. "key"=>$article->article_id,
  89. "title"=>$article->title,
  90. "level"=>$article->level
  91. ];
  92. }else{
  93. break;
  94. }
  95. }
  96. if($article->article_id === $this->uid){
  97. $level = $article->level;
  98. }
  99. }
  100. $data['toc'] = $subToc;
  101. }
  102. $collectionCount = ArticleCollection::where('article_id',$this->uid)->count();
  103. //render html
  104. if(isset($this->content) && !empty($this->content)){
  105. if($request->has('channel')){
  106. $channel = $request->get('channel');
  107. }else{
  108. //查找用户默认channel
  109. $studioChannel = Channel::where('owner_uid',$this->owner)
  110. ->where('type','translation')
  111. ->get();
  112. if($studioChannel){
  113. $channel = $studioChannel[0]->uid;
  114. }else{
  115. $channel = ChannelApi::getSysChannel('_community_translation_'.strtolower($this->lang).'_',
  116. '_community_translation_en_');
  117. }
  118. }
  119. $data["content"] = $this->content;
  120. $data["content_type"] = $this->content_type;
  121. $query_id = null;
  122. if($request->has('course')){
  123. if($request->has('exercise')){
  124. $query_id = $request->get('exercise');
  125. if($request->has('user')){
  126. /**
  127. * 显示指定用户作业
  128. * 查询用户在课程中的channel
  129. */
  130. $userId = UserApi::getIdByName($request->get('user'));
  131. $userInCourse = CourseMember::where('course_id',$request->get('course'))
  132. ->where('user_id',$userId)
  133. ->first();
  134. if($userInCourse){
  135. $channel = $userInCourse->channel_id;
  136. }
  137. }else if($request->get('view')==="answer"){
  138. /**
  139. * 显示答案
  140. * 算法:查询course 答案 channel
  141. */
  142. $channel = Course::where('id',$request->get('course'))->value('channel_id');
  143. }else{
  144. //显示答案
  145. $channel = Course::where('id',$request->get('course'))->value('channel_id');
  146. }
  147. }else{
  148. $channel = Course::where('id',$request->get('course'))->value('channel_id');
  149. }
  150. }
  151. if($request->has('mode')){
  152. $mode = $request->get('mode');
  153. }else{
  154. $mode = 'read';
  155. }
  156. $data["html"] = MdRender::render($this->content,[$channel],$query_id,$mode);
  157. }
  158. return $data;
  159. }
  160. }