ArticleResource.php 7.2 KB

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