Announcement

Collapse
No announcement yet.

Avoid duplicate entries in Multi Enum

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • telecastg
    replied
    Hello shalmaxb ,

    The error is pointing to an unexpected "{" character in line 34 of your code, I checked the code and the problem is that your are missing a closing parenthesis ")" character in the statement:

    Now is: if(!empty($additionalFunction) {

    Should be: if(!empty($additionalFunction)) {

    The "if" statement is not closed because of the missing closing parenthesis so the compiler sees the next character which is a "{" and then throws the error.

    Glad to be able to help. I normally do not get into script debugging but in your case I have seen how much time you spend helping others and I don't mind pitching in when you need it.

    Leave a comment:


  • shalmaxb
    replied
    I get this error:

    Code:
    [2022-02-02 15:05:33] ERROR: Slim Application Error Type: ParseError Code: 0 Message: syntax error, unexpected '{' File: G:\laragon\www\design\custom\Espo\Custom\Hooks\Objekte\objekte.php Line: 34

    Leave a comment:


  • shalmaxb
    replied
    Hi telecastg ,

    again thank you very much for your help. Unforunately I am not a programmer and this kind of "advanced" technique I don`t understand completely.

    Nevertheless I made my trial and error procedure (at least in the hope to learn something). But I ask you kindly for a little push in the right direction with my code.

    My entity is called Objekte, so I created the file under Espo/Custom/Hooks/Objekte/objekte.php

    The field in German is Zusatzfunktion (zusatzfunktion)
    The field in English is Additional Function (additionalFunction)

    Here is your code adapted to the real field and entity names:

    PHP Code:
    <?php

    namespace Espo\Custom\Hooks\Objekte;

    use 
    Espo\ORM\Entity;

    use 
    Espo\Core\Utils\Language;

    class 
    ObjekteHooks
    {

    public function 
    __construct(Language $language)
    {
    $this->language $language;
    }

    public function 
    beforeSave(Entity $entity, array $options=array())
    {
    $zusatzfunktion $entity->get('Zusatzfunktion');
    $additionalFunction = [
    'Datum' => 'Date',
    'Alarm' => 'Alarm',
    'Barometer' => 'Barometer',
    'Beleuchtung' => 'Light',
    'Höhenmesser' => 'Altimeter',
    'Kompass' => 'Compass',
    'Stoßsicherung' => 'Shock proof',
    'Stoßsicherung Incabloc' => 'Shock proof Incabloc',
    'Weckfunktion' => 'Alarm',
    'Weltzeitindikator' => 'Worldtime indicator'
    ];
    foreach(
    $zusatzfunktion as $zusatzfunktionValue) {
    $additionalFunction $this->language->translateOption($zusatzfunktionValue'Zusatzfunktion'$entity->getEntityType());
    (
    Line 34see error)     if(!empty($additionalFunction) {
    $additionalFunction[] = $additionalFunction;
    } else {
    $additionalFunction[] = $zusatzfunktionValue;
    }
    }
    }
    }

    Into the additionalFunction array I placed word pairs of german => english terms. But as it does not work, there must be one ore more mistakes. Help is appreciated.
    Last edited by shalmaxb; 02-02-2022, 03:08 PM.

    Leave a comment:


  • telecastg
    replied
    Hello shalmaxb

    I believe that the problem is that formula is actually a beforeSave hook so the array/push command will add a new element every time that you change and save the entity.

    I assume that the Field A multiEnum does not allow duplicate entries and you want Field B to contain one value for each Field A value, and if there is no translation then copy the same value from Filed A to Field B.

    That being the case, your logic would have to include a condition that a Field A value has not been already translated before it is added to Field B, and then you should also consider the possibility that a term was removed in Field A so it should also be removed from Field B.

    The program logic would be:

    a) Clear all values at Field B array
    a) Iterate array that contains the values for Field A
    b) For each element in Field A, find the translated value and append that value to the Field B array
    d) If there is no translated value, then copy the value from the element in Field A to the element in Field B

    This way you will always guarantee that Field B only contains an entry for each entry in Field A

    I don't use formula, so I don't know how easy or difficult would be to create that kind of logic using formula, therefore my approach would be to write a beforeSave hook (which is really what formula is anyway) and use plain vanilla PHP to implement the logic like this:

    PHP Code:
    namespace Espo\Custom\Hooks\YourEntity;

    use 
    Espo\ORM\Entity;

    use 
    Espo\Core\Utils\Language;

    class 
    YourEntityHooks
    {

        public function 
    __construct(Language $language)
        {
            
    $this->language $language;
        }

        public function 
    beforeSave(Entity $entity, array $options=array())
        {
            
    $arrayA $entity->get("fieldA");
            
    $arrayB = [];
            foreach(
    $arrayA as $aValue) {
               
    $translatedA =  $this->language->translateOption($aValue"fieldA"$entity->getEntityType());
               if(!empty(
    $translatedA) {
                   
    $arrayB[] = $translatedA;
                } else {
                    
    $arrayB[] = $aValue;
                }
            }
        }

    .
    Last edited by telecastg; 01-26-2022, 07:17 AM.

    Leave a comment:


  • shalmaxb
    started a topic Avoid duplicate entries in Multi Enum

    Avoid duplicate entries in Multi Enum

    Hi,
    I have a Multi Enum Field with some terms. In a second Multi Enum field I want these terms translated. I created a formula array/push, which fills in automatically the provided translation of the term. If there is term A-original in one language, it will fill in Term-translated in the other field.
    If I edit the record again, on saving, the formula array/push causes, that the same entry is filled in again (i.e. on every edit, I get a duplicate, triplicate .... entry).

    How can I avoid that?
Working...
X