]> _ Git - grandvision-ranking.git/commitdiff
wait #4462 @6
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 1 Jun 2021 15:05:23 +0000 (17:05 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 1 Jun 2021 15:05:23 +0000 (17:05 +0200)
app/Models/FeedbackProcess.php

index e657280be719c60ee5d3b7f967c8f434451563d0..49281fef80b6e296ff7f1ddab14454902a4a7e53 100644 (file)
@@ -20,6 +20,9 @@ class FeedbackProcess
     protected $_template;
     protected $_templateName;
     protected $_feedbacks = [];
+    protected static $_maxCol = 26 * 4;
+    protected static $_absoluteMaxCol = 16384;
+    protected static $_pictureNames = ['picture references', 'pictures references', 'picture reference', 'pictures reference'];
 
     public function __construct($id)
     {
@@ -168,6 +171,8 @@ class FeedbackProcess
                     $feedbackSheets[$name] = [];
                 }
                 $feedbackSheets[$name][] = $sheet;
+
+                $sheet->removeColumnByIndex(self::$_maxCol, self::$_absoluteMaxCol - self::$_maxCol);
             }
         }
 
@@ -183,6 +188,7 @@ class FeedbackProcess
                 continue;
             }
             $name = $this->_getSheetName($sheet);
+            $sheet->removeColumnByIndex(self::$_maxCol, self::$_absoluteMaxCol - self::$_maxCol);
             $this->_processSheet($sheet, $feedbackSheets[$name] ?? []);
         }
         // Make first sheet active
@@ -199,7 +205,18 @@ class FeedbackProcess
      */
     protected function _getSheetName($sheet)
     {
-        return mb_strtolower(trim($sheet->getCellByColumnAndRow(1, 1)->getValue()));
+        $highestCol = 300;
+        for ($j = 1; $j <= $highestCol; $j++) {
+            $v = $sheet->getCellByColumnAndRow($j, 1)->getValue();
+            if (null === $v) {
+                continue;
+            }
+            $v = trim($v);
+            if ($v === '') {
+                continue;
+            }
+            return mb_strtolower($v);
+        }
     }
 
     /**
@@ -210,10 +227,22 @@ class FeedbackProcess
     protected function _getHeadersRow($sheet)
     {
         $highestRow = $sheet->getHighestRow();
+        $highestCol = self::$_maxCol;
         for ($i = 1; $i <= $highestRow; $i++) {
-            if ($sheet->getCellByColumnAndRow(1, $i)->getValue() === 'Picture References') {
-                $headersRow = $i;
-                break;
+            for ($j = 0; $j <= $highestCol; $j++) {
+                try {
+                    $v = $sheet->getCellByColumnAndRow($j, $i)->getValue();
+                    if (null === $v) {
+                        continue;
+                    }
+                    $v = mb_strtolower(trim($v));
+                    if (in_array($v, self::$_pictureNames)) {
+                        $headersRow = $j;
+                        break 2;
+                    }
+                } catch (\Exception $e) {
+                    break;
+                }
             }
         }
         if (!isset($headersRow)) {
@@ -224,19 +253,7 @@ class FeedbackProcess
 
     protected function _getMaxCol($sheet)
     {
-        $hr = $this->_getHeadersRow($sheet);
-        $maxcol = Coordinate::columnIndexFromString($sheet->getHighestColumn());
-        $max = 0;
-        $values = [];
-        for ($i = 15; $i <= $maxcol; $i++) {
-            $v = $sheet->getCellByColumnAndRow($i, $hr)->getValue();
-            $values[] = $v;
-            if ($v === null || trim($v) === '') {
-                break;
-            }
-            $max = max($i, $max);
-        }
-        return $max;
+        return self::$_maxCol;
     }
 
     /**
@@ -252,11 +269,15 @@ class FeedbackProcess
         if (null === $headersRow) {
             return;
         }
-        $templateMaxCol = $this->_getMaxCol($templateSheet);
 
+        $templateMaxCol = $this->_getMaxCol($templateSheet);
+        $hr = $this->_getHeadersRow($templateSheet);
+        $templateStartCol = $this->_getStartCol($templateSheet, $hr);
         // Add ranking column
-        $templateSheet->insertNewColumnBefore('O', 2);
-        $h = $templateSheet->getCell('O' . $headersRow);
+
+        $templateSheet->insertNewColumnBefore(Coordinate::stringFromColumnIndex($templateStartCol ), 2);
+
+        $h = $templateSheet->getCell(Coordinate::stringFromColumnIndex($templateStartCol ) . $headersRow);
         // Style it
         $h->getStyle()->getFill()->setFillType(Fill::FILL_SOLID)
             ->getStartColor()
@@ -264,7 +285,7 @@ class FeedbackProcess
         // Set the caption
         $h->setValue('Rank');
         // Score column
-        $h = $templateSheet->getCell('P' . $headersRow);
+        $h = $templateSheet->getCell(Coordinate::stringFromColumnIndex($templateStartCol + 1) . $headersRow);
         // Style it
         $h->getStyle()->getFill()->setFillType(Fill::FILL_SOLID)
             ->getStartColor()
@@ -273,14 +294,14 @@ class FeedbackProcess
         $h->setValue('Global score');
 
         // Get data from feedbacks
-        $startCol = 15;
-        $eanCol = 4;
         $merged = [];
         $feedbackId = 0;
         foreach ($feedbacks as $feedback) {
             $hr = $this->_getHeadersRow($feedback);
+            $eanCol = $this->_getEANCol($feedback, $hr);
             $maxcol = $this->_getMaxCol($feedback);
             $maxrow = $feedback->getHighestRow();
+            $startCol = $this->_getStartCol($feedback, $hr);
             for ($j = $hr + 1; $j <= $maxrow; $j++) {
                 $ean = trim($feedback->getCellByColumnAndRow($eanCol, $j)->getValue());
                 if (null === $ean || $ean === '') {
@@ -306,9 +327,9 @@ class FeedbackProcess
 
         $offset = 0;
         // Add score & rank local columns
-        for ($i = $startCol; $i <= $templateMaxCol; $i += 4) {
+        for ($i = $templateStartCol; $i <= $templateMaxCol; $i += 4) {
             // Add ranking column
-            $colname = Coordinate::stringFromColumnIndex(2 + 3 + $i + $offset + 1);
+            $colname = Coordinate::stringFromColumnIndex( 2+3 + $i + $offset + 1);
             $templateSheet->insertNewColumnBefore($colname, 2);
             $h = $templateSheet->getCell($colname . $headersRow);
             // Style it
@@ -320,7 +341,7 @@ class FeedbackProcess
 
             $offset++;
 
-            $colname = Coordinate::stringFromColumnIndex(2 + 3 + $i + $offset + 1);
+            $colname = Coordinate::stringFromColumnIndex(2+3 + $i + $offset + 1);
             $h = $templateSheet->getCell($colname . $headersRow);
             // Style it
             $h->getStyle()->getFill()->setFillType(Fill::FILL_SOLID)
@@ -351,7 +372,7 @@ class FeedbackProcess
             foreach ($data as $col => $value) {
                 $i = $col % 4;
                 // Set the value
-                $templateSheet->setCellValueByColumnAndRow($startCol + 2 + $col + $offset, $row, $value);
+                $templateSheet->setCellValueByColumnAndRow($templateStartCol + 2 + $col + $offset, $row, $value);
                 // Gather data to make the ranking
                 if ($i === 0) {
                     $k = count($rankingData[$row]);
@@ -432,13 +453,13 @@ class FeedbackProcess
                 $formerScore = $score;
                 $formerRank = $rank;
             }
-            $templateSheet->setCellValueByColumnAndRow($startCol, $row, $orank);
-            $templateSheet->setCellValueByColumnAndRow($startCol + 1, $row, $score);
-            $templateSheet->getStyleByColumnAndRow($startCol, $row)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER);
-            $templateSheet->getStyleByColumnAndRow($startCol + 1, $row)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER);
+            $templateSheet->setCellValueByColumnAndRow($templateStartCol, $row, $orank);
+            $templateSheet->setCellValueByColumnAndRow($templateStartCol + 1, $row, $score);
+            $templateSheet->getStyleByColumnAndRow($templateStartCol, $row)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER);
+            $templateSheet->getStyleByColumnAndRow($templateStartCol + 1, $row)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER);
             $offset = 0;
             foreach ($detailScores[$row] as $locale => $detailScore) {
-                $col = $startCol + 1 + 5 + $offset;
+                $col = $templateStartCol + 1 + 5 + $offset;
                 $templateSheet->setCellValueByColumnAndRow($col, $row, $localeRanks[$locale][$row]);
                 $templateSheet->setCellValueByColumnAndRow($col + 1, $row, $detailScore);
                 $templateSheet->getStyleByColumnAndRow($col, $row)->getNumberFormat()->setFormatCode(NumberFormat::FORMAT_NUMBER);
@@ -484,6 +505,45 @@ class FeedbackProcess
 
     }
 
+    public function _getStartCol($sheet, $headersRow)
+    {
+        return $this->_getColContaining($sheet, $headersRow, ['yes/no', 'yes / no', 'yes /no', 'yes/ no']);
+    }
+
+    public function _getEANCol($sheet, $headersRow)
+    {
+        return $this->_getColContaining($sheet, $headersRow, ['ean', 'upc', 'ean code']);
+    }
+
+    public function _getPicturesCol($sheet, $headersRow)
+    {
+        return $this->_getColContaining($sheet, $headersRow, self::$_pictureNames);
+    }
+
+    /**
+     * @param $sheet Worksheet
+     * @param $headersRow
+     * @param array $contents
+     */
+    public function _getColContaining($sheet, $headersRow, $contents = [])
+    {
+        $maxcol = $sheet->getHighestColumn($headersRow);
+        for ($j = 1; $j <= $maxcol; $j++) {
+            $v = $sheet->getCellByColumnAndRow($j, $headersRow)->getValue();
+            if (null === $v) {
+                if ($j > 100) {
+                    return 1;
+                }
+                continue;
+            }
+            $v = mb_strtolower(trim($v));
+            if (in_array($v, $contents)) {
+                return $j;
+            }
+        }
+        return 1;
+    }
+
     public function getFinalFilename()
     {
         $e = explode('/', $this->getTemplateName());