RecentResource.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. use App\Http\Api\ChannelApi;
  5. use App\Models\Sentence;
  6. use App\Models\Article;
  7. use App\Models\DhammaTerm;
  8. class RecentResource extends JsonResource
  9. {
  10. /**
  11. * Transform the resource into an array.
  12. *
  13. * @param \Illuminate\Http\Request $request
  14. * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
  15. */
  16. public function toArray($request)
  17. {
  18. $data = [
  19. 'id' => $this->id,
  20. 'type' => $this->type,
  21. 'article_id' => $this->article_id,
  22. 'param' => $this->param,
  23. 'updated_at' => $this->updated_at,
  24. ];
  25. $title = '';
  26. switch ($this->type) {
  27. case 'article':
  28. $title = Article::where('uid', $this->article_id)->value('title');
  29. break;
  30. case 'chapter':
  31. if (!empty($this->param)) {
  32. $param = json_decode($this->param, true);
  33. if (isset($param['channel'])) {
  34. $channelId = explode('_', $param['channel'])[0];
  35. }
  36. }
  37. $paliChannel = ChannelApi::getSysChannel('_System_Pali_VRI_');
  38. if (!isset($channelId)) {
  39. $channelId = $paliChannel;
  40. }
  41. $para = explode('-', $this->article_id);
  42. if (count($para) === 2) {
  43. $title = Sentence::where('book_id', (int)$para[0])
  44. ->where('paragraph', (int)$para[1])
  45. ->where('channel_uid', $channelId)
  46. ->value('content');
  47. if (empty($title)) {
  48. $title = Sentence::where('book_id', (int)$para[0])
  49. ->where('paragraph', (int)$para[1])
  50. ->where('channel_uid', $paliChannel)
  51. ->value('content');
  52. }
  53. }
  54. break;
  55. case 'term':
  56. $title = DhammaTerm::where('guid', $this->article_id)->value('word');
  57. break;
  58. default:
  59. $title = $this->article_id;
  60. break;
  61. }
  62. $data['title'] = $title;
  63. return $data;
  64. }
  65. }