From: Johan Cwiklinski Date: Sun, 9 Feb 2020 11:13:36 +0000 (+0100) Subject: Factorize dynamic translations handling X-Git-Tag: 0.9.3.1~1^2~2 X-Git-Url: https://git.agnieray.net/?a=commitdiff_plain;h=e029b7d77db7fcedf8cfa5354a861d9349cac334;p=galette.git Factorize dynamic translations handling --- diff --git a/galette/includes/routes/management.routes.php b/galette/includes/routes/management.routes.php index a2f6b369a..f2d57c41f 100644 --- a/galette/includes/routes/management.routes.php +++ b/galette/includes/routes/management.routes.php @@ -3174,6 +3174,16 @@ $app->post( ); } + $warning_detected = $ptype->getWarnings(); + if (count($warning_detected)) { + foreach ($warning_detected as $warning) { + $this->flash->addMessage( + 'warning_detected', + $warning + ); + } + } + return $response ->withStatus(301) ->withHeader('Location', $this->router->pathFor('paymentTypes')); @@ -3263,6 +3273,16 @@ $app->post( $e->getMessage() ); } + } finally { + $warning_detected = $ptype->getWarnings(); + if (count($warning_detected)) { + foreach ($warning_detected as $warning) { + $this->flash->addMessage( + 'warning_detected', + $warning + ); + } + } } } @@ -3315,6 +3335,16 @@ $app->post( $ptype->name = $post['name']; $res = $ptype->store(); + $warning_detected = $ptype->getWarnings(); + if (count($warning_detected)) { + foreach ($warning_detected as $warning) { + $this->flash->addMessage( + 'warning_detected', + $warning + ); + } + } + if (!$res) { $this->flash->addMessage( 'error_detected', diff --git a/galette/lib/Galette/DynamicFields/DynamicField.php b/galette/lib/Galette/DynamicFields/DynamicField.php index 9cb68bbb7..bd41d022c 100644 --- a/galette/lib/Galette/DynamicFields/DynamicField.php +++ b/galette/lib/Galette/DynamicFields/DynamicField.php @@ -41,6 +41,7 @@ use Analog\Analog; use Galette\Core\Db; use Galette\Entity\DynamicFieldsHandle; use Galette\Entity\TranslatableTrait; +use Galette\Entity\I18nTrait; use Zend\Db\Sql\Expression; use Zend\Db\Sql\Predicate\Expression as PredicateExpression; @@ -60,6 +61,7 @@ use Zend\Db\Sql\Predicate\Expression as PredicateExpression; abstract class DynamicField { use TranslatableTrait; + use I18nTrait; const TABLE = 'field_types'; const PK = 'field_id'; @@ -109,7 +111,6 @@ abstract class DynamicField protected $form; protected $errors; - protected $warnings; protected $zdb; @@ -689,23 +690,8 @@ abstract class DynamicField } if ($this->old_name !== null) { - $deleted = \deleteDynamicTranslation($this->old_name); - if ($deleted === false) { - $this->warnings[] = str_replace( - '%field', - $this->old_name, - _T('Unable to remove old dynamic translation for %field :(') - ); - } - - $added = \addDynamicTranslation($this->name); - if ($added === false) { - $this->warnings[] = str_replace( - '%field', - $this->name, - _T('Unable to add dynamic translation for %field :(') - ); - } + $this->deleteTranslation($this->old_name); + $this->addTranslation($this->name); } try { @@ -747,14 +733,7 @@ abstract class DynamicField } if ($this->name != '') { - $translated = \addDynamicTranslation($this->name); - if (!$translated) { - $this->warnings[] = str_replace( - '%field', - $this->name, - _T('Unable to add dynamic translation for %field :(') - ); - } + $this->addTranslation($this->name); } } } catch (Exception $e) { @@ -1003,7 +982,7 @@ abstract class DynamicField \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE ); } - \deleteDynamicTranslation($this->name); + $this->deleteTranslation($this->name); $this->zdb->connection->commit(); return true; diff --git a/galette/lib/Galette/Entity/I18nTrait.php b/galette/lib/Galette/Entity/I18nTrait.php index ce6f30ec6..9bc524d7c 100644 --- a/galette/lib/Galette/Entity/I18nTrait.php +++ b/galette/lib/Galette/Entity/I18nTrait.php @@ -56,6 +56,8 @@ use Zend\Db\Sql\Expression; trait I18nTrait { + protected $warnings = []; + /** * Add a translation stored in the database * @@ -97,7 +99,6 @@ trait I18nTrait Analog::INFO ); - $where = array(); $owhere = $select->where; $update = $this->zdb->update(L10n::TABLE); @@ -128,7 +129,14 @@ trait I18nTrait $text_orig . '` | ' . $e->getMessage(), Analog::ERROR ); - throw $e; + + $this->warnings[] = str_replace( + '%field', + $text_orig, + _T('Unable to add dynamic translation for %field :(') + ); + + return false; } } @@ -141,7 +149,7 @@ trait I18nTrait * * @return boolean */ - protected function updateDynamicTranslation($text_orig, $text_locale, $text_trans) + protected function updateTranslation($text_orig, $text_locale, $text_trans) { try { //check if translation already exists @@ -167,7 +175,6 @@ trait I18nTrait ); if ($exists) { - $where = array(); $owhere = $select->where; $update = $this->zdb->update(L10n::TABLE); @@ -188,6 +195,13 @@ trait I18nTrait $text_orig . '` | ' . $e->getMessage(), Analog::ERROR ); + + $this->warnings[] = str_replace( + '%field', + $text_orig, + _T('Unable to update dynamic translation for %field :(') + ); + return false; } } @@ -201,8 +215,6 @@ trait I18nTrait */ protected function deleteTranslation($text_orig) { - global $i18n; - try { $delete = $this->zdb->delete(L10n::TABLE); $delete->where( @@ -215,11 +227,17 @@ trait I18nTrait } catch (Exception $e) { Analog::log( 'An error occurred deleting dynamic translation for `' . - $text_orig . '` (lang `' . $lang->getLongID() . '`) | ' . - $e->getMessage(), + $text_orig . ' | ' . $e->getMessage(), Analog::ERROR ); - throw $e; + + $this->warnings[] = str_replace( + '%field', + $text_orig, + _T('Unable to remove old dynamic translation for %field :(') + ); + + return false; } }