From ef45f1c4a1f4e6a929f06d2c16444146ce2286df Mon Sep 17 00:00:00 2001 From: Bozhidar Hristov Date: Wed, 21 Dec 2016 12:22:56 +0200 Subject: [PATCH] PSR2 --- src/Admin/Language.php | 45 +--- src/Admin/TokenTranslation.php | 9 +- src/Admin/TranslationToken.php | 93 +++----- src/Admin/Translations.php | 41 ++-- src/Controller/CRUDController.php | 34 ++- .../OverrideTranslatorCompilerPass.php | 8 +- .../Compiler/TemplatingCompilerPass.php | 1 - src/DependencyInjection/Configuration.php | 1 + .../ObjectBGTranslationExtension.php | 1 - src/Doctrine/Filter/LanguageFilter.php | 1 - src/Dumper/DatabaseDumper.php | 5 +- src/Entity/Language.php | 1 - src/Entity/Translation.php | 1 - src/Entity/TranslationToken.php | 5 +- .../CurrentTranslationLoader.php | 8 +- src/Exception/InvalidArgumentException.php | 32 +-- .../TranslationsFormsListener.php | 78 +++---- .../EventListener/TranslationsListener.php | 212 +++++++++--------- src/Form/Type/TranslatedEntityType.php | 26 ++- src/Form/Type/TranslationsFieldsType.php | 13 +- src/Form/Type/TranslationsFormsType.php | 19 +- .../Type/TranslationsLocalesSelectorType.php | 100 +++++---- src/Form/Type/TranslationsType.php | 147 ++++++------ src/Helper.php | 62 ----- src/ObjectBGTranslationBundle.php | 1 - src/Repository/Language.php | 19 +- src/Repository/Translation.php | 49 ++-- src/Repository/TranslationToken.php | 19 +- src/Resources/config/admins.xml | 3 - src/Resources/config/routing.xml | 6 +- src/Resources/config/services.xml | 10 - src/Resources/views/CRUD/list.html.twig | 2 +- .../Controller/DefaultControllerTest.php | 1 + src/TranslationLoader.php | 1 - src/Translator.php | 1 - 35 files changed, 473 insertions(+), 582 deletions(-) delete mode 100644 src/Helper.php diff --git a/src/Admin/Language.php b/src/Admin/Language.php index 9d34d9e..782f7dd 100644 --- a/src/Admin/Language.php +++ b/src/Admin/Language.php @@ -36,8 +36,8 @@ public function setContainer(Container $Container) protected function configureFormFields(FormMapper $formMapper) { $formMapper - ->add('locale') - ->add('name') + ->add('locale') + ->add('name') ; } @@ -45,8 +45,8 @@ protected function configureFormFields(FormMapper $formMapper) protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper - ->add('locale') - ->add('name') + ->add('locale') + ->add('name') ; } @@ -54,35 +54,14 @@ protected function configureDatagridFilters(DatagridMapper $datagridMapper) protected function configureListFields(ListMapper $listMapper) { $listMapper - ->addIdentifier('locale') - ->addIdentifier('name') - ->add('_action', 'actions', array( - 'actions' => array( - 'edit' => array(), - 'delete' => array(), - ) - )) + ->addIdentifier('locale') + ->addIdentifier('name') + ->add('_action', 'actions', array( + 'actions' => array( + 'edit' => array(), + 'delete' => array(), + ) + )) ; } - - public function postPersist($object) - { - $this->Container->get('object_bg.translation.helper')->addLanguageFile($object->getLocale()); - } - - public function postRemove($object) - { - $this->Container->get('object_bg.translation.helper')->removeLanguageFile($object->getLocale()); - } - - public function preUpdate($object) - { - $this->Container->get('object_bg.translation.helper')->removeLanguageFile($object->getLocale()); - } - - public function postUpdate($object) - { - $this->Container->get('object_bg.translation.helper')->addLanguageFile($object->getLocale()); - } - } diff --git a/src/Admin/TokenTranslation.php b/src/Admin/TokenTranslation.php index 2e9089c..c8d0470 100644 --- a/src/Admin/TokenTranslation.php +++ b/src/Admin/TokenTranslation.php @@ -29,11 +29,10 @@ class TokenTranslation extends Admin protected function configureFormFields(FormMapper $formMapper) { $formMapper - ->add('language', null, array( - 'disabled' => true - )) - ->add('translation') + ->add('language', null, array( + 'disabled' => true + )) + ->add('translation') ; } - } diff --git a/src/Admin/TranslationToken.php b/src/Admin/TranslationToken.php index db84b11..bce2759 100644 --- a/src/Admin/TranslationToken.php +++ b/src/Admin/TranslationToken.php @@ -9,7 +9,6 @@ use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormEvent; use Doctrine\ORM\EntityManager; -use ObjectBG\TranslationBundle\Helper; class TranslationToken extends Admin { @@ -34,12 +33,6 @@ class TranslationToken extends Admin */ private $em; - /** - * - * @var Helper - */ - private $helper; - /** * * @param \Doctrine\ORM\EntityManager $em @@ -49,38 +42,29 @@ public function setEntityManager(EntityManager $em) $this->em = $em; } - /** - * - * @param Helper $helper - */ - public function setHelper(Helper $helper) - { - $this->helper = $helper; - } - // Fields to be shown on create/edit forms protected function configureFormFields(FormMapper $formMapper) { $formMapper - ->add('token') - ->add('translations', 'sonata_type_collection', array( - 'type_options' => array( - 'delete' => false, - 'required' => false, - ), - 'btn_add' => false - ), array( - 'edit' => 'inline', - 'inline' => 'table', - 'admin_code' => 'objectbg.admin.token_translation' - )) + ->add('token') + ->add('translations', 'sonata_type_collection', array( + 'type_options' => array( + 'delete' => false, + 'required' => false, + ), + 'btn_add' => false + ), array( + 'edit' => 'inline', + 'inline' => 'table', + 'admin_code' => 'objectbg.admin.token_translation' + )) ; $languages = $this->em->getRepository('ObjectBGTranslationBundle:Language')->findAll(); $languages = new \Doctrine\Common\Collections\ArrayCollection($languages); $formMapper->getFormBuilder()->addEventListener( - FormEvents::PRE_SET_DATA, function(FormEvent $Event) use ($languages) { + FormEvents::PRE_SET_DATA, function(FormEvent $Event) use ($languages) { $data = $Event->getData(); if ($data) { $translations = $data->getTranslations(); @@ -103,7 +87,7 @@ protected function configureFormFields(FormMapper $formMapper) $subject = $this->getSubject(); $formMapper->getFormBuilder()->addEventListener( - FormEvents::POST_SUBMIT, function(FormEvent $Event) use ($subject) { + FormEvents::POST_SUBMIT, function(FormEvent $Event) use ($subject) { $data = $Event->getData(); foreach ($data->getTranslations() as $translation) { if ($translation->getTranslation() == null) { @@ -120,20 +104,20 @@ protected function configureFormFields(FormMapper $formMapper) protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper - ->add('token') + ->add('token') ; $datagridMapper - ->add('show-only-untranslated', 'doctrine_orm_callback', array( - 'label' => 'Show only untranslated', - 'callback' => function ($queryBuilder, $alias, $field, $value) { - if ($value['value'] == null) { - return; - } - $subQuery = 'SELECT COUNT(lang) FROM ObjectBGTranslationBundle:Language lang'; - $queryBuilder->andWhere(sprintf('SIZE(%s.translations) < (%s)', $alias, $subQuery)); - }, - 'field_type' => 'checkbox' + ->add('show-only-untranslated', 'doctrine_orm_callback', array( + 'label' => 'Show only untranslated', + 'callback' => function ($queryBuilder, $alias, $field, $value) { + if ($value['value'] == null) { + return; + } + $subQuery = 'SELECT COUNT(lang) FROM ObjectBGTranslationBundle:Language lang'; + $queryBuilder->andWhere(sprintf('SIZE(%s.translations) < (%s)', $alias, $subQuery)); + }, + 'field_type' => 'checkbox' )); } @@ -141,25 +125,14 @@ protected function configureDatagridFilters(DatagridMapper $datagridMapper) protected function configureListFields(ListMapper $listMapper) { $listMapper - ->addIdentifier('id') - ->addIdentifier('token') - ->add('_action', 'actions', array( - 'actions' => array( - 'edit' => array(), - 'delete' => array(), - ) - )) + ->addIdentifier('id') + ->addIdentifier('token') + ->add('_action', 'actions', array( + 'actions' => array( + 'edit' => array(), + 'delete' => array(), + ) + )) ; } - - public function postPersist($object) - { - $this->helper->clearTranslationCache(); - } - - public function postUpdate($object) - { - $this->helper->clearTranslationCache(); - } - } diff --git a/src/Admin/Translations.php b/src/Admin/Translations.php index e65d40f..b922c6a 100644 --- a/src/Admin/Translations.php +++ b/src/Admin/Translations.php @@ -33,10 +33,10 @@ class Translations extends Admin protected function configureFormFields(FormMapper $formMapper) { $formMapper - ->add('catalogue', 'hidden', array('data' => 'messages')) - ->add('translationToken') - ->add('language') - ->add('translation') + ->add('catalogue', 'hidden', array('data' => 'messages')) + ->add('translationToken') + ->add('language') + ->add('translation') ; } @@ -44,10 +44,10 @@ protected function configureFormFields(FormMapper $formMapper) protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper - ->add('catalogue') - ->add('translation') - ->add('language') - ->add('translationToken') + ->add('catalogue') + ->add('translation') + ->add('language') + ->add('translationToken') ; } @@ -55,18 +55,18 @@ protected function configureDatagridFilters(DatagridMapper $datagridMapper) protected function configureListFields(ListMapper $listMapper) { $listMapper - ->addIdentifier('id') - ->add('catalogue') - ->add('translation', null, array( - 'edit' => 'inline' - )) - ->add('language') - ->add('translationToken') - ->add('_action', 'actions', array( - 'actions' => array( - 'edit' => array(), - ) - )) + ->addIdentifier('id') + ->add('catalogue') + ->add('translation', null, array( + 'edit' => 'inline' + )) + ->add('language') + ->add('translationToken') + ->add('_action', 'actions', array( + 'actions' => array( + 'edit' => array(), + ) + )) ; } @@ -75,5 +75,4 @@ protected function configureRoutes(RouteCollection $collection) $collection->remove('create'); $collection->remove('edit'); } - } diff --git a/src/Controller/CRUDController.php b/src/Controller/CRUDController.php index e3c1910..87d925d 100644 --- a/src/Controller/CRUDController.php +++ b/src/Controller/CRUDController.php @@ -22,15 +22,15 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = $languages = $em->createQuery('SELECT lang FROM ObjectBGTranslationBundle:Language lang INDEX BY lang.id')->getResult(); $qb = $em->createQueryBuilder() - ->select('token', 'translation') - ->from('ObjectBGTranslationBundle:TranslationToken', 'token', 'token.id') - ->leftJoin('token.translations', 'translation') + ->select('token', 'translation') + ->from('ObjectBGTranslationBundle:TranslationToken', 'token', 'token.id') + ->leftJoin('token.translations', 'translation') ; $FilterFormBuilder = $this->createFormBuilder(null, array( - 'translation_domain' => 'ObjectBGTranslationBundle' - )) - ->setMethod('GET'); + 'translation_domain' => 'ObjectBGTranslationBundle' + )) + ->setMethod('GET'); $FilterFormBuilder->add('show-only-untranslated', 'checkbox', array( 'required' => false, @@ -48,7 +48,7 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = if ($FilterForm->isValid()) { if ($FilterForm['show-only-untranslated']->getData()) { $qb->andWhere('SIZE(token.translations) < :languagesCount') - ->setParameter('languagesCount', count($languages)); + ->setParameter('languagesCount', count($languages)); } } $tokens = $qb->getQuery()->getResult(); @@ -79,7 +79,7 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = )); $FormBuilder->addEventListener( - FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($tokens, $languages) { + FormEvents::PRE_SET_DATA, function(FormEvent $event) use ($tokens, $languages) { $data = $event->getData(); // $data['tokens'] = array(); $data['translations'] = array(); @@ -108,7 +108,7 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = $Form->handleRequest($Request); if ($Form->isValid()) { $TranslationsEntities = $em->createQuery('SELECT translation FROM ObjectBGTranslationBundle:Translation translation INDEX BY translation.id') - ->getResult(); + ->getResult(); $TranslationsEntities = new \Doctrine\Common\Collections\ArrayCollection($TranslationsEntities); foreach ($Form['translations'] as $TokenId => $Translations) { @@ -119,8 +119,8 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = foreach ($Translations as $LanguageId => $FormData) { $language = $languages[$LanguageId]; $Translation = $TranslationsEntities->filter(function($item) use ($token, $language) { - return $item->getTranslationToken() == $token && $item->getLanguage() == $language; - })->first(); + return $item->getTranslationToken() == $token && $item->getLanguage() == $language; + })->first(); if ($FormData->getData() == null) { if ($Translation) { @@ -140,7 +140,6 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = } $em->flush(); - $this->get('object_bg.translation.helper')->clearTranslationCache(); } $FormView = $Form->createView(); @@ -153,12 +152,11 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request = $FormExtension->renderer->setTheme($FilterFormView, $this->admin->getFilterTheme()); return $this->render('ObjectBGTranslationBundle:CRUD:list.html.twig', array( - 'action' => 'list', - 'languages' => $languages, - 'tokens' => $tokens, - 'form' => $FormView, - 'filterForm' => $FilterFormView + 'action' => 'list', + 'languages' => $languages, + 'tokens' => $tokens, + 'form' => $FormView, + 'filterForm' => $FilterFormView )); } - } diff --git a/src/DependencyInjection/Compiler/OverrideTranslatorCompilerPass.php b/src/DependencyInjection/Compiler/OverrideTranslatorCompilerPass.php index 0b8106e..6cdd920 100644 --- a/src/DependencyInjection/Compiler/OverrideTranslatorCompilerPass.php +++ b/src/DependencyInjection/Compiler/OverrideTranslatorCompilerPass.php @@ -2,8 +2,8 @@ namespace ObjectBG\TranslationBundle\DependencyInjection\Compiler; -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface, - Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; class OverrideTranslatorCompilerPass implements CompilerPassInterface { @@ -13,8 +13,6 @@ class OverrideTranslatorCompilerPass implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - $defaultTranslator = $container->getDefinition('translator.default'); - $defaultTranslator->setClass('ObjectBG\TranslationBundle\Translator'); + $container->setParameter('translator.class', 'ObjectBG\TranslationBundle\Translator'); } - } diff --git a/src/DependencyInjection/Compiler/TemplatingCompilerPass.php b/src/DependencyInjection/Compiler/TemplatingCompilerPass.php index 49d00f2..62a479e 100644 --- a/src/DependencyInjection/Compiler/TemplatingCompilerPass.php +++ b/src/DependencyInjection/Compiler/TemplatingCompilerPass.php @@ -21,5 +21,4 @@ public function process(ContainerBuilder $container) $container->setParameter('twig.form.resources', $resources); } } - } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 226d048..fa4c6c5 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -12,6 +12,7 @@ */ class Configuration implements ConfigurationInterface { + /** * {@inheritDoc} */ diff --git a/src/DependencyInjection/ObjectBGTranslationExtension.php b/src/DependencyInjection/ObjectBGTranslationExtension.php index 1983a93..d77ea34 100644 --- a/src/DependencyInjection/ObjectBGTranslationExtension.php +++ b/src/DependencyInjection/ObjectBGTranslationExtension.php @@ -27,5 +27,4 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('services.xml'); $loader->load('admins.xml'); } - } diff --git a/src/Doctrine/Filter/LanguageFilter.php b/src/Doctrine/Filter/LanguageFilter.php index 39bce0b..07e9e58 100644 --- a/src/Doctrine/Filter/LanguageFilter.php +++ b/src/Doctrine/Filter/LanguageFilter.php @@ -15,5 +15,4 @@ public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAli } return $targetTableAlias . '.locale = ' . $this->getParameter('locale'); } - } diff --git a/src/Dumper/DatabaseDumper.php b/src/Dumper/DatabaseDumper.php index fd47242..92c979c 100644 --- a/src/Dumper/DatabaseDumper.php +++ b/src/Dumper/DatabaseDumper.php @@ -30,8 +30,8 @@ public function dump(MessageCatalogue $messages, $options = array()) foreach ($domains as $domain) { foreach ($domain as $token => $val) { $exists = ((int) $this->em->createQuery($dql) - ->setParameter('token', $token) - ->getSingleScalarResult()) > 0; + ->setParameter('token', $token) + ->getSingleScalarResult()) > 0; if (!$exists) { $TokenEntity = new \ObjectBG\TranslationBundle\Entity\TranslationToken(); $TokenEntity->setToken($token); @@ -41,5 +41,4 @@ public function dump(MessageCatalogue $messages, $options = array()) } $this->em->flush(); } - } diff --git a/src/Entity/Language.php b/src/Entity/Language.php index 7d4ec71..47d9a05 100644 --- a/src/Entity/Language.php +++ b/src/Entity/Language.php @@ -61,5 +61,4 @@ public function __toString() { return $this->name; } - } diff --git a/src/Entity/Translation.php b/src/Entity/Translation.php index 4586af8..eac98af 100644 --- a/src/Entity/Translation.php +++ b/src/Entity/Translation.php @@ -104,5 +104,4 @@ public function __toString() { return (string) $this->translation; } - } diff --git a/src/Entity/TranslationToken.php b/src/Entity/TranslationToken.php index b6e9480..47d40fe 100644 --- a/src/Entity/TranslationToken.php +++ b/src/Entity/TranslationToken.php @@ -66,13 +66,12 @@ public function setTranslations($translations) public function getTranslation(Language $Language) { return $this->getTranslations()->filter(function($item) use ($Language) { - return $item->getLanguage() == $Language; - })->first(); + return $item->getLanguage() == $Language; + })->first(); } public function __toString() { return $this->token; } - } diff --git a/src/EventListener/CurrentTranslationLoader.php b/src/EventListener/CurrentTranslationLoader.php index 59a5271..34dc5b3 100644 --- a/src/EventListener/CurrentTranslationLoader.php +++ b/src/EventListener/CurrentTranslationLoader.php @@ -4,7 +4,6 @@ use Doctrine\Common\EventSubscriber; use ObjectBG\TranslationBundle\TranslatableInterface; -use ObjectBG\TranslationBundle\TranslationService; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\PropertyAccess\PropertyAccess; use Symfony\Component\PropertyAccess\PropertyAccessor; @@ -90,9 +89,9 @@ public function initializeTranslation($Entity, $Language) $PropertyAccess = $this->PropertyAccess; $CurrentTranslation = $Translations->filter(function($item) use ($TranslationService, $Language, $PropertyAccess) { - $TranslationLanguage = $PropertyAccess->getValue($item, $TranslationService->getLanguageField($item)); - return $Language instanceof \ObjectBG\TranslationBundle\Entity\Language ? ($TranslationLanguage == $Language) : ($TranslationLanguage->getLocale() == $Language); - })->first(); + $TranslationLanguage = $PropertyAccess->getValue($item, $TranslationService->getLanguageField($item)); + return $Language instanceof \ObjectBG\TranslationBundle\Entity\Language ? ($TranslationLanguage == $Language) : ($TranslationLanguage->getLocale() == $Language); + })->first(); if (!$CurrentTranslation) { return false; @@ -101,5 +100,4 @@ public function initializeTranslation($Entity, $Language) $this->PropertyAccess->setValue($Entity, $CurrentTranslationField, $CurrentTranslation); return true; } - } diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index 412a132..190f244 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -1,15 +1,17 @@ -getData(); - - foreach ($data as $locale => $translation) { - // Remove useless Translation object - if (!$translation) { - $data->removeElement($translation); - - } else { - $translation->setLocale($locale); - } - } - } - - public static function getSubscribedEvents() - { - return array( - FormEvents::SUBMIT => 'submit', - ); - } -} \ No newline at end of file +getData(); + + foreach ($data as $locale => $translation) { + // Remove useless Translation object + if (!$translation) { + $data->removeElement($translation); + } else { + $translation->setLocale($locale); + } + } + } + + public static function getSubscribedEvents() + { + return array( + FormEvents::SUBMIT => 'submit', + ); + } +} diff --git a/src/Form/EventListener/TranslationsListener.php b/src/Form/EventListener/TranslationsListener.php index ab16335..fea2c55 100644 --- a/src/Form/EventListener/TranslationsListener.php +++ b/src/Form/EventListener/TranslationsListener.php @@ -1,104 +1,108 @@ -TranslationService = $TranslationService; - $this->PropertyAccess = PropertyAccess::createPropertyAccessor(); - } - - /** - * - * @param \Symfony\Component\Form\FormEvent $event - */ - public function preSetData(FormEvent $event) { - $form = $event->getForm(); - - $data = $event->getData(); - if ($data) { - $newData = array(); - foreach ($data as $each) { - $newData[$each->getLanguage()->getLocale()] = $each; - } - $event->setData($newData); - } - - $translationClass = $form->getConfig()->getOption('translation_class'); - if ($translationClass === null) { - $translatableClass = $form->getParent()->getConfig()->getDataClass(); - $translationClass = $this->TranslationService->getTranslationClass($translatableClass); - } - - $formOptions = $form->getConfig()->getOptions(); - - $fieldsOptions = $this->TranslationService->getFieldsOptions($translationClass, $formOptions); - - foreach ($formOptions['locales'] as $locale => $name) { - if (isset($fieldsOptions[$locale])) { - $form->add($locale, 'object_bg_translation_fields', array( - 'label' => $name, - 'data_class' => $translationClass, - 'fields' => $fieldsOptions[$locale], - 'required' => in_array($locale, $formOptions['required_locales']) - )); - } - } - } - - /** - * - * @param \Symfony\Component\Form\FormEvent $event - */ - public function submit(FormEvent $event) { - $data = $event->getData(); - - $Translatable = $event->getForm()->getParent()->getData(); - foreach ($data as $locale => $translation) { - // Remove useless Translation object - if (!$translation) { - $Translatable->getTranslations()->removeElement($translation); - } else { - $LanguageField = $this->TranslationService->getLanguageField(get_class($translation)); - $TranslatableField = $this->TranslationService->getTranslatableField(get_class($translation)); - - $Language = $this->TranslationService->getLanguageByLocale($locale); - $this->PropertyAccess->setValue($translation, $LanguageField, $Language); - $this->PropertyAccess->setValue($translation, $TranslatableField, $Translatable); - } - } - $event->setData($data); - } - - public static function getSubscribedEvents() { - return array( - FormEvents::PRE_SET_DATA => 'preSetData', - FormEvents::SUBMIT => 'submit', - ); - } - -} +TranslationService = $TranslationService; + $this->PropertyAccess = PropertyAccess::createPropertyAccessor(); + } + + /** + * + * @param \Symfony\Component\Form\FormEvent $event + */ + public function preSetData(FormEvent $event) + { + $form = $event->getForm(); + + $data = $event->getData(); + if ($data) { + $newData = array(); + foreach ($data as $each) { + $newData[$each->getLanguage()->getLocale()] = $each; + } + $event->setData($newData); + } + + $translationClass = $form->getConfig()->getOption('translation_class'); + if ($translationClass === null) { + $translatableClass = $form->getParent()->getConfig()->getDataClass(); + $translationClass = $this->TranslationService->getTranslationClass($translatableClass); + } + + $formOptions = $form->getConfig()->getOptions(); + + $fieldsOptions = $this->TranslationService->getFieldsOptions($translationClass, $formOptions); + + foreach ($formOptions['locales'] as $locale => $name) { + if (isset($fieldsOptions[$locale])) { + $form->add($locale, 'object_bg_translation_fields', array( + 'label' => $name, + 'data_class' => $translationClass, + 'fields' => $fieldsOptions[$locale], + 'required' => in_array($locale, $formOptions['required_locales']) + )); + } + } + } + + /** + * + * @param \Symfony\Component\Form\FormEvent $event + */ + public function submit(FormEvent $event) + { + $data = $event->getData(); + + $Translatable = $event->getForm()->getParent()->getData(); + foreach ($data as $locale => $translation) { + // Remove useless Translation object + if (!$translation) { + $Translatable->getTranslations()->removeElement($translation); + } else { + $LanguageField = $this->TranslationService->getLanguageField(get_class($translation)); + $TranslatableField = $this->TranslationService->getTranslatableField(get_class($translation)); + + $Language = $this->TranslationService->getLanguageByLocale($locale); + $this->PropertyAccess->setValue($translation, $LanguageField, $Language); + $this->PropertyAccess->setValue($translation, $TranslatableField, $Translatable); + } + } + $event->setData($data); + } + + public static function getSubscribedEvents() + { + return array( + FormEvents::PRE_SET_DATA => 'preSetData', + FormEvents::SUBMIT => 'submit', + ); + } +} diff --git a/src/Form/Type/TranslatedEntityType.php b/src/Form/Type/TranslatedEntityType.php index 1bf38c5..fdf8e81 100644 --- a/src/Form/Type/TranslatedEntityType.php +++ b/src/Form/Type/TranslatedEntityType.php @@ -12,11 +12,13 @@ * * @author David ALLIX */ -class TranslatedEntityType extends AbstractType { +class TranslatedEntityType extends AbstractType +{ private $request; - public function setRequest(Request $request = null) { + public function setRequest(Request $request = null) + { $this->request = $request; } @@ -24,26 +26,28 @@ public function setRequest(Request $request = null) { * * @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver */ - public function setDefaultOptions(OptionsResolverInterface $resolver) { + public function setDefaultOptions(OptionsResolverInterface $resolver) + { $resolver->setDefaults(array( 'translation_path' => 'translations', 'translation_property' => null, 'property' => function(Options $options) { - if (null === $this->request) { - throw new \Exception('Error while getting request'); - } + if (null === $this->request) { + throw new \Exception('Error while getting request'); + } - return $options['translation_path'] . '[' . $this->request->getLocale() . '].' . $options['translation_property']; - }, + return $options['translation_path'] . '[' . $this->request->getLocale() . '].' . $options['translation_property']; + }, )); } - public function getParent() { + public function getParent() + { return 'entity'; } - public function getName() { + public function getName() + { return 'object_bg_translated_entity'; } - } diff --git a/src/Form/Type/TranslationsFieldsType.php b/src/Form/Type/TranslationsFieldsType.php index 3185887..53e3126 100644 --- a/src/Form/Type/TranslationsFieldsType.php +++ b/src/Form/Type/TranslationsFieldsType.php @@ -6,14 +6,16 @@ Symfony\Component\Form\FormBuilderInterface, Symfony\Component\OptionsResolver\OptionsResolverInterface; -class TranslationsFieldsType extends AbstractType { +class TranslationsFieldsType extends AbstractType +{ /** * * @param \Symfony\Component\Form\FormBuilderInterface $builder * @param array $options */ - public function buildForm(FormBuilderInterface $builder, array $options) { + public function buildForm(FormBuilderInterface $builder, array $options) + { foreach ($options['fields'] as $fieldName => $fieldConfig) { $fieldType = $fieldConfig['field_type']; unset($fieldConfig['field_type']); @@ -26,14 +28,15 @@ public function buildForm(FormBuilderInterface $builder, array $options) { * * @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver */ - public function setDefaultOptions(OptionsResolverInterface $resolver) { + public function setDefaultOptions(OptionsResolverInterface $resolver) + { $resolver->setDefaults(array( 'fields' => array(), )); } - public function getName() { + public function getName() + { return 'object_bg_translation_fields'; } - } diff --git a/src/Form/Type/TranslationsFormsType.php b/src/Form/Type/TranslationsFormsType.php index bbd8264..6b6a6ae 100644 --- a/src/Form/Type/TranslationsFormsType.php +++ b/src/Form/Type/TranslationsFormsType.php @@ -14,7 +14,8 @@ * * @author David ALLIX */ -class TranslationsFormsType extends AbstractType { +class TranslationsFormsType extends AbstractType +{ private $translationForm; private $translationsListener; @@ -30,7 +31,8 @@ class TranslationsFormsType extends AbstractType { * @param string $defaultLocale * @param array $requiredLocales */ - public function __construct(TranslationForm $translationForm, TranslationsFormsListener $translationsListener, array $locales, $defaultLocale, array $requiredLocales = array()) { + public function __construct(TranslationForm $translationForm, TranslationsFormsListener $translationsListener, array $locales, $defaultLocale, array $requiredLocales = array()) + { $this->translationForm = $translationForm; $this->translationsListener = $translationsListener; $this->locales = $locales; @@ -43,7 +45,8 @@ public function __construct(TranslationForm $translationForm, TranslationsFormsL * @param \Symfony\Component\Form\FormBuilderInterface $builder * @param array $options */ - public function buildForm(FormBuilderInterface $builder, array $options) { + public function buildForm(FormBuilderInterface $builder, array $options) + { $builder->addEventSubscriber($this->translationsListener); $formsOptions = $this->translationForm->getFormsOptions($options); @@ -61,7 +64,8 @@ public function buildForm(FormBuilderInterface $builder, array $options) { * @param \Symfony\Component\Form\FormInterface $form * @param array $options */ - public function buildView(FormView $view, FormInterface $form, array $options) { + public function buildView(FormView $view, FormInterface $form, array $options) + { $view->vars['default_locale'] = $this->defaultLocale; $view->vars['required_locales'] = $options['required_locales']; } @@ -70,7 +74,8 @@ public function buildView(FormView $view, FormInterface $form, array $options) { * * @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver */ - public function setDefaultOptions(OptionsResolverInterface $resolver) { + public function setDefaultOptions(OptionsResolverInterface $resolver) + { $resolver->setDefaults(array( 'by_reference' => false, 'locales' => $this->locales, @@ -80,8 +85,8 @@ public function setDefaultOptions(OptionsResolverInterface $resolver) { )); } - public function getName() { + public function getName() + { return 'object_bg_translations_forms'; } - } diff --git a/src/Form/Type/TranslationsLocalesSelectorType.php b/src/Form/Type/TranslationsLocalesSelectorType.php index 8dcb77f..a2f1578 100644 --- a/src/Form/Type/TranslationsLocalesSelectorType.php +++ b/src/Form/Type/TranslationsLocalesSelectorType.php @@ -1,48 +1,52 @@ -locales = $locales; -// $this->defaultLocale = $defaultLocale; - } - - /** - * - * @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver - */ - public function setDefaultOptions(OptionsResolverInterface $resolver) { - $resolver->setDefaults(array( - 'choices' => array_combine($this->locales, $this->locales), - 'expanded' => true, - 'multiple' => true, - 'attr' => array( - 'class' => "a2lix_translationsLocalesSelector" - ) - )); - } - - public function getParent() { - return 'choice'; - } - - public function getName() { - return 'object_bg_locales_selector'; - } - -} +locales = $locales; +// $this->defaultLocale = $defaultLocale; + } + + /** + * + * @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array( + 'choices' => array_combine($this->locales, $this->locales), + 'expanded' => true, + 'multiple' => true, + 'attr' => array( + 'class' => "a2lix_translationsLocalesSelector" + ) + )); + } + + public function getParent() + { + return 'choice'; + } + + public function getName() + { + return 'object_bg_locales_selector'; + } +} diff --git a/src/Form/Type/TranslationsType.php b/src/Form/Type/TranslationsType.php index 4ab3ffc..5b07ab9 100644 --- a/src/Form/Type/TranslationsType.php +++ b/src/Form/Type/TranslationsType.php @@ -1,71 +1,76 @@ -translationsListener = $translationsListener; - $this->TranslationService = $TranslationService; - } - - /** - * - * @param \Symfony\Component\Form\FormBuilderInterface $builder - * @param array $options - */ - public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->addEventSubscriber($this->translationsListener); - } - - /** - * - * @param \Symfony\Component\Form\FormView $view - * @param \Symfony\Component\Form\FormInterface $form - * @param array $options - */ - public function buildView(FormView $view, FormInterface $form, array $options) { - $view->vars['default_locale'] = $options['default_locale']; - $view->vars['required_locales'] = $options['required_locales']; - } - - /** - * - * @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver - */ - public function setDefaultOptions(OptionsResolverInterface $resolver) { - $resolver->setDefaults(array( - 'by_reference' => false, - 'empty_data' => new \Doctrine\Common\Collections\ArrayCollection(), - 'locales' => $this->TranslationService->getLocales(), - 'default_locale' => $this->TranslationService->getDefaultLocale(), - 'required_locales' => $this->TranslationService->getRequiredLocales(), - 'translation_class' => null, - 'fields' => array(), - 'exclude_fields' => array(), - )); - } - - public function getName() { - return 'object_bg_translations'; - } - -} +translationsListener = $translationsListener; + $this->TranslationService = $TranslationService; + } + + /** + * + * @param \Symfony\Component\Form\FormBuilderInterface $builder + * @param array $options + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder->addEventSubscriber($this->translationsListener); + } + + /** + * + * @param \Symfony\Component\Form\FormView $view + * @param \Symfony\Component\Form\FormInterface $form + * @param array $options + */ + public function buildView(FormView $view, FormInterface $form, array $options) + { + $view->vars['default_locale'] = $options['default_locale']; + $view->vars['required_locales'] = $options['required_locales']; + } + + /** + * + * @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array( + 'by_reference' => false, + 'empty_data' => new \Doctrine\Common\Collections\ArrayCollection(), + 'locales' => $this->TranslationService->getLocales(), + 'default_locale' => $this->TranslationService->getDefaultLocale(), + 'required_locales' => $this->TranslationService->getRequiredLocales(), + 'translation_class' => null, + 'fields' => array(), + 'exclude_fields' => array(), + )); + } + + public function getName() + { + return 'object_bg_translations'; + } +} diff --git a/src/Helper.php b/src/Helper.php deleted file mode 100644 index 060d9b2..0000000 --- a/src/Helper.php +++ /dev/null @@ -1,62 +0,0 @@ -Kernel = $Container->get('kernel'); - $this->FileSystem = new Filesystem; - } - - public function clearTranslationCache() - { -// $dirPath = $this->Kernel->getCacheDir() . '/translations/'; -// $this->FileSystem->remove($dirPath); - } - - private function getLanguageFile($locale, $domain = 'messages') - { - $file = $this->Kernel->getRootDir() . '/Resources/translations/' . $domain . '.' . $locale . '.db'; - return $file; - } - - public function addLanguageFile($locale, $domain = 'messages') - { -// $file = $this->getLanguageFile($locale, $domain); -// $dir = dirname($file); -// if (!$this->FileSystem->exists($dir)) { -// $this->FileSystem->mkdir($dir); -// } -// $this->FileSystem->touch($file); -// -// $this->clearTranslationCache(); - } - - public function removeLanguageFile($locale, $domain = 'messages') - { -// $file = $this->getLanguageFile($locale, $domain); -// $this->FileSystem->remove($file); -// -// $this->clearTranslationCache(); - } - -} diff --git a/src/ObjectBGTranslationBundle.php b/src/ObjectBGTranslationBundle.php index 4d6cc47..943624f 100644 --- a/src/ObjectBGTranslationBundle.php +++ b/src/ObjectBGTranslationBundle.php @@ -16,5 +16,4 @@ public function build(ContainerBuilder $container) $container->addCompilerPass(new DependencyInjection\Compiler\TemplatingCompilerPass); $container->addCompilerPass(new DependencyInjection\Compiler\OverrideTranslatorCompilerPass()); } - } diff --git a/src/Repository/Language.php b/src/Repository/Language.php index 6b76843..d3b08bf 100644 --- a/src/Repository/Language.php +++ b/src/Repository/Language.php @@ -1,9 +1,10 @@ - - */ - public function getTranslations(LanguageEntity $language, $catalogue = "messages") { - $query = $this->getEntityManager()->createQuery("SELECT t,token FROM ObjectBGTranslationBundle:Translation t JOIN t.translationToken token WHERE t.language = :language AND t.catalogue = :catalogue"); - $query->setParameter("language", $language); - $query->setParameter("catalogue", $catalogue); - $r = $query->getResult(); - return $r; - } - -} + + */ + public function getTranslations(LanguageEntity $language, $catalogue = "messages") + { + $query = $this->getEntityManager()->createQuery("SELECT t,token FROM ObjectBGTranslationBundle:Translation t JOIN t.translationToken token WHERE t.language = :language AND t.catalogue = :catalogue"); + $query->setParameter("language", $language); + $query->setParameter("catalogue", $catalogue); + $r = $query->getResult(); + return $r; + } +} diff --git a/src/Repository/TranslationToken.php b/src/Repository/TranslationToken.php index 65bc0c5..9bc07fb 100644 --- a/src/Repository/TranslationToken.php +++ b/src/Repository/TranslationToken.php @@ -1,9 +1,10 @@ - - - - diff --git a/src/Resources/config/routing.xml b/src/Resources/config/routing.xml index dda880f..3c53669 100644 --- a/src/Resources/config/routing.xml +++ b/src/Resources/config/routing.xml @@ -1,10 +1,10 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> - diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index baa70ca..e007959 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -7,20 +7,10 @@ ObjectBG\TranslationBundle\TranslationLoader - ObjectBG\TranslationBundle\Helper ObjectBG\TranslationBundle\Dumper\DatabaseDumper - - - - - - diff --git a/src/Resources/views/CRUD/list.html.twig b/src/Resources/views/CRUD/list.html.twig index f75ecad..0746219 100644 --- a/src/Resources/views/CRUD/list.html.twig +++ b/src/Resources/views/CRUD/list.html.twig @@ -73,7 +73,7 @@  -  {% endif %} - + {{ form_end(form) }} {% endblock %} diff --git a/src/Tests/Controller/DefaultControllerTest.php b/src/Tests/Controller/DefaultControllerTest.php index 25f7342..546ee97 100644 --- a/src/Tests/Controller/DefaultControllerTest.php +++ b/src/Tests/Controller/DefaultControllerTest.php @@ -6,6 +6,7 @@ class DefaultControllerTest extends WebTestCase { + public function testIndex() { $client = static::createClient(); diff --git a/src/TranslationLoader.php b/src/TranslationLoader.php index feb065e..50cbf7d 100644 --- a/src/TranslationLoader.php +++ b/src/TranslationLoader.php @@ -35,5 +35,4 @@ public function load($resource, $locale, $domain = 'messages') return $catalogue; } - } diff --git a/src/Translator.php b/src/Translator.php index 93d5097..ed42ae4 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -37,5 +37,4 @@ protected function loadCatalogue($locale) $this->catalogues[$locale]->addCatalogue($catalogue); } - }