public function csvCategory(Application $app, Request $request)
{
$form = $app['form.factory']->createBuilder('admin_csv_import')->getForm();
$headers = $this->getCategoryCsvHeader();
if ('POST' === $request->getMethod()) {
$form->handleRequest($request);
if ($form->isValid()) {
$formFile = $form['import_file']->getData();
if (!empty($formFile)) {
log_info('カテゴリCSV登録開始');
$data = $this->getImportData($app, $formFile);
if ($data === false) {
$this->addErrors('CSVのフォーマットが一致しません。');
return $this->render($app, $form, $headers, $this->categoryTwig);
}
$keys = array_keys($headers);
$columnHeaders = $data->getColumnHeaders();
if ($keys !== $columnHeaders) {
$this->addErrors('CSVのフォーマットが一致しません。');
return $this->render($app, $form, $headers, $this->categoryTwig);
}
$size = count($data);
if ($size < 1) {
$this->addErrors('CSVデータが存在しません。');
return $this->render($app, $form, $headers, $this->categoryTwig);
}
$headerSize = count($keys);
$this->em = $app['orm.em'];
$this->em->getConfiguration()->setSQLLogger(null);
$this->em->getConnection()->beginTransaction();
// CSVファイルの登録処理
foreach ($data as $row) {
if ($headerSize != count($row)) {
$this->addErrors($data->key() + 1 . '行目のCSVフォーマットが一致しません。');
return $this->render($app, $form, $headers, $this->categoryTwig);
}
if ($row['カテゴリID'] == '') {
$Category = new Category();
} else {
if (!preg_match('/^\\d+$/', $row['カテゴリID'])) {
$this->addErrors($data->key() + 1 . '行目のカテゴリIDが存在しません。');
return $this->render($app, $form, $headers, $this->categoryTwig);
}
$Category = $app['eccube.repository.category']->find($row['カテゴリID']);
if (!$Category) {
$this->addErrors($data->key() + 1 . '行目のカテゴリIDが存在しません。');
return $this->render($app, $form, $headers, $this->categoryTwig);
}
if ($row['カテゴリID'] == $row['親カテゴリID']) {
$this->addErrors($data->key() + 1 . '行目のカテゴリIDと親カテゴリIDが同じです。');
return $this->render($app, $form, $headers, $this->categoryTwig);
}
}
if (Str::isBlank($row['カテゴリ名'])) {
$this->addErrors($data->key() + 1 . '行目のカテゴリ名が設定されていません。');
return $this->render($app, $form, $headers, $this->categoryTwig);
} else {
$Category->setName(Str::trimAll($row['カテゴリ名']));
}
if ($row['親カテゴリID'] != '') {
if (!preg_match('/^\\d+$/', $row['親カテゴリID'])) {
$this->addErrors($data->key() + 1 . '行目の親カテゴリIDが存在しません。');
return $this->render($app, $form, $headers, $this->categoryTwig);
}
$ParentCategory = $app['eccube.repository.category']->find($row['親カテゴリID']);
if (!$ParentCategory) {
$this->addErrors($data->key() + 1 . '行目の親カテゴリIDが存在しません。');
return $this->render($app, $form, $headers, $this->categoryTwig);
}
} else {
$ParentCategory = null;
}
$Category->setParent($ParentCategory);
if ($ParentCategory) {
$Category->setLevel($ParentCategory->getLevel() + 1);
} else {
$Category->setLevel(1);
}
if ($app['config']['category_nest_level'] < $Category->getLevel()) {
$this->addErrors($data->key() + 1 . '行目のカテゴリが最大レベルを超えているため設定できません。');
return $this->render($app, $form, $headers, $this->categoryTwig);
}
$status = $app['eccube.repository.category']->save($Category);
if (!$status) {
$this->addErrors($data->key() + 1 . '行目のカテゴリが設定できません。');
}
if ($this->hasErrors()) {
return $this->render($app, $form, $headers, $this->categoryTwig);
}
$this->em->persist($Category);
}
$this->em->flush();
$this->em->getConnection()->commit();
log_info('カテゴリCSV登録完了');
$app->addSuccess('admin.category.csv_import.save.complete', 'admin');
}
}
}
return $this->render($app, $form, $headers, $this->categoryTwig);
}