From d462d4e57e8353d2eefec676e230c863dfbd7290 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Tue, 28 Nov 2023 21:00:58 +0100 Subject: [PATCH 1/3] Fix redirection when model add fails --- .../Controllers/Crud/ModelsController.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/GaletteAuto/Controllers/Crud/ModelsController.php b/lib/GaletteAuto/Controllers/Crud/ModelsController.php index 3b2ca39..d2848da 100644 --- a/lib/GaletteAuto/Controllers/Crud/ModelsController.php +++ b/lib/GaletteAuto/Controllers/Crud/ModelsController.php @@ -304,14 +304,15 @@ public function doEdit(Request $request, Response $response, int $id = null, $ac $this->session->auto_model = $post; if (!$is_new) { $id = $post[Model::PK]; + $route = $this->routeparser->urlFor( + 'modelEdit', + [ + 'id' => $id + ] + ); + } else { + $route = $this->routeparser->urlFor('modelAdd'); } - $route = $this->routeparser->urlFor( - 'modelEdit', - [ - 'action' => $action, - 'id' => $id - ] - ); foreach ($error_detected as $error) { $this->flash->addMessage( From 76b10c3596687048cc066008bd7f7f8867ebd6db Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Tue, 28 Nov 2023 21:19:56 +0100 Subject: [PATCH 2/3] Fix used models and properties removal closes #1745 --- lib/GaletteAuto/AbstractObject.php | 2 +- .../Controllers/Crud/ModelsController.php | 17 ++++++- .../Controllers/Crud/PropertiesController.php | 51 ++++++++++--------- lib/GaletteAuto/Model.php | 2 +- 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/lib/GaletteAuto/AbstractObject.php b/lib/GaletteAuto/AbstractObject.php index 66f165b..44f5198 100644 --- a/lib/GaletteAuto/AbstractObject.php +++ b/lib/GaletteAuto/AbstractObject.php @@ -213,7 +213,7 @@ public function delete($ids) ' from ids `' . implode(' - ', $ids) . '` | ' . $e->getMessage(), Analog::WARNING ); - return false; + throw $e; } } diff --git a/lib/GaletteAuto/Controllers/Crud/ModelsController.php b/lib/GaletteAuto/Controllers/Crud/ModelsController.php index d2848da..7aa1e86 100644 --- a/lib/GaletteAuto/Controllers/Crud/ModelsController.php +++ b/lib/GaletteAuto/Controllers/Crud/ModelsController.php @@ -402,7 +402,22 @@ protected function doDelete(array $args, array $post): bool $ids = $post['id']; } - return $model->delete($ids); + try { + return $model->delete($ids); + } catch (\Throwable $e) { + if ($this->zdb->isForeignKeyException($e)) { + $this->flash->addMessage( + 'error_detected', + _T("This model is used by one or more vehicles, it cannot be deleted.", "auto") + ); + } else { + $this->flash->addMessage( + 'error_detected', + _T("An error occurred while deleting model.", "auto") + ); + } + return false; + } } // /CRUD - Delete diff --git a/lib/GaletteAuto/Controllers/Crud/PropertiesController.php b/lib/GaletteAuto/Controllers/Crud/PropertiesController.php index cf34048..1255e7f 100644 --- a/lib/GaletteAuto/Controllers/Crud/PropertiesController.php +++ b/lib/GaletteAuto/Controllers/Crud/PropertiesController.php @@ -56,7 +56,7 @@ * Galette Auto plugin controller for properties (brands, models, colors, ...) * * @category Plugins - * @name Autos + * @name PropertiesList * @package GaletteAuto * @author Johan Cwiklinski * @copyright 2017-2023 The Galette Team @@ -609,37 +609,42 @@ public function doRemoveProperty(Request $request, Response $response, string $p $ids = $post['id']; } - $model = new Model($this->zdb); - $del = $model->delete($ids); - $classname = AbstractObject::getClassForPropName($property); $object = new $classname($this->zdb); - $del = $object->delete($ids); - - if ($del !== true) { - $error_detected = str_replace( - '%property', - $object->getFieldLabel(), - _T('An error occurred trying to remove %property :/', 'auto') - ); - $this->flash->addMessage( - 'error_detected', - $error_detected - ); - } else { - $success_detected = str_replace( - ['%count', '%property'], - [count($ids), $object->getFieldLabel()], - _T("%count %property have been successfully deleted.", "auto") - ); + try { + $object->delete($ids); $this->flash->addMessage( 'success_detected', - $success_detected + str_replace( + ['%count', '%property'], + [count($ids), $object->getFieldLabel()], + _T("%count %property have been successfully deleted.", "auto") + ) ); $success = true; + } catch (\Throwable $e) { + if ($this->zdb->isForeignKeyException($e)) { + $this->flash->addMessage( + 'error_detected', + str_replace( + '%property', + mb_strtolower($object->getFieldLabel()), + _T("This %property is used by one or more vehicles, it cannot be deleted.", "auto") + ) + ); + } else { + $this->flash->addMessage( + 'error_detected', + str_replace( + '%property', + $object->getFieldLabel(), + _T('An error occurred trying to remove %property :/', 'auto') + ) + ); + } } } diff --git a/lib/GaletteAuto/Model.php b/lib/GaletteAuto/Model.php index a2f09b3..41d699a 100644 --- a/lib/GaletteAuto/Model.php +++ b/lib/GaletteAuto/Model.php @@ -197,7 +197,7 @@ public function delete($ids) implode(' - ', $ids) . '` | ' . $e->getMessage(), Analog::WARNING ); - return false; + throw $e; } } From 2ab268df73c43335852516167d41a281b0471018 Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Tue, 28 Nov 2023 21:21:14 +0100 Subject: [PATCH 3/3] CI on PHP 8.3 --- .github/workflows/ci-linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-linux.yml b/.github/workflows/ci-linux.yml index 2b535a9..2ec33ec 100644 --- a/.github/workflows/ci-linux.yml +++ b/.github/workflows/ci-linux.yml @@ -16,7 +16,7 @@ jobs: strategy: matrix: - php-versions: [ '8.1', '8.2' ] + php-versions: [ '8.1', '8.3' ] coverage: [none] fail-fast: false