RecentResource.php 2.3 KB

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