Browse Source

Merge pull request #1918 from visuddhinanda/laravel

✨ render_grammar_lookup
visuddhinanda 2 years ago
parent
commit
b07685b898
1 changed files with 58 additions and 7 deletions
  1. 58 7
      app/Http/Api/TemplateRender.php

+ 58 - 7
app/Http/Api/TemplateRender.php

@@ -26,7 +26,8 @@ class TemplateRender{
     protected $debug = [];
     protected $format = 'react';
     protected $studioId = null;
-
+    protected $lang = 'en';
+    protected $langFamily = 'en';
 
     /**
      * Create a new command instance.
@@ -45,6 +46,17 @@ class TemplateRender{
         $this->format = $format;
         $this->studioId = $studioId;
         $this->debug = $debug;
+
+        if(count($this->channel_id)>0){
+            $channelId = $this->channel_id[0];
+            if(Str::isUuid($channelId)){
+                $lang = Channel::where('uid',$channelId)->value('lang');
+                if(!empty($lang)){
+                    $this->lang = $lang;
+                    $this->langFamily = explode('-',$lang)[0];
+                }
+            }
+        }
     }
     private function info($message,$debug){
         if(in_array($debug,$this->debug)){
@@ -92,6 +104,9 @@ class TemplateRender{
             case 'v':
                 $result = $this->render_video();
                 break;
+            case 'g':
+                $result = $this->render_grammar_lookup();
+                break;
             default:
                 # code...
                 $result = [
@@ -105,14 +120,17 @@ class TemplateRender{
         return $result;
     }
 
-    public function getTermProps($word,$tag){
-        if(count($this->channel_id)>0){
-            $channelId = $this->channel_id[0];
+    public function getTermProps($word,$tag=null,$channel=null){
+        if($channel && !empty($channel)){
+            $channelId = $channel;
         }else{
-            $channelId = null;
+            if(count($this->channel_id)>0){
+                $channelId = $this->channel_id[0];
+            }else{
+                $channelId = null;
+            }
         }
 
-
         if(count($this->channelInfo)===0){
             Log::error('channel is null');
             $output = [
@@ -133,7 +151,7 @@ class TemplateRender{
             $this->info('channel id'.$channelId,'term');
             $table = DhammaTerm::where("word",$word)
                                 ->where('channal',$channelId);
-            if(!empty($tag)){
+            if($tag && !empty($tag)){
                 $table = $table->where('tag',$tag);
             }
             $tplParam = $table->orderBy('updated_at','desc')
@@ -926,6 +944,39 @@ class TemplateRender{
         return $output;
     }
 
+    private function render_grammar_lookup(){
+        $word = $this->get_param($this->param,"word",1);
+        $props = ['word' => $word];
+
+        $localTermChannel = ChannelApi::getSysChannel(
+            "_System_Grammar_Term_".strtolower($this->lang)."_",
+            "_System_Grammar_Term_en_"
+        );
+        $term = $this->getTermProps($word,null,$localTermChannel);
+        $props['term'] = $term;
+        switch ($this->format) {
+            case 'react':
+                $output = [
+                    'props'=>base64_encode(\json_encode($props)),
+                    'html'=>"",
+                    'text'=>$props['word'],
+                    'tag'=>'span',
+                    'tpl'=>'grammar',
+                    ];
+                break;
+            case 'unity':
+                $output = [
+                    'props'=>base64_encode(\json_encode($props)),
+                    'tpl'=>'grammar',
+                    ];
+                break;
+            default:
+                $output = $props['word'];
+                break;
+        }
+        return $output;
+    }
+
     private  function render_video(){
 
         $url = $this->get_param($this->param,"url",1);