From 74d574f66361945d936b19a9545918770e42bbb5 Mon Sep 17 00:00:00 2001 From: Bozhidar Hristov Date: Wed, 25 Jan 2017 17:42:15 +0200 Subject: [PATCH] Allow to hide languages from translations admin --- src/Controller/CRUDController.php | 86 ++++++++++++------------- src/Dumper/DatabaseDumper.php | 7 +- src/Resources/views/CRUD/list.html.twig | 34 +++++++++- 3 files changed, 76 insertions(+), 51 deletions(-) diff --git a/src/Controller/CRUDController.php b/src/Controller/CRUDController.php index 13b9874..7dc39b1 100644 --- a/src/Controller/CRUDController.php +++ b/src/Controller/CRUDController.php @@ -10,13 +10,13 @@ class CRUDController extends \Sonata\AdminBundle\Controller\CRUDController public function listAction(\Symfony\Component\HttpFoundation\Request $request = null) { - $CanEdit = $this->admin->isGranted('EDIT'); - $CanView = $this->admin->isGranted('LIST'); - if (false === $CanView && false === $CanEdit) { + $canEdit = $this->admin->isGranted('EDIT'); + $canView = $this->admin->isGranted('LIST'); + if (false === $canView && false === $canEdit) { throw new AccessDeniedException(); } - $Request = $this->getRequest(); + $request = $this->getRequest(); $em = $this->getDoctrine()->getManager(); $languages = $em->createQuery( @@ -28,7 +28,7 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = ->from('ObjectBGTranslationBundle:TranslationToken', 'token', 'token.id') ->leftJoin('token.translations', 'translation'); - $FilterFormBuilder = $this->createFormBuilder( + $filterFormBuilder = $this->createFormBuilder( null, array( 'translation_domain' => 'ObjectBGTranslationBundle', @@ -36,7 +36,7 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = ) ->setMethod('GET'); - $FilterFormBuilder->add( + $filterFormBuilder->add( 'show-only-untranslated', 'checkbox', array( @@ -45,7 +45,7 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = ) ); - $FilterFormBuilder->add( + $filterFormBuilder->add( 'filter', 'submit', array( @@ -55,17 +55,17 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = ) ); - $FilterForm = $FilterFormBuilder->getForm(); - $FilterForm->handleRequest($Request); - if ($FilterForm->isValid()) { - if ($FilterForm['show-only-untranslated']->getData()) { + $filterForm = $filterFormBuilder->getForm(); + $filterForm->handleRequest($request); + if ($filterForm->isValid()) { + if ($filterForm['show-only-untranslated']->getData()) { $qb->andWhere('SIZE(token.translations) < :languagesCount') ->setParameter('languagesCount', count($languages)); } } $tokens = $qb->getQuery()->getResult(); - $FormBuilder = $this->createFormBuilder(); + $formBuilder = $this->createFormBuilder(); // $FormBuilder->add('tokens', 'collection', array( // 'type' => 'text', // 'label' => false, @@ -75,12 +75,12 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = // ) // )); - $FormBuilder->add( + $formBuilder->add( 'translations', 'collection', array( 'type' => 'collection', - 'disabled' => !$CanEdit, + 'disabled' => !$canEdit, 'label' => false, 'allow_add' => true, 'options' => array( @@ -94,16 +94,12 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = ) ); - $FormBuilder->addEventListener( + $formBuilder->addEventListener( FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($tokens, $languages) { $data = $event->getData(); -// $data['tokens'] = array(); $data['translations'] = array(); -// foreach ($tokens as $token) { -// $data['tokens'][$token->getId()] = $token->getToken(); -// } foreach ($tokens as $token) { $data['translations'][$token->getId()] = array(); foreach ($languages as $lang) { @@ -114,8 +110,8 @@ function (FormEvent $event) use ($tokens, $languages) { } ); - if ($CanEdit) { - $FormBuilder->add( + if ($canEdit) { + $formBuilder->add( 'Save', 'submit', array( @@ -126,55 +122,55 @@ function (FormEvent $event) use ($tokens, $languages) { ); } - $Form = $FormBuilder->getForm(); - $Form->handleRequest($Request); - if ($Form->isValid()) { - $TranslationsEntities = $em->createQuery( + $form = $formBuilder->getForm(); + $form->handleRequest($request); + if ($form->isValid()) { + $translationsEntities = $em->createQuery( 'SELECT translation FROM ObjectBGTranslationBundle:Translation translation INDEX BY translation.id' ) ->getResult(); - $TranslationsEntities = new \Doctrine\Common\Collections\ArrayCollection($TranslationsEntities); + $translationsEntities = new \Doctrine\Common\Collections\ArrayCollection($translationsEntities); - foreach ($Form['translations'] as $TokenId => $Translations) { + foreach ($form['translations'] as $TokenId => $translations) { $token = $tokens[$TokenId]; // $token->setToken($Form['tokens'][$TokenId]->getData()); // $em->persist($token); - foreach ($Translations as $LanguageId => $FormData) { - $language = $languages[$LanguageId]; - $Translation = $TranslationsEntities->filter( + foreach ($translations as $languageId => $formData) { + $language = $languages[$languageId]; + $translation = $translationsEntities->filter( function ($item) use ($token, $language) { return $item->getTranslationToken() == $token && $item->getLanguage() == $language; } )->first(); - if ($FormData->getData() == null) { - if ($Translation) { - $em->remove($Translation); + if ($formData->getData() == null) { + if ($translation) { + $em->remove($translation); } continue; } - if (!$Translation) { - $Translation = new \ObjectBG\TranslationBundle\Entity\Translation(); - $Translation->setLanguage($language); - $Translation->setTranslationToken($token); + if (!$translation) { + $translation = new \ObjectBG\TranslationBundle\Entity\Translation(); + $translation->setLanguage($language); + $translation->setTranslationToken($token); } - $Translation->setTranslation($FormData->getData()); - $em->persist($Translation); + $translation->setTranslation($formData->getData()); + $em->persist($translation); } } $em->flush(); } - $FormView = $Form->createView(); + $formView = $form->createView(); $Twig = $this->get('twig'); $FormExtension = $Twig->getExtension('form'); - $FormExtension->renderer->setTheme($FormView, $this->admin->getFormTheme()); + $FormExtension->renderer->setTheme($formView, $this->admin->getFormTheme()); - $FilterFormView = $FilterForm->createView(); - $FormExtension->renderer->setTheme($FilterFormView, $this->admin->getFilterTheme()); + $filterFormView = $filterForm->createView(); + $FormExtension->renderer->setTheme($filterFormView, $this->admin->getFilterTheme()); return $this->render( 'ObjectBGTranslationBundle:CRUD:list.html.twig', @@ -182,8 +178,8 @@ function ($item) use ($token, $language) { 'action' => 'list', 'languages' => $languages, 'tokens' => $tokens, - 'form' => $FormView, - 'filterForm' => $FilterFormView, + 'form' => $formView, + 'filterForm' => $filterFormView, ) ); } diff --git a/src/Dumper/DatabaseDumper.php b/src/Dumper/DatabaseDumper.php index c973c3f..8b59fb3 100644 --- a/src/Dumper/DatabaseDumper.php +++ b/src/Dumper/DatabaseDumper.php @@ -33,9 +33,10 @@ public function dump(MessageCatalogue $messages, $options = array()) ->setParameter('token', $token) ->getSingleScalarResult()) > 0; if (!$exists) { - $TokenEntity = new \ObjectBG\TranslationBundle\Entity\TranslationToken(); - $TokenEntity->setToken($token); - $this->em->persist($TokenEntity); + $translationToken = new \ObjectBG\TranslationBundle\Entity\TranslationToken(); + $translationToken->setToken($token); + $translationToken->setCatalogue($domain); + $this->em->persist($translationToken); } } } diff --git a/src/Resources/views/CRUD/list.html.twig b/src/Resources/views/CRUD/list.html.twig index 800c0e3..00fd029 100644 --- a/src/Resources/views/CRUD/list.html.twig +++ b/src/Resources/views/CRUD/list.html.twig @@ -19,6 +19,26 @@ {% endblock %} +{% block javascripts %} + {{ parent() }} + + +{% endblock %} + {% block list_table %}
@@ -27,6 +47,14 @@
+ {% for lang in languages %} + + {% endfor %} + {{ form_start(form) }} @@ -35,7 +63,7 @@ {% for lang in languages %} - + {% endfor %} @@ -45,8 +73,8 @@ - {% for translation in token %} - + {% for languageId, translation in token %} + {% endfor %} {% endfor %}
{% trans from "ObjectBGTranslationBundle" %}Token{% endtrans %} {% trans from "ObjectBGTranslationBundle" %}Domain{% endtrans %}{{ lang.name }}{{ lang.name }}
{{ tokens[i].token }} {{ tokens[i].catalogue }}{{ form_row(translation) }}{{ form_row(translation) }}