Skip to content

Commit db97347

Browse files
committed
Merge pull request #49 from chyzas/feature-messages-history
Implemented backend of translation history
2 parents 21ffee4 + 02ce648 commit db97347

File tree

18 files changed

+678
-16
lines changed

18 files changed

+678
-16
lines changed

Controller/ApiController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,16 @@ public function exportAction()
119119
$this->get('ongr_translations.command.export')->run(new ArrayInput([]), new NullOutput())
120120
);
121121
}
122+
123+
/**
124+
* Action for executing history command.
125+
*
126+
* @param Request $request Http request object.
127+
*
128+
* @return JsonResponse
129+
*/
130+
public function historyAction(Request $request)
131+
{
132+
return new JsonResponse($this->get('ongr_translations.history_manager')->history($request));
133+
}
122134
}

DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public function getConfigTreeBuilder()
5555
->defaultValue([])
5656
->end()
5757
->end()
58+
->booleanNode('history')
59+
->info('History enable/disable. Default is enable.')
60+
->defaultTrue()
61+
->end()
5862
->end()
5963
->end();
6064

DependencyInjection/ONGRTranslationsExtension.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public function load(array $configs, ContainerBuilder $container)
4545
$this->setFiltersManager($config['repository'], $container);
4646
$this->setTranslationManager($config['repository'], $container);
4747
$this->setControllerManager($config['repository'], 'list', $container);
48+
$this->setHistoryManager($this->editRepositoryName($config['repository']), $container);
49+
if ($config['history']) {
50+
$this->setEditMessageEvent($this->editRepositoryName($config['repository']), $container);
51+
}
4852
}
4953

5054
/**
@@ -59,12 +63,31 @@ private function setTranslationManager($repositoryId, ContainerBuilder $containe
5963
'ONGR\TranslationsBundle\Translation\TranslationManager',
6064
[
6165
new Reference($repositoryId),
66+
new Reference('event_dispatcher'),
6267
]
6368
);
6469

6570
$container->setDefinition('ongr_translations.translation_manager', $definition);
6671
}
6772

73+
/**
74+
* Adds history manager.
75+
*
76+
* @param string $repositoryId
77+
* @param ContainerBuilder $container
78+
*/
79+
private function setHistoryManager($repositoryId, ContainerBuilder $container)
80+
{
81+
$definition = new Definition(
82+
'ONGR\TranslationsBundle\Translation\HistoryManager',
83+
[
84+
new Reference($repositoryId),
85+
]
86+
);
87+
88+
$container->setDefinition('ongr_translations.history_manager', $definition);
89+
}
90+
6891
/**
6992
* Sets elasticsearch storage for translations.
7093
*
@@ -141,4 +164,37 @@ private function validateBundles($container, $bundles)
141164
}
142165
$container->setParameter('ongr_translations.bundles', $bundles);
143166
}
167+
168+
/**
169+
* Validates edit message event.
170+
*
171+
* @param string $repositoryId
172+
* @param ContainerBuilder $container
173+
*/
174+
private function setEditMessageEvent($repositoryId, ContainerBuilder $container)
175+
{
176+
$definition = new Definition(
177+
'ONGR\TranslationsBundle\Event\HistoryListener',
178+
[
179+
new Reference($repositoryId),
180+
]
181+
);
182+
$definition->addTag(
183+
'kernel.event_listener',
184+
['event' => 'translation.history.add', 'method' => 'addToHistory']
185+
);
186+
$container->setDefinition('ongr_translations.es_manager', $definition);
187+
}
188+
189+
/**
190+
* Edits repository name.
191+
*
192+
* @param string $repository
193+
*
194+
* @return string
195+
*/
196+
private function editRepositoryName($repository)
197+
{
198+
return substr_replace($repository, 'history', strrpos($repository, '.') + 1);
199+
}
144200
}

Document/History.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\TranslationsBundle\Document;
13+
14+
use ONGR\ElasticsearchBundle\Annotation as ES;
15+
use ONGR\ElasticsearchBundle\Document\AbstractDocument;
16+
17+
/**
18+
* Holds translations history.
19+
*
20+
* @ES\Document(type="history")
21+
*/
22+
class History extends AbstractDocument
23+
{
24+
/**
25+
* @var string
26+
*
27+
* @ES\Property(name="key", type="string", index="not_analyzed")
28+
*/
29+
public $key;
30+
31+
/**
32+
* @var string
33+
*
34+
* @ES\Property(name="locale", type="string", index="not_analyzed")
35+
*/
36+
public $locale;
37+
38+
/**
39+
* @var string
40+
*
41+
* @ES\Property(name="message", type="string", index="not_analyzed")
42+
*/
43+
public $message;
44+
45+
/**
46+
* @var string
47+
*
48+
* @ES\Property(name="domain", type="string", index="not_analyzed")
49+
*/
50+
public $domain;
51+
52+
/**
53+
* @var \DateTime
54+
*
55+
* @ES\Property(name="created_at", type="date")
56+
*/
57+
private $createdAt;
58+
59+
/**
60+
* Sets timestamps.
61+
*/
62+
public function __construct()
63+
{
64+
$this->createdAt = new \DateTime();
65+
}
66+
67+
/**
68+
* @return \DateTime
69+
*/
70+
public function getCreatedAt()
71+
{
72+
return $this->createdAt;
73+
}
74+
75+
/**
76+
* @param \DateTime $createdAt
77+
*/
78+
public function setCreatedAt($createdAt)
79+
{
80+
$this->createdAt = $createdAt;
81+
}
82+
}

Event/Events.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\TranslationsBundle\Event;
13+
14+
/**
15+
* Static class for events names constants.
16+
*/
17+
final class Events
18+
{
19+
/**
20+
* Event dispatched after a message is edited.
21+
*/
22+
const ADD_HISTORY = 'translation.history.add';
23+
}

Event/HistoryListener.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\TranslationsBundle\Event;
13+
14+
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
15+
use ONGR\ElasticsearchBundle\ORM\Repository;
16+
use Symfony\Component\Config\Definition\Exception\Exception;
17+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
18+
19+
/**
20+
* Listens for edit message request event and add old message to history.
21+
*/
22+
class HistoryListener
23+
{
24+
/**
25+
* @var Repository
26+
*/
27+
private $repository;
28+
29+
/**
30+
* @param Repository $repository
31+
*/
32+
public function __construct(Repository $repository)
33+
{
34+
$this->repository = $repository;
35+
}
36+
37+
/**
38+
* Edit message event.
39+
*
40+
* @param TranslationEditMessageEvent $event
41+
*/
42+
public function addToHistory(TranslationEditMessageEvent $event)
43+
{
44+
$manager = $this->repository->getManager();
45+
$document = $event->getDocument();
46+
$locale = $this->getLocale($event);
47+
$oldMessage = $this->getOldMessage($document, $locale);
48+
49+
$repository = $manager->getRepository('ONGRTranslationsBundle:History');
50+
$historyDocument = $this->setDocument($document, $repository, $oldMessage, $locale);
51+
52+
$manager->persist($historyDocument);
53+
$manager->commit();
54+
}
55+
56+
/**
57+
* @param TranslationEditMessageEvent $event
58+
*
59+
* @return string
60+
*/
61+
private function getLocale($event)
62+
{
63+
$request = $event->getRequest();
64+
$content = json_decode($request->getContent());
65+
66+
return $content->properties->locale;
67+
}
68+
69+
/**
70+
* @param DocumentInterface $document
71+
* @param Repository $repository
72+
* @param string $oldMessage
73+
* @param string $locale
74+
*
75+
* @return mixed
76+
*/
77+
private function setDocument($document, $repository, $oldMessage, $locale)
78+
{
79+
$newDocument = $repository->createDocument();
80+
$key = $document->getKey();
81+
$domain = $document->getDomain();
82+
$newDocument->setKey($key);
83+
$newDocument->setLocale($locale);
84+
$newDocument->setMessage($oldMessage);
85+
$newDocument->setDomain($domain);
86+
$newDocument->setId(sha1($document->getId() . $oldMessage));
87+
$newDocument->setCreatedAt(new \DateTime());
88+
89+
return $newDocument;
90+
}
91+
92+
/**
93+
* @param DocumentInterface $document
94+
* @param string $locale
95+
*
96+
* @return mixed
97+
*/
98+
private function getOldMessage($document, $locale)
99+
{
100+
$messages = $document->getMessages();
101+
foreach ($messages as $message) {
102+
$messageLocale = $message->getLocale();
103+
if ($locale == $messageLocale) {
104+
$oldMessage = $message->getMessage();
105+
106+
return $oldMessage;
107+
}
108+
}
109+
}
110+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\TranslationsBundle\Event;
13+
14+
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
15+
use Symfony\Component\EventDispatcher\Event;
16+
use Symfony\Component\HttpFoundation\Request;
17+
18+
/**
19+
* Event for edit message action.
20+
*/
21+
class TranslationEditMessageEvent extends Event
22+
{
23+
/**
24+
* @var Request
25+
*/
26+
private $request;
27+
28+
/**
29+
* @var DocumentInterface
30+
*/
31+
private $document;
32+
33+
/**
34+
* @param Request $request
35+
* @param DocumentInterface $document
36+
*/
37+
public function __construct(Request $request, DocumentInterface $document)
38+
{
39+
$this->request = $request;
40+
$this->document = $document;
41+
}
42+
43+
/**
44+
* @return Request
45+
*/
46+
public function getRequest()
47+
{
48+
return $this->request;
49+
}
50+
51+
/**
52+
* Returns document associated with the event.
53+
*
54+
* @return DocumentInterface
55+
*/
56+
public function getDocument()
57+
{
58+
return $this->document;
59+
}
60+
}

Resources/config/routing.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ ongr_translations_api_export:
5151
_controller: ONGRTranslationsBundle:Api:export
5252
options:
5353
expose: true
54+
55+
ongr_translations_api_history:
56+
pattern: /_api/history
57+
methods: [POST]
58+
defaults:
59+
_controller: ONGRTranslationsBundle:Api:history
60+
options:
61+
expose: true

0 commit comments

Comments
 (0)