Skip to content

Commit

Permalink
Fix export in Translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Bozhidar Hristov committed Mar 8, 2017
1 parent 7aa7cb2 commit 32d9e94
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Admin/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ObjectBG\TranslationBundle\Admin;

use Doctrine\ORM\EntityManagerInterface;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
Expand All @@ -28,6 +29,18 @@ class Translations extends Admin
'_sort_order' => 'DESC',
'_sort_by' => 'id',
);
/**
* @var EntityManagerInterface
*/
private $em;

/**n
* @param $em EntityManagerInterface
*/
public function setEntityManager(EntityManagerInterface $em)
{
$this->em = $em;
}

// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
Expand Down Expand Up @@ -78,4 +91,21 @@ protected function configureRoutes(RouteCollection $collection)
$collection->remove('create');
$collection->remove('edit');
}

/**
* @return VM5TranslationSourceIterator
*/
public function getDataSourceIterator()
{
$qb = $this->em->createQueryBuilder()
->select('token')
->from('ObjectBGTranslationBundle:TranslationToken', 'token')
->orderBy('token.id')
->getQuery();
$languages = $this->em->createQuery(
'SELECT lang FROM ObjectBGTranslationBundle:Language lang INDEX BY lang.id'
)->getResult();

return new VM5TranslationSourceIterator($qb, $languages);
}
}
107 changes: 107 additions & 0 deletions src/Admin/VM5TranslationSourceIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace ObjectBG\TranslationBundle\Admin;

use Doctrine\ORM\Query;
use Exporter\Exception\InvalidMethodCallException;
use ObjectBG\TranslationBundle\Entity\Language;


/**
* Description of DoctrineORMQuerySourceIterator
*
* @author Zuza
*/
class VM5TranslationSourceIterator extends \Exporter\Source\DoctrineORMQuerySourceIterator
{
/**
* @var Language[]
*/
protected $languages;

/**
* VM5TranslationSourceIterator constructor.
* @param Query $query
* @param array $languages
*/
public function __construct(Query $query, array $languages)
{
$this->query = clone $query;
$this->query->setParameters($query->getParameters());
foreach ($query->getHints() as $name => $value) {
$this->query->setHint($name, $value);
}

$this->languages = $languages;
}

/**
* {@inheritdoc}
*/
public function current()
{
$tokens = $this->iterator->current();
$field = [];
foreach ($tokens as $token) {
$field['id'] = $token->getId();
$field['token'] = $token->getToken();
$field['catalogue'] = $token->getCatalogue();

foreach ($this->languages as $lang) {
$translation = $token->getTranslation($lang);
if ($translation != null) {
$field[$lang->getName()] = $translation->getTranslation();
} else {
$field[$lang->getName()] = null;
}
}
}

$this->query->getEntityManager()->getUnitOfWork()->detach($token);

return $field;
}

/**
* {@inheritdoc}
*/
public function next()
{
$this->iterator->next();
}

/**
* {@inheritdoc}
*/
public function key()
{
return $this->iterator->key();
}

/**
* {@inheritdoc}
*/
public function valid()
{
return $this->iterator->valid();
}

/**
* {@inheritdoc}
*/
public function rewind()
{
if ($this->iterator) {
throw new InvalidMethodCallException('Cannot rewind a Doctrine\ORM\Query');
}

$this->iterator = $this->query->iterate();
$this->iterator->rewind();
}
}
3 changes: 3 additions & 0 deletions src/Resources/config/admins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<call method="setTranslationDomain">
<argument>ObjectBGTranslationBundle</argument>
</call>
<call method="setEntityManager">
<argument id="doctrine.orm.entity_manager" type="service" />
</call>
</service>

<service id="objectbg.admin.translationToken" class="%object_bg.translation.admin_class_token%">
Expand Down

0 comments on commit 32d9e94

Please sign in to comment.