From 7e0cc0b8d6d258538a0eb49c2c87c300988353e6 Mon Sep 17 00:00:00 2001 From: soufiane Date: Mon, 30 Oct 2023 16:22:35 +0100 Subject: [PATCH] wip #5475 @12:00 --- .../FluidbookPublication/StatsOperation.php | 45 ++++++++++++++++++- .../views/fluidbook_stats/stats.blade.php | 8 +++- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Admin/Operations/FluidbookPublication/StatsOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookPublication/StatsOperation.php index bb51875ab..d326226db 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookPublication/StatsOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookPublication/StatsOperation.php @@ -10,6 +10,13 @@ use Carbon\CarbonInterface; use Cubist\Matomo\Reporting; use Illuminate\Support\Facades\Route; use NumberFormatter; +use voku\helper\HtmlDomParser; +use Cubist\Excel\Excel; +use Cubist\Util\Files\Files; +use Cubist\Util\Str; +use PhpOffice\PhpSpreadsheet\Exception; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; // __('!! Statistiques') trait StatsOperation @@ -26,6 +33,10 @@ trait StatsOperation // API testing tool (intended for superadmins only) Route::get($segment . '/stats/API/{id?}', $controller . '@statsAPI'); + + // + Route::post($segment . '/stats/{fluidbook_id}_{hash}/{date?}/{period_override?}/generate', $controller . '@generateExcel') + ->name('generateexcel'); } protected function setupStatsDefaults() @@ -44,7 +55,6 @@ trait StatsOperation $data['locale'] = app()->getLocale(); $data['fluidbook'] = $fluidbook; return view('fluidbook_stats.stats', $data); - } protected function redirectMatomo($fluidbook_id, $hash, $period, $date) @@ -67,4 +77,37 @@ trait StatsOperation return view('fluidbook_stats.API', compact('matomo_tokens', 'id')); } + /** + * @throws NotFoundExceptionInterface + * @throws ContainerExceptionInterface + * @throws Exception + */ + public function generateExcel($fluidbook_id, $hash, $date = null, $period_override = null) { + $url = route('stats', compact('fluidbook_id', 'hash', 'date', 'period_override')); + + $name = "excel example"; + $safename = Str::slug($name); + $html = HtmlDomParser::file_get_html($url); + $tr = $html->find('table', 0)->find('tr'); + $excelData = []; + $keys = []; + $values = []; + + + foreach ($tr as $lines) { + $keys[] = $lines->find('td',0)->text; + $values[] = trim(preg_replace('/\s\s+/', ' ', $lines->find('td',1)->text)); + } + + $excelData[] = $keys; + $excelData[] = $values; + + //dump($excelData); + //die(); + + $tmpfile = Files::tempnam() . '.xlsx'; + Excel::fromArray('name', $excelData, null, $tmpfile); + return response()->download($tmpfile, $safename . '.xlsx')->deleteFileAfterSend(true); + die(); + } } diff --git a/resources/views/fluidbook_stats/stats.blade.php b/resources/views/fluidbook_stats/stats.blade.php index 2fcb958f3..7c10b8972 100644 --- a/resources/views/fluidbook_stats/stats.blade.php +++ b/resources/views/fluidbook_stats/stats.blade.php @@ -37,13 +37,17 @@

{{ __('Statistiques') }}

+
$start_date.','.$end_date, 'period_override'=>$period]) }}" method="POST"> + @csrf + +
- {!! $formatted_date_range !!} - + {!! $formatted_date_range !!} +
-- 2.39.5