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; |
11
|
|
|
|
12
|
|
|
use FSi\Bundle\AdminBundle\Admin\AbstractElement; |
13
|
|
|
use FSi\Bundle\AdminBundle\Exception\RuntimeException; |
14
|
|
|
use FSi\Component\DataGrid\DataGridFactoryInterface; |
15
|
|
|
use FSi\Component\DataGrid\DataGridInterface; |
16
|
|
|
use FSi\Component\DataSource\DataSourceFactoryInterface; |
17
|
|
|
use FSi\Component\DataSource\DataSourceInterface; |
18
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
19
|
|
|
use Symfony\Component\Form\FormInterface; |
20
|
|
|
use Symfony\Component\OptionsResolver\Options; |
21
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Norbert Orzechowicz <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractCRUD extends AbstractElement implements CRUDElement |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var \FSi\Component\DataSource\DataSourceFactoryInterface |
30
|
|
|
*/ |
31
|
|
|
protected $datasourceFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \FSi\Component\DataGrid\DataGridFactoryInterface |
35
|
|
|
*/ |
36
|
|
|
protected $datagridFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Symfony\Component\Form\FormFactoryInterface |
40
|
|
|
*/ |
41
|
|
|
protected $formFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function getRoute() |
47
|
|
|
{ |
48
|
|
|
return 'fsi_admin_list'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function getSuccessRoute() |
55
|
|
|
{ |
56
|
|
|
return $this->getRoute(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function getSuccessRouteParameters() |
63
|
|
|
{ |
64
|
|
|
return $this->getRouteParameters(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function setDefaultOptions(OptionsResolver $resolver) |
71
|
|
|
{ |
72
|
|
|
$resolver->setDefaults(array( |
73
|
|
|
'allow_delete' => true, |
74
|
|
|
'allow_add' => true, |
75
|
|
|
'template_crud_list' => null, |
76
|
|
|
'template_crud_create' => null, |
77
|
|
|
'template_crud_edit' => null, |
78
|
|
|
'template_list' => function (Options $options) { |
79
|
|
|
return $options['template_crud_list']; |
80
|
|
|
}, |
81
|
|
|
'template_form' => function (Options $options) { |
82
|
|
|
return $options['template_crud_edit']; |
83
|
|
|
} |
84
|
|
|
)); |
85
|
|
|
|
86
|
|
|
$resolver->setNormalizer('template_crud_create', function (Options $options, $value) { |
87
|
|
|
if ($value !== $options['template_crud_edit']) { |
88
|
|
|
throw new RuntimeException( |
89
|
|
|
'CRUD admin element options "template_crud_create" and "template_crud_edit" have both to have the same value' |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $value; |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
$resolver->setAllowedTypes('allow_delete', 'bool'); |
97
|
|
|
$resolver->setAllowedTypes('allow_add', 'bool'); |
98
|
|
|
$resolver->setAllowedTypes('template_crud_list', array('null', 'string')); |
99
|
|
|
$resolver->setAllowedTypes('template_crud_create', array('null', 'string')); |
100
|
|
|
$resolver->setAllowedTypes('template_crud_edit', array('null', 'string')); |
101
|
|
|
$resolver->setAllowedTypes('template_list', array('null', 'string')); |
102
|
|
|
$resolver->setAllowedTypes('template_form', array('null', 'string')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritdoc |
107
|
|
|
*/ |
108
|
|
|
public function apply($object) |
109
|
|
|
{ |
110
|
|
|
$this->delete($object); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function setDataGridFactory(DataGridFactoryInterface $factory) |
117
|
|
|
{ |
118
|
|
|
$this->datagridFactory = $factory; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public function setDataSourceFactory(DataSourceFactoryInterface $factory) |
125
|
|
|
{ |
126
|
|
|
$this->datasourceFactory = $factory; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
|
|
public function setFormFactory(FormFactoryInterface $factory) |
133
|
|
|
{ |
134
|
|
|
$this->formFactory = $factory; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
|
|
public function createDataGrid() |
141
|
|
|
{ |
142
|
|
|
$datagrid = $this->initDataGrid($this->datagridFactory); |
143
|
|
|
|
144
|
|
|
if (!is_object($datagrid) || !$datagrid instanceof DataGridInterface) { |
145
|
|
|
throw new RuntimeException('initDataGrid should return instanceof FSi\\Component\\DataGrid\\DataGridInterface'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($this->options['allow_delete']) { |
149
|
|
|
if (!$datagrid->hasColumnType('batch')) { |
150
|
|
|
$datagrid->addColumn('batch', 'batch', array( |
151
|
|
|
'actions' => array( |
152
|
|
|
'delete' => array( |
153
|
|
|
'route_name' => 'fsi_admin_batch', |
154
|
|
|
'additional_parameters' => array('element' => $this->getId()), |
155
|
|
|
'label' => 'crud.list.batch.delete' |
156
|
|
|
) |
157
|
|
|
), |
158
|
|
|
'display_order' => -1000 |
159
|
|
|
)); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $datagrid; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
|
|
public function createDataSource() |
170
|
|
|
{ |
171
|
|
|
$datasource = $this->initDataSource($this->datasourceFactory); |
172
|
|
|
|
173
|
|
|
if (!is_object($datasource) || !$datasource instanceof DataSourceInterface) { |
174
|
|
|
throw new RuntimeException('initDataSource should return instanceof FSi\\Component\\DataSource\\DataSourceInterface'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $datasource; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritdoc} |
182
|
|
|
*/ |
183
|
|
|
public function createForm($data = null) |
184
|
|
|
{ |
185
|
|
|
$form = $this->initForm($this->formFactory, $data); |
186
|
|
|
|
187
|
|
|
if (!is_object($form) || !$form instanceof FormInterface) { |
188
|
|
|
throw new RuntimeException('initForm should return instanceof Symfony\\Component\\Form\\FormInterface'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $form; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Initialize DataGrid. |
196
|
|
|
* |
197
|
|
|
* @param \FSi\Component\DataGrid\DataGridFactoryInterface $factory |
198
|
|
|
* @return \FSi\Component\DataGrid\DataGridInterface |
199
|
|
|
*/ |
200
|
|
|
abstract protected function initDataGrid(DataGridFactoryInterface $factory); |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Initialize DataSource. |
204
|
|
|
* |
205
|
|
|
* @param \FSi\Component\DataSource\DataSourceFactoryInterface $factory |
206
|
|
|
* @return \FSi\Component\DataSource\DataSourceInterface |
207
|
|
|
*/ |
208
|
|
|
abstract protected function initDataSource(DataSourceFactoryInterface $factory); |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Initialize create Form. This form will be used in createAction in CRUDController. |
212
|
|
|
* |
213
|
|
|
* @param \Symfony\Component\Form\FormFactoryInterface $factory |
214
|
|
|
* @param mixed $data |
215
|
|
|
* @return \Symfony\Component\Form\FormInterface |
216
|
|
|
*/ |
217
|
|
|
abstract protected function initForm(FormFactoryInterface $factory, $data = null); |
218
|
|
|
} |
219
|
|
|
|