|
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
|
|
|
namespace Eccube\Form\Extension; |
|
24
|
|
|
|
|
25
|
|
|
use Doctrine\ORM\EntityManager; |
|
26
|
|
|
use Eccube\Application; |
|
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
|
|
|
|
|
33
|
|
|
class DoctrineOrmExtension extends AbstractTypeExtension |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var EntityManager |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $em; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(EntityManager $em) |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
$this->em = $em; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
|
49
|
|
|
{ |
|
50
|
|
|
$builder->addEventListener( |
|
51
|
|
|
FormEvents::PRE_SET_DATA, |
|
52
|
|
|
function (FormEvent $event) { |
|
53
|
|
|
$form = $event->getForm(); |
|
54
|
|
|
$config = $form->getConfig(); |
|
55
|
|
|
// data_classオプションが必要 |
|
56
|
|
|
$class = $config->getDataClass(); |
|
57
|
|
|
if (is_null($class)) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
// メタデータの取得 |
|
61
|
|
|
try { |
|
62
|
|
|
$meta = $this->em->getClassMetadata($class); |
|
63
|
|
|
} catch (\Exception $e) { |
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
// フィールドからフォームへ定義 |
|
67
|
|
|
$names = $meta->getFieldNames(); |
|
68
|
|
|
foreach ($names as $name) { |
|
69
|
|
|
$mapping = $meta->getFieldMapping($name); |
|
70
|
|
|
// eccube_form_renderがtrueのときに実行 |
|
71
|
|
|
// -> @ORM\Column(..., options={"eccube_form_render": true}) |
|
72
|
|
|
if (isset($mapping['options']['eccube_form_render']) && $mapping['options']['eccube_form_render']) { |
|
73
|
|
|
$fieldName = $mapping['fieldName']; |
|
74
|
|
|
if (!isset($form[$fieldName])) { |
|
75
|
|
|
$form->add($mapping['fieldName']); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
// TODO Assosiationも対応する |
|
80
|
|
|
} |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getExtendedType() |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
return FormType::class; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|