1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FSi\Bundle\AdminBundle\Admin\CRUD\Context; |
11
|
|
|
|
12
|
|
|
use FSi\Bundle\AdminBundle\Admin\Context\ContextAbstract; |
13
|
|
|
use FSi\Bundle\AdminBundle\Admin\CRUD\FormElement; |
14
|
|
|
use FSi\Bundle\AdminBundle\Admin\Element; |
15
|
|
|
use FSi\Bundle\AdminBundle\Event\FormEvent; |
16
|
|
|
use Symfony\Component\Form\FormInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
19
|
|
|
|
20
|
|
|
class FormElementContext extends ContextAbstract |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var FormElement |
24
|
|
|
*/ |
25
|
|
|
protected $element; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var FormInterface |
29
|
|
|
*/ |
30
|
|
|
protected $form; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function hasTemplateName() |
36
|
|
|
{ |
37
|
|
|
return $this->element->hasOption('template_form'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function getTemplateName() |
44
|
|
|
{ |
45
|
|
|
return $this->element->getOption('template_form'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function getData() |
52
|
|
|
{ |
53
|
|
|
$data = array( |
54
|
|
|
'form' => $this->form->createView(), |
55
|
|
|
'element' => $this->element |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
if ($this->form->getData()) { |
59
|
|
|
$data['id'] = $this->element->getDataIndexer()->getIndex($this->form->getData()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $data; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function setElement(Element $element) |
69
|
|
|
{ |
70
|
|
|
$this->element = $element; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
protected function createEvent(Request $request) |
77
|
|
|
{ |
78
|
|
|
$this->form = $this->element->createForm($this->getObject($request)); |
79
|
|
|
|
80
|
|
|
return new FormEvent($this->element, $request, $this->form); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
protected function getSupportedRoute() |
87
|
|
|
{ |
88
|
|
|
return 'fsi_admin_form'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
protected function supportsElement(Element $element) |
95
|
|
|
{ |
96
|
|
|
return $element instanceof FormElement; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Request $request |
101
|
|
|
* @return object|null |
102
|
|
|
*/ |
103
|
|
|
private function getObject(Request $request) |
104
|
|
|
{ |
105
|
|
|
$id = $request->get('id'); |
106
|
|
|
if (empty($id)) { |
107
|
|
|
$this->checkAllowAddOption(); |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$object = $this->element->getDataIndexer()->getData($id); |
112
|
|
|
if (!$object) { |
113
|
|
|
throw new NotFoundHttpException(sprintf('Can\'t find object with id %s', $id)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $object; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @throws NotFoundHttpException |
121
|
|
|
*/ |
122
|
|
|
private function checkAllowAddOption() |
123
|
|
|
{ |
124
|
|
|
if ($this->element->hasOption('allow_add') |
125
|
|
|
&& !$this->element->getOption('allow_add') |
126
|
|
|
) { |
127
|
|
|
throw new NotFoundHttpException(sprintf( |
128
|
|
|
'Cannot add objects through element "%s", because it has option "allow_add" set to false', |
129
|
|
|
get_class($this->element) |
130
|
|
|
)); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.