|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\I18nBundle\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Orm\EntityManager; |
|
6
|
|
|
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate; |
|
7
|
|
|
use Victoire\Bundle\CoreBundle\Entity\View; |
|
8
|
|
|
use Victoire\Bundle\PageBundle\Entity\BasePage; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Page helper |
|
12
|
|
|
* ref: victoire_i18n.view_translation_manager. |
|
13
|
|
|
*/ |
|
14
|
|
|
class ViewTranslationManager |
|
15
|
|
|
{ |
|
16
|
|
|
protected $parameterConverter; |
|
17
|
|
|
protected $businessEntityHelper; |
|
18
|
|
|
protected $em; |
|
19
|
|
|
protected $viewReferenceBuilder; |
|
20
|
|
|
protected $viewReferenceHelper; |
|
21
|
|
|
protected $viewReferenceProvider; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param EntityManager $entityManager |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(EntityManager $entityManager) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$this->em = $entityManager; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* this methods allow you to add a translation to any view |
|
35
|
|
|
* recursively to its subview. |
|
36
|
|
|
* |
|
37
|
|
|
* @param View $view, the view to translatate |
|
|
|
|
|
|
38
|
|
|
* @param $templatename the new name of the view |
|
39
|
|
|
* @param $loopindex the current loop of iteration in recursion |
|
40
|
|
|
* @param $locale the target locale to translate view |
|
41
|
|
|
* |
|
42
|
|
|
* @return View |
|
43
|
|
|
*/ |
|
44
|
|
|
public function addTranslation(View $view, $viewName, $locale) |
|
45
|
|
|
{ |
|
46
|
|
|
$template = null; |
|
47
|
|
|
if ($view->getTemplate()) { |
|
48
|
|
|
$template = $view->getTemplate(); |
|
49
|
|
|
if ($template->getI18n()->getTranslation($locale)) { |
|
|
|
|
|
|
50
|
|
|
$template = $template->getI18n()->getTranslation($locale); |
|
|
|
|
|
|
51
|
|
|
} else { |
|
52
|
|
|
$templateName = $template->getName().'-'.$locale; |
|
|
|
|
|
|
53
|
|
|
$this->em->refresh($view); |
|
54
|
|
|
$template = $this->addTranslation($template, $templateName, $locale); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
$view->setLocale($locale); |
|
58
|
|
|
$view->setTemplate($template); |
|
59
|
|
|
$clonedView = $this->cloneView($view, $viewName, $locale); |
|
|
|
|
|
|
60
|
|
|
if ($clonedView instanceof BasePage && $view->getTemplate()) { |
|
61
|
|
|
$template->addPage($clonedView); |
|
62
|
|
|
} |
|
63
|
|
|
$i18n = $view->getI18n(); |
|
64
|
|
|
$i18n->setTranslation($locale, $clonedView); |
|
|
|
|
|
|
65
|
|
|
$this->em->persist($clonedView); |
|
66
|
|
|
$this->em->refresh($view); |
|
67
|
|
|
$this->em->flush(); |
|
68
|
|
|
|
|
69
|
|
|
return $clonedView; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* this methods allows you to clone a view and its widgets and also the widgetmap. |
|
74
|
|
|
* |
|
75
|
|
|
* @param View $view |
|
76
|
|
|
* @param $templateName |
|
77
|
|
|
* |
|
78
|
|
|
* @return View |
|
79
|
|
|
*/ |
|
80
|
|
|
public function cloneView(View $view, $templateName = null) |
|
81
|
|
|
{ |
|
82
|
|
|
$clonedView = clone $view; |
|
83
|
|
|
$this->em->refresh($view); |
|
84
|
|
|
$clonedView->setSlug(null); |
|
85
|
|
|
$widgetMapClone = $clonedView->getWidgetMaps(); |
|
86
|
|
|
$arrayMapOfWidgetMap = []; |
|
87
|
|
|
if (null !== $templateName) { |
|
88
|
|
|
$clonedView->setName($templateName); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$clonedView->setId(null); |
|
|
|
|
|
|
92
|
|
|
$this->em->persist($clonedView); |
|
93
|
|
|
|
|
94
|
|
|
if (!$clonedView instanceof BusinessTemplate) { |
|
95
|
|
|
$widgetLayoutSlots = []; |
|
96
|
|
|
$newWidgets = []; |
|
97
|
|
|
foreach ($clonedView->getWidgets() as $widgetKey => $widgetVal) { |
|
|
|
|
|
|
98
|
|
|
$clonedWidget = clone $widgetVal; |
|
99
|
|
|
$clonedWidget->setId(null); |
|
100
|
|
|
$clonedWidget->setView($clonedView); |
|
101
|
|
|
$this->em->persist($clonedWidget); |
|
102
|
|
|
$newWidgets[] = $clonedWidget; |
|
103
|
|
|
$arrayMapOfWidgetMap[$widgetVal->getId()] = $clonedWidget; |
|
104
|
|
|
if ($widgetVal instanceof WidgetLayout) { |
|
|
|
|
|
|
105
|
|
|
$id = $widgetVal->getId(); |
|
106
|
|
|
$widgetLayoutSlots[$id] = $clonedWidget; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
$clonedView->setWidgets($newWidgets); |
|
|
|
|
|
|
110
|
|
|
$this->em->persist($clonedView); |
|
111
|
|
|
$this->em->flush(); |
|
112
|
|
|
$widgetSlotMap = []; |
|
113
|
|
|
foreach ($widgetLayoutSlots as $_id => $_widget) { |
|
114
|
|
|
foreach ($clonedView->getWidgets() as $_clonedWidget) { |
|
|
|
|
|
|
115
|
|
|
if (preg_match('/^'.$_id.'_(.)/', $_clonedWidget->getSlot(), $matches)) { |
|
116
|
|
|
$newSlot = $_widget->getId().'_'.$matches[1]; |
|
117
|
|
|
$oldSlot = $_clonedWidget->getSlot(); |
|
118
|
|
|
$_clonedWidget->setSlot($newSlot); |
|
119
|
|
|
$widgetSlotMap[$oldSlot] = $newSlot; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$this->em->flush(); |
|
125
|
|
|
foreach ($widgetMapClone as $wigetSlotCloneKey => $widgetSlotCloneVal) { |
|
126
|
|
|
foreach ($widgetSlotCloneVal as $widgetMapItemKey => $widgetMapItemVal) { |
|
127
|
|
|
if (isset($arrayMapOfWidgetMap[$widgetMapItemVal['widgetId']])) { |
|
128
|
|
|
$widgetId = $arrayMapOfWidgetMap[$widgetMapItemVal['widgetId']]->getId(); |
|
129
|
|
|
$widgetMapItemVal['widgetId'] = $widgetId; |
|
130
|
|
|
if (array_key_exists($wigetSlotCloneKey, $widgetSlotMap)) { |
|
131
|
|
|
$wigetSlotCloneKey = $widgetSlotMap[$wigetSlotCloneKey]; |
|
132
|
|
|
} |
|
133
|
|
|
$widgetMapClone[$wigetSlotCloneKey][$widgetMapItemKey] = $widgetMapItemVal; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$clonedView->setSlots([]); |
|
|
|
|
|
|
139
|
|
|
$clonedView->setWidgetMap($widgetMapClone); |
|
|
|
|
|
|
140
|
|
|
$this->em->persist($clonedView); |
|
141
|
|
|
$this->em->flush(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $clonedView; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.