ArticleResource.php 8.2 KB

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