Completed
Push — 4.0 ( b48f64...137622 )
by chihiro
20:21 queued 10s
created

Controller/Admin/Customer/CustomerController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller\Admin\Customer;
15
16
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
17
use Doctrine\ORM\QueryBuilder;
18
use Eccube\Common\Constant;
19
use Eccube\Controller\AbstractController;
20
use Eccube\Entity\Master\CsvType;
21
use Eccube\Event\EccubeEvents;
22
use Eccube\Event\EventArgs;
23
use Eccube\Form\Type\Admin\SearchCustomerType;
24
use Eccube\Repository\CustomerRepository;
25
use Eccube\Repository\Master\PageMaxRepository;
26
use Eccube\Repository\Master\PrefRepository;
27
use Eccube\Repository\Master\SexRepository;
28
use Eccube\Service\CsvExportService;
29
use Eccube\Service\MailService;
30
use Eccube\Util\FormUtil;
31
use Knp\Component\Pager\Paginator;
32
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\HttpFoundation\StreamedResponse;
35
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
36
use Symfony\Component\Routing\Annotation\Route;
37
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
38
use Symfony\Component\Translation\TranslatorInterface;
39
40
class CustomerController extends AbstractController
41
{
42
    /**
43
     * @var CsvExportService
44
     */
45
    protected $csvExportService;
46
47
    /**
48
     * @var MailService
49
     */
50
    protected $mailService;
51
52
    /**
53
     * @var PrefRepository
54
     */
55
    protected $prefRepository;
56
57
    /**
58
     * @var SexRepository
59
     */
60
    protected $sexRepository;
61
62
    /**
63
     * @var PageMaxRepository
64
     */
65
    protected $pageMaxRepository;
66
67
    /**
68
     * @var CustomerRepository
69
     */
70
    protected $customerRepository;
71
72
    public function __construct(
73 10
        PageMaxRepository $pageMaxRepository,
74
        CustomerRepository $customerRepository,
75
        SexRepository $sexRepository,
76
        PrefRepository $prefRepository,
77
        MailService $mailService,
78
        CsvExportService $csvExportService
79
    ) {
80
        $this->pageMaxRepository = $pageMaxRepository;
81 10
        $this->customerRepository = $customerRepository;
82 10
        $this->sexRepository = $sexRepository;
83 10
        $this->prefRepository = $prefRepository;
84 10
        $this->mailService = $mailService;
85 10
        $this->csvExportService = $csvExportService;
86 10
    }
87
88
    /**
89
     * @Route("/%eccube_admin_route%/customer", name="admin_customer")
90
     * @Route("/%eccube_admin_route%/customer/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_customer_page")
91
     * @Template("@admin/Customer/index.twig")
92
     */
93
    public function index(Request $request, $page_no = null, Paginator $paginator)
94 7
    {
95
        $session = $this->session;
96 7
        $builder = $this->formFactory->createBuilder(SearchCustomerType::class);
97 7
98
        $event = new EventArgs(
99 7
            [
100
                'builder' => $builder,
101 7
            ],
102
            $request
103 7
        );
104
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_INITIALIZE, $event);
105 7
106
        $searchForm = $builder->getForm();
107 7
108
        $pageMaxis = $this->pageMaxRepository->findAll();
109 7
        $pageCount = $session->get('eccube.admin.customer.search.page_count', $this->eccubeConfig['eccube_default_page_count']);
110 7
        $pageCountParam = $request->get('page_count');
111 7
        if ($pageCountParam && is_numeric($pageCountParam)) {
112 7
            foreach ($pageMaxis as $pageMax) {
113
                if ($pageCountParam == $pageMax->getName()) {
114
                    $pageCount = $pageMax->getName();
115
                    $session->set('eccube.admin.customer.search.page_count', $pageCount);
116
                    break;
117
                }
118
            }
119
        }
120
121
        if ('POST' === $request->getMethod()) {
122 7
            $searchForm->handleRequest($request);
123 5
            if ($searchForm->isValid()) {
124 5
                $searchData = $searchForm->getData();
125 5
                $page_no = 1;
126 5
127
                $session->set('eccube.admin.customer.search', FormUtil::getViewData($searchForm));
128 5
                $session->set('eccube.admin.customer.search.page_no', $page_no);
129 5
            } else {
130
                return [
131
                    'searchForm' => $searchForm->createView(),
132 5
                    'pagination' => [],
133
                    'pageMaxis' => $pageMaxis,
134
                    'page_no' => $page_no,
135
                    'page_count' => $pageCount,
136
                    'has_errors' => true,
137
                ];
138
            }
139
        } else {
140
            if (null !== $page_no || $request->get('resume')) {
141 2
                if ($page_no) {
142 1
                    $session->set('eccube.admin.customer.search.page_no', (int) $page_no);
143 1
                } else {
144
                    $page_no = $session->get('eccube.admin.customer.search.page_no', 1);
145
                }
146
                $viewData = $session->get('eccube.admin.customer.search', []);
147 1
            } else {
148
                $page_no = 1;
149 1
                $viewData = FormUtil::getViewData($searchForm);
150 1
                $session->set('eccube.admin.customer.search', $viewData);
151 1
                $session->set('eccube.admin.customer.search.page_no', $page_no);
152 1
            }
153
            $searchData = FormUtil::submitAndGetData($searchForm, $viewData);
154 2
        }
155
156
        /** @var QueryBuilder $qb */
157
        $qb = $this->customerRepository->getQueryBuilderBySearchData($searchData);
158 7
159
        $event = new EventArgs(
160 7
            [
161
                'form' => $searchForm,
162 7
                'qb' => $qb,
163 7
            ],
164
            $request
165 7
        );
166
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_SEARCH, $event);
167 7
168
        $pagination = $paginator->paginate(
169 7
            $qb,
170 7
            $page_no,
171 7
            $pageCount
172 7
        );
173
174
        return [
175
            'searchForm' => $searchForm->createView(),
176 7
            'pagination' => $pagination,
177 7
            'pageMaxis' => $pageMaxis,
178 7
            'page_no' => $page_no,
179 7
            'page_count' => $pageCount,
180 7
            'has_errors' => false,
181
        ];
182
    }
183
184
    /**
185
     * @Route("/%eccube_admin_route%/customer/{id}/resend", requirements={"id" = "\d+"}, name="admin_customer_resend")
186
     */
187
    public function resend(Request $request, $id)
188 1
    {
189
        $this->isTokenValid();
190 1
191
        $Customer = $this->customerRepository
192 1
            ->find($id);
193 1
194
        if (is_null($Customer)) {
195 1
            throw new NotFoundHttpException();
196
        }
197
198
        $activateUrl = $this->generateUrl(
199 1
            'entry_activate',
200 1
            ['secret_key' => $Customer->getSecretKey()],
201 1
            UrlGeneratorInterface::ABSOLUTE_URL
202 1
        );
203
204
        // メール送信
205
        $this->mailService->sendAdminCustomerConfirmMail($Customer, $activateUrl);
206 1
207
        $event = new EventArgs(
208 1
            [
209
                'Customer' => $Customer,
210 1
                'activateUrl' => $activateUrl,
211 1
            ],
212
            $request
213 1
        );
214
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_RESEND_COMPLETE, $event);
215 1
216
        $this->addSuccess('admin.common.send_complete', 'admin');
217 1
218
        return $this->redirectToRoute('admin_customer');
219 1
    }
220
221
    /**
222
     * @Route("/%eccube_admin_route%/customer/{id}/delete", requirements={"id" = "\d+"}, name="admin_customer_delete", methods={"DELETE"})
223
     */
224
    public function delete(Request $request, $id, TranslatorInterface $translator)
0 ignored issues
show
The parameter $translator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
225
    {
226 1
        $this->isTokenValid();
227
228 1
        log_info('会員削除開始', [$id]);
229
230 1
        $page_no = intval($this->session->get('eccube.admin.customer.search.page_no'));
231
        $page_no = $page_no ? $page_no : Constant::ENABLED;
232 1
233 1
        $Customer = $this->customerRepository
234
            ->find($id);
235 1
236 1
        if (!$Customer) {
237
            $this->deleteMessage();
238 1
239
            return $this->redirect($this->generateUrl('admin_customer_page',
240
                    ['page_no' => $page_no]).'?resume='.Constant::ENABLED);
241
        }
242
243
        try {
244
            $this->entityManager->remove($Customer);
245
            $this->entityManager->flush($Customer);
246 1
            $this->addSuccess('admin.customer.delete.complete', 'admin');
247 1
        } catch (ForeignKeyConstraintViolationException $e) {
248 1
            log_error('会員削除失敗', [$e], 'admin');
249
250
            $message = trans('admin.common.delete_error_foreign_key', ['%name%' => $Customer->getName01().' '.$Customer->getName02()]);
251
            $this->addError($message, 'admin');
252
        }
253
254
        log_info('会員削除完了', [$id]);
255
256 1
        $event = new EventArgs(
257
            [
258 1
                'Customer' => $Customer,
259
            ],
260 1
            $request
261
        );
262 1
        $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELETE_COMPLETE, $event);
263
264 1
        return $this->redirect($this->generateUrl('admin_customer_page',
265
                ['page_no' => $page_no]).'?resume='.Constant::ENABLED);
266 1
    }
267 1
268
    /**
269
     * 会員CSVの出力.
270
     *
271
     * @Route("/%eccube_admin_route%/customer/export", name="admin_customer_export")
272
     *
273
     * @param Request $request
274
     *
275
     * @return StreamedResponse
276
     */
277
    public function export(Request $request)
278
    {
279 1
        // タイムアウトを無効にする.
280
        set_time_limit(0);
281
282 1
        // sql loggerを無効にする.
283
        $em = $this->entityManager;
284
        $em->getConfiguration()->setSQLLogger(null);
285 1
286 1
        $response = new StreamedResponse();
287
        $response->setCallback(function () use ($request) {
288 1
            // CSV種別を元に初期化.
289 1
            $this->csvExportService->initCsvType(CsvType::CSV_TYPE_CUSTOMER);
290
291 1
            // ヘッダ行の出力.
292
            $this->csvExportService->exportHeader();
293
294 1
            // 会員データ検索用のクエリビルダを取得.
295
            $qb = $this->csvExportService
296
                ->getCustomerQueryBuilder($request);
297 1
298 1
            // データ行の出力.
299
            $this->csvExportService->setExportQueryBuilder($qb);
300 View Code Duplication
            $this->csvExportService->exportData(function ($entity, $csvService) use ($request) {
301 1
                $Csvs = $csvService->getCsvs();
302 1
303 1
                /** @var $Customer \Eccube\Entity\Customer */
304
                $Customer = $entity;
305
306 1
                $ExportCsvRow = new \Eccube\Entity\ExportCsvRow();
307
308 1
                // CSV出力項目と合致するデータを取得.
309
                foreach ($Csvs as $Csv) {
310
                    // 会員データを検索.
311 1
                    $ExportCsvRow->setData($csvService->getData($Csv, $Customer));
312
313 1
                    $event = new EventArgs(
314
                        [
315 1
                            'csvService' => $csvService,
316
                            'Csv' => $Csv,
317 1
                            'Customer' => $Customer,
318 1
                            'ExportCsvRow' => $ExportCsvRow,
319 1
                        ],
320 1
                        $request
321
                    );
322 1
                    $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_CUSTOMER_CSV_EXPORT, $event);
323
324 1
                    $ExportCsvRow->pushData();
325
                }
326 1
327
                //$row[] = number_format(memory_get_usage(true));
328
                // 出力.
329
                $csvService->fputcsv($ExportCsvRow->getRow());
330
            });
331 1
        });
332 1
333 1
        $now = new \DateTime();
334
        $filename = 'customer_'.$now->format('YmdHis').'.csv';
335 1
        $response->headers->set('Content-Type', 'application/octet-stream');
336 1
        $response->headers->set('Content-Disposition', 'attachment; filename='.$filename);
337 1
338 1
        $response->send();
339
340 1
        log_info('会員CSVファイル名', [$filename]);
341
342 1
        return $response;
343
    }
344
}
345