ArticleResource.php 8.6 KB

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