Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revisions and Translation Issue - double json encode #20

Open
Spodnet opened this issue Feb 16, 2017 · 8 comments
Open

Revisions and Translation Issue - double json encode #20

Spodnet opened this issue Feb 16, 2017 · 8 comments

Comments

@Spodnet
Copy link

Spodnet commented Feb 16, 2017

I've been testing the revisions code with translations, and it appears to double encode and add the content as a language when a revision is made.

Change 1 From:
{"en":"Test"}

Change 1 To:
{"en":"Changed Test"}

Then undo the change:

Change 2 From:
{"en":"Changed Test"}

Change 2 To:
{"en":"{\"en\":\"Test\"}"}

This appears to happen when update() is called, the translation trait checks the field is translatable and adds the locale on. I thought possibly saving direct:

$entry->{$revision->key} = $revision->old_value; $entry->save();

would work but it doesn't seem to save correctly either

@tabacitu tabacitu added the bug Something isn't working label Apr 20, 2017
@tabacitu
Copy link
Member

Hi @Spodnet , thank you for reporting this. It seems to be the same issue as #20 so let's move the conversation there. Cheers!

@ASVVIZIT
Copy link

ASVVIZIT commented May 3, 2021

Hello. Sorry for my bad english.

Is this problem still not resolved?

On any return of the revision, there is a wrapper in the default localization

It looks like this
{"ru": "{" ru \ ": " {\\ "ru \": \\ "\\ u041e \\ u0442 \\ u0447 \\ u0451 \\ u0442 Voluptas quidem assumenda et non beatae. \\ "} "} "}

This is three times the return of the field revision

It looks like the HasTranslations component

when creating any entry in $ translatable fields

Creates a json "en" wrapper

Please help me to figure out at what stage the localization key is substituted.

@pxpm
Copy link
Contributor

pxpm commented May 4, 2021

Hello @ASVVIZIT this issue is from 2017.

Not sure what was it though. What version are you using ? Run:

php artisan backpack:version

Also when you change languages in configs clear config cache ``php artisan config:clear`.

If in the later versions 4.1 of backpack, please open a new issue, and provide steps to reproduce it.

Thanks,
Pedro

@phpust
Copy link

phpust commented Feb 23, 2022

dear @tabacitu , i changed restoreRevision function in ReviseOperation.

public function restoreRevision($id)

new code is :

public function restoreRevision($id)
    {
        $this->crud->hasAccessOrFail('revise');

        $revisionId = \Request::input('revision_id', false);
        if (! $revisionId) {
            abort(500, 'Can\'t restore revision without revision_id');
        } else {
            $entry = $this->crud->getEntryWithoutFakes($id);
            $revision = Revision::findOrFail($revisionId);

            // save current entry and revision temporary
            $this->data['entry'] = $entry;
            $this->data['revision'] = $revision; 

            // check if revision key is translatable in $entry model
            if( $this->isTranslatable($revision->key) ) {
            	$this->prepareTranslationsForRevision();

                $this->forgetLocalesThatAreNotExistInRevision();

                $this->returnForgettenLocalesThatAreNotExistInCurrentVersion();

                $this->updateAllLocalesThatAreCommonInOldAndCurrentVersion();
                
                $entry->update();
            } 
            else {
                // Update the revisioned field with the old value
                $entry->update([$revision->key => $revision->old_value]);
            }

            $this->data['entry'] = $this->crud->getEntry($id);
            $this->data['crud'] = $this->crud;
            $this->data['revisions'] = $this->crud->getRevisionsForEntry($id); // Reload revisions as they have changed

            // Rebuild the revision timeline HTML and return it to the AJAX call
            return view($this->crud->get('revise.timelineView') ?? 'revise-operation::revision_timeline', $this->data);
        }
    }

    private function isTranslatable($key)
    {
        return method_exists($this->data['entry'], 'isTranslatableAttribute') && 
            	$this->data['entry']->isTranslatableAttribute($key);
    }

    private function prepareTranslationsForRevision()
    {
        $this->data['old_values'] = $this->data['entry']->fromJson($this->data['revision']->old_value, false);
        $this->data['current_values_locales'] = collect($this->data['entry']->getTranslatedLocales($this->data['revision']->key));
        $this->data['old_values_locales'] = collect(array_keys($this->data['old_values']));   
    }

    private function forgetLocalesThatAreNotExistInRevision()
    {
        foreach ($this->data['current_values_locales']->diff($this->data['old_values_locales']) as $locale) {
            $this->data['entry']->forgetTranslation($this->data['revision']->key, $locale);
        }
    }

    private function returnForgettenLocalesThatAreNotExistInCurrentVersion()
    {
        foreach ($this->data['old_values_locales']->diff($this->data['current_values_locales']) as $locale) {
            $this->data['entry']->setTranslation($this->data['revision']->key, $locale, $this->data['old_values'] [$locale]);
        }
    }

    private function updateAllLocalesThatAreCommonInOldAndCurrentVersion()
    {
        foreach ($this->data['old_values'] as $locale => $revisionTranslation) {
            if( $revisionTranslation !=  $this->data['entry']->getTranslation($this->data['revision']->key, $locale)) {
                $this->data['entry']->setTranslation($this->data['revision']->key, $locale, $revisionTranslation);
        	}
        }
    }

it is ugly but do the trick

@tabacitu
Copy link
Member

Hi @phpust ,

Thanks for sharing your code. What version of Backpack are you using?

Cheers!

@phpust
Copy link

phpust commented Mar 2, 2022

Hi @phpust ,

Thanks for sharing your code. What version of Backpack are you using?

Cheers!

I use backpack version 5.

@tabacitu tabacitu reopened this Mar 9, 2022
@tabacitu tabacitu assigned pxpm and unassigned ctudorache87 Mar 9, 2022
@tabacitu tabacitu added Possible Bug Priority: MUST and removed bug Something isn't working labels Mar 9, 2022
@tabacitu tabacitu assigned tabacitu and unassigned pxpm Apr 11, 2022
@welcome
Copy link

welcome bot commented May 3, 2022

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication channels:

  • Bug Reports, Feature Requests - Github Issues (here);
  • Quick help (How do I do X) - Gitter Chatroom;
  • Long questions (I have done X and Y and it won't do Z wtf) - Stackoverflow, using the backpack-for-laravel tag;
  • Showing off something you've made, asking for opinion on Backpack/Laravel matters - Reddit;

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

--
Justin Case
The Backpack Robot

@tabacitu tabacitu transferred this issue from Laravel-Backpack/CRUD May 3, 2022
@request-info
Copy link

request-info bot commented May 3, 2022

Hi there!

Could you please provide us with more info about this? Looks like you skipped the title/body.

Thank you!

--
Justin Case
The Backpack Robot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants