|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunstmaan\TranslatorBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Kunstmaan\TranslatorBundle\Model\Translation\Translation; |
|
6
|
|
|
use Kunstmaan\TranslatorBundle\Model\Translation\TranslationGroup; |
|
7
|
|
|
use Kunstmaan\TranslatorBundle\Repository\TranslationRepository; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* TranslationGroupManager |
|
11
|
|
|
* For managing/creating TranslationGroup objects |
|
12
|
|
|
*/ |
|
13
|
|
|
class TranslationGroupManager |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var TranslationRepository */ |
|
16
|
|
|
private $translationRepository; |
|
17
|
|
|
|
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
private $dbCopy; |
|
20
|
|
|
|
|
21
|
|
|
/** @var int */ |
|
22
|
|
|
private $maxId = 1; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(TranslationRepository $translationRepository) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->translationRepository = $translationRepository; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Checks if the translation exists in the group for this locale, if not, it creates it |
|
31
|
|
|
*/ |
|
32
|
|
|
public function addTranslation(TranslationGroup $translationGroup, $locale, $text, $filename) |
|
33
|
|
|
{ |
|
34
|
|
|
$translation = new \Kunstmaan\TranslatorBundle\Entity\Translation(); |
|
35
|
|
|
$translation->setLocale($locale); |
|
36
|
|
|
$translation->setText($text); |
|
37
|
|
|
$translation->setDomain($translationGroup->getDomain()); |
|
38
|
|
|
$translation->setFile($filename); |
|
39
|
|
|
$translation->setKeyword($translationGroup->getKeyword()); |
|
40
|
|
|
$translation->setCreatedAt(new \DateTime()); |
|
41
|
|
|
$translation->setUpdatedAt(new \DateTime()); |
|
42
|
|
|
$translation->setTranslationId($translationGroup->getId()); |
|
43
|
|
|
|
|
44
|
|
|
$this->translationRepository->persist($translation); |
|
45
|
|
|
$this->dbCopy[] = $translation; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function updateTranslation(TranslationGroup $translationGroup, $locale, $text, $filename) |
|
49
|
|
|
{ |
|
50
|
|
|
$translation = $translationGroup->getTranslationByLocale($locale); |
|
51
|
|
|
$translation->setText($text); |
|
52
|
|
|
$translation->setFile($filename); |
|
53
|
|
|
|
|
54
|
|
|
$this->translationRepository->persist($translation); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function pullDBInMemory() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->dbCopy = $this->translationRepository->findAll(); |
|
60
|
|
|
$this->maxId = $this->translationRepository->getUniqueTranslationId(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function flushAndClearDBFromMemory() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->translationRepository->flush(); |
|
66
|
|
|
unset($this->dbCopy); |
|
67
|
|
|
$this->maxId = 1; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns a TranslationGroup with the given keyword and domain, and fills in the translations |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getTranslationGroupByKeywordAndDomain($keyword, $domain) |
|
74
|
|
|
{ |
|
75
|
|
|
$translations = $this->findTranslations($keyword, $domain); |
|
76
|
|
|
$translationGroup = new TranslationGroup(); |
|
77
|
|
|
$translationGroup->setDomain($domain); |
|
78
|
|
|
$translationGroup->setKeyword($keyword); |
|
79
|
|
|
if (empty($translations)) { |
|
80
|
|
|
$translationGroup->setId($this->maxId); |
|
81
|
|
|
++$this->maxId; |
|
82
|
|
|
} else { |
|
83
|
|
|
$translationGroup->setId($translations[0]->getTranslationId()); |
|
84
|
|
|
} |
|
85
|
|
|
$translationGroup->setTranslations($translations); |
|
86
|
|
|
|
|
87
|
|
|
return $translationGroup; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function findTranslations($keyword, $domain) |
|
91
|
|
|
{ |
|
92
|
|
|
$result = []; |
|
93
|
|
|
|
|
94
|
|
|
/** @var \Kunstmaan\TranslatorBundle\Entity\Translation $translation */ |
|
95
|
|
|
foreach ($this->dbCopy as $translation) { |
|
96
|
|
|
if ($translation->getKeyword() === $keyword && $translation->getDomain() === $domain) { |
|
97
|
|
|
$result[] = $translation; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $result; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|