]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5475
authorsoufiane <soufiane@cubedesigners.com>
Mon, 6 Nov 2023 14:12:18 +0000 (15:12 +0100)
committersoufiane <soufiane@cubedesigners.com>
Mon, 6 Nov 2023 16:43:51 +0000 (17:43 +0100)
# Conflicts:
# resources/views/fluidbook_stats/stats.blade.php

app/Http/Controllers/Admin/Operations/FluidbookPublication/StatsOperation.php
resources/views/fluidbook_stats/stats.blade.php

index 2878ec371864cc1fd1f1423fce63c16ba29b9dea..e2ee7fa76da2ec9fd51f40baac910670424b018b 100644 (file)
@@ -5,6 +5,8 @@ namespace App\Http\Controllers\Admin\Operations\FluidbookPublication;
 use App\Fluidbook\Stats;
 use App\Http\Middleware\CheckIfAdmin;
 use App\Models\FluidbookPublication;
+use App\Models\FluidbookTranslate;
+use App\Models\ToolboxTranslate;
 use Carbon\Carbon;
 use Carbon\CarbonInterface;
 use Cubist\Matomo\Reporting;
@@ -92,28 +94,35 @@ trait StatsOperation
 
         $name = "excel";
         $safename = Str::slug($name);
-        $html = HtmlDomParser::file_get_html($url);
-        foreach ($html->find('html') as $e) {
-            $e->lang = "fr";
-        }
+
+        $locale = backpack_user()->locale;
+        $acceptLang = [
+            "fr" => "fr-FR,fr;q=0.9",
+            "en" => "en-US,en;q=0.5"
+        ];
+
+        $html = $this->translateHtml($url,$acceptLang[$locale]);
+        $html = HtmlDomParser::str_get_html($html);
+
+        $names = [...$html->find('h2'), ...$html->find('h3')];
 
         $tables = $html->find('table');
         $tmpfile = Files::tempnam() . '.xlsx';
-        $this->Excel_($tables, $name, null, $tmpfile);
+        $this->Excel_($tables, $names, null, $tmpfile);
         return response()->download($tmpfile, $safename . '.xlsx')->deleteFileAfterSend(true);
         die();
     }
 
     /**
      * @param $tables array
-     * @param $sheetname string
+     * @param $sheetnames array
      * @param $head array|null
      * @param $output string|null
      * @param $width int|null
      * @return Spreadsheet
      * @throws \PhpOffice\PhpSpreadsheet\Exception
      */
-    public function Excel_($tables, $sheetname, $head = null, $output = null, $width = null)
+    public function Excel_($tables, $sheetnames, $head = null, $output = null, $width = null)
     {
         $columns = 0;
 
@@ -125,8 +134,10 @@ trait StatsOperation
             );
 
         foreach ($tables as $key => $table) {
-            $sheet = $excel->createSheet();
-            $sheet->setTitle($sheetname.'-'.$key);
+            $sheet = $key === 0 ? $excel->getActiveSheet() : $excel->createSheet();
+            $sheetname = strip_tags($sheetnames[$key]->getAttribute('data-title-excel'));
+            $sheetname = \Illuminate\Support\Str::limit($sheetname, 28, '...');
+            $sheet->setTitle($sheetname);
 
             $tr = $table->find('tr');
             $theads = $table->find('th');
@@ -195,4 +206,16 @@ trait StatsOperation
         $writer->save($output);
     }
 
+    protected function translateHtml($url,$lang) {
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL,$url);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        $headers = [
+            'Accept-Language: '.$lang,
+        ];
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+        $html = curl_exec ($ch);
+        curl_close ($ch);
+        return $html;
+    }
 }
index 3bd5b58d577b0c7c9d87fd75db1abd8aa85c7fbf..b23b4c511cd65b48cd5c7b7f4895bf19661083a5 100644 (file)
                             {{ $period_title['singular'] }}
                         </a>
                     @endforeach
-                        <form action="{{ route("generateexcel", compact('fluidbook_id', 'hash') + ['date'=>$start_date.','.$end_date, 'period_override'=>$period]) }}" method="POST">
-                            @csrf
-                            <button class="btn btn-primary ml-3" type="submit">{{ __('Exporter au format Excel') }}</button>
-                        </form>
                 </div>
             </div>
 
                     <tbody>
                     @foreach($multimedia as $label=>$media)
                         <tr>
-                            <td class="whitespace-nowrap" data-sort-value="{{$label }}" data-type-media="{{ $media['type'] }}">
+                            <td class="whitespace-nowrap" data-sort-value="{{$label }}">
                                 <i class="{{$icons[$media['type']]}}"></i> {{$label }}
                             </td>
                             @include('fluidbook_stats.pagelink',['group'=>$media['pages'][0]??''])
                 {{-- Search Keywords --}}
                 @if($searches->isNotEmpty())
                     <div>
-                        <h3 data-title-excel="{{ __('Recherches') }}">{{ __('Mots recherchés') }}</h3>
+                        <h3 data-title-excel="{{ __('Mots') }}">{{ __('Mots recherchés') }}</h3>
 
                         <table class="{!! $tableClasses !!}">
                             <thead>
                             </tr>
                             </thead>
                             <tbody>
-                            @foreach($countries as $key => $country)
+                            @foreach($countries as $country)
                                 <tr>
-                                    <td data-flag="{{ $key }}" class="whitespace-nowrap" data-sort-value="{{ $country['label'] }}">
+                                    <td class="whitespace-nowrap" data-sort-value="{{ $country['label'] }}">
                                         <img src="{{ $country['flag'] }}" alt="{{ $country['label'] }}"
                                              style="width: 1.5em; margin-right: 0.75em;">
                                         {{ $country['label'] }}