CsvImportController   D
last analyzed

Complexity

Total Complexity 198

Size/Duplication

Total Lines 1143
Duplicated Lines 18.29 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 209
loc 1143
rs 4.4102
c 0
b 0
f 0
wmc 198
lcom 1
cbo 9

16 Methods

Rating   Name   Duplication   Size   Complexity  
F csvProduct() 51 347 70
D csvCategory() 34 135 21
B csvTemplate() 0 36 4
B render() 0 24 5
B getImportData() 0 39 6
C createProductImage() 0 39 7
C createProductCategory() 9 49 9
C createProductTag() 0 35 7
F createProductClass() 52 136 30
F updateProductClass() 52 141 33
A addErrors() 0 5 1
A getErrors() 0 4 1
A hasErrors() 0 4 1
B getProductCsvHeader() 0 29 1
A getCategoryCsvHeader() 0 8 1
A makeProductCategory() 11 11 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like CsvImportController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CsvImportController, and based on these observations, apply Extract Interface, too.

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
    public function csvProduct(Application $app, Request $request)
62
    {
63
        $form = $app['form.factory']->createBuilder('admin_csv_import')->getForm();
64
65
        $headers = $this->getProductCsvHeader();
66
67
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
68
69
            $form->handleRequest($request);
70
71
            if ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
72
73
                $formFile = $form['import_file']->getData();
74
75
                if (!empty($formFile)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
76
77
                    log_info('商品CSV登録開始');
78
79
                    $data = $this->getImportData($app, $formFile);
80
                    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
                    $keys = array_keys($headers);
86
                    $columnHeaders = $data->getColumnHeaders();
87
                    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
                    $size = count($data);
93
                    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
                    $headerSize = count($keys);
99
100
                    $this->em = $app['orm.em'];
101
                    $this->em->getConfiguration()->setSQLLogger(null);
102
103
                    $this->em->getConnection()->beginTransaction();
104
105
                    $BaseInfo = $app['eccube.repository.base_info']->get();
106
107
                    // CSVファイルの登録処理
108
                    foreach ($data as $row) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
109
110
                        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
                        if ($row['商品ID'] == '') {
116
                            $Product = new Product();
117
                            $this->em->persist($Product);
118
                        } else {
119
                            if (preg_match('/^\d+$/', $row['商品ID'])) {
120
                                $Product = $app['eccube.repository.product']->find($row['商品ID']);
121
                                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
                                    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
                        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
                            if (preg_match('/^\d+$/', $row['公開ステータス(ID)'])) {
136
                                $Disp = $app['eccube.repository.master.disp']->find($row['公開ステータス(ID)']);
137
                                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
                                    $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 View Code Duplication
                        if (Str::isBlank($row['商品名'])) {
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
                            $Product->setName(Str::trimAll($row['商品名']));
152
                        }
153
154 View Code Duplication
                        if (Str::isNotBlank($row['ショップ用メモ欄'])) {
155
                            $Product->setNote(Str::trimAll($row['ショップ用メモ欄']));
156
                        } else {
157
                            $Product->setNote(null);
158
                        }
159
160 View Code Duplication
                        if (Str::isNotBlank($row['商品説明(一覧)'])) {
161
                            $Product->setDescriptionList(Str::trimAll($row['商品説明(一覧)']));
162
                        } else {
163
                            $Product->setDescriptionList(null);
164
                        }
165
166 View Code Duplication
                        if (Str::isNotBlank($row['商品説明(詳細)'])) {
167
                            $Product->setDescriptionDetail(Str::trimAll($row['商品説明(詳細)']));
168
                        } else {
169
                            $Product->setDescriptionDetail(null);
170
                        }
171
172 View Code Duplication
                        if (Str::isNotBlank($row['検索ワード'])) {
173
                            $Product->setSearchWord(Str::trimAll($row['検索ワード']));
174
                        } else {
175
                            $Product->setSearchWord(null);
176
                        }
177
178 View Code Duplication
                        if (Str::isNotBlank($row['フリーエリア'])) {
179
                            $Product->setFreeArea(Str::trimAll($row['フリーエリア']));
180
                        } else {
181
                            $Product->setFreeArea(null);
182
                        }
183
184
                        if ($row['商品削除フラグ'] == '') {
185
                            $Product->setDelFlg(Constant::DISABLED);
186
                        } else {
187
                            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
                                $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
                        $this->createProductImage($row, $Product, $data);
197
198
                        $this->em->flush($Product);
199
200
                        // 商品カテゴリ登録
201
                        $this->createProductCategory($row, $Product, $app, $data);
202
203
                        //タグ登録
204
                        $this->createProductTag($row, $Product, $app, $data);
205
206
                        // 商品規格が存在しなければ新規登録
207
                        $ProductClasses = $Product->getProductClasses();
208
                        if ($ProductClasses->count() < 1) {
209
                            // 規格分類1(ID)がセットされていると規格なし商品、規格あり商品を作成
210
                            $ProductClassOrg = $this->createProductClass($row, $Product, $app, $data);
211
                            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
                            if ($row['規格分類1(ID)'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
223
224
                                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
                                    $ProductClass = clone $ProductClassOrg;
230
                                    $ProductStock = clone $ProductClassOrg->getProductStock();
231
232
                                    // 規格分類1、規格分類2がnullであるデータの削除フラグを1にセット
233
                                    $ProductClassOrg->setDelFlg(Constant::ENABLED);
234
235
                                    // 規格分類1、2をそれぞれセットし作成
236
                                    $ClassCategory1 = null;
237
                                    if (preg_match('/^\d+$/', $row['規格分類1(ID)'])) {
238
                                        $ClassCategory1 = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']);
239
                                        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
                                            $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
                                    if ($row['規格分類2(ID)'] != '') {
249
                                        if (preg_match('/^\d+$/', $row['規格分類2(ID)'])) {
250
                                            $ClassCategory2 = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']);
251 View Code Duplication
                                            if (!$ClassCategory2) {
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
                                                if ($ClassCategory1 &&
255
                                                    ($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
                                                    $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
                                    $ProductClass->setProductStock($ProductStock);
267
                                    $ProductStock->setProductClass($ProductClass);
268
269
                                    $this->em->persist($ProductClass);
270
                                    $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
                                if ($row['規格分類2(ID)'] != '') {
275
                                    $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
                            $flag = false;
283
                            $classCategoryId1 = $row['規格分類1(ID)'] == '' ? null : $row['規格分類1(ID)'];
284
                            $classCategoryId2 = $row['規格分類2(ID)'] == '' ? null : $row['規格分類2(ID)'];
285
286
                            foreach ($ProductClasses as $pc) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
287
288
                                $classCategory1 = is_null($pc->getClassCategory1()) ? null : $pc->getClassCategory1()->getId();
289
                                $classCategory2 = is_null($pc->getClassCategory2()) ? null : $pc->getClassCategory2()->getId();
290
291
                                // 登録されている商品規格を更新
292
                                if ($classCategory1 == $classCategoryId1 &&
293
                                    $classCategory2 == $classCategoryId2
294
                                ) {
295
                                    $this->updateProductClass($row, $Product, $pc, $app, $data);
296
297
                                    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
                                    $flag = true;
309
                                    break;
310
                                }
311
                            }
312
313
                            // 商品規格を登録
314
                            if (!$flag) {
315
                                $pc = $ProductClasses[0];
316
                                if ($pc->getClassCategory1() == null &&
317
                                    $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
                                    $pc->setDelFlg(Constant::ENABLED);
322
                                }
323
324
                                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
                                    $ClassCategory1 = null;
331
                                    if (preg_match('/^\d+$/', $classCategoryId1)) {
332
                                        $ClassCategory1 = $app['eccube.repository.class_category']->find($classCategoryId1);
333
                                        if (!$ClassCategory1) {
334
                                            $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
                                    $ClassCategory2 = null;
341
                                    if ($row['規格分類2(ID)'] != '') {
342
                                        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
                                            if (preg_match('/^\d+$/', $classCategoryId2)) {
346
                                                $ClassCategory2 = $app['eccube.repository.class_category']->find($classCategoryId2);
347 View Code Duplication
                                                if (!$ClassCategory2) {
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
                                                    if ($ClassCategory1 &&
351
                                                        ($ClassCategory1->getClassName()->getId() == $ClassCategory2->getClassName()->getId())
352
                                                    ) {
353
                                                        $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
                                                $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
                                    $ProductClass = $this->createProductClass($row, $Product, $app, $data, $ClassCategory1, $ClassCategory2);
367
368
                                    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
                                    $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
                        if ($this->hasErrors()) {
388
                            return $this->render($app, $form, $headers, $this->productTwig);
389
                        }
390
391
                        $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
                    $this->em->flush();
396
                    $this->em->getConnection()->commit();
397
398
                    log_info('商品CSV登録完了');
399
400
                    $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
        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
    public function csvCategory(Application $app, Request $request)
413
    {
414
415
        $form = $app['form.factory']->createBuilder('admin_csv_import')->getForm();
416
417
        $headers = $this->getCategoryCsvHeader();
418
419
        if ('POST' === $request->getMethod()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
420
421
            $form->handleRequest($request);
422
423
            if ($form->isValid()) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
424
425
                $formFile = $form['import_file']->getData();
426
427
                if (!empty($formFile)) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
428
429
                    log_info('カテゴリCSV登録開始');
430
431
                    $data = $this->getImportData($app, $formFile);
432 View Code Duplication
                    if ($data === false) {
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
                    $keys = array_keys($headers);
438
                    $columnHeaders = $data->getColumnHeaders();
439
                    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
                    $size = count($data);
445
                    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
                    $headerSize = count($keys);
451
452
                    $this->em = $app['orm.em'];
453
                    $this->em->getConfiguration()->setSQLLogger(null);
454
455
                    $this->em->getConnection()->beginTransaction();
456
457
                    // CSVファイルの登録処理
458
                    foreach ($data as $row) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
459
460
                        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
                        if ($row['カテゴリID'] == '') {
466
                            $Category = new Category();
467
                        } else {
468 View Code Duplication
                            if (!preg_match('/^\d+$/', $row['カテゴリID'])) {
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
                            $Category = $app['eccube.repository.category']->find($row['カテゴリID']);
473 View Code Duplication
                            if (!$Category) {
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 View Code Duplication
                            if ($row['カテゴリID'] == $row['親カテゴリID']) {
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 View Code Duplication
                        if (Str::isBlank($row['カテゴリ名'])) {
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
                            $Category->setName(Str::trimAll($row['カテゴリ名']));
489
                        }
490
491
                        if ($row['親カテゴリID'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
492
493 View Code Duplication
                            if (!preg_match('/^\d+$/', $row['親カテゴリID'])) {
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
                            $ParentCategory = $app['eccube.repository.category']->find($row['親カテゴリID']);
499 View Code Duplication
                            if (!$ParentCategory) {
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
                                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
                            $ParentCategory = null;
506
                        }
507
508
                        $Category->setParent($ParentCategory);
509
                        if ($ParentCategory) {
510
                            $Category->setLevel($ParentCategory->getLevel() + 1);
511
                        } else {
512
                            $Category->setLevel(1);
513
                        }
514
515 View Code Duplication
                        if ($app['config']['category_nest_level'] < $Category->getLevel()) {
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
                        $status = $app['eccube.repository.category']->save($Category);
521
522
                        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
                        if ($this->hasErrors()) {
527
                            return $this->render($app, $form, $headers, $this->categoryTwig);
528
                        }
529
530
                        $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
                    $this->em->flush();
535
                    $this->em->getConnection()->commit();
536
537
                    log_info('カテゴリCSV登録完了');
538
539
                    $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
        return $this->render($app, $form, $headers, $this->categoryTwig);
546
    }
547
548
549
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$type" missing
Loading history...
introduced by
Doc comment for parameter "$app" missing
Loading history...
introduced by
Doc comment for parameter "$request" 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
    protected function render($app, $form, $headers, $twig)
595
    {
596
597
        if ($this->hasErrors()) {
598
            if ($this->em) {
599
                $this->em->getConnection()->rollback();
600
            }
601
        }
602
603
        if (!empty($this->fileName)) {
604
            try {
605
                $fs = new Filesystem();
606
                $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
        return $app->render($twig, array(
613
            'form' => $form->createView(),
614
            'headers' => $headers,
615
            'errors' => $this->errors,
616
        ));
617
    }
618
619
620
    /**
621
     * アップロードされたCSVファイルの行ごとの処理
622
     *
623
     * @param $formFile
624
     * @return CsvImportService
625
     */
626
    protected function getImportData($app, $formFile)
627
    {
628
        // アップロードされたCSVファイルを一時ディレクトリに保存
629
        $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
        $formFile->move($app['config']['csv_temp_realdir'], $this->fileName);
631
632
        $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
        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
            $encode = Str::characterEncoding(substr($file, 0, 6));
644
            if ($encode != 'UTF-8') {
645
                $file = mb_convert_encoding($file, 'UTF-8', $encode);
646
            }
647
        }
648
        $file = Str::convertLineFeed($file);
649
650
        $tmp = tmpfile();
651
        fwrite($tmp, $file);
652
        rewind($tmp);
653
        $meta = stream_get_meta_data($tmp);
654
        $file = new \SplFileObject($meta['uri']);
655
656
        set_time_limit(0);
657
658
        // アップロードされたCSVファイルを行ごとに取得
659
        $data = new CsvImportService($file, $app['config']['csv_import_delimiter'], $app['config']['csv_import_enclosure']);
660
661
        $ret = $data->setHeaderRowNumber(0);
662
663
        return ($ret !== false) ? $data : false;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $ret !== false ? $data : false; of type Eccube\Service\CsvImportService|false adds false to the return on line 663 which is incompatible with the return type documented by Eccube\Controller\Admin\...ntroller::getImportData of type Eccube\Service\CsvImportService. It seems like you forgot to handle an error condition.
Loading history...
664
    }
665
666
667
    /**
668
     * 商品画像の削除、登録
669
     */
670
    protected function createProductImage($row, Product $Product, $data)
671
    {
672
        if ($row['商品画像'] != '') {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
673
674
            // 画像の削除
675
            $ProductImages = $Product->getProductImage();
676
            foreach ($ProductImages as $ProductImage) {
677
                $Product->removeProductImage($ProductImage);
678
                $this->em->remove($ProductImage);
679
            }
680
681
            // 画像の登録
682
            $images = explode(',', $row['商品画像']);
683
            $rank = 1;
684
685
            $pattern = "/\\$|^.*.\.\\\.*|\/$|^.*.\.\/\.*/";
686
            foreach ($images as $image) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
687
688
                $fileName = Str::trimAll($image);
689
690
                // 商品画像名のフォーマットチェック
691
                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
                    if (!empty($fileName)) {
696
                        $ProductImage = new ProductImage();
697
                        $ProductImage->setFileName($fileName);
698
                        $ProductImage->setProduct($Product);
699
                        $ProductImage->setRank($rank);
700
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
701
                        $Product->addProductImage($ProductImage);
702
                        $rank++;
703
                        $this->em->persist($ProductImage);
704
                    }
705
                }
706
            }
707
        }
708
    }
709
710
711
    /**
712
     * 商品カテゴリの削除、登録
713
     */
714
    protected function createProductCategory($row, Product $Product, $app, $data)
715
    {
716
        // カテゴリの削除
717
        $ProductCategories = $Product->getProductCategories();
718
        foreach ($ProductCategories as $ProductCategory) {
719
            $Product->removeProductCategory($ProductCategory);
720
            $this->em->remove($ProductCategory);
721
            $this->em->flush($ProductCategory);
722
        }
723
724
        if ($row['商品カテゴリ(ID)'] == '') {
725
            // 入力されていなければ削除のみ
726
            return;
727
        }
728
729
        // カテゴリの登録
730
        $categories = explode(',', $row['商品カテゴリ(ID)']);
731
        $rank = 1;
732
        $categoriesIdList = array();
733
        foreach ($categories as $category) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
734
735
            if (preg_match('/^\d+$/', $category)) {
736
                $Category = $app['eccube.repository.category']->find($category);
737
                if (!$Category) {
738
                    $this->addErrors(($data->key() + 1).'行目の商品カテゴリ(ID)「'.$category.'」が存在しません。');
739
                } else {
740 View Code Duplication
                    foreach($Category->getPath() as $ParentCategory){
741
                        if (!isset($categoriesIdList[$ParentCategory->getId()])){
742
                            $ProductCategory = $this->makeProductCategory($Product, $ParentCategory, $rank);
743
                            $app['orm.em']->persist($ProductCategory);
744
                            $rank++;
745
                            $Product->addProductCategory($ProductCategory);
746
                            $categoriesIdList[$ParentCategory->getId()] = true;
747
                        }
748
                    }
749
                    if (!isset($categoriesIdList[$Category->getId()])){
750
                        $ProductCategory = $this->makeProductCategory($Product, $Category, $rank);
751
                        $rank++;
752
                        $this->em->persist($ProductCategory);
753
                        $Product->addProductCategory($ProductCategory);
754
                        $categoriesIdList[$Category->getId()] = true;
755
                    }
756
                }
757
            } else {
758
                $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
    protected function createProductTag($row, Product $Product, $app, $data)
774
    {
775
        // タグの削除
776
        $ProductTags = $Product->getProductTag();
777
        foreach ($ProductTags as $ProductTags) {
778
            $Product->removeProductTag($ProductTags);
779
            $this->em->remove($ProductTags);
780
        }
781
782
        if ($row['タグ(ID)'] == '') {
783
            return;
784
        }
785
786
        // タグの登録
787
        $tags = explode(',', $row['タグ(ID)']);
788
        foreach ($tags as $tag_id) {
789
            $Tag = null;
790
            if (preg_match('/^\d+$/', $tag_id)) {
791
                $Tag = $app['eccube.repository.master.tag']->find($tag_id);
792
                if ($Tag) {
793
                    $ProductTags = new ProductTag();
794
                    $ProductTags
795
                        ->setProduct($Product)
796
                        ->setTag($Tag);
797
798
                    $Product->addProductTag($ProductTags);
799
800
                    $this->em->persist($ProductTags);
801
                }
802
            }
803
            if (!$Tag) {
804
                $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
    protected function createProductClass($row, Product $Product, $app, $data, $ClassCategory1 = null, $ClassCategory2 = null)
814
    {
815
        // 規格分類1、規格分類2がnullとなる商品を作成
816
817
        $ProductClass = new ProductClass();
818
        $ProductClass->setProduct($Product);
819
820
821 View Code Duplication
        if ($row['商品種別(ID)'] == '') {
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
            if (preg_match('/^\d+$/', $row['商品種別(ID)'])) {
825
                $ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']);
826
                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
                    $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
        $ProductClass->setClassCategory1($ClassCategory1);
837
        $ProductClass->setClassCategory2($ClassCategory2);
838
839
        if ($row['発送日目安(ID)'] != '') {
840
            if (preg_match('/^\d+$/', $row['発送日目安(ID)'])) {
841
                $DeliveryDate = $app['eccube.repository.delivery_date']->find($row['発送日目安(ID)']);
842
                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
                    $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 View Code Duplication
        if (Str::isNotBlank($row['商品コード'])) {
853
            $ProductClass->setCode(Str::trimAll($row['商品コード']));
854
        } else {
855
            $ProductClass->setCode(null);
856
        }
857
858 View Code Duplication
        if ($row['在庫数無制限フラグ'] == '') {
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
            if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) {
862
                $ProductClass->setStockUnlimited(Constant::DISABLED);
863
                // 在庫数が設定されていなければエラー
864
                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
                    $stock = str_replace(',', '', $row['在庫数']);
868
                    if (preg_match('/^\d+$/', $stock) && $stock >= 0) {
869
                        $ProductClass->setStock($stock);
870
                    } else {
871
                        $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
        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
        if ($row['通常価格'] != '') {
893
            $price01 = str_replace(',', '', $row['通常価格']);
894
            if (preg_match('/^\d+$/', $price01) && $price01 >= 0) {
895
                $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
        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
            $price02 = str_replace(',', '', $row['販売価格']);
905
            if (preg_match('/^\d+$/', $price02) && $price02 >= 0) {
906
                $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
        if ($row['送料'] != '') {
913
            $delivery_fee = str_replace(',', '', $row['送料']);
914
            if (preg_match('/^\d+$/', $delivery_fee) && $delivery_fee >= 0) {
915
                $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 View Code Duplication
        if ($row['商品規格削除フラグ'] == '') {
922
            $ProductClass->setDelFlg(Constant::DISABLED);
923
        } else {
924
            if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) {
925
                $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
        $Product->addProductClass($ProductClass);
932
        $ProductStock = new ProductStock();
933
        $ProductClass->setProductStock($ProductStock);
934
        $ProductStock->setProductClass($ProductClass);
935
936
        if (!$ProductClass->getStockUnlimited()) {
937
            $ProductStock->setStock($ProductClass->getStock());
938
        } else {
939
            // 在庫無制限時はnullを設定
940
            $ProductStock->setStock(null);
941
        }
942
943
        $this->em->persist($ProductClass);
944
        $this->em->persist($ProductStock);
945
946
        return $ProductClass;
947
948
    }
949
950
951
    /**
952
     * 商品規格情報を更新
953
     */
954
    protected function updateProductClass($row, Product $Product, ProductClass $ProductClass, $app, $data)
955
    {
956
957
        $ProductClass->setProduct($Product);
958
959 View Code Duplication
        if ($row['商品種別(ID)'] == '') {
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
            if (preg_match('/^\d+$/', $row['商品種別(ID)'])) {
963
                $ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']);
964
                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
                    $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
        if ($row['規格分類1(ID)'] != '') {
976
            if (preg_match('/^\d+$/', $row['規格分類1(ID)'])) {
977
                $ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']);
978
                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
                    $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
        if ($row['規格分類2(ID)'] != '') {
989
            if (preg_match('/^\d+$/', $row['規格分類2(ID)'])) {
990
                $ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']);
991
                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
                    $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
        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 View Code Duplication
        if (Str::isNotBlank($row['商品コード'])) {
1015
            $ProductClass->setCode(Str::trimAll($row['商品コード']));
1016
        } else {
1017
            $ProductClass->setCode(null);
1018
        }
1019
1020 View Code Duplication
        if ($row['在庫数無制限フラグ'] == '') {
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
            if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) {
1024
                $ProductClass->setStockUnlimited(Constant::DISABLED);
1025
                // 在庫数が設定されていなければエラー
1026
                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
                    $stock = str_replace(',', '', $row['在庫数']);
1030
                    if (preg_match('/^\d+$/', $stock) && $stock >= 0) {
1031
                        $ProductClass->setStock($row['在庫数']);
1032
                    } else {
1033
                        $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
            } else if ($row['在庫数無制限フラグ'] == (string) Constant::ENABLED) {
1038
                $ProductClass->setStockUnlimited(Constant::ENABLED);
1039
                $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
        if ($row['販売制限数'] != '') {
1046
            $saleLimit = str_replace(',', '', $row['販売制限数']);
1047
            if (preg_match('/^\d+$/', $saleLimit) && $saleLimit >= 0) {
1048
                $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
        if ($row['通常価格'] != '') {
1055
            $price01 = str_replace(',', '', $row['通常価格']);
1056
            if (preg_match('/^\d+$/', $price01) && $price01 >= 0) {
1057
                $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
        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
            $price02 = str_replace(',', '', $row['販売価格']);
1067
            if (preg_match('/^\d+$/', $price02) && $price02 >= 0) {
1068
                $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 View Code Duplication
        if ($row['商品規格削除フラグ'] == '') {
1075
            $ProductClass->setDelFlg(Constant::DISABLED);
1076
        } else {
1077
            if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) {
1078
                $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
        $ProductStock = $ProductClass->getProductStock();
1085
1086
        if (!$ProductClass->getStockUnlimited()) {
1087
            $ProductStock->setStock($ProductClass->getStock());
1088
        } else {
1089
            // 在庫無制限時はnullを設定
1090
            $ProductStock->setStock(null);
1091
        }
1092
1093
        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
    protected function getErrors()
1110
    {
1111
        return $this->errors;
1112
    }
1113
1114
    /**
1115
     *
1116
     * @return boolean
1117
     */
1118
    protected function hasErrors()
1119
    {
1120
        return count($this->getErrors()) > 0;
1121
    }
1122
1123
    /**
1124
     * 商品登録CSVヘッダー定義
1125
     */
1126
    private function getProductCsvHeader()
1127
    {
1128
        return array(
1129
            '商品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
    private function getCategoryCsvHeader()
1161
    {
1162
        return array(
1163
            'カテゴリ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 View Code Duplication
    private function makeProductCategory($Product, $Category, $rank)
1176
    {
1177
        $ProductCategory = new ProductCategory();
1178
        $ProductCategory->setProduct($Product);
1179
        $ProductCategory->setProductId($Product->getId());
1180
        $ProductCategory->setCategory($Category);
1181
        $ProductCategory->setCategoryId($Category->getId());
1182
        $ProductCategory->setRank($rank);
1183
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
1184
        return $ProductCategory;
1185
    }
1186
}
1187