Skip to content

Commit

Permalink
PSR2
Browse files Browse the repository at this point in the history
  • Loading branch information
Bozhidar Hristov committed Dec 21, 2016
1 parent 93a6c64 commit ef45f1c
Show file tree
Hide file tree
Showing 35 changed files with 473 additions and 582 deletions.
45 changes: 12 additions & 33 deletions src/Admin/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,53 +36,32 @@ public function setContainer(Container $Container)
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('locale')
->add('name')
->add('locale')
->add('name')
;
}

// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('locale')
->add('name')
->add('locale')
->add('name')
;
}

// Fields to be shown on lists
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());
}

}
9 changes: 4 additions & 5 deletions src/Admin/TokenTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
;
}

}
93 changes: 33 additions & 60 deletions src/Admin/TranslationToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -34,12 +33,6 @@ class TranslationToken extends Admin
*/
private $em;

/**
*
* @var Helper
*/
private $helper;

/**
*
* @param \Doctrine\ORM\EntityManager $em
Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -120,46 +104,35 @@ 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'
));
}

// Fields to be shown on lists
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();
}

}
41 changes: 20 additions & 21 deletions src/Admin/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,40 @@ 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')
;
}

// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('catalogue')
->add('translation')
->add('language')
->add('translationToken')
->add('catalogue')
->add('translation')
->add('language')
->add('translationToken')
;
}

// Fields to be shown on lists
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(),
)
))
;
}

Expand All @@ -75,5 +75,4 @@ protected function configureRoutes(RouteCollection $collection)
$collection->remove('create');
$collection->remove('edit');
}

}
34 changes: 16 additions & 18 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -140,7 +140,6 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request =
}

$em->flush();
$this->get('object_bg.translation.helper')->clearTranslationCache();
}

$FormView = $Form->createView();
Expand All @@ -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
));
}

}
Loading

0 comments on commit ef45f1c

Please sign in to comment.