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
// 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()
$data['locale'] = app()->getLocale();
$data['fluidbook'] = $fluidbook;
return view('fluidbook_stats.stats', $data);
-
}
protected function redirectMatomo($fluidbook_id, $hash, $period, $date)
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();
+ }
}
<h2 class="mt-4">
{{ __('Statistiques') }}
</h2>
+ <form action="{{ route("generateexcel", compact('fluidbook_id', 'hash') + ['date'=>$start_date.','.$end_date, 'period_override'=>$period]) }}" method="POST">
+ @csrf
+ <button type="submit">Generate</button>
+ </form>
<div data-daterangepicker class="mb-4" style="cursor: pointer"
title="{{ __('Période').' - '.__('Cliquer pour changer l\'intervalle') }}">
<i class="las la-calendar-week align-middle mr-1" style="font-size: 32px;"></i>
<span class="date-range-text">
- {!! $formatted_date_range !!}
- </span>
+ {!! $formatted_date_range !!}
+ </span>
</div>