Răsfoiți Sursa

:construction: create

visuddhinanda 3 ani în urmă
părinte
comite
bbfa11422c

+ 61 - 0
app/Console/Commands/ExportTag.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Storage;
+use App\Models\Tag;
+
+class ExportTag extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'export:tag';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $filename = "public/export/offline/tag.csv";
+        Storage::disk('local')->put($filename, "");
+        $file = fopen(storage_path("app/{$filename}"),"w");
+        fputcsv($file,['id','name','description','color','owner_id']);
+        $bar = $this->output->createProgressBar(Tag::count());
+        foreach (Tag::select(['id','name','description','color','owner_id'])->cursor() as $chapter) {
+            fputcsv($file,[
+                            $chapter->id,
+                            $chapter->name,
+                            $chapter->description,
+                            $chapter->color,
+                            $chapter->owner_id,
+                            ]);
+            $bar->advance();
+        }
+        fclose($file);
+        $bar->finish();
+        return 0;
+    }
+}

+ 59 - 0
app/Console/Commands/ExportTagmap.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Storage;
+use App\Models\TagMap;
+class ExportTagmap extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'export:tag.map';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $filename = "public/export/offline/tag_map.csv";
+        Storage::disk('local')->put($filename, "");
+        $file = fopen(storage_path("app/{$filename}"),"w");
+        fputcsv($file,['id','table_name','anchor_id','tag_id']);
+        $bar = $this->output->createProgressBar(TagMap::count());
+        foreach (TagMap::select(['id','table_name','anchor_id','tag_id'])->cursor() as $chapter) {
+            fputcsv($file,[
+                            $chapter->id,
+                            $chapter->table_name,
+                            $chapter->anchor_id,
+                            $chapter->tag_id,
+                            ]);
+            $bar->advance();
+        }
+        fclose($file);
+        $bar->finish();
+        return 0;
+    }
+}

+ 200 - 0
app/Http/Api/TemplateRender.php

@@ -0,0 +1,200 @@
+<?php
+namespace App\Http\Api;
+
+use App\Models\DhammaTerm;
+use App\Models\PaliText;
+use App\Http\Controllers\CorpusController;
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Log;
+
+class TemplateRender{
+    protected $param = [];
+    protected $mode = "read";
+    protected $channel_id = "";
+
+    /**
+     * Create a new command instance.
+     * int $mode  'read' | 'edit'
+     * @return void
+     */
+    public function __construct($param, $channel_id, $mode)
+    {
+        $this->param = $param;
+        $this->channel_id = $channel_id;
+        $this->mode = $mode;
+    }
+
+    public function render($tpl_name){
+        switch ($tpl_name) {
+            case 'term':
+                # 术语
+                $result = $this->render_term();
+                break;
+            case 'note':
+                $result = $this->render_note();
+                break;
+            case 'sent':
+                $result = $this->render_sent();
+                break;
+            case 'quote':
+                $result = $this->render_quote();
+                break;
+            case 'exercise':
+                $result = $this->render_exercise();
+                break;
+            default:
+                # code...
+                $result = [
+                    'props'=>base64_encode(\json_encode([])),
+                    'html'=>'',
+                    'tag'=>'span',
+                    'tpl'=>'unknown',
+                ];
+                break;
+        }
+        return $result;
+    }
+
+    private function render_term(){
+        $word = $this->get_param($this->param,"word",1);
+        $channelId = $this->channel_id;
+        $props = Cache::remember("/term/{$this->channel_id}/{$word}",
+              60,
+              function() use($word,$channelId){
+                $tplParam = DhammaTerm::where("word",$word)->first();
+                $output = [
+                    "word" => $word,
+                    "channel" => $channelId,
+                    ];
+                    $innerString = $output["word"];
+                if($tplParam){
+                    $output["id"] = $tplParam->guid;
+                    $output["meaning"] = $tplParam->meaning;
+                    $innerString = "{$output["meaning"]}({$output["word"]})";
+                    if(!empty($tplParam->other_meaning)){
+                        $output["meaning2"] = $tplParam->other_meaning;
+                    }
+                }
+                $output['innerHtml'] = $innerString;
+                return $output;
+              });
+        return [
+            'props'=>base64_encode(\json_encode($props)),
+            'html'=>$props['innerHtml'],
+            'tag'=>'span',
+            'tpl'=>'term',
+            ];
+    }
+
+    private  function render_note(){
+
+        $props = ["note" => $this->get_param($this->param,"text",1)];
+        $trigger = $this->get_param($param,"trigger",2);
+        if(!empty($trigger)){
+            $props["trigger"] = $trigger;
+            $innerString = $props["trigger"];
+        }
+        return [
+            'props'=>base64_encode(\json_encode($props)),
+            'html'=>$innerString,
+            'tag'=>'span',
+            'tpl'=>'note',
+            ];
+    }
+
+    private  function render_exercise(){
+
+        $id = $this->get_param($this->param,"id",1);
+        $title = $this->get_param($this->param,"title",1);
+        $props = [
+                    "id" => $id,
+                    "title" => $title,
+                    "channel" => $this->channel_id,
+                ];
+
+        return [
+            'props'=>base64_encode(\json_encode($props)),
+            'html'=>"",
+            'tag'=>'span',
+            'tpl'=>'exercise',
+            ];
+    }
+
+    private  function render_quote(){
+        $paraId = $this->get_param($this->param,"para",1);
+        $channelId = $this->channel_id;
+        $props = Cache::remember("/quote/{$channelId}/{$paraId}",
+              60,
+              function() use($paraId,$channelId){
+                $para = \explode('-',$paraId);
+                $output = [
+                    "paraId" => $paraId,
+                    "channel" => $channelId,
+                    "innerString" => $paraId,
+                    ];
+                if(count($para)<2){
+                    return $output;
+                }
+                $PaliText = PaliText::where("book",$para[0])
+                                    ->where("paragraph",$para[1])
+                                    ->select(['toc','path'])
+                                    ->first();
+
+                if($PaliText){
+                    $output["pali"] = $PaliText->toc;
+                    $output["paliPath"] = \json_decode($PaliText->path);
+                    $output["innerString"]= $PaliText->toc;
+                }
+                return $output;
+              });
+        return [
+            'props'=>base64_encode(\json_encode($props)),
+            'html'=>$props["innerString"],
+            'tag'=>'span',
+            'tpl'=>'quote',
+            ];
+    }
+    private  function render_sent(){
+
+        $sid = $this->get_param($this->param,"sid",1);
+        $channel = $this->get_param($this->param,"channel",2);
+        if(!empty($channel)){
+            $mChannel = $channel;
+        }else{
+            $mChannel = $this->channel_id;
+        }
+        $sentInfo = explode('@',trim($sid));
+        $sentId = $sentInfo[0];
+        if(empty($mChannel)){
+            $channels = [];
+        }else{
+            $channels = [$mChannel];
+        }
+        if(isset($sentInfo[1])){
+            $channels = [$sentInfo[1]];
+        }
+        $Sent = new CorpusController();
+        $props = $Sent->getSentTpl($sentId,$channels,$this->mode,true);
+        if($this->mode==='read'){
+            $tpl = "sentread";
+        }else{
+            $tpl = "sentedit";
+        }
+        return [
+            'props'=>base64_encode(\json_encode($props)),
+            'html'=>"",
+            'tag'=>'span',
+            'tpl'=>$tpl,
+            ];
+    }
+
+    private  function get_param(array $param,string $name,int $id,string $default=''){
+        if(isset($param[$name])){
+            return trim($param[$name]);
+        }else if(isset($param["{$id}"])){
+            return trim($param["1"]);
+        }else{
+            return $default;
+        }
+    }
+}