src/PN/Bundle/ProductBundle/Controller/Administration/ProductUpdateGroupController.php
/**
     * Add a Product Price entity.
     *
     * @Route("/group-update/update", name="product_group_update_action_update", methods={"POST"})
     */
Add a Product Price entity.
Add a Product Price entity .
public function productGroupUpdateAction(Request $request)
    {
        $this->denyAccessUnlessGranted(['ROLE_ADMIN']);
        $session = $request->getSession();
        $em = $this->getDoctrine()->getManager();
        $type = $request->request->get('type');
        $data = $request->request->get('data');
        if (!Validate::not_null($type)) {
            $this->addFlash('error', 'Error in Type');
            return $this->redirectToRoute('product_group_update_action');
        }
        $productSelected = $session->get('productSelected');
        if (count($productSelected) == 0) {
            $this->addFlash('error', 'Please add some products');
            return $this->redirectToRoute('product_group_update_action');
        }
        $entities = array();
        foreach ($productSelected as $value) {
            if (Validate::not_null($value)) {
                $product = $em->getRepository(Product::class)->find($value);
                $entities[] = $product;
            }
        }
        $userName = $this->get('user')->getUserName();
        $n = 0;
        if ($type == 'checkbox') {
            foreach ($entities as $entity) {
                $n++;
                $this->updateCheckboxs($entity, $data);
            }
        } elseif ($type == 'promotion') {
            if (!isset($data['removeDiscount'])) {
                if (isset($data['discount']) AND (!Validate::not_null($data['discount']) OR !is_numeric($data['discount']))) {
                    $this->addFlash('error', 'Please enter the promotion discount');
                    return $this->redirectToRoute('product_group_update_action');
                }
                if (Validate::not_null($data['expiryDate']) AND !Validate::date($data['expiryDate'])) {
                    $this->addFlash('error', 'Please enter a valid promotion expiry date');
                    return $this->redirectToRoute('product_group_update_action');
                }
            }
            $promotionalExpiryDate = null;
            if (isset($data['expiryDate']) AND Validate::not_null($data['expiryDate'])) {
                $promotionalExpiryDate = Date::convertDateFormat($data['expiryDate'], Date::DATE_FORMAT3,
                    Date::DATE_FORMAT2);
                $promotionalExpiryDate = new \DateTime($promotionalExpiryDate);
            }
            foreach ($entities as $entity) {
                $n++;
                $this->updatePromotionPrice($entity, $data, $promotionalExpiryDate);
            }
        } elseif ($type == 'content') {
            foreach ($entities as $entity) {
                $n++;
                $this->updateContent($entity, $data);
            }
        }
        $em->flush();
        $this->addFlash('success', $n . ' Products updated successfully');
        if ($request->request->get('action') == "saveAndNext") {
            return $this->redirectToRoute('product_group_update_action');
        }
        $session->remove('productSelected');
        return $this->redirectToRoute('product_group_update_index');
    }
public function productGroupUpdateAction ( Request $ request ) { $ this -> denyAccessUnlessGranted ( [ 'ROLE_ADMIN' ] ) ; $ session = $ request -> getSession ( ) ; $ em = $ this -> getDoctrine ( ) -> getManager ( ) ; $ type = $ request -> request -> get ( 'type' ) ; $ data = $ request -> request -> get ( 'data' ) ; if ( ! Validate :: not_null ( $ type ) ) { $ this -> addFlash ( 'error' , 'Error in Type' ) ; return $ this -> redirectToRoute ( 'product_group_update_action' ) ; } $ productSelected = $ session -> get ( 'productSelected' ) ; if ( count ( $ productSelected ) == 0 ) { $ this -> addFlash ( 'error' , 'Please add some products' ) ; return $ this -> redirectToRoute ( 'product_group_update_action' ) ; } $ entities = array ( ) ; foreach ( $ productSelected as $ value ) { if ( Validate :: not_null ( $ value ) ) { $ product = $ em -> getRepository ( Product :: class ) -> find ( $ value ) ; $ entities [ ] = $ product ; } } $ userName = $ this -> get ( 'user' ) -> getUserName ( ) ; $ n = 0 ; if ( $ type == 'checkbox' ) { foreach ( $ entities as $ entity ) { $ n ++ ; $ this -> updateCheckboxs ( $ entity , $ data ) ; } } elseif ( $ type == 'promotion' ) { if ( ! isset ( $ data [ 'removeDiscount' ] ) ) { if ( isset ( $ data [ 'discount' ] ) AND ( ! Validate :: not_null ( $ data [ 'discount' ] ) OR ! is_numeric ( $ data [ 'discount' ] ) ) ) { $ this -> addFlash ( 'error' , 'Please enter the promotion discount' ) ; return $ this -> redirectToRoute ( 'product_group_update_action' ) ; } if ( Validate :: not_null ( $ data [ 'expiryDate' ] ) AND ! Validate :: date ( $ data [ 'expiryDate' ] ) ) { $ this -> addFlash ( 'error' , 'Please enter a valid promotion expiry date' ) ; return $ this -> redirectToRoute ( 'product_group_update_action' ) ; } } $ promotionalExpiryDate = null ; if ( isset ( $ data [ 'expiryDate' ] ) AND Validate :: not_null ( $ data [ 'expiryDate' ] ) ) { $ promotionalExpiryDate = Date :: convertDateFormat ( $ data [ 'expiryDate' ] , Date :: DATE_FORMAT3 , Date :: DATE_FORMAT2 ) ; $ promotionalExpiryDate = new \ DateTime ( $ promotionalExpiryDate ) ; } foreach ( $ entities as $ entity ) { $ n ++ ; $ this -> updatePromotionPrice ( $ entity , $ data , $ promotionalExpiryDate ) ; } } elseif ( $ type == 'content' ) { foreach ( $ entities as $ entity ) { $ n ++ ; $ this -> updateContent ( $ entity , $ data ) ; } } $ em -> flush ( ) ; $ this -> addFlash ( 'success' , $ n . ' Products updated successfully' ) ; if ( $ request -> request -> get ( 'action' ) == " saveAndNext " ) { return $ this -> redirectToRoute ( 'product_group_update_action' ) ; } $ session -> remove ( 'productSelected' ) ; return $ this -> redirectToRoute ( 'product_group_update_index' ) ; }
Add a Product Price entity.
Add a Product Price entity .