Skip to content

Commit

Permalink
Merge pull request #5 from Warxcell/catalogue_support
Browse files Browse the repository at this point in the history
Catalogue support + Formatting
  • Loading branch information
Warxcell authored Jan 25, 2017
2 parents 775e6d6 + 5ddfdcc commit 6ebd32b
Show file tree
Hide file tree
Showing 38 changed files with 461 additions and 335 deletions.
23 changes: 12 additions & 11 deletions src/Admin/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace ObjectBG\TranslationBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\DependencyInjection\Container;

Expand Down Expand Up @@ -37,17 +37,15 @@ protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('locale')
->add('name')
;
->add('name');
}

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

// Fields to be shown on lists
Expand All @@ -56,12 +54,15 @@ protected function configureListFields(ListMapper $listMapper)
$listMapper
->addIdentifier('locale')
->addIdentifier('name')
->add('_action', 'actions', array(
'actions' => array(
'edit' => array(),
'delete' => array(),
->add(
'_action',
'actions',
array(
'actions' => array(
'edit' => array(),
'delete' => array(),
),
)
))
;
);
}
}
16 changes: 8 additions & 8 deletions src/Admin/TokenTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
namespace ObjectBG\TranslationBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;

class TokenTranslation extends Admin
{
Expand All @@ -29,10 +26,13 @@ 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');
}
}
138 changes: 78 additions & 60 deletions src/Admin/TranslationToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace ObjectBG\TranslationBundle\Admin;

use Doctrine\ORM\EntityManager;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\FormEvents;

class TranslationToken extends Admin
{
Expand All @@ -34,7 +34,7 @@ class TranslationToken extends Admin
private $em;

/**
*
*
* @param \Doctrine\ORM\EntityManager $em
*/
public function setEntityManager(EntityManager $em)
Expand All @@ -47,78 +47,92 @@ protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('token')
->add('translations', 'sonata_type_collection', array(
'type_options' => array(
'delete' => false,
'required' => false,
->add('catalogue')
->add(
'translations',
'sonata_type_collection',
array(
'type_options' => array(
'delete' => false,
'required' => false,
),
'btn_add' => false,
),
'btn_add' => false
), array(
'edit' => 'inline',
'inline' => 'table',
'admin_code' => 'objectbg.admin.token_translation'
))
;
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) {
$data = $Event->getData();
if ($data) {
$translations = $data->getTranslations();

$langs = array();
foreach ($translations as $trans) {
$langs[] = $trans->getLanguage();
}
$missingLanguages = $languages->filter(function($item) use ($langs) {
return array_search($item, $langs) === false;
});

foreach ($missingLanguages as $lang) {
$newTranslation = new \ObjectBG\TranslationBundle\Entity\Translation();
$newTranslation->setLanguage($lang);
$data->getTranslations()->add($newTranslation);
FormEvents::PRE_SET_DATA,
function (FormEvent $Event) use ($languages) {
$data = $Event->getData();
if ($data) {
$translations = $data->getTranslations();

$langs = array();
foreach ($translations as $trans) {
$langs[] = $trans->getLanguage();
}
$missingLanguages = $languages->filter(
function ($item) use ($langs) {
return array_search($item, $langs) === false;
}
);

foreach ($missingLanguages as $lang) {
$newTranslation = new \ObjectBG\TranslationBundle\Entity\Translation();
$newTranslation->setLanguage($lang);
$data->getTranslations()->add($newTranslation);
}
}
}
});
);

$subject = $this->getSubject();
$formMapper->getFormBuilder()->addEventListener(
FormEvents::POST_SUBMIT, function(FormEvent $Event) use ($subject) {
$data = $Event->getData();
foreach ($data->getTranslations() as $translation) {
if ($translation->getTranslation() == null) {
$data->getTranslations()->removeElement($translation);
} else {
$translation->setCatalogue('messages');
$translation->setTranslationToken($subject);
FormEvents::POST_SUBMIT,
function (FormEvent $Event) use ($subject) {
$data = $Event->getData();
foreach ($data->getTranslations() as $translation) {
if ($translation->getTranslation() == null) {
$data->getTranslations()->removeElement($translation);
} else {
$translation->setTranslationToken($subject);
}
}
}
});
);
}

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

$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
Expand All @@ -127,12 +141,16 @@ protected function configureListFields(ListMapper $listMapper)
$listMapper
->addIdentifier('id')
->addIdentifier('token')
->add('_action', 'actions', array(
'actions' => array(
'edit' => array(),
'delete' => array(),
->addIdentifier('catalogue')
->add(
'_action',
'actions',
array(
'actions' => array(
'edit' => array(),
'delete' => array(),
),
)
))
;
);
}
}
35 changes: 19 additions & 16 deletions src/Admin/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace ObjectBG\TranslationBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;

Expand All @@ -26,7 +26,7 @@ class Translations extends Admin
protected $baseRoutePattern = 'translation-bundle/translations';
protected $datagridValues = array(
'_sort_order' => 'DESC',
'_sort_by' => 'id'
'_sort_by' => 'id',
);

// Fields to be shown on create/edit forms
Expand All @@ -36,38 +36,41 @@ protected function configureFormFields(FormMapper $formMapper)
->add('catalogue', 'hidden', array('data' => 'messages'))
->add('translationToken')
->add('language')
->add('translation')
;
->add('translation');
}

// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('catalogue')
->add('translation')
->add('language')
->add('translationToken')
;
->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(
'translation',
null,
array(
'edit' => 'inline',
)
)
->add('language')
->add('translationToken')
->add('_action', 'actions', array(
'actions' => array(
'edit' => array(),
->add(
'_action',
'actions',
array(
'actions' => array(
'edit' => array(),
),
)
))
;
);
}

protected function configureRoutes(RouteCollection $collection)
Expand Down
2 changes: 1 addition & 1 deletion src/Annotation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
*/
class Column
{

}
2 changes: 1 addition & 1 deletion src/Annotation/CurrentTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
*/
class CurrentTranslation
{

}
2 changes: 1 addition & 1 deletion src/Annotation/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
*/
class Language
{

}
2 changes: 1 addition & 1 deletion src/Annotation/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
*/
class Translatable
{

}
2 changes: 1 addition & 1 deletion src/Annotation/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
*/
class Translations
{

}
Loading

0 comments on commit 6ebd32b

Please sign in to comment.