1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\NodeBundle\Helper\Services; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\ORMException; |
7
|
|
|
use Kunstmaan\AdminBundle\Repository\UserRepository; |
8
|
|
|
use Kunstmaan\NodeBundle\Entity\HasNodeInterface; |
9
|
|
|
use Kunstmaan\NodeBundle\Entity\Node; |
10
|
|
|
use Kunstmaan\NodeBundle\Repository\NodeRepository; |
11
|
|
|
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface; |
12
|
|
|
use Kunstmaan\SeoBundle\Repository\SeoRepository; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Service to create new pages. |
17
|
|
|
*/ |
18
|
|
|
class PageCreatorService |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var EntityManager |
22
|
|
|
*/ |
23
|
|
|
protected $entityManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ACLPermissionCreatorService |
27
|
|
|
*/ |
28
|
|
|
protected $aclPermissionCreatorService; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $userEntityClass; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
public function setEntityManager($entityManager) |
37
|
|
|
{ |
38
|
|
|
$this->entityManager = $entityManager; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setACLPermissionCreatorService($aclPermissionCreatorService) |
42
|
|
|
{ |
43
|
|
|
$this->aclPermissionCreatorService = $aclPermissionCreatorService; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setUserEntityClass($userEntityClass) |
47
|
|
|
{ |
48
|
|
|
$this->userEntityClass = $userEntityClass; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets the Container. This is still here for backwards compatibility. |
53
|
|
|
* |
54
|
|
|
* The ContainerAwareInterface has been removed so the container won't be injected automatically. |
55
|
|
|
* This function is just there for code that calls it manually. |
56
|
|
|
* |
57
|
|
|
* @param ContainerInterface $container A ContainerInterface instance. |
|
|
|
|
58
|
|
|
* |
59
|
|
|
* @api |
60
|
|
|
*/ |
61
|
|
|
public function setContainer(ContainerInterface $container = null) |
62
|
|
|
{ |
63
|
|
|
$this->setEntityManager($container->get('doctrine.orm.entity_manager')); |
|
|
|
|
64
|
|
|
$this->setACLPermissionCreatorService($container->get('kunstmaan_node.acl_permission_creator_service')); |
65
|
|
|
$this->setUserEntityClass($container->getParameter('fos_user.model.user.class')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param HasNodeInterface $pageTypeInstance The page. |
70
|
|
|
* @param array $translations Containing arrays. Sample: |
71
|
|
|
* [ |
72
|
|
|
* [ "language" => "nl", |
73
|
|
|
* "callback" => function($page, $translation) { |
74
|
|
|
* $translation->setTitle('NL titel'); |
75
|
|
|
* } |
76
|
|
|
* ], |
77
|
|
|
* [ "language" => "fr", |
78
|
|
|
* "callback" => function($page, $translation) { |
79
|
|
|
* $translation->setTitle('FR titel'); |
80
|
|
|
* } |
81
|
|
|
* ] |
82
|
|
|
* ] |
83
|
|
|
* Perhaps it's cleaner when you create one array and append another array for each language. |
84
|
|
|
* |
85
|
|
|
* @param array $options Possible options: |
86
|
|
|
* parent: type node, nodetransation or page. |
87
|
|
|
* page_internal_name: string. name the page will have in the database. |
88
|
|
|
* set_online: bool. if true the page will be set as online after creation. |
89
|
|
|
* hidden_from_nav: bool. if true the page will not be show in the navigation |
90
|
|
|
* creator: username |
91
|
|
|
* |
92
|
|
|
* Automatically calls the ACL + sets the slugs to empty when the page is an Abstract node. |
93
|
|
|
* |
94
|
|
|
* @return Node The new node for the page. |
|
|
|
|
95
|
|
|
* |
96
|
|
|
* @throws \InvalidArgumentException |
97
|
|
|
*/ |
98
|
|
|
public function createPage(HasNodeInterface $pageTypeInstance, array $translations, array $options = array()) |
99
|
|
|
{ |
100
|
|
|
if (is_null($options)) { |
101
|
|
|
$options = array(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (is_null($translations) || (count($translations) == 0)) { |
105
|
|
|
throw new \InvalidArgumentException('There has to be at least 1 translation in the translations array'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$em = $this->entityManager; |
109
|
|
|
|
110
|
|
|
/** @var NodeRepository $nodeRepo */ |
111
|
|
|
$nodeRepo = $em->getRepository('KunstmaanNodeBundle:Node'); |
112
|
|
|
/** @var $userRepo UserRepository */ |
113
|
|
|
$userRepo = $em->getRepository($this->userEntityClass); |
114
|
|
|
/** @var $seoRepo SeoRepository */ |
115
|
|
|
try { |
116
|
|
|
$seoRepo = $em->getRepository('KunstmaanSeoBundle:Seo'); |
117
|
|
|
} catch (ORMException $e) { |
118
|
|
|
$seoRepo = null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$pagecreator = array_key_exists('creator', $options) ? $options['creator'] : 'pagecreator'; |
122
|
|
|
$creator = $userRepo->findOneBy(array('username' => $pagecreator)); |
123
|
|
|
|
124
|
|
|
$parent = isset($options['parent']) ? $options['parent'] : null; |
125
|
|
|
|
126
|
|
|
$pageInternalName = isset($options['page_internal_name']) ? $options['page_internal_name'] : null; |
127
|
|
|
|
128
|
|
|
$setOnline = isset($options['set_online']) ? $options['set_online'] : false; |
129
|
|
|
|
130
|
|
|
// We need to get the language of the first translation so we can create the rootnode. |
131
|
|
|
// This will also create a translationnode for that language attached to the rootnode. |
132
|
|
|
$first = true; |
133
|
|
|
$rootNode = null; |
134
|
|
|
|
135
|
|
|
/* @var \Kunstmaan\NodeBundle\Repository\NodeTranslationRepository $nodeTranslationRepo*/ |
136
|
|
|
$nodeTranslationRepo = $em->getRepository('KunstmaanNodeBundle:NodeTranslation'); |
137
|
|
|
|
138
|
|
|
foreach ($translations as $translation) { |
139
|
|
|
$language = $translation['language']; |
140
|
|
|
$callback = $translation['callback']; |
141
|
|
|
|
142
|
|
|
$translationNode = null; |
|
|
|
|
143
|
|
|
if ($first) { |
144
|
|
|
$first = false; |
145
|
|
|
|
146
|
|
|
$em->persist($pageTypeInstance); |
147
|
|
|
$em->flush($pageTypeInstance); |
148
|
|
|
|
149
|
|
|
// Fetch the translation instead of creating it. |
150
|
|
|
// This returns the rootnode. |
151
|
|
|
$rootNode = $nodeRepo->createNodeFor($pageTypeInstance, $language, $creator, $pageInternalName); |
|
|
|
|
152
|
|
|
|
153
|
|
|
if (array_key_exists('hidden_from_nav', $options)) { |
154
|
|
|
$rootNode->setHiddenFromNav($options['hidden_from_nav']); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
if (!is_null($parent)) { |
158
|
|
|
if ($parent instanceof HasPagePartsInterface) { |
159
|
|
|
$parent = $nodeRepo->getNodeFor($parent); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
$rootNode->setParent($parent); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$em->persist($rootNode); |
165
|
|
|
$em->flush($rootNode); |
166
|
|
|
|
167
|
|
|
$translationNode = $rootNode->getNodeTranslation($language, true); |
168
|
|
|
} else { |
169
|
|
|
// Clone the $pageTypeInstance. |
170
|
|
|
$pageTypeInstance = clone $pageTypeInstance; |
171
|
|
|
|
172
|
|
|
$em->persist($pageTypeInstance); |
173
|
|
|
$em->flush($pageTypeInstance); |
174
|
|
|
|
175
|
|
|
// Create the translationnode. |
176
|
|
|
$translationNode = $nodeTranslationRepo->createNodeTranslationFor($pageTypeInstance, $language, $rootNode, $creator); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Make SEO. |
180
|
|
|
$seo = null; |
181
|
|
|
|
182
|
|
|
if (!is_null($seoRepo)) { |
183
|
|
|
$seo = $seoRepo->findOrCreateFor($pageTypeInstance); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$callback($pageTypeInstance, $translationNode, $seo); |
187
|
|
|
|
188
|
|
|
// Overwrite the page title with the translated title |
189
|
|
|
$pageTypeInstance->setTitle($translationNode->getTitle()); |
190
|
|
|
$em->persist($pageTypeInstance); |
191
|
|
|
$em->persist($translationNode); |
192
|
|
|
$em->flush($pageTypeInstance); |
193
|
|
|
$em->flush($translationNode); |
194
|
|
|
|
195
|
|
|
$translationNode->setOnline($setOnline); |
196
|
|
|
|
197
|
|
|
if (!is_null($seo)) { |
198
|
|
|
$em->persist($seo); |
199
|
|
|
$em->flush($seo); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$em->persist($translationNode); |
203
|
|
|
$em->flush($translationNode); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// ACL |
207
|
|
|
$this->aclPermissionCreatorService->createPermission($rootNode); |
|
|
|
|
208
|
|
|
|
209
|
|
|
return $rootNode; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.