]> _ Git - fluidbook-toolbox.git/commitdiff
wip #5819 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 23 Mar 2023 13:36:26 +0000 (14:36 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 23 Mar 2023 13:36:26 +0000 (14:36 +0100)
app/Jobs/FluidbookCompiler.php
app/Models/FluidbookDocument.php

index 2a4ec48d2ca07ee89dcf14dd861498f0bc2609a2..512bf968b30d4195c6c6e49d4aa56888869b5132 100644 (file)
@@ -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 {
index 42f9acf81341664c9672f01856a2cb114d469aa0..cf0bd14623c9984b41efbc846d000d216901d7f7 100644 (file)
@@ -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)