1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\InformationCollectionBundle\Form\Builder; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\ContentTypeService; |
6
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
7
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataWrapper; |
8
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Form\Payload\InformationCollectionStruct; |
9
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Form\Type\InformationCollectionType; |
10
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
11
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
12
|
|
|
use Symfony\Component\Routing\RouterInterface; |
13
|
|
|
|
14
|
|
|
class FormBuilder |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var FormFactoryInterface |
18
|
|
|
*/ |
19
|
|
|
protected $formFactory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $useCsrf; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ContentTypeService |
28
|
|
|
*/ |
29
|
|
|
protected $contentTypeService; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RouterInterface |
33
|
|
|
*/ |
34
|
|
|
protected $router; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* FormBuilder constructor. |
38
|
|
|
* |
39
|
|
|
* @param FormFactoryInterface $formFactory |
40
|
|
|
* @param ContentTypeService $contentTypeService |
41
|
|
|
* @param RouterInterface $router |
42
|
|
|
* @param bool $useCsrf |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
FormFactoryInterface $formFactory, |
46
|
|
|
ContentTypeService $contentTypeService, |
47
|
|
|
RouterInterface $router, |
48
|
|
|
$useCsrf |
49
|
|
|
) { |
50
|
|
|
$this->formFactory = $formFactory; |
51
|
|
|
$this->useCsrf = $useCsrf; |
52
|
|
|
$this->contentTypeService = $contentTypeService; |
53
|
|
|
$this->router = $router; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Creates Information collection Form object for given Location object. |
58
|
|
|
* |
59
|
|
|
* @param Location $location |
60
|
|
|
* |
61
|
|
|
* @return FormBuilderInterface |
62
|
|
|
*/ |
63
|
|
|
public function createFormForLocation(Location $location) |
64
|
|
|
{ |
65
|
|
|
return $this->createBuilder($location); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Creates Information collection Form object for given Location object. |
70
|
|
|
* Form handling is supposed to be done by Ajax. |
71
|
|
|
* |
72
|
|
|
* @param Location $location |
73
|
|
|
* |
74
|
|
|
* @return FormBuilderInterface |
75
|
|
|
*/ |
76
|
|
|
public function createFormForLocationWithAjax(Location $location) |
77
|
|
|
{ |
78
|
|
|
$formBuilder = $this->createBuilder($location); |
79
|
|
|
$formBuilder->setAction($this->router->generate('netgen_information_collection_handle_ajax', array('location' => $location->id))); |
80
|
|
|
|
81
|
|
|
return $formBuilder; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Creates FormBuilder with default settings |
86
|
|
|
* |
87
|
|
|
* @param Location $location |
88
|
|
|
* |
89
|
|
|
* @return FormBuilderInterface |
90
|
|
|
*/ |
91
|
|
|
protected function createBuilder(Location $location) |
92
|
|
|
{ |
93
|
|
|
$contentInfo = $location->contentInfo; |
94
|
|
|
$contentType = $this->contentTypeService->loadContentType($contentInfo->contentTypeId); |
95
|
|
|
$data = new DataWrapper(new InformationCollectionStruct(), $contentType, $location); |
96
|
|
|
|
97
|
|
|
$formBuilder = $this->formFactory |
98
|
|
|
->createBuilder( |
99
|
|
|
InformationCollectionType::class, |
100
|
|
|
$data, |
101
|
|
|
array( |
102
|
|
|
'csrf_protection' => $this->useCsrf, |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
return $formBuilder; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|