TermService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Services;
  3. use App\Models\DhammaTerm;
  4. use App\Http\Api\ChannelApi;
  5. use App\Http\Resources\TermResource;
  6. use Illuminate\Http\Request;
  7. class TermService
  8. {
  9. public function getCommunityGlossary($lang)
  10. {
  11. $localTermChannel = ChannelApi::getSysChannel(
  12. "_community_term_" . strtolower($lang) . "_",
  13. "_community_term_en_"
  14. );
  15. $result = DhammaTerm::select(['word', 'tag', 'meaning', 'other_meaning'])
  16. ->where('channal', $localTermChannel)
  17. ->get();
  18. return ['items' => $result, 'total' => count($result)];
  19. }
  20. public function getGrammarGlossary($lang)
  21. {
  22. $localTermChannel = ChannelApi::getSysChannel(
  23. "_System_Grammar_Term_" . strtolower($lang) . "_",
  24. "_System_Grammar_Term_en_"
  25. );
  26. $result = DhammaTerm::select(['word', 'tag', 'meaning', 'other_meaning'])
  27. ->where('channal', $localTermChannel)
  28. ->get();
  29. return ['items' => $result, 'total' => count($result)];
  30. }
  31. public function getRaw($id)
  32. {
  33. $result = DhammaTerm::find($id);
  34. return $result;
  35. }
  36. public function isCommunity(?string $channelId)
  37. {
  38. $channel = ChannelApi::getById($channelId);
  39. if (!$channel) {
  40. return false;
  41. }
  42. if (strpos($channel['name'], '_community_term_') === false) {
  43. return false;
  44. } else {
  45. return true;
  46. }
  47. }
  48. public function communityTerm(string $word, string $lang, string $format)
  49. {
  50. $localTermChannel = ChannelApi::getSysChannel(
  51. "_community_term_" . strtolower($lang) . "_"
  52. );
  53. $result = DhammaTerm::where('word', $word)
  54. ->where('channal', $localTermChannel)
  55. ->first();
  56. if ($result) {
  57. $resource = new TermResource($result);
  58. $urlParam = ['format' => $format];
  59. $fakeRequest = Request::create('', 'GET', $urlParam);
  60. $termArray = $resource->toArray($fakeRequest);
  61. if ($result) {
  62. return $termArray;
  63. } else {
  64. return null;
  65. }
  66. } else {
  67. return null;
  68. }
  69. }
  70. public function communityTerms(string $lang)
  71. {
  72. $localTermChannel = ChannelApi::getSysChannel(
  73. "_community_term_" . strtolower($lang) . "_"
  74. );
  75. $result = DhammaTerm::where('channal', $localTermChannel)
  76. ->whereNotNull('note')
  77. ->where('note', '<>', '')
  78. ->take(10)
  79. ->orderBy('updated_at', 'desc')
  80. ->get();
  81. return [
  82. "data" => TermResource::collection($result),
  83. "count" => 10
  84. ];
  85. }
  86. public function find(string $id, string $format): ?array
  87. {
  88. $result = DhammaTerm::find($id);
  89. $resource = new TermResource($result);
  90. $urlParam = ['format' => $format];
  91. $fakeRequest = Request::create('', 'GET', $urlParam);
  92. $termArray = $resource->toArray($fakeRequest);
  93. if ($result) {
  94. return $termArray;
  95. } else {
  96. return null;
  97. }
  98. }
  99. }