From 94a991eb287e2255db33ebaafc691afcb909786a Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 6 Oct 2025 21:16:03 +0200 Subject: [PATCH] Fix vehicle picture upload Galette 1.2.0 compatibility --- lib/GaletteAuto/Auto.php | 32 ++++++++-------------- lib/GaletteAuto/Controllers/Controller.php | 13 +++++++++ 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/lib/GaletteAuto/Auto.php b/lib/GaletteAuto/Auto.php index e81ee23..a8b6d48 100644 --- a/lib/GaletteAuto/Auto.php +++ b/lib/GaletteAuto/Auto.php @@ -30,6 +30,7 @@ use Galette\Core\Plugins; use Galette\Entity\Adherent; use Laminas\Db\Sql\Expression; +use Psr\Http\Message\UploadedFileInterface; /** * Automobile Transmissions class for galette Auto plugin @@ -386,10 +387,6 @@ public function store(bool $new = false): bool strtoupper($this->name) ); $this->history->load((int)$this->id); - - //handle picture for newly added cars - $this->picture = new Picture($this->plugins, (int)$this->id); - $this->handlePicture(); } else { $hist->add(_T("Fail to add new car.", "auto")); throw new \Exception( @@ -799,11 +796,6 @@ public function check(array $post): bool }//switch }//foreach - if (isset($this->id)) { - //handle picture for updated cars - $this->handlePicture(); - } - //delete photo if (isset($post['del_photo'])) { if (!$this->picture->delete()) { @@ -844,20 +836,18 @@ public function getRequired(): array /** * Handle car picture upload * - * @return void + * @param array $files Files sent + * + * @return bool */ - private function handlePicture(): void + public function handleFiles(array $files): bool { - // picture upload - if (isset($_FILES['photo'])) { - if ($_FILES['photo']['tmp_name'] != '') { - if (is_uploaded_file($_FILES['photo']['tmp_name'])) { - $res = $this->picture->store($_FILES['photo']); - if ($res < 0) { - $this->errors[] = $this->picture->getErrorMessage($res); - } - } - } + $this->errors = []; + $this->picture = new Picture($this->plugins, (int)$this->id); + if (!$this->picture->upload(request_files: $files, key: 'photo')) { + $this->errors = array_merge($this->errors, $this->picture->uploadErrors()); } + + return !count($this->errors); } } diff --git a/lib/GaletteAuto/Controllers/Controller.php b/lib/GaletteAuto/Controllers/Controller.php index 4d27c19..5663af2 100644 --- a/lib/GaletteAuto/Controllers/Controller.php +++ b/lib/GaletteAuto/Controllers/Controller.php @@ -420,6 +420,7 @@ public function doAddEditVehicle(Request $request, Response $response, string $a // initialize warnings $error_detected = []; + $warning_detected = []; $success_detected = []; if (isset($post['id_adh'])) { @@ -447,6 +448,9 @@ public function doAddEditVehicle(Request $request, Response $response, string $a if (!$this->checkAclsFor($response, $id_adh, false) || $this->login->id == $id_adh) { $route = $this->routeparser->urlFor('myVehiclesList'); } + if (!$auto->handleFiles($request->getUploadedFiles())) { + $warning_detected = $auto->getErrors(); + } } } @@ -467,6 +471,15 @@ public function doAddEditVehicle(Request $request, Response $response, string $a } } + if (count($warning_detected) > 0) { + foreach ($warning_detected as $warning) { + $this->flash->addMessage( + 'warning_detected', + $warning + ); + } + } + if (count($success_detected) > 0) { foreach ($success_detected as $success) { $this->flash->addMessage(