1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of EC-CUBE |
4
|
|
|
* |
5
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
6
|
|
|
* |
7
|
|
|
* http://www.lockon.co.jp/ |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or |
10
|
|
|
* modify it under the terms of the GNU General Public License |
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
12
|
|
|
* of the License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program; if not, write to the Free Software |
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace Eccube\Form\Extension; |
25
|
|
|
|
26
|
|
|
use Doctrine\ORM\EntityManager; |
27
|
|
|
use Symfony\Component\Form\AbstractTypeExtension; |
28
|
|
|
use Symfony\Component\Form\Extension\Core\Type\FormType; |
29
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
30
|
|
|
use Symfony\Component\Form\FormEvent; |
31
|
|
|
use Symfony\Component\Form\FormEvents; |
32
|
|
|
use Symfony\Component\Form\FormInterface; |
33
|
|
|
use Symfony\Component\Form\FormView; |
34
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
35
|
|
|
|
36
|
|
|
class DoctrineOrmExtension extends AbstractTypeExtension |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var EntityManager |
40
|
|
|
*/ |
41
|
|
|
protected $em; |
42
|
|
|
|
43
|
|
|
public function __construct(EntityManager $em) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$this->em = $em; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
52
|
|
|
{ |
53
|
|
|
$builder->addEventListener( |
54
|
|
|
FormEvents::PRE_SET_DATA, |
55
|
|
|
function (FormEvent $event) { |
56
|
|
|
$form = $event->getForm(); |
57
|
|
|
$config = $form->getConfig(); |
58
|
|
|
// data_classオプションが必要 |
59
|
|
|
$class = $config->getDataClass(); |
60
|
|
|
if (is_null($class)) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
// メタデータの取得 |
64
|
|
|
try { |
65
|
|
|
$meta = $this->em->getClassMetadata($class); |
66
|
|
|
} catch (\Exception $e) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
// フィールドからフォームへ定義 |
70
|
|
|
$names = $meta->getFieldNames(); |
71
|
|
|
foreach ($names as $name) { |
72
|
|
|
$mapping = $meta->getFieldMapping($name); |
73
|
|
|
if (isset($mapping['options']['eccube_form_options'])) { |
74
|
|
|
$options = $mapping['options']['eccube_form_options']; |
75
|
|
|
if (isset($options['auto_render']) && true === $options['auto_render']) { |
76
|
|
|
$fieldName = $mapping['fieldName']; |
77
|
|
|
if (!isset($form[$fieldName])) { |
78
|
|
|
$form->add( |
79
|
|
|
$mapping['fieldName'], |
80
|
|
|
null, |
81
|
|
|
[ |
82
|
|
|
'eccube_form_options' => $options, |
83
|
|
|
] |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
// TODO Assosiationも対応する |
90
|
|
|
} |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options) |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$options = $form->getConfig()->getOption('eccube_form_options'); |
97
|
|
|
$view->vars['eccube_form_options'] = $options; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$resolver->setDefault( |
103
|
|
|
'eccube_form_options', |
104
|
|
|
[ |
105
|
|
|
'auto_render' => false, |
106
|
|
|
'form_theme' => null, |
107
|
|
|
] |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getExtendedType() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
return FormType::class; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|