]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5475 @7:00
authorsoufiane <soufiane@cubedesigners.com>
Thu, 2 Nov 2023 15:05:19 +0000 (16:05 +0100)
committersoufiane <soufiane@cubedesigners.com>
Thu, 2 Nov 2023 15:05:19 +0000 (16:05 +0100)
app/Http/Controllers/Admin/Operations/FluidbookPublication/StatsOperation.php
resources/views/fluidbook_stats/stats.blade.php

index d326226db4fadf1c734946733590b639ab8f032a..e79de3d4607aaffd53ec456659a1263105ad2552 100644 (file)
@@ -11,13 +11,18 @@ 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;
 
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PhpOffice\PhpSpreadsheet\Style\Alignment;
+use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
+use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
+
+
 // __('!! Statistiques')
 trait StatsOperation
 {
@@ -85,29 +90,110 @@ trait StatsOperation
     public function generateExcel($fluidbook_id, $hash, $date = null, $period_override = null) {
         $url = route('stats', compact('fluidbook_id', 'hash', 'date', 'period_override'));
 
-        $name = "excel example";
+        $name = "excel";
         $safename = Str::slug($name);
         $html = HtmlDomParser::file_get_html($url);
-        $tr = $html->find('table', 0)->find('tr');
-        $excelData = [];
-        $keys = [];
-        $values = [];
+        foreach ($html->find('html') as $e) {
+            $e->lang = "fr";
+        }
+        dd($html->plaintext);
 
+        $tables = $html->find('table');
+        $tmpfile = Files::tempnam() . '.xlsx';
+        $this->Excel_($tables, $name, null, $tmpfile);
+        return response()->download($tmpfile, $safename . '.xlsx')->deleteFileAfterSend(true);
+        die();
+    }
 
-        foreach ($tr as $lines) {
-            $keys[] = $lines->find('td',0)->text;
-            $values[] = trim(preg_replace('/\s\s+/', ' ', $lines->find('td',1)->text));
+    /**
+     * @param $tables array
+     * @param $sheetname string
+     * @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)
+    {
+        $columns = 0;
+
+        $excel = new Spreadsheet();
+        $excel->getDefaultStyle()
+            ->getNumberFormat()
+            ->setFormatCode(
+                NumberFormat::FORMAT_TEXT
+            );
+
+        foreach ($tables as $key => $table) {
+            $sheet = $excel->createSheet();
+            $sheet->setTitle($sheetname.'-'.$key);
+
+            $tr = $table->find('tr');
+            $theads = $table->find('th');
+            $head = [];
+            $datas = [];
+
+            if($theads) {
+                foreach ($theads as $thead) {
+                    $head[] = $thead->text;
+                }
+            }
+
+            foreach ($tr as $k => $lines) {
+                foreach ($lines->find('td') as $td) {
+                    $datas[$k][] = trim(preg_replace('/\s\s+/', ' ', $td->text));
+                }
+            }
+
+            $line = 1;
+            if (!is_null($head) && sizeof($head) > 0) {
+                $c = 0;
+                foreach ($head as $label) {
+                    $c++; //must be positive value
+                    $columns = max($columns, $c);
+                    $cell = $sheet->getCellByColumnAndRow($c, $line);
+                    $cell->setValue($label);
+                    $style = $sheet->getStyleByColumnAndRow($c, $line);
+                    $style->getFont()->setBold(true);
+                    $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
+                }
+                $line++;
+            }
+
+            foreach ($datas as $l) {
+                $c = 1;
+                foreach ($l as $v) {
+                    $columns = max($columns, $c);
+                    $sheet->getCellByColumnAndRow($c, $line)->setValue($v);
+                    $style = $sheet->getStyleByColumnAndRow($c, $line);
+                    $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
+                    $style->getAlignment()->setWrapText(true);
+                    $c++;
+                }
+                $line++;
+            }
+
+            for ($i = 0; $i <= $columns; $i++) {
+                if (null === $width || !isset($width[$i]) || null === $width[$i] || $width[$i] == 'auto') {
+                    $sheet->getColumnDimensionByColumn($i)->setAutoSize(true);
+                } else {
+                    $sheet->getColumnDimensionByColumn($i)->setAutoSize(false)->setWidth($width[$i]);
+                }
+            }
         }
 
-        $excelData[] = $keys;
-        $excelData[] = $values;
+        if (null !== $output) {
+            self::_save($excel, $output);
+        }
 
-        //dump($excelData);
-        //die();
+        return $excel;
+    }
 
-        $tmpfile = Files::tempnam() . '.xlsx';
-        Excel::fromArray('name', $excelData, null, $tmpfile);
-        return response()->download($tmpfile, $safename . '.xlsx')->deleteFileAfterSend(true);
-        die();
+    protected static function _save($excel, $output)
+    {
+        $writer = new Xlsx($excel);
+        $writer->save($output);
     }
+
 }
index 7c10b8972efc36c591c4af5a61b740f47743cbf4..c8d2e9b94ec981e5937e12e13c73b2c3d9730b84 100644 (file)
@@ -51,7 +51,7 @@
         </div>
 
 
-        <table class="{!! $tableClasses !!}">
+        <table class="{!! $tableClasses !!}" data-table="resume">
             <thead style="display: none">
             </thead>
             <tr>