CsvImportController::getProductCsvHeader()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 26
nc 1
nop 0
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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
25
namespace Eccube\Controller\Admin\Product;
26
27
use Eccube\Application;
28
use Eccube\Common\Constant;
29
use Eccube\Entity\Category;
30
use Eccube\Entity\Product;
31
use Eccube\Entity\ProductCategory;
32
use Eccube\Entity\ProductClass;
33
use Eccube\Entity\ProductImage;
34
use Eccube\Entity\ProductStock;
35
use Eccube\Entity\ProductTag;
36
use Eccube\Exception\CsvImportException;
37
use Eccube\Service\CsvImportService;
38
use Eccube\Util\Str;
39
use Symfony\Component\Filesystem\Filesystem;
40
use Symfony\Component\HttpFoundation\Request;
41
use Symfony\Component\HttpFoundation\StreamedResponse;
42
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
43
44
class CsvImportController
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
45
{
46
47
    private $errors = array();
48
49
    private $fileName;
50
51
    private $em;
52
53
    private $productTwig = 'Product/csv_product.twig';
54
55
    private $categoryTwig = 'Product/csv_category.twig';
56
57
58
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
59
     * 商品登録CSVアップロード
60
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
61 3
    public function csvProduct(Application $app, Request $request)
62
    {
63 3
        $form = $app['form.factory']->createBuilder('admin_csv_import')->getForm();
64
65 3
        $headers = $this->getProductCsvHeader();
66
67 3
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
68
69 3
            $form->handleRequest($request);
70
71 3
            if ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
72
73 3
                $formFile = $form['import_file']->getData();
74
75 3
                if (!empty($formFile)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
76
77 3
                    log_info('商品CSV登録開始');
78
79 3
                    $data = $this->getImportData($app, $formFile);
80 3
                    if ($data === false) {
81
                        $this->addErrors('CSVのフォーマットが一致しません。');
82
                        return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
83
                    }
84
85 3
                    $keys = array_keys($headers);
86 3
                    $columnHeaders = $data->getColumnHeaders();
87 3
                    if ($keys !== $columnHeaders) {
88
                        $this->addErrors('CSVのフォーマットが一致しません。');
89
                        return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
90
                    }
91
92 3
                    $size = count($data);
93 3
                    if ($size < 1) {
94
                        $this->addErrors('CSVデータが存在しません。');
95
                        return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
96
                    }
97
98 3
                    $headerSize = count($keys);
99
100 3
                    $this->em = $app['orm.em'];
101 3
                    $this->em->getConfiguration()->setSQLLogger(null);
102
103 3
                    $this->em->getConnection()->beginTransaction();
104
105 3
                    $BaseInfo = $app['eccube.repository.base_info']->get();
106
107
                    // CSVファイルの登録処理
108 3
                    foreach ($data as $row) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
109
110 3
                        if ($headerSize != count($row)) {
111
                            $this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
112
                            return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
113
                        }
114
115 3
                        if ($row['商品ID'] == '') {
116 2
                            $Product = new Product();
117 2
                            $this->em->persist($Product);
118
                        } else {
119 2
                            if (preg_match('/^\d+$/', $row['商品ID'])) {
120 2
                                $Product = $app['eccube.repository.product']->find($row['商品ID']);
121 2
                                if (!$Product) {
122
                                    $this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
123 2
                                    return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
124
                                }
125
                            } else {
126
                                $this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
127
                                return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
128
                            }
129
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
130
                        }
131
132 3
                        if ($row['公開ステータス(ID)'] == '') {
133
                            $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
134
                        } else {
135 3
                            if (preg_match('/^\d+$/', $row['公開ステータス(ID)'])) {
136 3
                                $Disp = $app['eccube.repository.master.disp']->find($row['公開ステータス(ID)']);
137 3
                                if (!$Disp) {
138
                                    $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
139
                                } else {
140 3
                                    $Product->setStatus($Disp);
141
                                }
142
                            } else {
143
                                $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
144
                            }
145
                        }
146
147 3 View Code Duplication
                        if (Str::isBlank($row['商品名'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
                            $this->addErrors(($data->key() + 1) . '行目の商品名が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
149
                            return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
150
                        } else {
151 3
                            $Product->setName(Str::trimAll($row['商品名']));
152
                        }
153
154 3 View Code Duplication
                        if (Str::isNotBlank($row['ショップ用メモ欄'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155 2
                            $Product->setNote(Str::trimAll($row['ショップ用メモ欄']));
156
                        } else {
157 1
                            $Product->setNote(null);
158
                        }
159
160 3 View Code Duplication
                        if (Str::isNotBlank($row['商品説明(一覧)'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161 3
                            $Product->setDescriptionList(Str::trimAll($row['商品説明(一覧)']));
162
                        } else {
163 1
                            $Product->setDescriptionList(null);
164
                        }
165
166 3 View Code Duplication
                        if (Str::isNotBlank($row['商品説明(詳細)'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167 3
                            $Product->setDescriptionDetail(Str::trimAll($row['商品説明(詳細)']));
168
                        } else {
169
                            $Product->setDescriptionDetail(null);
170
                        }
171
172 3 View Code Duplication
                        if (Str::isNotBlank($row['検索ワード'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173 2
                            $Product->setSearchWord(Str::trimAll($row['検索ワード']));
174
                        } else {
175 1
                            $Product->setSearchWord(null);
176
                        }
177
178 3 View Code Duplication
                        if (Str::isNotBlank($row['フリーエリア'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179 2
                            $Product->setFreeArea(Str::trimAll($row['フリーエリア']));
180
                        } else {
181 1
                            $Product->setFreeArea(null);
182
                        }
183
184 3
                        if ($row['商品削除フラグ'] == '') {
185
                            $Product->setDelFlg(Constant::DISABLED);
186
                        } else {
187 3
                            if ($row['商品削除フラグ'] == (string)Constant::DISABLED || $row['商品削除フラグ'] == (string)Constant::ENABLED) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
188 3
                                $Product->setDelFlg($row['商品削除フラグ']);
189
                            } else {
190
                                $this->addErrors(($data->key() + 1) . '行目の商品削除フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
191
                                return $this->render($app, $form, $headers, $this->productTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
192
                            }
193
                        }
194
195
                        // 商品画像登録
196 3
                        $this->createProductImage($row, $Product, $data);
197
198 3
                        $this->em->flush($Product);
199
200
                        // 商品カテゴリ登録
201 3
                        $this->createProductCategory($row, $Product, $app, $data);
202
203
                        //タグ登録
204 3
                        $this->createProductTag($row, $Product, $app, $data);
205
206
                        // 商品規格が存在しなければ新規登録
207 3
                        $ProductClasses = $Product->getProductClasses();
208 3
                        if ($ProductClasses->count() < 1) {
209
                            // 規格分類1(ID)がセットされていると規格なし商品、規格あり商品を作成
210 2
                            $ProductClassOrg = $this->createProductClass($row, $Product, $app, $data);
211 2
                            if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) {
212
                                if ($row['送料'] != '') {
213
                                    $deliveryFee = str_replace(',', '', $row['送料']);
214
                                    if (preg_match('/^\d+$/', $deliveryFee) && $deliveryFee >= 0) {
215
                                        $ProductClassOrg->setDeliveryFee($deliveryFee);
216
                                    } else {
217
                                        $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
218
                                    }
219
                                }
220
                            }
221
222 2
                            if ($row['規格分類1(ID)'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
223
224 1
                                if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) {
225
                                    $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
226
                                } else {
227
                                    // 商品規格あり
228
                                    // 企画分類あり商品を作成
229 1
                                    $ProductClass = clone $ProductClassOrg;
230 1
                                    $ProductStock = clone $ProductClassOrg->getProductStock();
231
232
                                    // 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット
233 1
                                    $ProductClassOrg->setDelFlg(Constant::ENABLED);
234
235
                                    // 規格分類1、2をそれぞれセットし作成
236 1
                                    $ClassCategory1 = null;
237 1
                                    if (preg_match('/^\d+$/', $row['規格分類1(ID)'])) {
238 1
                                        $ClassCategory1 = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']);
239 1
                                        if (!$ClassCategory1) {
240
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
241
                                        } else {
242 1
                                            $ProductClass->setClassCategory1($ClassCategory1);
243
                                        }
244
                                    } else {
245
                                        $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
246
                                    }
247
248 1
                                    if ($row['規格分類2(ID)'] != '') {
249 1
                                        if (preg_match('/^\d+$/', $row['規格分類2(ID)'])) {
250 1
                                            $ClassCategory2 = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']);
251 1 View Code Duplication
                                            if (!$ClassCategory2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
252
                                                $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
253
                                            } else {
254 1
                                                if ($ClassCategory1 &&
255 1
                                                    ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId())
256
                                                ) {
257
                                                    $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
258
                                                } else {
259 1
                                                    $ProductClass->setClassCategory2($ClassCategory2);
260
                                                }
261
                                            }
262
                                        } else {
263
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
264
                                        }
265
                                    }
266 1
                                    $ProductClass->setProductStock($ProductStock);
267 1
                                    $ProductStock->setProductClass($ProductClass);
268
269 1
                                    $this->em->persist($ProductClass);
270 1
                                    $this->em->persist($ProductStock);
271
                                }
272
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
273
                            } else {
274 1
                                if ($row['規格分類2(ID)'] != '') {
275 2
                                    $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
276
                                }
277
                            }
278
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
279
                        } else {
280
                            // 商品規格の更新
281
282 2
                            $flag = false;
283 2
                            $classCategoryId1 = $row['規格分類1(ID)'] == '' ? null : $row['規格分類1(ID)'];
284 2
                            $classCategoryId2 = $row['規格分類2(ID)'] == '' ? null : $row['規格分類2(ID)'];
285
286 2
                            foreach ($ProductClasses as $pc) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
287
288 2
                                $classCategory1 = is_null($pc->getClassCategory1()) ? null : $pc->getClassCategory1()->getId();
289 2
                                $classCategory2 = is_null($pc->getClassCategory2()) ? null : $pc->getClassCategory2()->getId();
290
291
                                // 登録されている商品規格を更新
292 2
                                if ($classCategory1 == $classCategoryId1 &&
293 2
                                    $classCategory2 == $classCategoryId2
294
                                ) {
295 1
                                    $this->updateProductClass($row, $Product, $pc, $app, $data);
296
297 1
                                    if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) {
298
                                        if ($row['送料'] != '') {
299
                                            $deliveryFee = str_replace(',', '', $row['送料']);
300
                                            if (preg_match('/^\d+$/', $deliveryFee) && $deliveryFee >= 0) {
301
                                                $pc->setDeliveryFee($deliveryFee);
302
                                            } else {
303
                                                $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
304
                                            }
305
                                        }
306
                                    }
307
308 1
                                    $flag = true;
309 2
                                    break;
310
                                }
311
                            }
312
313
                            // 商品規格を登録
314 2
                            if (!$flag) {
315 1
                                $pc = $ProductClasses[0];
316 1
                                if ($pc->getClassCategory1() == null &&
317 1
                                    $pc->getClassCategory2() == null
318
                                ) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
319
320
                                    // 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット
321 1
                                    $pc->setDelFlg(Constant::ENABLED);
322
                                }
323
324 1
                                if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) {
325
                                    $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
326
                                } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
327
328
                                    // 必ず規格分類1がセットされている
329
                                    // 規格分類1、2をそれぞれセットし作成
330 1
                                    $ClassCategory1 = null;
331 1
                                    if (preg_match('/^\d+$/', $classCategoryId1)) {
332 1
                                        $ClassCategory1 = $app['eccube.repository.class_category']->find($classCategoryId1);
333 1
                                        if (!$ClassCategory1) {
334 1
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
335
                                        }
336
                                    } else {
337
                                        $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
338
                                    }
339
340 1
                                    $ClassCategory2 = null;
341 1
                                    if ($row['規格分類2(ID)'] != '') {
342 1
                                        if ($pc->getClassCategory1() != null && $pc->getClassCategory2() == null) {
343
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)は設定できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
344
                                        } else {
345 1
                                            if (preg_match('/^\d+$/', $classCategoryId2)) {
346 1
                                                $ClassCategory2 = $app['eccube.repository.class_category']->find($classCategoryId2);
347 1 View Code Duplication
                                                if (!$ClassCategory2) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
348
                                                    $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
349
                                                } else {
350 1
                                                    if ($ClassCategory1 &&
351 1
                                                        ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId())
352
                                                    ) {
353 1
                                                        $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)の規格名が同じです。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
354
                                                    }
355
                                                }
356
                                            } else {
357 1
                                                $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
358
                                            }
359
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
360
                                        }
361
                                    } else {
362
                                        if ($pc->getClassCategory1() != null && $pc->getClassCategory2() != null) {
363
                                            $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)に値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
364
                                        }
365
                                    }
366 1
                                    $ProductClass = $this->createProductClass($row, $Product, $app, $data, $ClassCategory1, $ClassCategory2);
367
368 1
                                    if ($BaseInfo->getOptionProductDeliveryFee() == Constant::ENABLED) {
369
                                        if ($row['送料'] != '') {
370
                                            $deliveryFee = str_replace(',', '', $row['送料']);
371
                                            if (preg_match('/^\d+$/', $deliveryFee) && $deliveryFee >= 0) {
372
                                                $ProductClass->setDeliveryFee($deliveryFee);
373
                                            } else {
374
                                                $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
375
                                            }
376
                                        }
377
                                    }
378
379 1
                                    $Product->addProductClass($ProductClass);
380
                                }
381
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
382
                            }
383
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
384
                        }
385
386
387 3
                        if ($this->hasErrors()) {
388
                            return $this->render($app, $form, $headers, $this->productTwig);
389
                        }
390
391 3
                        $this->em->persist($Product);
392
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
393
                    }
394
395 3
                    $this->em->flush();
396 3
                    $this->em->getConnection()->commit();
397
398 3
                    log_info('商品CSV登録完了');
399
400 3
                    $app->addSuccess('admin.product.csv_import.save.complete', 'admin');
401
                }
402
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
403
            }
404
        }
405
406 3
        return $this->render($app, $form, $headers, $this->productTwig);
407
    }
408
409
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
410
     * カテゴリ登録CSVアップロード
411
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
412 2
    public function csvCategory(Application $app, Request $request)
413
    {
414
415 2
        $form = $app['form.factory']->createBuilder('admin_csv_import')->getForm();
416
417 2
        $headers = $this->getCategoryCsvHeader();
418
419 2
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
420
421 2
            $form->handleRequest($request);
422
423 2
            if ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
424
425 2
                $formFile = $form['import_file']->getData();
426
427 2
                if (!empty($formFile)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
428
429 2
                    log_info('カテゴリCSV登録開始');
430
431 2
                    $data = $this->getImportData($app, $formFile);
432 2 View Code Duplication
                    if ($data === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
433
                        $this->addErrors('CSVのフォーマットが一致しません。');
434
                        return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
435
                    }
436
437 2
                    $keys = array_keys($headers);
438 2
                    $columnHeaders = $data->getColumnHeaders();
439 2
                    if ($keys !== $columnHeaders) {
440
                        $this->addErrors('CSVのフォーマットが一致しません。');
441
                        return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
442
                    }
443
444 2
                    $size = count($data);
445 2
                    if ($size < 1) {
446
                        $this->addErrors('CSVデータが存在しません。');
447
                        return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
448
                    }
449
450 2
                    $headerSize = count($keys);
451
452 2
                    $this->em = $app['orm.em'];
453 2
                    $this->em->getConfiguration()->setSQLLogger(null);
454
455 2
                    $this->em->getConnection()->beginTransaction();
456
457
                    // CSVファイルの登録処理
458 2
                    foreach ($data as $row) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
459
460 2
                        if ($headerSize != count($row)) {
461
                            $this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
462
                            return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
463
                        }
464
465 2
                        if ($row['カテゴリID'] == '') {
466 1
                            $Category = new Category();
467
                        } else {
468 1 View Code Duplication
                            if (!preg_match('/^\d+$/', $row['カテゴリID'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
469
                                $this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
470
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
471
                            }
472 1
                            $Category = $app['eccube.repository.category']->find($row['カテゴリID']);
473 1 View Code Duplication
                            if (!$Category) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
474
                                $this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
475
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
476
                            }
477 1 View Code Duplication
                            if ($row['カテゴリID'] == $row['親カテゴリID']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
478
                                $this->addErrors(($data->key() + 1) . '行目のカテゴリIDと親カテゴリIDが同じです。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
479
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
480
                            }
481
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
482
                        }
483
484 2 View Code Duplication
                        if (Str::isBlank($row['カテゴリ名'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
485
                            $this->addErrors(($data->key() + 1) . '行目のカテゴリ名が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
486
                            return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
487
                        } else {
488 2
                            $Category->setName(Str::trimAll($row['カテゴリ名']));
489
                        }
490
491 2
                        if ($row['親カテゴリID'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
492
493 1 View Code Duplication
                            if (!preg_match('/^\d+$/', $row['親カテゴリID'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
494
                                $this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
495
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
496
                            }
497
498 1
                            $ParentCategory = $app['eccube.repository.category']->find($row['親カテゴリID']);
499 1 View Code Duplication
                            if (!$ParentCategory) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
500
                                $this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
501 1
                                return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
502
                            }
503
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
504
                        } else {
505 2
                            $ParentCategory = null;
506
                        }
507
508 2
                        $Category->setParent($ParentCategory);
509 2
                        if ($ParentCategory) {
510 1
                            $Category->setLevel($ParentCategory->getLevel() + 1);
511
                        } else {
512 2
                            $Category->setLevel(1);
513
                        }
514
515 2 View Code Duplication
                        if ($app['config']['category_nest_level'] < $Category->getLevel()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
516
                            $this->addErrors(($data->key() + 1) . '行目のカテゴリが最大レベルを超えているため設定できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
517
                            return $this->render($app, $form, $headers, $this->categoryTwig);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
518
                        }
519
520 2
                        $status = $app['eccube.repository.category']->save($Category);
521
522 2
                        if (!$status) {
523
                            $this->addErrors(($data->key() + 1) . '行目のカテゴリが設定できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
524
                        }
525
526 2
                        if ($this->hasErrors()) {
527
                            return $this->render($app, $form, $headers, $this->categoryTwig);
528
                        }
529
530 2
                        $this->em->persist($Category);
531
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
532
                    }
533
534 2
                    $this->em->flush();
535 2
                    $this->em->getConnection()->commit();
536
537 2
                    log_info('カテゴリCSV登録完了');
538
539 2
                    $app->addSuccess('admin.category.csv_import.save.complete', 'admin');
540
                }
541
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
542
            }
543
        }
544
545 2
        return $this->render($app, $form, $headers, $this->categoryTwig);
546
    }
547
548
549
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" missing
Loading history...
introduced by
Doc comment for parameter "$type" missing
Loading history...
550
     * アップロード用CSV雛形ファイルダウンロード
551
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
552
    public function csvTemplate(Application $app, Request $request, $type)
553
    {
554
        set_time_limit(0);
555
556
        $response = new StreamedResponse();
557
558
        if ($type == 'product') {
559
            $headers = $this->getProductCsvHeader();
560
            $filename = 'product.csv';
561
        } else if ($type == 'category') {
562
            $headers = $this->getCategoryCsvHeader();
563
            $filename = 'category.csv';
564
        } else {
565
            throw new NotFoundHttpException();
566
        }
567
568
        $response->setCallback(function () use ($app, $request, $headers) {
569
570
            // ヘッダ行の出力
571
            $row = array();
572
            foreach ($headers as $key => $value) {
573
                $row[] = mb_convert_encoding($key, $app['config']['csv_export_encoding'], 'UTF-8');
574
            }
575
576
            $fp = fopen('php://output', 'w');
577
            fputcsv($fp, $row, $app['config']['csv_export_separator']);
578
            fclose($fp);
579
580
        });
581
582
        $response->headers->set('Content-Type', 'application/octet-stream');
583
        $response->headers->set('Content-Disposition', 'attachment; filename=' . $filename);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
584
        $response->send();
585
586
        return $response;
587
    }
588
589
590
    /**
591
     * 登録、更新時のエラー画面表示
592
     *
593
     */
594 5
    protected function render($app, $form, $headers, $twig)
595
    {
596
597 5
        if ($this->hasErrors()) {
598
            if ($this->em) {
599
                $this->em->getConnection()->rollback();
600
            }
601
        }
602
603 5
        if (!empty($this->fileName)) {
604
            try {
605 5
                $fs = new Filesystem();
606 5
                $fs->remove($app['config']['csv_temp_realdir'] . '/' . $this->fileName);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
607
            } catch (\Exception $e) {
608
                // エラーが発生しても無視する
609
            }
610
        }
611
612 5
        return $app->render($twig, array(
613 5
            'form' => $form->createView(),
614 5
            'headers' => $headers,
615 5
            'errors' => $this->errors,
616
        ));
617
    }
618
619
620
    /**
621
     * アップロードされたCSVファイルの行ごとの処理
622
     *
623
     * @param $formFile
624
     * @return CsvImportService
625
     */
626 5
    protected function getImportData($app, $formFile)
627
    {
628
        // アップロードされたCSVファイルを一時ディレクトリに保存
629 5
        $this->fileName = 'upload_' . Str::random() . '.' . $formFile->getClientOriginalExtension();
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
630 5
        $formFile->move($app['config']['csv_temp_realdir'], $this->fileName);
631
632 5
        $file = file_get_contents($app['config']['csv_temp_realdir'] . '/' . $this->fileName);
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
633
634 5
        if ('\\' === DIRECTORY_SEPARATOR && PHP_VERSION_ID >= 70000) {
635
            // Windows 環境の PHP7 の場合はファイルエンコーディングを CP932 に合わせる
636
            // see https://github.com/EC-CUBE/ec-cube/issues/1780
637
            setlocale(LC_ALL, ''); // 既定のロケールに設定
638
            if (mb_detect_encoding($file) === 'UTF-8') { // UTF-8 を検出したら SJIS-win に変換
639
                $file = mb_convert_encoding($file, 'SJIS-win', 'UTF-8');
640
            }
641
        } else {
642
            // アップロードされたファイルがUTF-8以外は文字コード変換を行う
643 5
            $encode = Str::characterEncoding(substr($file, 0, 6));
644 5
            if ($encode != 'UTF-8') {
645 2
                $file = mb_convert_encoding($file, 'UTF-8', $encode);
646
            }
647
        }
648 5
        $file = Str::convertLineFeed($file);
649
650 5
        $tmp = tmpfile();
651 5
        fwrite($tmp, $file);
652 5
        rewind($tmp);
653 5
        $meta = stream_get_meta_data($tmp);
654 5
        $file = new \SplFileObject($meta['uri']);
655
656 5
        set_time_limit(0);
657
658
        // アップロードされたCSVファイルを行ごとに取得
659 5
        $data = new CsvImportService($file, $app['config']['csv_import_delimiter'], $app['config']['csv_import_enclosure']);
660
661 5
        $ret = $data->setHeaderRowNumber(0);
662
663 5
        return ($ret !== false) ? $data : false;
664
    }
665
666
667
    /**
668
     * 商品画像の削除、登録
669
     */
670 3
    protected function createProductImage($row, Product $Product, $data)
671
    {
672 3
        if ($row['商品画像'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
673
674
            // 画像の削除
675 3
            $ProductImages = $Product->getProductImage();
676 3
            foreach ($ProductImages as $ProductImage) {
677 2
                $Product->removeProductImage($ProductImage);
678 3
                $this->em->remove($ProductImage);
679
            }
680
681
            // 画像の登録
682 3
            $images = explode(',', $row['商品画像']);
683 3
            $rank = 1;
684
685 3
            $pattern = "/\\$|^.*.\.\\\.*|\/$|^.*.\.\/\.*/";
686 3
            foreach ($images as $image) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
687
688 3
                $fileName = Str::trimAll($image);
689
690
                // 商品画像名のフォーマットチェック
691 3
                if (strlen($fileName) > 0 && preg_match($pattern, $fileName)) {
692
                    $this->addErrors(($data->key() + 1) . '行目の商品画像には末尾に"/"や"../"を使用できません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
693
                } else {
694
                    // 空文字は登録対象外
695 3
                    if (!empty($fileName)) {
696 3
                        $ProductImage = new ProductImage();
697 3
                        $ProductImage->setFileName($fileName);
698 3
                        $ProductImage->setProduct($Product);
699 3
                        $ProductImage->setRank($rank);
700
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
701 3
                        $Product->addProductImage($ProductImage);
702 3
                        $rank++;
703 3
                        $this->em->persist($ProductImage);
704
                    }
705
                }
706
            }
707
        }
708
    }
709
710
711
    /**
712
     * 商品カテゴリの削除、登録
713
     */
714 3
    protected function createProductCategory($row, Product $Product, $app, $data)
715
    {
716
        // カテゴリの削除
717 3
        $ProductCategories = $Product->getProductCategories();
718 3
        foreach ($ProductCategories as $ProductCategory) {
719 2
            $Product->removeProductCategory($ProductCategory);
720 2
            $this->em->remove($ProductCategory);
721 3
            $this->em->flush($ProductCategory);
722
        }
723
724 3
        if ($row['商品カテゴリ(ID)'] == '') {
725
            // 入力されていなければ削除のみ
726
            return;
727
        }
728
729
        // カテゴリの登録
730 3
        $categories = explode(',', $row['商品カテゴリ(ID)']);
731 3
        $rank = 1;
732 3
        $categoriesIdList = array();
733 3
        foreach ($categories as $category) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
734
735 3
            if (preg_match('/^\d+$/', $category)) {
736 3
                $Category = $app['eccube.repository.category']->find($category);
737 3
                if (!$Category) {
738
                    $this->addErrors(($data->key() + 1).'行目の商品カテゴリ(ID)「'.$category.'」が存在しません。');
739
                } else {
740 3 View Code Duplication
                    foreach($Category->getPath() as $ParentCategory){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
741 3
                        if (!isset($categoriesIdList[$ParentCategory->getId()])){
742 3
                            $ProductCategory = $this->makeProductCategory($Product, $ParentCategory, $rank);
743 3
                            $app['orm.em']->persist($ProductCategory);
744 3
                            $rank++;
745 3
                            $Product->addProductCategory($ProductCategory);
746 3
                            $categoriesIdList[$ParentCategory->getId()] = true;
747
                        }
748
                    }
749 3
                    if (!isset($categoriesIdList[$Category->getId()])){
750
                        $ProductCategory = $this->makeProductCategory($Product, $Category, $rank);
751
                        $rank++;
752
                        $this->em->persist($ProductCategory);
753
                        $Product->addProductCategory($ProductCategory);
754 3
                        $categoriesIdList[$Category->getId()] = true;
755
                    }
756
                }
757
            } else {
758 3
                $this->addErrors(($data->key() + 1).'行目の商品カテゴリ(ID)「'.$category.'」が存在しません。');
759
            }
760
        }
761
762
    }
763
764
765
    /**
766
     * タグの登録
767
     *
768
     * @param array $row
769
     * @param Product $Product
770
     * @param Application $app
771
     * @param CsvImportService $data
772
     */
773 3
    protected function createProductTag($row, Product $Product, $app, $data)
774
    {
775
        // タグの削除
776 3
        $ProductTags = $Product->getProductTag();
777 3
        foreach ($ProductTags as $ProductTags) {
778 1
            $Product->removeProductTag($ProductTags);
779 3
            $this->em->remove($ProductTags);
780
        }
781
782 3
        if ($row['タグ(ID)'] == '') {
783
            return;
784
        }
785
786
        // タグの登録
787 3
        $tags = explode(',', $row['タグ(ID)']);
788 3
        foreach ($tags as $tag_id) {
789 3
            $Tag = null;
790 3
            if (preg_match('/^\d+$/', $tag_id)) {
791 3
                $Tag = $app['eccube.repository.master.tag']->find($tag_id);
792 3
                if ($Tag) {
793 3
                    $ProductTags = new ProductTag();
794
                    $ProductTags
795 3
                        ->setProduct($Product)
796 3
                        ->setTag($Tag);
797
798 3
                    $Product->addProductTag($ProductTags);
799
800 3
                    $this->em->persist($ProductTags);
801
                }
802
            }
803 3
            if (!$Tag) {
804 3
                $this->addErrors(($data->key() + 1) . '行目のタグ(ID)「' . $tag_id . '」が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
805
            }
806
        }
807
    }
808
809
810
    /**
811
     * 商品規格分類1、商品規格分類2がnullとなる商品規格情報を作成
812
     */
813 3
    protected function createProductClass($row, Product $Product, $app, $data, $ClassCategory1 = null, $ClassCategory2 = null)
814
    {
815
        // 規格分類1、規格分類2がnullとなる商品を作成
816
817 3
        $ProductClass = new ProductClass();
818 3
        $ProductClass->setProduct($Product);
819
820
821 3 View Code Duplication
        if ($row['商品種別(ID)'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
822
            $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
823
        } else {
824 3
            if (preg_match('/^\d+$/', $row['商品種別(ID)'])) {
825 3
                $ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']);
826 3
                if (!$ProductType) {
827
                    $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
828
                } else {
829 3
                    $ProductClass->setProductType($ProductType);
830
                }
831
            } else {
832
                $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
833
            }
834
        }
835
836 3
        $ProductClass->setClassCategory1($ClassCategory1);
837 3
        $ProductClass->setClassCategory2($ClassCategory2);
838
839 3
        if ($row['発送日目安(ID)'] != '') {
840 2
            if (preg_match('/^\d+$/', $row['発送日目安(ID)'])) {
841 2
                $DeliveryDate = $app['eccube.repository.delivery_date']->find($row['発送日目安(ID)']);
842 2
                if (!$DeliveryDate) {
843
                    $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
844
                } else {
845 2
                    $ProductClass->setDeliveryDate($DeliveryDate);
846
                }
847
            } else {
848
                $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
849
            }
850
        }
851
852 3 View Code Duplication
        if (Str::isNotBlank($row['商品コード'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
853 2
            $ProductClass->setCode(Str::trimAll($row['商品コード']));
854
        } else {
855 1
            $ProductClass->setCode(null);
856
        }
857
858 3 View Code Duplication
        if ($row['在庫数無制限フラグ'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
859
            $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
860
        } else {
861 3
            if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) {
862 3
                $ProductClass->setStockUnlimited(Constant::DISABLED);
863
                // 在庫数が設定されていなければエラー
864 3
                if ($row['在庫数'] == '') {
865
                    $this->addErrors(($data->key() + 1) . '行目の在庫数が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
866
                } else {
867 3
                    $stock = str_replace(',', '', $row['在庫数']);
868 3
                    if (preg_match('/^\d+$/', $stock) && $stock >= 0) {
869 3
                        $ProductClass->setStock($stock);
870
                    } else {
871 3
                        $this->addErrors(($data->key() + 1) . '行目の在庫数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
872
                    }
873
                }
874
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
875
            } else if ($row['在庫数無制限フラグ'] == (string) Constant::ENABLED) {
876
                $ProductClass->setStockUnlimited(Constant::ENABLED);
877
                $ProductClass->setStock(null);
878
            } else {
879
                $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
880
            }
881
        }
882
883 3
        if ($row['販売制限数'] != '') {
884
            $saleLimit = str_replace(',', '', $row['販売制限数']);
885
            if (preg_match('/^\d+$/', $saleLimit) && $saleLimit >= 0) {
886
                $ProductClass->setSaleLimit($saleLimit);
887
            } else {
888
                $this->addErrors(($data->key() + 1) . '行目の販売制限数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
889
            }
890
        }
891
892 3
        if ($row['通常価格'] != '') {
893 2
            $price01 = str_replace(',', '', $row['通常価格']);
894 2
            if (preg_match('/^\d+$/', $price01) && $price01 >= 0) {
895 2
                $ProductClass->setPrice01($price01);
896
            } else {
897
                $this->addErrors(($data->key() + 1) . '行目の通常価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
898
            }
899
        }
900
901 3
        if ($row['販売価格'] == '') {
902
            $this->addErrors(($data->key() + 1) . '行目の販売価格が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
903
        } else {
904 3
            $price02 = str_replace(',', '', $row['販売価格']);
905 3
            if (preg_match('/^\d+$/', $price02) && $price02 >= 0) {
906 3
                $ProductClass->setPrice02($price02);
907
            } else {
908
                $this->addErrors(($data->key() + 1) . '行目の販売価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
909
            }
910
        }
911
912 3
        if ($row['送料'] != '') {
913 2
            $delivery_fee = str_replace(',', '', $row['送料']);
914 2
            if (preg_match('/^\d+$/', $delivery_fee) && $delivery_fee >= 0) {
915 2
                $ProductClass->setDeliveryFee($delivery_fee);
916
            } else {
917
                $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
918
            }
919
        }
920
921 3 View Code Duplication
        if ($row['商品規格削除フラグ'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
922
            $ProductClass->setDelFlg(Constant::DISABLED);
923
        } else {
924 3
            if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) {
925 3
                $ProductClass->setDelFlg($row['商品規格削除フラグ']);
926
            } else {
927
                $this->addErrors(($data->key() + 1) . '行目の商品規格削除フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
928
            }
929
        }
930
931 3
        $Product->addProductClass($ProductClass);
932 3
        $ProductStock = new ProductStock();
933 3
        $ProductClass->setProductStock($ProductStock);
934 3
        $ProductStock->setProductClass($ProductClass);
935
936 3
        if (!$ProductClass->getStockUnlimited()) {
937 3
            $ProductStock->setStock($ProductClass->getStock());
938
        } else {
939
            // 在庫無制限時はnullを設定
940
            $ProductStock->setStock(null);
941
        }
942
943 3
        $this->em->persist($ProductClass);
944 3
        $this->em->persist($ProductStock);
945
946 3
        return $ProductClass;
947
948
    }
949
950
951
    /**
952
     * 商品規格情報を更新
953
     */
954 1
    protected function updateProductClass($row, Product $Product, ProductClass $ProductClass, $app, $data)
955
    {
956
957 1
        $ProductClass->setProduct($Product);
958
959 1 View Code Duplication
        if ($row['商品種別(ID)'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
960
            $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
961
        } else {
962 1
            if (preg_match('/^\d+$/', $row['商品種別(ID)'])) {
963 1
                $ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']);
964 1
                if (!$ProductType) {
965
                    $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
966
                } else {
967 1
                    $ProductClass->setProductType($ProductType);
968
                }
969
            } else {
970
                $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
971
            }
972
        }
973
974
        // 規格分類1、2をそれぞれセットし作成
975 1
        if ($row['規格分類1(ID)'] != '') {
976 1
            if (preg_match('/^\d+$/', $row['規格分類1(ID)'])) {
977 1
                $ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']);
978 1
                if (!$ClassCategory) {
979
                    $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
980
                } else {
981 1
                    $ProductClass->setClassCategory1($ClassCategory);
982
                }
983
            } else {
984
                $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
985
            }
986
        }
987
988 1
        if ($row['規格分類2(ID)'] != '') {
989 1
            if (preg_match('/^\d+$/', $row['規格分類2(ID)'])) {
990 1
                $ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']);
991 1
                if (!$ClassCategory) {
992
                    $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
993
                } else {
994 1
                    $ProductClass->setClassCategory2($ClassCategory);
995
                }
996
            } else {
997
                $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
998
            }
999
        }
1000
1001 1
        if ($row['発送日目安(ID)'] != '') {
1002
            if (preg_match('/^\d+$/', $row['発送日目安(ID)'])) {
1003
                $DeliveryDate = $app['eccube.repository.delivery_date']->find($row['発送日目安(ID)']);
1004
                if (!$DeliveryDate) {
1005
                    $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1006
                } else {
1007
                    $ProductClass->setDeliveryDate($DeliveryDate);
1008
                }
1009
            } else {
1010
                $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1011
            }
1012
        }
1013
1014 1 View Code Duplication
        if (Str::isNotBlank($row['商品コード'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1015 1
            $ProductClass->setCode(Str::trimAll($row['商品コード']));
1016
        } else {
1017
            $ProductClass->setCode(null);
1018
        }
1019
1020 1 View Code Duplication
        if ($row['在庫数無制限フラグ'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1021
            $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1022
        } else {
1023 1
            if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) {
1024 1
                $ProductClass->setStockUnlimited(Constant::DISABLED);
1025
                // 在庫数が設定されていなければエラー
1026 1
                if ($row['在庫数'] == '') {
1027
                    $this->addErrors(($data->key() + 1) . '行目の在庫数が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1028
                } else {
1029 1
                    $stock = str_replace(',', '', $row['在庫数']);
1030 1
                    if (preg_match('/^\d+$/', $stock) && $stock >= 0) {
1031 1
                        $ProductClass->setStock($row['在庫数']);
1032
                    } else {
1033 1
                        $this->addErrors(($data->key() + 1) . '行目の在庫数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1034
                    }
1035
                }
1036
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
1037 1
            } else if ($row['在庫数無制限フラグ'] == (string) Constant::ENABLED) {
1038 1
                $ProductClass->setStockUnlimited(Constant::ENABLED);
1039 1
                $ProductClass->setStock(null);
1040
            } else {
1041
                $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1042
            }
1043
        }
1044
1045 1
        if ($row['販売制限数'] != '') {
1046 1
            $saleLimit = str_replace(',', '', $row['販売制限数']);
1047 1
            if (preg_match('/^\d+$/', $saleLimit) && $saleLimit >= 0) {
1048 1
                $ProductClass->setSaleLimit($saleLimit);
1049
            } else {
1050
                $this->addErrors(($data->key() + 1) . '行目の販売制限数は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1051
            }
1052
        }
1053
1054 1
        if ($row['通常価格'] != '') {
1055 1
            $price01 = str_replace(',', '', $row['通常価格']);
1056 1
            if (preg_match('/^\d+$/', $price01) && $price01 >= 0) {
1057 1
                $ProductClass->setPrice01($price01);
1058
            } else {
1059
                $this->addErrors(($data->key() + 1) . '行目の通常価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1060
            }
1061
        }
1062
1063 1
        if ($row['販売価格'] == '') {
1064
            $this->addErrors(($data->key() + 1) . '行目の販売価格が設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1065
        } else {
1066 1
            $price02 = str_replace(',', '', $row['販売価格']);
1067 1
            if (preg_match('/^\d+$/', $price02) && $price02 >= 0) {
1068 1
                $ProductClass->setPrice02($price02);
1069
            } else {
1070
                $this->addErrors(($data->key() + 1) . '行目の販売価格は0以上の数値を設定してください。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1071
            }
1072
        }
1073
1074 1 View Code Duplication
        if ($row['商品規格削除フラグ'] == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1075
            $ProductClass->setDelFlg(Constant::DISABLED);
1076
        } else {
1077 1
            if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) {
1078 1
                $ProductClass->setDelFlg($row['商品規格削除フラグ']);
1079
            } else {
1080
                $this->addErrors(($data->key() + 1) . '行目の商品規格削除フラグが設定されていません。');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
1081
            }
1082
        }
1083
1084 1
        $ProductStock = $ProductClass->getProductStock();
1085
1086 1
        if (!$ProductClass->getStockUnlimited()) {
1087 1
            $ProductStock->setStock($ProductClass->getStock());
1088
        } else {
1089
            // 在庫無制限時はnullを設定
1090 1
            $ProductStock->setStock(null);
1091
        }
1092
1093 1
        return $ProductClass;
1094
    }
1095
1096
    /**
1097
     * 登録、更新時のエラー画面表示
1098
     *
1099
     */
1100
    protected function addErrors($message)
1101
    {
1102
        $e = new CsvImportException($message);
1103
        $this->errors[] = $e;
1104
    }
1105
1106
    /**
1107
     * @return array
1108
     */
1109 5
    protected function getErrors()
1110
    {
1111 5
        return $this->errors;
1112
    }
1113
1114
    /**
1115
     *
1116
     * @return boolean
1117
     */
1118 5
    protected function hasErrors()
1119
    {
1120 5
        return count($this->getErrors()) > 0;
1121
    }
1122
1123
    /**
1124
     * 商品登録CSVヘッダー定義
1125
     */
1126 3
    private function getProductCsvHeader()
1127
    {
1128
        return array(
1129 3
            '商品ID' => 'id',
1130
            '公開ステータス(ID)' => 'status',
1131
            '商品名' => 'name',
1132
            'ショップ用メモ欄' => 'note',
1133
            '商品説明(一覧)' => 'description_list',
1134
            '商品説明(詳細)' => 'description_detail',
1135
            '検索ワード' => 'search_word',
1136
            'フリーエリア' => 'free_area',
1137
            '商品削除フラグ' => 'product_del_flg',
1138
            '商品画像' => 'product_image',
1139
            '商品カテゴリ(ID)' => 'product_category',
1140
            'タグ(ID)' => 'product_tag',
1141
            '商品種別(ID)' => 'product_type',
1142
            '規格分類1(ID)' => 'class_category1',
1143
            '規格分類2(ID)' => 'class_category2',
1144
            '発送日目安(ID)' => 'deliveryFee',
1145
            '商品コード' => 'product_code',
1146
            '在庫数' => 'stock',
1147
            '在庫数無制限フラグ' => 'stock_unlimited',
1148
            '販売制限数' => 'sale_limit',
1149
            '通常価格' => 'price01',
1150
            '販売価格' => 'price02',
1151
            '送料' => 'delivery_fee',
1152
            '商品規格削除フラグ' => 'product_class_del_flg',
1153
        );
1154
    }
1155
1156
1157
    /**
1158
     * カテゴリCSVヘッダー定義
1159
     */
1160 2
    private function getCategoryCsvHeader()
1161
    {
1162
        return array(
1163 2
            'カテゴリID' => 'id',
1164
            'カテゴリ名' => 'category_name',
1165
            '親カテゴリID' => 'parent_category_id',
1166
        );
1167
    }
1168
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
1169
        /**
1170
     * ProductCategory作成
1171
     * @param \Eccube\Entity\Product $Product
1172
     * @param \Eccube\Entity\Category $Category
1173
     * @return ProductCategory
1174
     */
1175 3 View Code Duplication
    private function makeProductCategory($Product, $Category, $rank)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1176
    {
1177 3
        $ProductCategory = new ProductCategory();
1178 3
        $ProductCategory->setProduct($Product);
1179 3
        $ProductCategory->setProductId($Product->getId());
1180 3
        $ProductCategory->setCategory($Category);
1181 3
        $ProductCategory->setCategoryId($Category->getId());
1182 3
        $ProductCategory->setRank($rank);
1183
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
1184 3
        return $ProductCategory;
1185
    }
1186
}
1187