From: soufiane Date: Mon, 6 Nov 2023 14:12:18 +0000 (+0100) Subject: wip #5475 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=623cab318b8a6ebb2566b18b6f5addcab0edf232;p=fluidbook-toolbox.git wip #5475 --- diff --git a/app/Http/Controllers/Admin/Operations/FluidbookPublication/StatsOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookPublication/StatsOperation.php index 2878ec371..e2ee7fa76 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookPublication/StatsOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookPublication/StatsOperation.php @@ -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; + } } diff --git a/resources/views/fluidbook_stats/stats.blade.php b/resources/views/fluidbook_stats/stats.blade.php index c8d2e9b94..b23b4c511 100644 --- a/resources/views/fluidbook_stats/stats.blade.php +++ b/resources/views/fluidbook_stats/stats.blade.php @@ -34,7 +34,7 @@ $fluidbookBaseURL=$fluidbook->getPreviewURL(); @endphp -

+

{{ __('Statistiques') }}

$start_date.','.$end_date, 'period_override'=>$period]) }}" method="POST"> @@ -116,7 +116,7 @@ {{-- Period (segmentation) override controls [ Day / Week / Month / Year ] --}}