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
|
|
|
* @param bool $useAjax |
61
|
|
|
* |
62
|
|
|
* @return FormBuilderInterface |
63
|
|
|
*/ |
64
|
|
|
public function createFormForLocation(Location $location, $useAjax = false) |
65
|
|
|
{ |
66
|
|
|
$contentInfo = $location->contentInfo; |
67
|
|
|
|
68
|
|
|
$contentType = $this->contentTypeService->loadContentType($contentInfo->contentTypeId); |
69
|
|
|
|
70
|
|
|
$data = new DataWrapper(new InformationCollectionStruct(), $contentType, $location); |
71
|
|
|
|
72
|
|
|
$formBuilder = $this->formFactory |
73
|
|
|
->createBuilder( |
74
|
|
|
InformationCollectionType::class, |
75
|
|
|
$data, |
76
|
|
|
array( |
77
|
|
|
'csrf_protection' => $this->useCsrf, |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
if ($useAjax) { |
82
|
|
|
$formBuilder->setAction($this->router->generate('netgen_information_collection_handle_ajax', array('location' => $location->id))); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $formBuilder; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|