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