Sonata\ProductBundle\Controller\ProductVariationAdminController::createAction PHP Method

createAction() public method

public createAction ( Request $request = null ) : RedirectResponse | Response
$request Symfony\Component\HttpFoundation\Request
return Symfony\Component\HttpFoundation\RedirectResponse | Symfony\Component\HttpFoundation\Response
    public function createAction(Request $request = null)
    {
        if (!$this->admin->getParent()) {
            throw new \RuntimeException('The admin cannot be call directly, it must be embedded');
        }
        if (!$this->admin->isGranted('EDIT') || !$this->admin->isGranted('DELETE')) {
            throw new AccessDeniedException();
        }
        $form = $this->createFormBuilder(null, array())->add('number', 'integer', array('required' => true, 'label' => $this->getTranslator()->trans('variations_number', array(), 'SonataProductBundle'), 'attr' => array('min' => 1, 'max' => 10), 'constraints' => array(new NotBlank(), new Range(array('min' => 1, 'max' => 10)))))->getForm();
        // product is the main product object, used to create a set of variation
        $product = $this->admin->getParent()->getSubject();
        if ($request->isMethod('POST')) {
            $form->submit($request);
            if ($form->isValid()) {
                $number = $form->get('number')->getData();
                $manager = $this->getProductManager();
                $productProvider = $this->getProductPool()->getProvider($product);
                for ($i = 1; $i <= $number; ++$i) {
                    try {
                        $variation = $productProvider->createVariation($product);
                        $manager->persist($variation);
                    } catch (\Exception $e) {
                        $this->addFlash('sonata_flash_error', 'flash_create_variation_error');
                        return new RedirectResponse($this->admin->generateUrl('create'));
                    }
                }
                $manager->flush();
                $this->addFlash('sonata_flash_success', $this->getTranslator()->trans('flash_create_variation_success', array(), 'SonataProductBundle'));
                return new RedirectResponse($this->admin->generateUrl('list'));
            }
        }
        return $this->render('SonataProductBundle:ProductAdmin:create_variation.html.twig', array('object' => $product, 'form' => $form->createView(), 'action' => 'edit'));
    }