From: Vincent Vanwaelscappel Date: Tue, 20 Apr 2021 19:22:45 +0000 (+0200) Subject: wip #4406 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=d1fd092c0cd5477462030811946d5df4d9ae38af;p=grandvision-ranking.git wip #4406 @2 --- diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index fb9547c..205b7fd 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -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() diff --git a/app/Models/FeedbackProcess.php b/app/Models/FeedbackProcess.php index c8b2dc5..3d1f515 100644 --- a/app/Models/FeedbackProcess.php +++ b/app/Models/FeedbackProcess.php @@ -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; } } diff --git a/resources/css/app.less b/resources/css/app.less index d91a599..7ff00a9 100644 --- a/resources/css/app.less +++ b/resources/css/app.less @@ -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; + } } } diff --git a/resources/js/app.js b/resources/js/app.js index ea421d6..ad8a722 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -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('
'); - $(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; }); }); diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index b7b70d1..a48d8e6 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -400,7 +400,7 @@

Traitement des feedbacks

-
+ @csrf - +
diff --git a/routes/web.php b/routes/web.php index 6520c30..de449f6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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']);