]> _ Git - grandvision-ranking.git/commitdiff
wip #4406 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 20 Apr 2021 19:22:45 +0000 (21:22 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 20 Apr 2021 19:22:45 +0000 (21:22 +0200)
app/Http/Controllers/Controller.php
app/Models/FeedbackProcess.php
resources/css/app.less
resources/js/app.js
resources/views/welcome.blade.php
routes/web.php

index fb9547c4bbaead8a0a487c78cea82070c34abb0f..205b7fd8b0007d27d71b7b0f48e5793f55ff03e0 100644 (file)
@@ -17,15 +17,14 @@ class Controller extends BaseController
     public function process()
     {
         $process = new FeedbackProcess(request()->get('_session'));
-        $res=$process->process();
-
-        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($res);
-        $tmp = Files::tempnam().".xlsx";
-        $writer->setPreCalculateFormulas(false);
-        $writer->save($tmp);
+        $process->process();
+        return response()->json(['ok' => true]);
+    }
 
-        $res= response()->download($tmp, 'res.xlsx', ['content-type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'])->deleteFileAfterSend();
-        return $res;
+    public function download()
+    {
+        $process = new FeedbackProcess(request()->get('session'));
+        return response()->download($process->getPath() . '/res.xlsx', $process->getFinalFilename(), ['content-type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
     }
 
     public function upload()
index c8b2dc52e21b47b82e2ca8ad853d108b03b2777e..3d1f515086d81e47305c997b6badc84b8567dc5d 100644 (file)
@@ -3,11 +3,13 @@
 
 namespace App\Models;
 
+use Cubist\Util\Files\Files;
 use Cubist\Util\PHP;
 use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
 use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
 use PhpOffice\PhpSpreadsheet\Style\Fill;
 use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
+use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
 use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
 use Symfony\Component\HttpFoundation\File\UploadedFile;
 
@@ -15,6 +17,7 @@ class FeedbackProcess
 {
     protected $_path;
     protected $_template;
+    protected $_templateName;
     protected $_feedbacks = [];
 
     public function __construct($id)
@@ -36,7 +39,9 @@ class FeedbackProcess
         if (file_exists($jsonFile)) {
             $json = json_decode(file_get_contents($jsonFile), true);
             $this->setTemplate($json['template']);
+            $this->setTemplateName($json['templateName']);
             $this->setFeedbacks($json['feedbacks']);
+
         }
 
     }
@@ -55,6 +60,7 @@ class FeedbackProcess
         $target = $uploadedFile->move($this->getPath());
 
         if ($name === 'template') {
+            $this->setTemplateName($uploadedFile->getClientOriginalName());
             $this->setTemplate($target->getPathname());
         } else if ($name === 'feedbacks') {
             $this->addFeedback($target->getPathname());
@@ -114,6 +120,22 @@ class FeedbackProcess
         $this->_feedbacks[] = $feedback;
     }
 
+    /**
+     * @return mixed
+     */
+    public function getTemplateName()
+    {
+        return $this->_templateName;
+    }
+
+    /**
+     * @param mixed $templateName
+     */
+    public function setTemplateName($templateName): void
+    {
+        $this->_templateName = $templateName;
+    }
+
     public function __destruct()
     {
         $this->saveSession();
@@ -124,7 +146,7 @@ class FeedbackProcess
      */
     public function saveSession()
     {
-        file_put_contents($this->_getSessionFile(), json_encode(['template' => $this->getTemplate(), 'feedbacks' => $this->getFeedbacks()], JSON_THROW_ON_ERROR));
+        file_put_contents($this->_getSessionFile(), json_encode(['template' => $this->getTemplate(), 'templateName' => $this->getTemplateName(), 'feedbacks' => $this->getFeedbacks()], JSON_THROW_ON_ERROR));
     }
 
     public function process()
@@ -158,7 +180,11 @@ class FeedbackProcess
         }
         // Make first sheet active
         $template->setActiveSheetIndex(0);
-        return $template;
+
+        $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($template);
+        $res = $this->getPath() . "/res.xlsx";
+        $writer->setPreCalculateFormulas(false);
+        $writer->save($res);
     }
 
     /**
@@ -251,6 +277,8 @@ class FeedbackProcess
             }
         }
 
+        // Define score coef
+
         $coefs = [
             'YA' => 100000,
             'YB' => 10000,
@@ -259,6 +287,7 @@ class FeedbackProcess
             'NB' => 10,
             'NC' => 1];
 
+        //Compute score by line
         $scores = [];
         foreach ($rankingData as $row => $data) {
             $scores[$row] = 0;
@@ -267,6 +296,8 @@ class FeedbackProcess
                 $scores[$row] += $s;
             }
         }
+
+        // Sort score to define ranks
         arsort($scores);
         $rank = 1;
         foreach ($scores as $row => $score) {
@@ -275,5 +306,34 @@ class FeedbackProcess
             $rank++;
         }
 
+        // Fix images
+        foreach ($templateSheet->getDrawingCollection() as $drawing) {
+            if ($drawing instanceof Drawing) {
+                $dim = getimagesize($drawing->getPath());
+                $drawing->setResizeProportional(false);
+                $drawing->setWidth($dim[0]);
+                $drawing->setHeight($dim[1]);
+                $drawing->setResizeProportional(true);
+                $drawing->setWidth(300);
+            } else {
+                $drawing->setResizeProportional(false);
+                $drawing->setWidth(300);
+                $drawing->setHeight(80);
+                $drawing->setResizeProportional(true);
+            }
+            $drawing->setOffsetX(0);
+            $drawing->setOffsetY(0);
+
+        }
+
+    }
+
+    public function getFinalFilename()
+    {
+        $e = explode('/', $this->getTemplateName());
+        $f = array_pop($e);
+        $e = explode('.', $f);
+        $ext = array_pop($e);
+        return implode('.', $e) . '-rank.' . $ext;
     }
 }
index d91a599cc7bf2d1b64f9dc018bdf1818a23ea142..7ff00a92be9f27a74f816fe6fbbac2180faa3e55 100644 (file)
@@ -6,7 +6,7 @@ body {
     color: @blue;
     background-color: #eee;
     font-weight: 300;
-    font-family: Roboto;
+    font-family: Roboto, sans-serif;
 }
 
 main {
@@ -48,7 +48,7 @@ form {
         }
     }
 
-    input[type="submit"] {
+    .submit {
         display: block;
         font-size: 20px;
         padding: 12px 30px;
@@ -57,9 +57,17 @@ form {
         border-radius: 5px;
         cursor: pointer;
         margin: 50px auto;
-        width: 200px;
+        width: 400px;
         font-weight: 300;
         font-family: Roboto;
+        opacity: 0;
+        pointer-events: none;
+        transition: opacity 300ms;
+
+        &.visible{
+            opacity: 1;
+            pointer-events: auto;
+        }
     }
 }
 
index ea421d607e93e48b70d5d03a5f832dc3a8cd4167..ad8a7224da663475e7a789dda247fc4c44b6cd21 100644 (file)
@@ -23,9 +23,13 @@ var removeFile = 'Retirer ce fichier';
 var token = $('input[name="_token"]').attr('value');
 var session = $('input[name="_session"]').attr('value');
 
+var hasTemplate = false;
+var hasFeedback = false;
+
 var myDropzone = new Dropzone(dropzone1, {
     dictDefaultMessage: message,
     dictRemoveFile: removeFile,
+    acceptedFiles: '.xlsx',
     url: '/upload',
     autoProcessQueue: true,
     autoDiscover: false,
@@ -37,11 +41,17 @@ var myDropzone = new Dropzone(dropzone1, {
         formData.append("_token", token);
         formData.append("_session", session);
     },
+
+});
+myDropzone.on("success", function (file) {
+    hasTemplate = true;
+    checkFiles();
 });
 dropzones.push(myDropzone)
 myDropzone = new Dropzone(dropzone2, {
     dictDefaultMessage: message,
     url: '/upload',
+    acceptedFiles: '.xlsx',
     autoProcessQueue: true,
     autoDiscover: false,
     uploadMultiple: false,
@@ -53,13 +63,29 @@ myDropzone = new Dropzone(dropzone2, {
         formData.append("_session", session);
     },
 });
+myDropzone.on("success", function (file) {
+    hasFeedback = true;
+    checkFiles();
+});
 dropzones.push(myDropzone)
 
+function checkFiles() {
+    if (hasTemplate && hasFeedback) {
+        $('.submit').addClass('visible');
+    }
+}
+
 $(function () {
     $("body").append('<div id="wait"></div>');
-    $(document).on('click', 'input[type="submit"]', function () {
+    $(document).on('click', '.submit', function () {
         $("#wait").show();
-        return true;
+        $(this).attr('value', 'Traitement en cours');
+        $.post('/process', {'_token': token, '_session': session}, function (data) {
+            $(".submit").attr('value', 'Envoyer');
+            $("#wait").hide();
+            window.location = '/download?session=' + session;
+        });
+        return false;
     });
 });
 
index b7b70d1475b4b5d93e6b411a8c2d0b06b6e28150..a48d8e61bc3dc23f2f16375872bd7ba98563897c 100644 (file)
     <img src="/images/logoGV.png" alt="GrandVision" class="logo"/>
     <h1>Traitement des feedbacks</h1>
 
-    <form action="/process" method="post">
+    <form action="/process" method="post" target="_blank">
         @csrf
         <input type="hidden" name="_session" value="{{ \Illuminate\Support\Str::random(12) }}" >
         <label>
             <span>Feedbacks</span>
             <div id="feedbacks" class="dropzone"></div>
         </label>
-        <input type="submit">
+        <input type="submit" class="submit">
     </form>
 </main>
 <script src="js/app.js"></script>
index 6520c307ed5a728462d43596ac594fedb92c86f1..de449f6d120fd3a1ca27140599c6158574e56459 100644 (file)
@@ -18,4 +18,5 @@ Route::get('/', function () {
 });
 
 Route::post('/process', [\App\Http\Controllers\Controller::class, 'process'])->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class);
+Route::get('/download', [\App\Http\Controllers\Controller::class, 'download']);
 Route::post('/upload', [\App\Http\Controllers\Controller::class, 'upload']);