|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Translatable\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Sludio\HelperBundle\Translatable\Entity\BaseEntity; |
|
7
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
|
8
|
|
|
use Symfony\Component\Form\FormInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Manager |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var EntityManager |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $entityManager; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(RegistryInterface $registry) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->entityManager = $registry->getManager(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getTranslatedFields($class, $field, $identifier, $locales) |
|
23
|
|
|
{ |
|
24
|
|
|
$entity = $this->entityManager->getRepository($class)->find($identifier); |
|
25
|
|
|
|
|
26
|
|
|
$translated = []; |
|
27
|
|
|
/** @var $locales array */ |
|
28
|
|
|
foreach ($locales as $locale) { |
|
29
|
|
|
$translated[$locale][$field] = $this->getField($entity, $field, $locale); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $translated; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function getField(BaseEntity $entity, $field, $locale) |
|
36
|
|
|
{ |
|
37
|
|
|
return $entity->getVariableByLocale($field, $locale); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getNewTranslatedFields($field, $locales) |
|
41
|
|
|
{ |
|
42
|
|
|
$translated = []; |
|
43
|
|
|
/** @var $locales array */ |
|
44
|
|
|
foreach ($locales as $locale) { |
|
45
|
|
|
$translated[$locale][$field] = ''; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $translated; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function persistTranslations(FormInterface $form, $class, $field, $identifier, $locales) |
|
52
|
|
|
{ |
|
53
|
|
|
$translations = $form->getData(); |
|
54
|
|
|
|
|
55
|
|
|
$repository = $this->entityManager->getRepository($class); |
|
56
|
|
|
/** @var $entity BaseEntity */ |
|
57
|
|
|
if (!$identifier) { |
|
58
|
|
|
$entity = new $class(); |
|
59
|
|
|
} else { |
|
60
|
|
|
$entity = $repository->find($identifier); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** @var $locales array */ |
|
64
|
|
|
foreach ($locales as $locale) { |
|
65
|
|
|
if (array_key_exists($locale, $translations) && ($translations[$locale] !== null)) { |
|
66
|
|
|
$postedValue = $translations[$locale]; |
|
67
|
|
|
if ($this->getField($entity, $field, $locale) !== $postedValue) { |
|
68
|
|
|
$lang = explode('_', $locale); |
|
69
|
|
|
$entity->__set($field.'_'.strtolower(reset($lang)), $postedValue); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function setField(BaseEntity $entity, $field, $value) |
|
76
|
|
|
{ |
|
77
|
|
|
$setterFunctionName = 'set'.$field; |
|
78
|
|
|
$entity->{$setterFunctionName}($value); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|