1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FSi\Bundle\AdminBundle\Admin\CRUD; |
13
|
|
|
|
14
|
|
|
use FSi\Bundle\AdminBundle\Admin\AbstractElement; |
15
|
|
|
use FSi\Bundle\AdminBundle\Exception\RuntimeException; |
16
|
|
|
use FSi\Component\DataGrid\DataGridFactoryInterface; |
17
|
|
|
use FSi\Component\DataGrid\DataGridInterface; |
18
|
|
|
use FSi\Component\DataSource\DataSourceFactoryInterface; |
19
|
|
|
use FSi\Component\DataSource\DataSourceInterface; |
20
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
21
|
|
|
use Symfony\Component\Form\FormInterface; |
22
|
|
|
use Symfony\Component\OptionsResolver\Options; |
23
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
24
|
|
|
|
25
|
|
|
abstract class GenericCRUDElement extends AbstractElement implements CRUDElement |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var DataSourceFactoryInterface |
29
|
|
|
*/ |
30
|
|
|
protected $datasourceFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var DataGridFactoryInterface |
34
|
|
|
*/ |
35
|
|
|
protected $datagridFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var FormFactoryInterface |
39
|
|
|
*/ |
40
|
|
|
protected $formFactory; |
41
|
|
|
|
42
|
|
|
public function getRoute(): string |
43
|
|
|
{ |
44
|
|
|
return 'fsi_admin_list'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getSuccessRoute(): string |
48
|
|
|
{ |
49
|
|
|
return $this->getRoute(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getSuccessRouteParameters(): array |
53
|
|
|
{ |
54
|
|
|
return $this->getRouteParameters(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function configureOptions(OptionsResolver $resolver): void |
58
|
|
|
{ |
59
|
|
|
$resolver->setDefaults([ |
60
|
|
|
'allow_delete' => true, |
61
|
|
|
'allow_add' => true, |
62
|
|
|
'template_crud_list' => null, |
63
|
|
|
'template_crud_create' => null, |
64
|
|
|
'template_crud_edit' => null, |
65
|
|
|
'template_list' => function (Options $options) { |
66
|
|
|
return $options['template_crud_list']; |
67
|
|
|
}, |
68
|
|
|
'template_form' => function (Options $options) { |
69
|
|
|
return $options['template_crud_edit']; |
70
|
|
|
} |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
$resolver->setNormalizer('template_crud_create', function (Options $options, $value) { |
74
|
|
|
if ($value !== $options['template_crud_edit']) { |
75
|
|
|
throw new RuntimeException( |
76
|
|
|
'CRUD admin element options "template_crud_create" and "template_crud_edit" have both to have the same value' |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $value; |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
$resolver->setAllowedTypes('allow_delete', 'bool'); |
84
|
|
|
$resolver->setAllowedTypes('allow_add', 'bool'); |
85
|
|
|
$resolver->setAllowedTypes('template_crud_list', ['null', 'string']); |
86
|
|
|
$resolver->setAllowedTypes('template_crud_create', ['null', 'string']); |
87
|
|
|
$resolver->setAllowedTypes('template_crud_edit', ['null', 'string']); |
88
|
|
|
$resolver->setAllowedTypes('template_list', ['null', 'string']); |
89
|
|
|
$resolver->setAllowedTypes('template_form', ['null', 'string']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function apply($object): void |
93
|
|
|
{ |
94
|
|
|
$this->delete($object); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setDataGridFactory(DataGridFactoryInterface $factory): void |
98
|
|
|
{ |
99
|
|
|
$this->datagridFactory = $factory; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function setDataSourceFactory(DataSourceFactoryInterface $factory): void |
103
|
|
|
{ |
104
|
|
|
$this->datasourceFactory = $factory; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function setFormFactory(FormFactoryInterface $factory): void |
108
|
|
|
{ |
109
|
|
|
$this->formFactory = $factory; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function createDataGrid(): DataGridInterface |
113
|
|
|
{ |
114
|
|
|
$datagrid = $this->initDataGrid($this->datagridFactory); |
115
|
|
|
|
116
|
|
|
if ($this->getOption('allow_delete') && !$datagrid->hasColumnType('batch')) { |
117
|
|
|
$datagrid->addColumn('batch', 'batch', [ |
118
|
|
|
'actions' => [ |
119
|
|
|
'delete' => [ |
120
|
|
|
'route_name' => 'fsi_admin_batch', |
121
|
|
|
'additional_parameters' => ['element' => $this->getId()], |
122
|
|
|
'label' => 'crud.list.batch.delete' |
123
|
|
|
] |
124
|
|
|
], |
125
|
|
|
'display_order' => -1000 |
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $datagrid; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function createDataSource(): DataSourceInterface |
133
|
|
|
{ |
134
|
|
|
return $this->initDataSource($this->datasourceFactory); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function createForm($data = null): FormInterface |
138
|
|
|
{ |
139
|
|
|
return $this->initForm($this->formFactory, $data); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
abstract protected function initDataGrid(DataGridFactoryInterface $factory): DataGridInterface; |
143
|
|
|
|
144
|
|
|
abstract protected function initDataSource(DataSourceFactoryInterface $factory): DataSourceInterface; |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Initialize form. This form will be used in create and update actions. |
148
|
|
|
* |
149
|
|
|
* @param FormFactoryInterface $factory |
150
|
|
|
* @param mixed $data |
151
|
|
|
* @return FormInterface |
152
|
|
|
*/ |
153
|
|
|
abstract protected function initForm(FormFactoryInterface $factory, $data = null): FormInterface; |
154
|
|
|
} |
155
|
|
|
|