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
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace FSi\Bundle\AdminBundle\Admin\CRUD\Context; |
13
|
|
|
|
14
|
|
|
use FSi\Bundle\AdminBundle\Admin\Context\ContextAbstract; |
15
|
|
|
use FSi\Bundle\AdminBundle\Admin\CRUD\FormElement; |
16
|
|
|
use FSi\Bundle\AdminBundle\Admin\Element; |
17
|
|
|
use FSi\Bundle\AdminBundle\Event\AdminEvent; |
18
|
|
|
use FSi\Bundle\AdminBundle\Event\FormEvent; |
19
|
|
|
use Symfony\Component\Form\FormInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
22
|
|
|
|
23
|
|
|
class FormElementContext extends ContextAbstract |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var FormElement |
27
|
|
|
*/ |
28
|
|
|
protected $element; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var FormInterface |
32
|
|
|
*/ |
33
|
|
|
protected $form; |
34
|
|
|
|
35
|
|
|
public function hasTemplateName(): bool |
36
|
|
|
{ |
37
|
|
|
return $this->element->hasOption('template_form') || parent::hasTemplateName(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getTemplateName(): ?string |
41
|
|
|
{ |
42
|
|
|
return $this->element->hasOption('template_form') |
43
|
|
|
? $this->element->getOption('template_form') |
44
|
|
|
: parent::getTemplateName() |
45
|
|
|
; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getData(): array |
49
|
|
|
{ |
50
|
|
|
return ['form' => $this->form->createView(), 'element' => $this->element]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setElement(Element $element): void |
54
|
|
|
{ |
55
|
|
|
$this->element = $element; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function createEvent(Request $request): AdminEvent |
59
|
|
|
{ |
60
|
|
|
$this->form = $this->element->createForm($this->getObject($request)); |
61
|
|
|
|
62
|
|
|
return new FormEvent($this->element, $request, $this->form); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function getSupportedRoute(): string |
66
|
|
|
{ |
67
|
|
|
return 'fsi_admin_form'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function supportsElement(Element $element): bool |
71
|
|
|
{ |
72
|
|
|
return $element instanceof FormElement; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Request $request |
77
|
|
|
* @return object|null |
78
|
|
|
*/ |
79
|
|
|
private function getObject(Request $request) |
80
|
|
|
{ |
81
|
|
|
$id = $request->get('id'); |
82
|
|
|
if (null === $id || '' === $id) { |
83
|
|
|
$this->checkAllowAddOption(); |
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$object = $this->element->getDataIndexer()->getData($id); |
88
|
|
|
if (null === $object) { |
89
|
|
|
throw new NotFoundHttpException("Can\'t find object with id \"{$id}\""); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $object; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function checkAllowAddOption(): void |
96
|
|
|
{ |
97
|
|
|
if (true === $this->element->hasOption('allow_add') |
98
|
|
|
&& true !== $this->element->getOption('allow_add') |
99
|
|
|
) { |
100
|
|
|
throw new NotFoundHttpException(sprintf( |
101
|
|
|
'Cannot add objects through element "%s", because it has option "allow_add" set to false', |
102
|
|
|
get_class($this->element) |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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.