|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\SeoBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
9
|
|
|
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage; |
|
10
|
|
|
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate; |
|
11
|
|
|
use Victoire\Bundle\CoreBundle\Controller\VictoireAlertifyControllerTrait; |
|
12
|
|
|
use Victoire\Bundle\CoreBundle\Entity\View; |
|
13
|
|
|
use Victoire\Bundle\PageBundle\Entity\BasePage; |
|
14
|
|
|
use Victoire\Bundle\SeoBundle\Entity\PageSeo; |
|
15
|
|
|
use Victoire\Bundle\SeoBundle\Form\PageSeoType; |
|
16
|
|
|
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The Page seo controller. |
|
20
|
|
|
* |
|
21
|
|
|
* @Route("/victoire-dcms/seo") |
|
22
|
|
|
*/ |
|
23
|
|
|
class PageSeoController extends Controller |
|
24
|
|
|
{ |
|
25
|
|
|
use VictoireAlertifyControllerTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* BasePage settings. |
|
29
|
|
|
* |
|
30
|
|
|
* @param BasePage $page |
|
31
|
|
|
* |
|
32
|
|
|
* @Route("/{id}", name="victoire_seo_pageSeo_settings") |
|
33
|
|
|
* @Template() |
|
34
|
|
|
* |
|
35
|
|
|
* @return JsonResponse |
|
36
|
|
|
*/ |
|
37
|
|
|
public function settingsAction(View $page) |
|
38
|
|
|
{ |
|
39
|
|
|
//services |
|
40
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
41
|
|
|
|
|
42
|
|
|
$businessProperties = []; |
|
43
|
|
|
|
|
44
|
|
|
//if the page is a business entity template page |
|
45
|
|
|
if ($page instanceof BusinessPage || $page instanceof BusinessTemplate) { |
|
46
|
|
|
//we can use the business entity properties on the seo |
|
47
|
|
|
$businessEntity = $this->get('victoire_core.helper.business_entity_helper')->findById($page->getBusinessEntityId()); |
|
48
|
|
|
$businessProperties = $businessEntity->getBusinessPropertiesByType('seoable'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$pageSeo = $page->getSeo() ? $page->getSeo() : new PageSeo($page); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
//url for the form |
|
54
|
|
|
$formUrl = $this->get('router')->generate('victoire_seo_pageSeo_settings', |
|
55
|
|
|
[ |
|
56
|
|
|
'id' => $page->getId(), |
|
57
|
|
|
] |
|
58
|
|
|
); |
|
59
|
|
|
//create the form |
|
60
|
|
|
$form = $this->get('form.factory')->create(PageSeoType::class, $pageSeo, |
|
61
|
|
|
[ |
|
62
|
|
|
'action' => $formUrl, |
|
63
|
|
|
'method' => 'POST', |
|
64
|
|
|
] |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$form->handleRequest($this->get('request')); |
|
68
|
|
|
$novalidate = $this->get('request')->query->get('novalidate', false); |
|
69
|
|
|
|
|
70
|
|
|
$template = 'VictoireSeoBundle:PageSeo:form.html.twig'; |
|
71
|
|
|
if ($novalidate === false) { |
|
72
|
|
|
$template = 'VictoireSeoBundle:PageSeo:settings.html.twig'; |
|
73
|
|
|
} |
|
74
|
|
|
if (false === $novalidate && $form->isValid()) { |
|
75
|
|
|
$em->persist($pageSeo); |
|
76
|
|
|
$page->setSeo($pageSeo); |
|
|
|
|
|
|
77
|
|
|
$em->persist($page); |
|
78
|
|
|
$em->flush(); |
|
79
|
|
|
|
|
80
|
|
|
//redirect to the page url |
|
81
|
|
|
if (!method_exists($page, 'getUrl')) { |
|
82
|
|
|
$url = $this->generateUrl('victoire_business_template_show', ['id' => $page->getId()]); |
|
83
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
84
|
|
|
/** @var ViewReference $viewReference */ |
|
85
|
|
|
$viewReference = $this->container->get('victoire_view_reference.repository') |
|
86
|
|
|
->getOneReferenceByParameters(['viewId' => $page->getId()]); |
|
87
|
|
|
|
|
88
|
|
|
$page->setReference($viewReference); |
|
89
|
|
|
$url = $this->generateUrl('victoire_core_page_show', ['url' => $viewReference->getUrl()]); |
|
90
|
|
|
} |
|
91
|
|
|
$this->get('victoire_core.current_view')->setCurrentView($page); |
|
92
|
|
|
$this->congrat('victoire_seo.save.success'); |
|
93
|
|
|
|
|
94
|
|
|
return new JsonResponse([ |
|
95
|
|
|
'success' => true, |
|
96
|
|
|
'url' => $url, |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return new JsonResponse([ |
|
101
|
|
|
'success' => !$form->isSubmitted(), |
|
102
|
|
|
'html' => $this->container->get('templating')->render( |
|
103
|
|
|
$template, |
|
104
|
|
|
[ |
|
105
|
|
|
'page' => $page, |
|
106
|
|
|
'form' => $form->createView(), |
|
107
|
|
|
'businessProperties' => $businessProperties, |
|
108
|
|
|
] |
|
109
|
|
|
), |
|
110
|
|
|
]); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: