ArticleResource.php 8.5 KB

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