public function index(Application $app, Request $request, $parent_id = null, $id = null)
{
if ($parent_id) {
$Parent = $app['eccube.repository.category']->find($parent_id);
if (!$Parent) {
throw new NotFoundHttpException('親カテゴリが存在しません');
}
} else {
$Parent = null;
}
if ($id) {
$TargetCategory = $app['eccube.repository.category']->find($id);
if (!$TargetCategory) {
throw new NotFoundHttpException('カテゴリが存在しません');
}
$Parent = $TargetCategory->getParent();
} else {
$TargetCategory = new \Eccube\Entity\Category();
$TargetCategory->setParent($Parent);
if ($Parent) {
$TargetCategory->setLevel($Parent->getLevel() + 1);
} else {
$TargetCategory->setLevel(1);
}
}
//
$builder = $app['form.factory']->createBuilder('admin_category', $TargetCategory);
$event = new EventArgs(array('builder' => $builder, 'Parent' => $Parent, 'TargetCategory' => $TargetCategory), $request);
$app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_CATEGORY_INDEX_INITIALIZE, $event);
$form = $builder->getForm();
//
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
if ($app['config']['category_nest_level'] < $TargetCategory->getLevel()) {
throw new BadRequestHttpException('リクエストが不正です');
}
log_info('カテゴリ登録開始', array($id));
$status = $app['eccube.repository.category']->save($TargetCategory);
if ($status) {
log_info('カテゴリ登録完了', array($id));
$event = new EventArgs(array('form' => $form, 'Parent' => $Parent, 'TargetCategory' => $TargetCategory), $request);
$app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_CATEGORY_INDEX_COMPLETE, $event);
$app->addSuccess('admin.category.save.complete', 'admin');
if ($Parent) {
return $app->redirect($app->url('admin_product_category_show', array('parent_id' => $Parent->getId())));
} else {
return $app->redirect($app->url('admin_product_category'));
}
} else {
log_info('カテゴリ登録エラー', array($id));
$app->addError('admin.category.save.error', 'admin');
}
}
}
$Categories = $app['eccube.repository.category']->getList($Parent);
// ツリー表示のため、ルートからのカテゴリを取得
$TopCategories = $app['eccube.repository.category']->getList(null);
return $app->render('Product/category.twig', array('form' => $form->createView(), 'Parent' => $Parent, 'Categories' => $Categories, 'TopCategories' => $TopCategories, 'TargetCategory' => $TargetCategory));
}