]> git.agnieray.net Git - galette.git/commitdiff
Factorize dynamic translations handling
authorJohan Cwiklinski <jcwiklinski@teclib.com>
Sun, 9 Feb 2020 11:13:36 +0000 (12:13 +0100)
committerJohan Cwiklinski <jcwiklinski@teclib.com>
Sun, 9 Feb 2020 11:25:00 +0000 (12:25 +0100)
galette/includes/routes/management.routes.php
galette/lib/Galette/DynamicFields/DynamicField.php
galette/lib/Galette/Entity/I18nTrait.php

index a2f6b369a4a6f29ec8311cab8473836e7fdd7242..f2d57c41f0d073915e9b6e65ac556975229a3703 100644 (file)
@@ -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',
index 9cb68bbb78bec120fc363baa1363437131cb0e71..bd41d022c746990b457b4915fea98ed1f5092a8e 100644 (file)
@@ -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;
index ce6f30ec607a453150e3dac8f11684eafcda95b5..9bc524d7c1382b7ac722872d727b3e82c752119a 100644 (file)
@@ -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;
         }
     }