From: Vincent Vanwaelscappel Date: Thu, 20 Nov 2025 15:28:17 +0000 (+0100) Subject: wait #7828 @1.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=de884d9a658740bfe5c2d4f1c291ec6fe51a0133;p=fluidbook-toolbox.git wait #7828 @1.5 --- diff --git a/app/Http/Controllers/Admin/Operations/FluidbookCollection/AuditLinksOperation.php b/app/Http/Controllers/Admin/Operations/FluidbookCollection/AuditLinksOperation.php index 6b75226ef..54aa733fb 100644 --- a/app/Http/Controllers/Admin/Operations/FluidbookCollection/AuditLinksOperation.php +++ b/app/Http/Controllers/Admin/Operations/FluidbookCollection/AuditLinksOperation.php @@ -5,12 +5,14 @@ namespace App\Http\Controllers\Admin\Operations\FluidbookCollection; use App\Models\FluidbookAuditLink; use App\Models\FluidbookCollection; use Cubist\Util\Files\Files; +use Cubist\Util\Str; use Illuminate\Support\Facades\Route; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; use PhpOffice\PhpSpreadsheet\Style\Protection; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; trait AuditLinksOperation @@ -20,93 +22,67 @@ trait AuditLinksOperation protected function setupAuditLinksRoutes($segment, $routeName, $controller) { - Route::match(['get','post'],$segment . '/{id}/export_excel', $controller . '@exportExcel')->name("download_audit_links"); + Route::match(['get', 'post'], $segment . '/{id}/export_excel', $controller . '@exportExcel')->name("download_audit_links"); } - public function exportExcel($id) { - $errorlinks = FluidbookAuditLink::where('error_code','!=','')->get(); - $redirectionlinks = FluidbookAuditLink::where('redirection_code','!=','')->get(); + public function exportExcel($id) + { - $links = [ - "erreurs" => $errorlinks->makeHidden(['updated_at', 'created_at', 'deleted_at'])->toArray(), - "avertissements" => $redirectionlinks->makeHidden(['updated_at', 'created_at', 'deleted_at'])->toArray() + /** @var FluidbookCollection $collection */ + $collection = FluidbookCollection::find($id); + $fluidbooks = []; + foreach ($collection->publications as $fluidbook) { + $fluidbooks[] = $fluidbook['fluidbook']; + } + + $columns = [ + 'fluidbook_id' => 'Fluidbook #', + 'page' => 'Page', + 'link_id' => 'Link #', + 'url' => 'URL', + 'http_code' => 'Response code', + 'code_date' => 'Response seen first', + 'target_code' => 'Redirection target code', + 'target_url' => 'Redirected URL', + 'updated_at' => 'Tested at', + 'new_url' => 'New URL', ]; - $errorKeys = array_keys($links['erreurs'][0]); + $links = FluidbookAuditLink::where('http_code', '!=', '200')->whereIn('fluidbook_id', $fluidbooks)->get(); + $errors = []; + $warnings = []; + + foreach ($links as $link) { + $code = $link->http_code; + if (null !== $link->target_code) { + $code = $link->target_code; + } + if ($code >= 400) { + $errors[] = $link; + } else { + $warnings[] = $link; + } + } - $excel = new Spreadsheet(); + $excel = new Spreadsheet(); $excel->getDefaultStyle() ->getNumberFormat() ->setFormatCode( NumberFormat::FORMAT_TEXT - ); - - $index = 0; - foreach ($links as $key => $link) { - $sheet = $excel->createSheet($index); - $sheetname = ucfirst($key); - $sheet->setTitle($sheetname); - - $line = 1; - $columns = 0; - $c = 0; - - for ($i = 1; $i < sizeof($errorKeys); $i++) { - $c++; - $columns = max($columns, $c); - $cell = $sheet->getCellByColumnAndRow($c, $line); - $cell->setValue($errorKeys[$i]); - $style = $sheet->getStyleByColumnAndRow($c, $line); - $style->getFont()->setBold(true); - $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); - } + ); + $excel->getActiveSheet()->setTitle('Errors'); - $maxColSize = []; - - for ($i = 0; $i < sizeof($link); $i++) { - $c = 1; - $line++; - foreach ($link[$i] as $k => $value) { - if($k === "id") { - continue; - } - $columns = max($columns, $c); - $maxColSize[$c][] = strlen($value); - $sheet->getCellByColumnAndRow($c, $line)->setValue($value); - $style = $sheet->getStyleByColumnAndRow($c, $line); - if ($k !== "new_url") { - $style->getProtection()->setLocked(Protection::PROTECTION_PROTECTED); - } - $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); - $style->getAlignment()->setWrapText(false); - $c++; - } - } - - $j = 1; - for ($i = 0; $i <= $columns; $i++) { - if (array_key_exists($j, $maxColSize)) { - if (max($maxColSize[$j]) > 50) { - $sheet->getColumnDimensionByColumn($j)->setWidth(100); - } else { - $sheet->getColumnDimensionByColumn($j)->setAutoSize(true); - } - } else { - $sheet->getColumnDimensionByColumn($j)->setAutoSize(true); - } - $j++; - } + $this->_setDataInSheet($excel->getActiveSheet(), $errors, $columns); - $index++; - } + $sheet = $excel->createSheet(); + $sheet->setTitle('Warnings'); + $this->_setDataInSheet($sheet, $warnings, $columns); $excel->setActiveSheetIndex(0); $excel->getActiveSheet()->getProtection()->setSheet(true); $excel->getDefaultStyle()->getProtection()->setLocked(false); - // Il y a une feuille "Worksheet" qui apparait alors qu'elle a pas été invitée - $excel->removeSheetByIndex(2); // Direct download $tmpfile = Files::tempnam() . '.xlsx'; @@ -114,8 +90,89 @@ trait AuditLinksOperation $writer->save($tmpfile); $collection = FluidbookCollection::find($id); - $filename = "linksaudit_".$collection->title."_".time().".xlsx"; + $filename = "linksaudit_" . Str::slug($collection->title) . "_" . time() . ".xlsx"; + + return response()->download($tmpfile, $filename)->deleteFileAfterSend(); + } + + /** + * @param $sheet Worksheet + * @param $links array + * @param $columns array + * @return void + */ + protected function _setDataInSheet($sheet, $links, $columns) + { + $codes = [ + 200 => __('Everything is fine'), + 301 => __('Permanent redirection: the page has definitively moved, please update the url; the former URL may not be valid indefinitely'), + 308 => __('Permanent redirection: the page has definitively moved, please update the url; the former URL may not be valid indefinitely'), + 307 => __('Temporary redirection: '), + 404 => __('Not found: the page does not exist anymore'), + 401 => __('Unauthorized: the page may be accessible only to logged in users'), + 403 => __('The auditing tool may have been blocked by a anti bot protection. A manual check may be needed'), + 429 => __('The URL is invalid'), + 629 => __('The URL is invalid'), + 622 => __('Timeout: the server took too much time to respond. This should be temporary'), + 500 => __('There is a problem on the server. This should be temporary'), + 502 => __('There is a problem on the server. This should be temporary'), + 503 => __('There is a problem on the server. This should be temporary'), + 628 => __('The auditing tool was blocked by CloudFlare DDOS Protection. Please check manually'), + 603 => __('The auditing tool was blocked by CloudFlare DDOS Protection. Please check manually'), + 695 => __('There is a problem with the SSL certificate. The auditing tool could not check if the URL is valid or not. Please check manually'), + ]; + + $row = 1; + $col = 1; + $maxColSize = []; + foreach ($columns as $column => $title) { + $cell = $sheet->getCell([$col, $row]); + $cell->setValue($title); + + $maxColSize[$column] = mb_strlen($title); + $style = $sheet->getStyle([$col, $row]); + $style->getFont()->setBold(true); + $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); + $col++; + } + $row++; + + foreach ($links as $link) { + $col = 1; + foreach ($columns as $column => $title) { + $value = $link[$column] ?? ''; + $maxColSize[$column] = max($maxColSize[$column], mb_strlen($value)); + $sheet->getCell([$col, $row])->setValue($value); + $style = $sheet->getStyle([$col, $row]); + if ($column !== "new_url") { + $style->getProtection()->setLocked(Protection::PROTECTION_PROTECTED); + } + if ($column === 'http_code' || $column === 'target_code' && isset($codes[$value])) { + $c = $sheet->getComment([$col, $row]); + $c->getText()->createText($codes[$value]); + $c->setWidth('300px'); + $c->setHeight('50px'); + } + $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); + $col++; + } + $row++; + } + + $col = 1; + foreach ($columns as $column => $title) { + $dim = $sheet->getColumnDimensionByColumn($col); + if ($column == 'new_url') { + $dim->setWidth(150); + } else { + if ($maxColSize[$column] > 75) { + $dim->setWidth(100); + } else { + $dim->setAutoSize(true); + } + } + $col++; + } - return response()->download($tmpfile, $filename)->deleteFileAfterSend(); } }