ArticleResource.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. //render html
  103. $channels = array();
  104. if(isset($this->content) && !empty($this->content)){
  105. if($request->has('channel')){
  106. $channels = explode('_',$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. $channelId = $studioChannel[0]->uid;
  114. $channels = [$channelId];
  115. }else{
  116. $channelId = ChannelApi::getSysChannel('_community_translation_'.strtolower($this->lang).'_',
  117. '_community_translation_en_');
  118. if($channelId){
  119. $channels = [$channelId];
  120. }
  121. }
  122. }
  123. $data["content"] = $this->content;
  124. $data["content_type"] = $this->content_type;
  125. $query_id = null;
  126. if($request->has('course')){
  127. if($request->has('exercise')){
  128. $query_id = $request->get('exercise');
  129. if($request->has('user')){
  130. /**
  131. * 显示指定用户作业
  132. * 查询用户在课程中的channel
  133. */
  134. $userId = UserApi::getIdByName($request->get('user'));
  135. $userInCourse = CourseMember::where('course_id',$request->get('course'))
  136. ->where('user_id',$userId)
  137. ->first();
  138. if($userInCourse){
  139. $channelId = $userInCourse->channel_id;
  140. $channels = [$channelId];
  141. }
  142. }else if($request->get('view')==="answer"){
  143. /**
  144. * 显示答案
  145. * 算法:查询course 答案 channel
  146. */
  147. $channelId = Course::where('id',$request->get('course'))->value('channel_id');
  148. $channels = [$channelId];
  149. }else{
  150. //显示答案
  151. $channelId = Course::where('id',$request->get('course'))->value('channel_id');
  152. $channels = [$channelId];
  153. }
  154. }else{
  155. $channelId = Course::where('id',$request->get('course'))->value('channel_id');
  156. $channels = [$channelId];
  157. }
  158. }
  159. if($request->has('mode')){
  160. $mode = $request->get('mode');
  161. }else{
  162. $mode = 'read';
  163. }
  164. $data["html"] = MdRender::render($this->content,$channels,$query_id,$mode);
  165. }
  166. return $data;
  167. }
  168. }