From 234f2b31f4beea358c55f9eda4dc3396579ff94e Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 23 Mar 2023 14:36:26 +0100 Subject: [PATCH] wip #5819 @1.5 --- app/Jobs/FluidbookCompiler.php | 56 ++++++++++++++++++++---------- app/Models/FluidbookDocument.php | 59 +++++++++++++++++--------------- 2 files changed, 68 insertions(+), 47 deletions(-) diff --git a/app/Jobs/FluidbookCompiler.php b/app/Jobs/FluidbookCompiler.php index 2a4ec48d2..512bf968b 100644 --- a/app/Jobs/FluidbookCompiler.php +++ b/app/Jobs/FluidbookCompiler.php @@ -286,6 +286,8 @@ class FluidbookCompiler extends Base implements CompilerInterface function __construct(FluidbookPublication $book, $scormVariant = false, $phonegap = false, $phonegapVersion = 'latest', $dir = null, $standalone = false, $appcache = false, $home = false, FluidbookTheme $theme = null, $hybrid = false, Command $command = null) { + ExcelToArray::setCache(protected_path('fluidbookpublication/cache/exceltoarray')); + parent::__construct(); $this->setFluidbook($book); @@ -871,12 +873,7 @@ class FluidbookCompiler extends Base implements CompilerInterface $this->addJsLib('cfoc', 'js/libs/fluidbook/cart/fluidbook.cart.cfoc.js'); - // NOTE: $this->config->basketReferences initially contains the uploaded filename and if we don't - // modify it, the writeCartConfig() function will replace it with the extracted data from the spreadsheet 🤐 - // It would be much better if the extracted data had its own variable but it would break too many things to - // change it now, so we'll follow the same approach. - - if (!empty($this->config->basketReferences)) { + if (!empty($this->config->basketReferences) && is_string($this->config->basketReferences)) { if (file_exists($this->config->basketReferences) || Url::isDistant($this->config->basketReferences)) { $referencesFile = $this->config->basketReferences; } else { @@ -887,16 +884,16 @@ class FluidbookCompiler extends Base implements CompilerInterface $references = []; if (file_exists($referencesFile) || Url::isDistant($referencesFile)) { - $raw_data = ExcelToArray::excelToArray($referencesFile); - $first_sheet_rows = reset($raw_data); // First sheet's data will be returned since it's the first array element + $rows = ExcelToArray::excelToArrayFirstSheet($referencesFile); + // Expected headings are: EXCLU, LIGNE, EAN, REF, DESIGNATION, COULEUR, QTE MINI, PRIX TTC - $column_headings = array_shift($first_sheet_rows); // We assume the first row will be the headings, so we slice it off + $column_headings = array_shift($rows); // We assume the first row will be the headings, so we slice it off $column_headings = array_map(function ($heading) { // Clean the headings a bit return trim(strtoupper($heading)); }, $column_headings); - foreach ($first_sheet_rows as $row) { + foreach ($rows as $row) { // First, trim values in case there are any stray spaces $row = array_map('trim', $row); @@ -930,6 +927,9 @@ class FluidbookCompiler extends Base implements CompilerInterface $this->config->cartEmailSubject = $extra['email_subject'] ?? 'Récapitulatif de votre commande CFOC'; } + /** + * @throws \Exception + */ public function writeBastideCart() { @@ -937,10 +937,6 @@ class FluidbookCompiler extends Base implements CompilerInterface $this->addJsLib('bastide', 'js/libs/fluidbook/cart/fluidbook.cart.bastide.js'); - // NOTE: $this->config->basketReferences initially contains the uploaded filename and if we don't - // modify it, the writeCartConfig() function will replace it with the extracted data from the spreadsheet 🤐 - // It would be much better if the extracted data had its own variable but it would break too many things to - // change it now, so we'll follow the same approach. if (!empty($this->config->basketReferences)) { if (file_exists($this->config->basketReferences) || Url::isDistant($this->config->basketReferences)) { @@ -953,16 +949,15 @@ class FluidbookCompiler extends Base implements CompilerInterface $references = []; if (file_exists($referencesFile) || Url::isDistant($referencesFile)) { - $raw_data = ExcelToArray::excelToArray($referencesFile); - $first_sheet_rows = reset($raw_data); // First sheet's data will be returned since it's the first array element + $rows = ExcelToArray::excelToArrayFirstSheet($referencesFile); // Expected headings are: n° page, Chapitre, Article Code, Article, Conditionnement - $column_headings = array_shift($first_sheet_rows); // We assume the first row will be the headings, so we slice it off + $column_headings = array_shift($rows); // We assume the first row will be the headings, so we slice it off $column_headings = array_map(function ($heading) { // Normalise the headings, removing extra spaces and line breaks return trim(strtoupper(preg_replace('/\s+/', ' ', $heading))); }, $column_headings); - foreach ($first_sheet_rows as $row) { + foreach ($rows as $row) { // First, trim values in case there are any stray spaces $row = array_map('trim', $row); @@ -981,6 +976,29 @@ class FluidbookCompiler extends Base implements CompilerInterface } $this->config->basketReferences = $references; + + // Allow individual Fluidbooks to override the columns shown in the cart + $extra = Link::parseExtras($this->config->cartExtraSettings, true); + + if (!empty($extra['cart_columns'])) { + // In the "Paramètres panier" field (cartExtraSettings), the cart columns can be defined in this format: + // cart_columns=XLS COL NAME|Display name,XLS COL 2|Display name 2 + // This setting needs to be trimmed and converted into an associative array with column_name => display_name + // Split by commas, then by pipes | + $columns = array_map(function ($heading) { + return explode('|', $heading); + }, explode(',', $extra['cart_columns'])); + $processed_columns = []; + foreach ($columns as $column) { + $processed_columns[strtoupper(trim($column[0]))] = trim($column[1] ?? ''); + } + + // Ensure that special QUANTITY and DELETE columns are present (see getColumns() in fluidbook.cart.bastide.js) + $processed_columns['QUANTITY'] = $processed_columns['QUANTITY'] ?? 'Quantité'; + $processed_columns['DELETE'] = ''; + + $this->config->cartColumns = $processed_columns; + } } public function writeCartConfig() @@ -1049,7 +1067,7 @@ class FluidbookCompiler extends Base implements CompilerInterface } } - if ($this->config->basketReferences) { + if ($this->config->basketReferences && is_string($this->config->basketReferences)) { if (file_exists($this->config->basketReferences) || Url::isDistant($this->config->basketReferences)) { $referencesFile = $this->config->basketReferences; } else { diff --git a/app/Models/FluidbookDocument.php b/app/Models/FluidbookDocument.php index 42f9acf81..cf0bd1462 100644 --- a/app/Models/FluidbookDocument.php +++ b/app/Models/FluidbookDocument.php @@ -455,35 +455,38 @@ class FluidbookDocument extends ToolboxModel protected function detectPageDifferences($pagesInfos) { + return false; // Vérifie si la cropbox et la trimbox sont identiques pour toutes les pages - $difference = false; - foreach ($pagesInfos->pdf_data['page'] as $page => $infos) { - if (!isset($infos['crop']) || !isset($infos['crop'])) { - continue; - } - if ($infos['crop'] != $infos['trim']) { - $difference = true; - } - } - if (!$difference) { - return false; - } - // Vérifie si la trimbox définie toutes les pages de la même taille - $heights = array(); - $widths = array(); - foreach ($pagesInfos->pdf_data['page'] as $page => $infos) { - $heights[] = round($infos['trim']->height); - $widths[] = round($infos['trim']->width); - } - $heights = array_unique($heights); - $widths = array_unique($widths); - if (count($heights) == 1 && count($widths) == 1) { - $this->autocrop = 'trim'; - $this->manualcrop = false; - } else { - $this->autocrop = false; - $this->manualcrop = true; - } +// $difference = false; +// foreach ($pagesInfos->pdf_data['page'] as $page => $infos) { +// if (!isset($infos['crop']) || !isset($infos['crop'])) { +// continue; +// } +// if ($infos['crop'] != $infos['trim']) { +// $difference = true; +// } +// } +// if (!$difference) { +// return false; +// } +// // Vérifie si la trimbox définie toutes les pages de la même taille +// $heights = array(); +// $widths = array(); +// foreach ($pagesInfos->pdf_data['page'] as $page => $infos) { +// if (isset($infos['trim']['height'])) { +// $heights[] = round($infos['trim']['height']); +// $widths[] = round($infos['trim']['width']); +// } +// } +// $heights = array_unique($heights); +// $widths = array_unique($widths); +// if (count($heights) <= 1 && count($widths) <= 1) { +// $this->autocrop = 'trim'; +// $this->manualcrop = false; +// } else { +// $this->autocrop = false; +// $this->manualcrop = true; +// } } protected function detectSpreads($pagesInfos) -- 2.39.5