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\Controller\Admin\Customer; |
25
|
|
|
|
26
|
|
|
use Doctrine\ORM\EntityManager; |
27
|
|
|
use Eccube\Annotation\Inject; |
28
|
|
|
use Eccube\Application; |
29
|
|
|
use Eccube\Common\Constant; |
30
|
|
|
use Eccube\Controller\AbstractController; |
31
|
|
|
use Eccube\Entity\CustomerAddress; |
32
|
|
|
use Eccube\Event\EccubeEvents; |
33
|
|
|
use Eccube\Event\EventArgs; |
34
|
|
|
use Eccube\Form\Type\Admin\CustomerType; |
35
|
|
|
use Eccube\Repository\CustomerRepository; |
36
|
|
|
use Eccube\Repository\CustomerAddressRepository; |
37
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
38
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
39
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
40
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
41
|
|
|
use Symfony\Component\Form\FormFactory; |
42
|
|
|
use Symfony\Component\HttpFoundation\Request; |
43
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
44
|
|
|
use Eccube\Form\Type\Front\CustomerAddressType; |
45
|
|
|
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException; |
46
|
|
|
use Eccube\Entity\Customer; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @Route(service=CustomerDeliveryEditController::class) |
50
|
|
|
*/ |
51
|
|
|
class CustomerDeliveryEditController extends AbstractController |
52
|
|
|
{ |
53
|
|
|
/** |
54
|
|
|
* @Inject("eccube.event.dispatcher") |
55
|
|
|
* @var EventDispatcher |
56
|
|
|
*/ |
57
|
|
|
protected $eventDispatcher; |
58
|
|
|
|
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @Inject("form.factory") |
61
|
|
|
* @var FormFactory |
62
|
|
|
*/ |
63
|
|
|
protected $formFactory; |
64
|
|
|
|
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @Inject("config") |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
protected $appConfig; |
70
|
|
|
|
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @Inject("orm.em") |
73
|
|
|
* @var EntityManager |
74
|
|
|
*/ |
75
|
|
|
protected $entityManager; |
76
|
|
|
|
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @Inject(CustomerRepository::class) |
79
|
|
|
* @var CustomerRepository |
80
|
|
|
*/ |
81
|
|
|
protected $customerRepository; |
82
|
|
|
|
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @Inject(CustomerAddressRepository::class) |
85
|
|
|
* @var CustomerAddressRepository |
86
|
|
|
*/ |
87
|
|
|
protected $customerAddressRepository; |
88
|
|
|
|
|
|
|
|
89
|
|
|
/** |
|
|
|
|
90
|
|
|
* お届け先編集画面. |
91
|
|
|
* |
92
|
|
|
* @Route("/{_admin}/customer/{id}/delivery/new", name="admin_customer_delivery_new", requirements={"id" = "\d+"}) |
93
|
|
|
* @Route("/{_admin}/customer/{id}/delivery/{did}/edit", name="admin_customer_delivery_edit", requirements={"id" = "\d+", "did" = "\d+"}) |
94
|
|
|
* @Template("Customer/delivery_edit.twig") |
95
|
|
|
*/ |
|
|
|
|
96
|
|
|
public function edit(Application $app, Request $request, Customer $Customer, $did = null) |
97
|
|
|
{ |
|
|
|
|
98
|
|
|
// 配送先住所最大値判定 |
99
|
|
|
// $idが存在する際は、追加処理ではなく、編集の処理ため本ロジックスキップ |
100
|
|
|
if (is_null($did)) { |
101
|
|
|
$addressCurrNum = count($Customer->getCustomerAddresses()); |
102
|
|
|
$addressMax = $this->appConfig['deliv_addr_max']; |
103
|
|
|
if ($addressCurrNum >= $addressMax) { |
104
|
|
|
throw new NotFoundHttpException('お届け先の登録数の上限を超えています'); |
105
|
|
|
} |
106
|
|
|
} else { |
107
|
|
|
$CustomerAddress = $this->customerAddressRepository->find($did); |
108
|
|
|
if (is_null($CustomerAddress) || $CustomerAddress->getCustomer()->getId() != $Customer->getId()) { |
109
|
|
|
throw new NotFoundHttpException(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
$CustomerAddress = $this->customerAddressRepository->findOrCreateByCustomerAndId($Customer, $did); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$builder = $this->formFactory |
116
|
|
|
->createBuilder(CustomerAddressType::class, $CustomerAddress); |
117
|
|
|
|
|
|
|
|
118
|
|
|
$event = new EventArgs( |
119
|
|
|
array( |
120
|
|
|
'builder' => $builder, |
121
|
|
|
'Customer' => $Customer, |
122
|
|
|
'CustomerAddress' => $CustomerAddress, |
123
|
|
|
), |
124
|
|
|
$request |
125
|
|
|
); |
126
|
|
|
|
|
|
|
|
127
|
|
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELIVERY_EDIT_INDEX_INITIALIZE, $event); |
128
|
|
|
|
|
|
|
|
129
|
|
|
$form = $builder->getForm(); |
130
|
|
|
|
|
|
|
|
131
|
|
|
if ('POST' === $request->getMethod()) { |
132
|
|
|
$form->handleRequest($request); |
133
|
|
|
if ($form->isValid()) { |
134
|
|
|
log_info('お届け先登録開始', array($did)); |
135
|
|
|
|
|
|
|
|
136
|
|
|
$this->entityManager->persist($CustomerAddress); |
137
|
|
|
$this->entityManager->flush(); |
138
|
|
|
|
|
|
|
|
139
|
|
|
log_info('お届け先登録完了', array($did)); |
140
|
|
|
|
|
|
|
|
141
|
|
|
$event = new EventArgs( |
142
|
|
|
array( |
143
|
|
|
'form' => $form, |
144
|
|
|
'Customer' => $Customer, |
145
|
|
|
'CustomerAddress' => $CustomerAddress, |
146
|
|
|
), |
147
|
|
|
$request |
148
|
|
|
); |
149
|
|
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELIVERY_EDIT_INDEX_COMPLETE, $event); |
150
|
|
|
|
|
|
|
|
151
|
|
|
$app->addSuccess('admin.customer.delivery.save.complete', 'admin'); |
152
|
|
|
|
|
|
|
|
153
|
|
|
return $app->redirect($app->url('admin_customer_delivery_edit', array( |
154
|
|
|
'id' => $Customer->getId(), |
155
|
|
|
'did' => $CustomerAddress->getId(), |
156
|
|
|
))); |
157
|
|
|
} else { |
158
|
|
|
$app->addError('admin.customer.delivery.save.failed', 'admin'); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
return [ |
163
|
|
|
'form' => $form->createView(), |
164
|
|
|
'Customer' => $Customer, |
165
|
|
|
'CustomerAddress' => $CustomerAddress, |
166
|
|
|
]; |
167
|
|
|
|
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
/** |
|
|
|
|
171
|
|
|
* @Method("DELETE") |
172
|
|
|
* @Route("/{_admin}/customer/{id}/delivery/{did}/delete", requirements={"id" = "\d+", "did" = "\d+"}, name="admin_customer_delivery_delete") |
173
|
|
|
*/ |
|
|
|
|
174
|
|
|
public function delete(Application $app, Request $request, Customer $Customer, $did) |
175
|
|
|
{ |
176
|
|
|
$this->isTokenValid($app); |
177
|
|
|
|
|
|
|
|
178
|
|
|
log_info('お届け先削除開始', array($did)); |
179
|
|
|
|
|
|
|
|
180
|
|
|
$CustomerAddress = $this->customerAddressRepository->find($did); |
181
|
|
|
if (is_null($CustomerAddress)) { |
182
|
|
|
throw new NotFoundHttpException(); |
183
|
|
|
} else if ($CustomerAddress->getCustomer()->getId() != $Customer->getId()) { |
184
|
|
|
$app->deleteMessage(); |
185
|
|
|
return $app->redirect($app->url('admin_customer_edit', array('id' => $Customer->getId()))); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
try { |
189
|
|
|
$this->customerAddressRepository->delete($CustomerAddress); |
190
|
|
|
$app->addSuccess('admin.customer.delivery.delete.complete', 'admin'); |
191
|
|
|
} catch (ForeignKeyConstraintViolationException $e) { |
192
|
|
|
log_error('お届け先削除失敗', [$e], 'admin'); |
|
|
|
|
193
|
|
|
|
|
|
|
|
194
|
|
|
$message = $app->trans('admin.delete.failed.foreign_key', ['%name%' => 'お届け先']); |
195
|
|
|
$app->addError($message, 'admin'); |
196
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
log_info('お届け先削除完了', array($did)); |
199
|
|
|
|
|
|
|
|
200
|
|
|
$event = new EventArgs( |
201
|
|
|
array( |
202
|
|
|
'Customer' => $Customer, |
203
|
|
|
'CustomerAddress' => $CustomerAddress, |
204
|
|
|
), |
205
|
|
|
$request |
206
|
|
|
); |
207
|
|
|
$this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELIVERY_DELETE_COMPLETE, $event); |
208
|
|
|
|
|
|
|
|
209
|
|
|
return $app->redirect($app->url('admin_customer_edit', array('id' => $Customer->getId()))); |
210
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
|
|
} |
213
|
|
|
|