Skip to content

Commit

Permalink
Merge pull request #7 from Warxcell/catalogue_support
Browse files Browse the repository at this point in the history
Catalogue support
  • Loading branch information
Warxcell authored Jan 25, 2017
2 parents 6ebd32b + e042c61 commit 0a06eb6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 51 deletions.
86 changes: 41 additions & 45 deletions src/Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -28,15 +28,15 @@ 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',
)
)
->setMethod('GET');

$FilterFormBuilder->add(
$filterFormBuilder->add(
'show-only-untranslated',
'checkbox',
array(
Expand All @@ -45,7 +45,7 @@ public function listAction(\Symfony\Component\HttpFoundation\Request $request =
)
);

$FilterFormBuilder->add(
$filterFormBuilder->add(
'filter',
'submit',
array(
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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) {
Expand All @@ -114,8 +110,8 @@ function (FormEvent $event) use ($tokens, $languages) {
}
);

if ($CanEdit) {
$FormBuilder->add(
if ($canEdit) {
$formBuilder->add(
'Save',
'submit',
array(
Expand All @@ -126,64 +122,64 @@ 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',
array(
'action' => 'list',
'languages' => $languages,
'tokens' => $tokens,
'form' => $FormView,
'filterForm' => $FilterFormView,
'form' => $formView,
'filterForm' => $filterFormView,
)
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/Dumper/DatabaseDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
34 changes: 31 additions & 3 deletions src/Resources/views/CRUD/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@
</style>
{% endblock %}

{% block javascripts %}
{{ parent() }}

<script>
$(function () {
$("[data-language-toggle]").on('ifChanged', function (event) {
toggleLanguage($(this).val());
});
$("[data-language-toggle]").on('change', function (event) {
toggleLanguage($(this).val());
});
function toggleLanguage(languageId) {
$("[data-language-id='" + languageId + "']").toggle();
}
})
;
</script>
{% endblock %}

{% block list_table %}

<div class="filter-form">
Expand All @@ -27,6 +47,14 @@

<div class="sonata-ba-form">

{% for lang in languages %}
<label>
Show
{{ lang.name }}
<input type="checkbox" checked value="{{ lang.id }}" data-language-toggle/>
</label>
{% endfor %}

{{ form_start(form) }}

<table class="table table-bordered table-striped translation-table">
Expand All @@ -35,7 +63,7 @@
<th>{% trans from "ObjectBGTranslationBundle" %}Token{% endtrans %}</th>
<th>{% trans from "ObjectBGTranslationBundle" %}Domain{% endtrans %}</th>
{% for lang in languages %}
<th>{{ lang.name }}</th>
<th data-language-id="{{ lang.id }}">{{ lang.name }}</th>
{% endfor %}
</tr>
</thead>
Expand All @@ -45,8 +73,8 @@
<tr>
<td>{{ tokens[i].token }}</td>
<td>{{ tokens[i].catalogue }}</td>
{% for translation in token %}
<td>{{ form_row(translation) }}</td>
{% for languageId, translation in token %}
<td data-language-id="{{ languageId }}">{{ form_row(translation) }}</td>
{% endfor %}
</tr>
{% endfor %}
Expand Down

0 comments on commit 0a06eb6

Please sign in to comment.