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 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 |
||
44 | class CsvImportController |
||
|
|||
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 | /** |
||
59 | * 商品登録CSVアップロード |
||
60 | */ |
||
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()) { |
|
68 | |||
69 | 3 | $form->handleRequest($request); |
|
70 | |||
71 | 3 | if ($form->isValid()) { |
|
72 | |||
73 | 3 | $formFile = $form['import_file']->getData(); |
|
74 | |||
75 | 3 | if (!empty($formFile)) { |
|
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); |
||
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); |
||
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); |
||
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) { |
|
109 | |||
110 | 3 | if ($headerSize != count($row)) { |
|
111 | $this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。'); |
||
112 | return $this->render($app, $form, $headers, $this->productTwig); |
||
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が存在しません。'); |
||
123 | 2 | return $this->render($app, $form, $headers, $this->productTwig); |
|
124 | } |
||
125 | View Code Duplication | } else { |
|
126 | $this->addErrors(($data->key() + 1) . '行目の商品IDが存在しません。'); |
||
127 | return $this->render($app, $form, $headers, $this->productTwig); |
||
128 | } |
||
129 | |||
130 | } |
||
131 | |||
132 | 3 | if ($row['公開ステータス(ID)'] == '') { |
|
133 | $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が設定されていません。'); |
||
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)が存在しません。'); |
||
139 | } else { |
||
140 | 3 | $Product->setStatus($Disp); |
|
141 | } |
||
142 | } else { |
||
143 | $this->addErrors(($data->key() + 1) . '行目の公開ステータス(ID)が存在しません。'); |
||
144 | } |
||
145 | } |
||
146 | |||
147 | 3 | View Code Duplication | if (Str::isBlank($row['商品名'])) { |
148 | $this->addErrors(($data->key() + 1) . '行目の商品名が設定されていません。'); |
||
149 | return $this->render($app, $form, $headers, $this->productTwig); |
||
150 | } else { |
||
151 | 3 | $Product->setName(Str::trimAll($row['商品名'])); |
|
152 | } |
||
153 | |||
154 | 3 | View Code Duplication | if (Str::isNotBlank($row['ショップ用メモ欄'])) { |
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['商品説明(一覧)'])) { |
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['商品説明(詳細)'])) { |
167 | 3 | $Product->setDescriptionDetail(Str::trimAll($row['商品説明(詳細)'])); |
|
168 | } else { |
||
169 | $Product->setDescriptionDetail(null); |
||
170 | } |
||
171 | |||
172 | 3 | View Code Duplication | if (Str::isNotBlank($row['検索ワード'])) { |
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['フリーエリア'])) { |
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) { |
|
188 | 3 | $Product->setDelFlg($row['商品削除フラグ']); |
|
189 | View Code Duplication | } else { |
|
190 | $this->addErrors(($data->key() + 1) . '行目の商品削除フラグが設定されていません。'); |
||
191 | return $this->render($app, $form, $headers, $this->productTwig); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | // 商品画像登録 |
||
196 | 3 | $this->createProductImage($row, $Product); |
|
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以上の数値を設定してください。'); |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | |||
222 | 2 | if ($row['規格分類1(ID)'] != '') { |
|
223 | |||
224 | 1 | if ($row['規格分類1(ID)'] == $row['規格分類2(ID)']) { |
|
225 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)と規格分類2(ID)には同じ値を使用できません。'); |
||
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)が存在しません。'); |
||
241 | } else { |
||
242 | 1 | $ProductClass->setClassCategory1($ClassCategory1); |
|
243 | } |
||
244 | } else { |
||
245 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
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) { |
252 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
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)の規格名が同じです。'); |
||
258 | } else { |
||
259 | 1 | $ProductClass->setClassCategory2($ClassCategory2); |
|
260 | } |
||
261 | } |
||
262 | } else { |
||
263 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
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 | |||
273 | } else { |
||
274 | 1 | if ($row['規格分類2(ID)'] != '') { |
|
275 | 2 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
|
276 | } |
||
277 | } |
||
278 | |||
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) { |
|
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以上の数値を設定してください。'); |
||
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 | ) { |
||
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)には同じ値を使用できません。'); |
||
326 | } else { |
||
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)が存在しません。'); |
|
335 | } |
||
336 | } else { |
||
337 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
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)は設定できません。'); |
||
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) { |
348 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
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)の規格名が同じです。'); |
|
354 | } |
||
355 | } |
||
356 | } else { |
||
357 | 1 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
|
358 | } |
||
359 | |||
360 | } |
||
361 | } else { |
||
362 | if ($pc->getClassCategory1() != null && $pc->getClassCategory2() != null) { |
||
363 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)に値を設定してください。'); |
||
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以上の数値を設定してください。'); |
||
375 | } |
||
376 | } |
||
377 | } |
||
378 | |||
379 | 1 | $Product->addProductClass($ProductClass); |
|
380 | } |
||
381 | |||
382 | } |
||
383 | |||
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 | |||
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 | |||
403 | } |
||
404 | } |
||
405 | |||
406 | 3 | return $this->render($app, $form, $headers, $this->productTwig); |
|
407 | } |
||
408 | |||
409 | /** |
||
410 | * カテゴリ登録CSVアップロード |
||
411 | */ |
||
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()) { |
|
420 | |||
421 | 2 | $form->handleRequest($request); |
|
422 | |||
423 | 2 | if ($form->isValid()) { |
|
424 | |||
425 | 2 | $formFile = $form['import_file']->getData(); |
|
426 | |||
427 | 2 | if (!empty($formFile)) { |
|
428 | |||
429 | 2 | log_info('カテゴリCSV登録開始'); |
|
430 | |||
431 | 2 | $data = $this->getImportData($app, $formFile); |
|
432 | 2 | View Code Duplication | if ($data === false) { |
433 | $this->addErrors('CSVのフォーマットが一致しません。'); |
||
434 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
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); |
||
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); |
||
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) { |
|
459 | |||
460 | 2 | if ($headerSize != count($row)) { |
|
461 | $this->addErrors(($data->key() + 1) . '行目のCSVフォーマットが一致しません。'); |
||
462 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
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'])) { |
469 | $this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。'); |
||
470 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
471 | } |
||
472 | 1 | $Category = $app['eccube.repository.category']->find($row['カテゴリID']); |
|
473 | 1 | View Code Duplication | if (!$Category) { |
474 | $this->addErrors(($data->key() + 1) . '行目のカテゴリIDが存在しません。'); |
||
475 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
476 | } |
||
477 | 1 | View Code Duplication | if ($row['カテゴリID'] == $row['親カテゴリID']) { |
478 | $this->addErrors(($data->key() + 1) . '行目のカテゴリIDと親カテゴリIDが同じです。'); |
||
479 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
480 | } |
||
481 | |||
482 | } |
||
483 | |||
484 | 2 | View Code Duplication | if (Str::isBlank($row['カテゴリ名'])) { |
485 | $this->addErrors(($data->key() + 1) . '行目のカテゴリ名が設定されていません。'); |
||
486 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
487 | } else { |
||
488 | 2 | $Category->setName(Str::trimAll($row['カテゴリ名'])); |
|
489 | } |
||
490 | |||
491 | 2 | if ($row['親カテゴリID'] != '') { |
|
492 | |||
493 | 1 | View Code Duplication | if (!preg_match('/^\d+$/', $row['親カテゴリID'])) { |
494 | $this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。'); |
||
495 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
496 | } |
||
497 | |||
498 | 1 | $ParentCategory = $app['eccube.repository.category']->find($row['親カテゴリID']); |
|
499 | 1 | View Code Duplication | if (!$ParentCategory) { |
500 | $this->addErrors(($data->key() + 1) . '行目の親カテゴリIDが存在しません。'); |
||
501 | 1 | return $this->render($app, $form, $headers, $this->categoryTwig); |
|
502 | } |
||
503 | |||
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()) { |
516 | $this->addErrors(($data->key() + 1) . '行目のカテゴリが最大レベルを超えているため設定できません。'); |
||
517 | return $this->render($app, $form, $headers, $this->categoryTwig); |
||
518 | } |
||
519 | |||
520 | 2 | $status = $app['eccube.repository.category']->save($Category); |
|
521 | |||
522 | 2 | if (!$status) { |
|
523 | $this->addErrors(($data->key() + 1) . '行目のカテゴリが設定できません。'); |
||
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 | |||
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 | |||
542 | } |
||
543 | } |
||
544 | |||
545 | 2 | return $this->render($app, $form, $headers, $this->categoryTwig); |
|
546 | } |
||
547 | |||
548 | |||
549 | /** |
||
550 | * アップロード用CSV雛形ファイルダウンロード |
||
551 | */ |
||
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); |
||
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); |
|
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(); |
|
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); |
|
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) |
|
671 | { |
||
672 | 3 | if ($row['商品画像'] != '') { |
|
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 | 3 | foreach ($images as $image) { |
|
685 | |||
686 | 3 | $ProductImage = new ProductImage(); |
|
687 | 3 | $ProductImage->setFileName(Str::trimAll($image)); |
|
688 | 3 | $ProductImage->setProduct($Product); |
|
689 | 3 | $ProductImage->setRank($rank); |
|
690 | |||
691 | 3 | $Product->addProductImage($ProductImage); |
|
692 | 3 | $rank++; |
|
693 | 3 | $this->em->persist($ProductImage); |
|
694 | } |
||
695 | } |
||
696 | } |
||
697 | |||
698 | |||
699 | /** |
||
700 | * 商品カテゴリの削除、登録 |
||
701 | */ |
||
702 | 3 | protected function createProductCategory($row, Product $Product, $app, $data) |
|
703 | { |
||
704 | // カテゴリの削除 |
||
705 | 3 | $ProductCategories = $Product->getProductCategories(); |
|
706 | 3 | foreach ($ProductCategories as $ProductCategory) { |
|
707 | 2 | $Product->removeProductCategory($ProductCategory); |
|
708 | 2 | $this->em->remove($ProductCategory); |
|
709 | 3 | $this->em->flush($ProductCategory); |
|
710 | } |
||
711 | |||
712 | 3 | if ($row['商品カテゴリ(ID)'] == '') { |
|
713 | // 入力されていなければ削除のみ |
||
714 | return; |
||
715 | } |
||
716 | |||
717 | // カテゴリの登録 |
||
718 | 3 | $categories = explode(',', $row['商品カテゴリ(ID)']); |
|
719 | 3 | $rank = 1; |
|
720 | 3 | foreach ($categories as $category) { |
|
721 | |||
722 | 3 | if (preg_match('/^\d+$/', $category)) { |
|
723 | 3 | $Category = $app['eccube.repository.category']->find($category); |
|
724 | 3 | if (!$Category) { |
|
725 | $this->addErrors(($data->key() + 1).'行目の商品カテゴリ(ID)「'.$category.'」が存在しません。'); |
||
726 | } else { |
||
727 | 3 | $ProductCategory = new ProductCategory(); |
|
728 | 3 | $ProductCategory->setProductId($Product->getId()); |
|
729 | 3 | $ProductCategory->setCategoryId($Category->getId()); |
|
730 | 3 | $ProductCategory->setProduct($Product); |
|
731 | 3 | $ProductCategory->setCategory($Category); |
|
732 | 3 | $ProductCategory->setRank($rank); |
|
733 | 3 | $Product->addProductCategory($ProductCategory); |
|
734 | 3 | $rank++; |
|
735 | 3 | $this->em->persist($ProductCategory); |
|
736 | } |
||
737 | } else { |
||
738 | 3 | $this->addErrors(($data->key() + 1).'行目の商品カテゴリ(ID)「'.$category.'」が存在しません。'); |
|
739 | } |
||
740 | } |
||
741 | |||
742 | } |
||
743 | |||
744 | |||
745 | /** |
||
746 | * タグの登録 |
||
747 | * |
||
748 | * @param array $row |
||
749 | * @param Product $Product |
||
750 | * @param Application $app |
||
751 | * @param CsvImportService $data |
||
752 | */ |
||
753 | 3 | protected function createProductTag($row, Product $Product, $app, $data) |
|
754 | { |
||
755 | // タグの削除 |
||
756 | 3 | $ProductTags = $Product->getProductTag(); |
|
757 | 3 | foreach ($ProductTags as $ProductTags) { |
|
758 | 1 | $Product->removeProductTag($ProductTags); |
|
759 | 3 | $this->em->remove($ProductTags); |
|
760 | } |
||
761 | |||
762 | 3 | if ($row['タグ(ID)'] == '') { |
|
763 | return; |
||
764 | } |
||
765 | |||
766 | // タグの登録 |
||
767 | 3 | $tags = explode(',', $row['タグ(ID)']); |
|
768 | 3 | foreach ($tags as $tag_id) { |
|
769 | 3 | $Tag = null; |
|
770 | 3 | if (preg_match('/^\d+$/', $tag_id)) { |
|
771 | 3 | $Tag = $app['eccube.repository.master.tag']->find($tag_id); |
|
772 | 3 | if ($Tag) { |
|
773 | 3 | $ProductTags = new ProductTag(); |
|
774 | $ProductTags |
||
775 | 3 | ->setProduct($Product) |
|
776 | 3 | ->setTag($Tag); |
|
777 | |||
778 | 3 | $Product->addProductTag($ProductTags); |
|
779 | |||
780 | 3 | $this->em->persist($ProductTags); |
|
781 | } |
||
782 | } |
||
783 | 3 | if (!$Tag) { |
|
784 | 3 | $this->addErrors(($data->key() + 1) . '行目のタグ(ID)「' . $tag_id . '」が存在しません。'); |
|
785 | } |
||
786 | } |
||
787 | } |
||
788 | |||
789 | |||
790 | /** |
||
791 | * 商品規格分類1、商品規格分類2がnullとなる商品規格情報を作成 |
||
792 | */ |
||
793 | 3 | protected function createProductClass($row, Product $Product, $app, $data, $ClassCategory1 = null, $ClassCategory2 = null) |
|
794 | { |
||
795 | // 規格分類1、規格分類2がnullとなる商品を作成 |
||
796 | |||
797 | 3 | $ProductClass = new ProductClass(); |
|
798 | 3 | $ProductClass->setProduct($Product); |
|
799 | |||
800 | |||
801 | 3 | View Code Duplication | if ($row['商品種別(ID)'] == '') { |
802 | $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が設定されていません。'); |
||
803 | } else { |
||
804 | 3 | if (preg_match('/^\d+$/', $row['商品種別(ID)'])) { |
|
805 | 3 | $ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']); |
|
806 | 3 | if (!$ProductType) { |
|
807 | $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。'); |
||
808 | } else { |
||
809 | 3 | $ProductClass->setProductType($ProductType); |
|
810 | } |
||
811 | } else { |
||
812 | $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。'); |
||
813 | } |
||
814 | } |
||
815 | |||
816 | 3 | $ProductClass->setClassCategory1($ClassCategory1); |
|
817 | 3 | $ProductClass->setClassCategory2($ClassCategory2); |
|
818 | |||
819 | 3 | if ($row['発送日目安(ID)'] != '') { |
|
820 | 2 | if (preg_match('/^\d+$/', $row['発送日目安(ID)'])) { |
|
821 | 2 | $DeliveryDate = $app['eccube.repository.delivery_date']->find($row['発送日目安(ID)']); |
|
822 | 2 | if (!$DeliveryDate) { |
|
823 | $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。'); |
||
824 | } else { |
||
825 | 2 | $ProductClass->setDeliveryDate($DeliveryDate); |
|
826 | } |
||
827 | } else { |
||
828 | $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。'); |
||
829 | } |
||
830 | } |
||
831 | |||
832 | 3 | View Code Duplication | if (Str::isNotBlank($row['商品コード'])) { |
833 | 2 | $ProductClass->setCode(Str::trimAll($row['商品コード'])); |
|
834 | } else { |
||
835 | 1 | $ProductClass->setCode(null); |
|
836 | } |
||
837 | |||
838 | 3 | View Code Duplication | if ($row['在庫数無制限フラグ'] == '') { |
839 | $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。'); |
||
840 | } else { |
||
841 | 3 | if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) { |
|
842 | 3 | $ProductClass->setStockUnlimited(Constant::DISABLED); |
|
843 | // 在庫数が設定されていなければエラー |
||
844 | 3 | if ($row['在庫数'] == '') { |
|
845 | $this->addErrors(($data->key() + 1) . '行目の在庫数が設定されていません。'); |
||
846 | } else { |
||
847 | 3 | $stock = str_replace(',', '', $row['在庫数']); |
|
848 | 3 | if (preg_match('/^\d+$/', $stock) && $stock >= 0) { |
|
849 | 3 | $ProductClass->setStock($stock); |
|
850 | } else { |
||
851 | 3 | $this->addErrors(($data->key() + 1) . '行目の在庫数は0以上の数値を設定してください。'); |
|
852 | } |
||
853 | } |
||
854 | |||
855 | } else if ($row['在庫数無制限フラグ'] == (string) Constant::ENABLED) { |
||
856 | $ProductClass->setStockUnlimited(Constant::ENABLED); |
||
857 | $ProductClass->setStock(null); |
||
858 | } else { |
||
859 | $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。'); |
||
860 | } |
||
861 | } |
||
862 | |||
863 | 3 | if ($row['販売制限数'] != '') { |
|
864 | $saleLimit = str_replace(',', '', $row['販売制限数']); |
||
865 | if (preg_match('/^\d+$/', $saleLimit) && $saleLimit >= 0) { |
||
866 | $ProductClass->setSaleLimit($saleLimit); |
||
867 | } else { |
||
868 | $this->addErrors(($data->key() + 1) . '行目の販売制限数は0以上の数値を設定してください。'); |
||
869 | } |
||
870 | } |
||
871 | |||
872 | 3 | if ($row['通常価格'] != '') { |
|
873 | 2 | $price01 = str_replace(',', '', $row['通常価格']); |
|
874 | 2 | if (preg_match('/^\d+$/', $price01) && $price01 >= 0) { |
|
875 | 2 | $ProductClass->setPrice01($price01); |
|
876 | } else { |
||
877 | $this->addErrors(($data->key() + 1) . '行目の通常価格は0以上の数値を設定してください。'); |
||
878 | } |
||
879 | } |
||
880 | |||
881 | 3 | if ($row['販売価格'] == '') { |
|
882 | $this->addErrors(($data->key() + 1) . '行目の販売価格が設定されていません。'); |
||
883 | } else { |
||
884 | 3 | $price02 = str_replace(',', '', $row['販売価格']); |
|
885 | 3 | if (preg_match('/^\d+$/', $price02) && $price02 >= 0) { |
|
886 | 3 | $ProductClass->setPrice02($price02); |
|
887 | } else { |
||
888 | $this->addErrors(($data->key() + 1) . '行目の販売価格は0以上の数値を設定してください。'); |
||
889 | } |
||
890 | } |
||
891 | |||
892 | 3 | if ($row['送料'] != '') { |
|
893 | 2 | $delivery_fee = str_replace(',', '', $row['送料']); |
|
894 | 2 | if (preg_match('/^\d+$/', $delivery_fee) && $delivery_fee >= 0) { |
|
895 | 2 | $ProductClass->setDeliveryFee($delivery_fee); |
|
896 | } else { |
||
897 | $this->addErrors(($data->key() + 1) . '行目の送料は0以上の数値を設定してください。'); |
||
898 | } |
||
899 | } |
||
900 | |||
901 | 3 | View Code Duplication | if ($row['商品規格削除フラグ'] == '') { |
902 | $ProductClass->setDelFlg(Constant::DISABLED); |
||
903 | } else { |
||
904 | 3 | if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) { |
|
905 | 3 | $ProductClass->setDelFlg($row['商品規格削除フラグ']); |
|
906 | } else { |
||
907 | $this->addErrors(($data->key() + 1) . '行目の商品規格削除フラグが設定されていません。'); |
||
908 | } |
||
909 | } |
||
910 | |||
911 | 3 | $Product->addProductClass($ProductClass); |
|
912 | 3 | $ProductStock = new ProductStock(); |
|
913 | 3 | $ProductClass->setProductStock($ProductStock); |
|
914 | 3 | $ProductStock->setProductClass($ProductClass); |
|
915 | |||
916 | 3 | if (!$ProductClass->getStockUnlimited()) { |
|
917 | 3 | $ProductStock->setStock($ProductClass->getStock()); |
|
918 | } else { |
||
919 | // 在庫無制限時はnullを設定 |
||
920 | $ProductStock->setStock(null); |
||
921 | } |
||
922 | |||
923 | 3 | $this->em->persist($ProductClass); |
|
924 | 3 | $this->em->persist($ProductStock); |
|
925 | |||
926 | 3 | return $ProductClass; |
|
927 | |||
928 | } |
||
929 | |||
930 | |||
931 | /** |
||
932 | * 商品規格情報を更新 |
||
933 | */ |
||
934 | 1 | protected function updateProductClass($row, Product $Product, ProductClass $ProductClass, $app, $data) |
|
935 | { |
||
936 | |||
937 | 1 | $ProductClass->setProduct($Product); |
|
938 | |||
939 | 1 | View Code Duplication | if ($row['商品種別(ID)'] == '') { |
940 | $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が設定されていません。'); |
||
941 | } else { |
||
942 | 1 | if (preg_match('/^\d+$/', $row['商品種別(ID)'])) { |
|
943 | 1 | $ProductType = $app['eccube.repository.master.product_type']->find($row['商品種別(ID)']); |
|
944 | 1 | if (!$ProductType) { |
|
945 | $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。'); |
||
946 | } else { |
||
947 | 1 | $ProductClass->setProductType($ProductType); |
|
948 | } |
||
949 | } else { |
||
950 | $this->addErrors(($data->key() + 1) . '行目の商品種別(ID)が存在しません。'); |
||
951 | } |
||
952 | } |
||
953 | |||
954 | // 規格分類1、2をそれぞれセットし作成 |
||
955 | 1 | if ($row['規格分類1(ID)'] != '') { |
|
956 | 1 | if (preg_match('/^\d+$/', $row['規格分類1(ID)'])) { |
|
957 | 1 | $ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類1(ID)']); |
|
958 | 1 | if (!$ClassCategory) { |
|
959 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
960 | } else { |
||
961 | 1 | $ProductClass->setClassCategory1($ClassCategory); |
|
962 | } |
||
963 | } else { |
||
964 | $this->addErrors(($data->key() + 1) . '行目の規格分類1(ID)が存在しません。'); |
||
965 | } |
||
966 | } |
||
967 | |||
968 | 1 | if ($row['規格分類2(ID)'] != '') { |
|
969 | 1 | if (preg_match('/^\d+$/', $row['規格分類2(ID)'])) { |
|
970 | 1 | $ClassCategory = $app['eccube.repository.class_category']->find($row['規格分類2(ID)']); |
|
971 | 1 | if (!$ClassCategory) { |
|
972 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
973 | } else { |
||
974 | 1 | $ProductClass->setClassCategory2($ClassCategory); |
|
975 | } |
||
976 | } else { |
||
977 | $this->addErrors(($data->key() + 1) . '行目の規格分類2(ID)が存在しません。'); |
||
978 | } |
||
979 | } |
||
980 | |||
981 | 1 | if ($row['発送日目安(ID)'] != '') { |
|
982 | if (preg_match('/^\d+$/', $row['発送日目安(ID)'])) { |
||
983 | $DeliveryDate = $app['eccube.repository.delivery_date']->find($row['発送日目安(ID)']); |
||
984 | if (!$DeliveryDate) { |
||
985 | $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。'); |
||
986 | } else { |
||
987 | $ProductClass->setDeliveryDate($DeliveryDate); |
||
988 | } |
||
989 | } else { |
||
990 | $this->addErrors(($data->key() + 1) . '行目の発送日目安(ID)が存在しません。'); |
||
991 | } |
||
992 | } |
||
993 | |||
994 | 1 | View Code Duplication | if (Str::isNotBlank($row['商品コード'])) { |
995 | 1 | $ProductClass->setCode(Str::trimAll($row['商品コード'])); |
|
996 | } else { |
||
997 | $ProductClass->setCode(null); |
||
998 | } |
||
999 | |||
1000 | 1 | View Code Duplication | if ($row['在庫数無制限フラグ'] == '') { |
1001 | $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。'); |
||
1002 | } else { |
||
1003 | 1 | if ($row['在庫数無制限フラグ'] == (string) Constant::DISABLED) { |
|
1004 | 1 | $ProductClass->setStockUnlimited(Constant::DISABLED); |
|
1005 | // 在庫数が設定されていなければエラー |
||
1006 | 1 | if ($row['在庫数'] == '') { |
|
1007 | $this->addErrors(($data->key() + 1) . '行目の在庫数が設定されていません。'); |
||
1008 | } else { |
||
1009 | 1 | $stock = str_replace(',', '', $row['在庫数']); |
|
1010 | 1 | if (preg_match('/^\d+$/', $stock) && $stock >= 0) { |
|
1011 | 1 | $ProductClass->setStock($row['在庫数']); |
|
1012 | } else { |
||
1013 | 1 | $this->addErrors(($data->key() + 1) . '行目の在庫数は0以上の数値を設定してください。'); |
|
1014 | } |
||
1015 | } |
||
1016 | |||
1017 | 1 | } else if ($row['在庫数無制限フラグ'] == (string) Constant::ENABLED) { |
|
1018 | 1 | $ProductClass->setStockUnlimited(Constant::ENABLED); |
|
1019 | 1 | $ProductClass->setStock(null); |
|
1020 | } else { |
||
1021 | $this->addErrors(($data->key() + 1) . '行目の在庫数無制限フラグが設定されていません。'); |
||
1022 | } |
||
1023 | } |
||
1024 | |||
1025 | 1 | if ($row['販売制限数'] != '') { |
|
1026 | 1 | $saleLimit = str_replace(',', '', $row['販売制限数']); |
|
1027 | 1 | if (preg_match('/^\d+$/', $saleLimit) && $saleLimit >= 0) { |
|
1028 | 1 | $ProductClass->setSaleLimit($saleLimit); |
|
1029 | } else { |
||
1030 | $this->addErrors(($data->key() + 1) . '行目の販売制限数は0以上の数値を設定してください。'); |
||
1031 | } |
||
1032 | } |
||
1033 | |||
1034 | 1 | if ($row['通常価格'] != '') { |
|
1035 | 1 | $price01 = str_replace(',', '', $row['通常価格']); |
|
1036 | 1 | if (preg_match('/^\d+$/', $price01) && $price01 >= 0) { |
|
1037 | 1 | $ProductClass->setPrice01($price01); |
|
1038 | } else { |
||
1039 | $this->addErrors(($data->key() + 1) . '行目の通常価格は0以上の数値を設定してください。'); |
||
1040 | } |
||
1041 | } |
||
1042 | |||
1043 | 1 | if ($row['販売価格'] == '') { |
|
1044 | $this->addErrors(($data->key() + 1) . '行目の販売価格が設定されていません。'); |
||
1045 | } else { |
||
1046 | 1 | $price02 = str_replace(',', '', $row['販売価格']); |
|
1047 | 1 | if (preg_match('/^\d+$/', $price02) && $price02 >= 0) { |
|
1048 | 1 | $ProductClass->setPrice02($price02); |
|
1049 | } else { |
||
1050 | $this->addErrors(($data->key() + 1) . '行目の販売価格は0以上の数値を設定してください。'); |
||
1051 | } |
||
1052 | } |
||
1053 | |||
1054 | 1 | View Code Duplication | if ($row['商品規格削除フラグ'] == '') { |
1055 | $ProductClass->setDelFlg(Constant::DISABLED); |
||
1056 | } else { |
||
1057 | 1 | if ($row['商品規格削除フラグ'] == (string) Constant::DISABLED || $row['商品規格削除フラグ'] == (string) Constant::ENABLED) { |
|
1058 | 1 | $ProductClass->setDelFlg($row['商品規格削除フラグ']); |
|
1059 | } else { |
||
1060 | $this->addErrors(($data->key() + 1) . '行目の商品規格削除フラグが設定されていません。'); |
||
1061 | } |
||
1062 | } |
||
1063 | |||
1064 | 1 | $ProductStock = $ProductClass->getProductStock(); |
|
1065 | |||
1066 | 1 | if (!$ProductClass->getStockUnlimited()) { |
|
1067 | 1 | $ProductStock->setStock($ProductClass->getStock()); |
|
1068 | } else { |
||
1069 | // 在庫無制限時はnullを設定 |
||
1070 | 1 | $ProductStock->setStock(null); |
|
1071 | } |
||
1072 | |||
1073 | 1 | return $ProductClass; |
|
1074 | } |
||
1075 | |||
1076 | /** |
||
1077 | * 登録、更新時のエラー画面表示 |
||
1078 | * |
||
1079 | */ |
||
1080 | protected function addErrors($message) |
||
1081 | { |
||
1082 | $e = new CsvImportException($message); |
||
1083 | $this->errors[] = $e; |
||
1084 | } |
||
1085 | |||
1086 | /** |
||
1087 | * @return array |
||
1088 | */ |
||
1089 | 5 | protected function getErrors() |
|
1093 | |||
1094 | /** |
||
1095 | * |
||
1096 | * @return boolean |
||
1097 | */ |
||
1098 | 5 | protected function hasErrors() |
|
1102 | |||
1103 | /** |
||
1104 | * 商品登録CSVヘッダー定義 |
||
1105 | */ |
||
1106 | 3 | private function getProductCsvHeader() |
|
1107 | { |
||
1108 | return array( |
||
1109 | 3 | '商品ID' => 'id', |
|
1110 | '公開ステータス(ID)' => 'status', |
||
1111 | '商品名' => 'name', |
||
1112 | 'ショップ用メモ欄' => 'note', |
||
1113 | '商品説明(一覧)' => 'description_list', |
||
1135 | |||
1136 | |||
1137 | /** |
||
1138 | * カテゴリCSVヘッダー定義 |
||
1139 | */ |
||
1140 | 2 | private function getCategoryCsvHeader() |
|
1148 | } |
||
1149 |