TermService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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(['guid', '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 communityWiki(string $word, string $lang, string $format)
  71. {
  72. $localTermChannel = ChannelApi::getSysChannel(
  73. "_community_translation_" . strtolower($lang) . "_"
  74. );
  75. $result = DhammaTerm::where('word', $word)
  76. ->where('channal', $localTermChannel)
  77. ->first();
  78. if ($result) {
  79. $resource = new TermResource($result);
  80. $urlParam = ['format' => $format];
  81. $fakeRequest = Request::create('', 'GET', $urlParam);
  82. $termArray = $resource->toArray($fakeRequest);
  83. if ($result) {
  84. return $termArray;
  85. } else {
  86. return null;
  87. }
  88. } else {
  89. return null;
  90. }
  91. }
  92. public function communityTerms(string $lang)
  93. {
  94. $localTermChannel = ChannelApi::getSysChannel(
  95. "_community_term_" . strtolower($lang) . "_"
  96. );
  97. $result = DhammaTerm::where('channal', $localTermChannel)
  98. ->whereNotNull('note')
  99. ->where('note', '<>', '')
  100. ->take(10)
  101. ->orderBy('updated_at', 'desc')
  102. ->get();
  103. return [
  104. "data" => TermResource::collection($result),
  105. "count" => 10
  106. ];
  107. }
  108. public function find(string $id, string $format): ?array
  109. {
  110. $result = DhammaTerm::find($id);
  111. $resource = new TermResource($result);
  112. $urlParam = ['format' => $format];
  113. $fakeRequest = Request::create('', 'GET', $urlParam);
  114. $termArray = $resource->toArray($fakeRequest);
  115. if ($result) {
  116. return $termArray;
  117. } else {
  118. return null;
  119. }
  120. }
  121. public function update(string $id, array $data)
  122. {
  123. DhammaTerm::where('guid', $id)->update($data);
  124. }
  125. }