|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\API; |
|
6
|
|
|
|
|
7
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\BaseView; |
|
8
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\CachableView; |
|
9
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\ContentValueView; |
|
10
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\LocationValueView; |
|
11
|
|
|
use Netgen\InformationCollection\Handler; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
13
|
|
|
|
|
14
|
|
|
trait InformationCollectionTrait |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Builds Form, checks if Form is valid and dispatches InformationCollected event. |
|
18
|
|
|
* |
|
19
|
|
|
* @param \eZ\Publish\Core\MVC\Symfony\View\ContentValueView $view |
|
20
|
|
|
* @param array $options |
|
21
|
|
|
* |
|
22
|
|
|
* @return array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function collectInformation(ContentValueView $view, array $options): ContentValueView |
|
25
|
|
|
{ |
|
26
|
|
|
$isValid = false; |
|
27
|
|
|
|
|
28
|
|
|
if (!$view instanceof LocationValueView) { |
|
29
|
|
|
throw new \BadMethodCallException('eZ view needs to implement LocationValueView interface'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** @var Handler $handler */ |
|
33
|
|
|
$handler = $this->container->get('netgen_information_collection.handler'); |
|
|
|
|
|
|
34
|
|
|
/** @var RequestStack $requestStack */ |
|
35
|
|
|
$requestStack = $this->container->get('request_stack'); |
|
36
|
|
|
|
|
37
|
|
|
$form = $handler->getForm($view->getContent(), $view->getLocation()); |
|
38
|
|
|
$request = $requestStack->getCurrentRequest(); |
|
39
|
|
|
$form->handleRequest($request); |
|
40
|
|
|
|
|
41
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
42
|
|
|
$isValid = true; |
|
43
|
|
|
|
|
44
|
|
|
$handler->handle($form->getData(), $options); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($view instanceof BaseView) { |
|
48
|
|
|
$view->addParameters([ |
|
49
|
|
|
'is_valid' => $isValid, |
|
50
|
|
|
'form' => $form->createView(), |
|
51
|
|
|
'collected_fields' => $form->getData()->getFieldsData(), |
|
52
|
|
|
]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($view instanceof CachableView) { |
|
56
|
|
|
$view->setCacheEnabled(false); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $view; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: