public function newShippingAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $entity  = new NewsletterShipping();
        $data = $request->request->get('corebundle_newslettershippingtype');
        $formConfig = array();
        if(isset($data['type']) && $data['type'] == 'token'){
            $formConfig['token'] = true;
        }
        $form = $this->createForm('CoreExtraBundle\Form\NewsletterShippingType', $entity, $formConfig);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $emailArray = $this->get('core_manager')->getSubscriptorFromType($entity);
            $entity->setTotalSent(count($emailArray));
            
            /////////////////////////////////////////////////////////
            //Send every email with diferent token and store on DB //
            /////////////////////////////////////////////////////////
            if($entity->getType() == NewsletterShipping::TYPE_TOKEN){
                 //truncate table email token
                $em = $this->getDoctrine()->getEntityManager();
                $connection = $em->getConnection();
                $statement = $connection->prepare("TRUNCATE TABLE email_token");
                $statement->execute();
                
                foreach ($emailArray as $email) {
                    $emailToken = new EmailToken();
                    $emailToken->setEmail($email);
                    $token = sha1(uniqid());
                    $emailToken->setToken($token);
                    $em->persist($emailToken);
                    $body = preg_replace('/(#TOKEN#)/', $token, $entity->getNewsletter()->getBody());
                    $this->get('core.mailer')->sendShipping(array($email), NewsletterShipping::TYPE_TOKEN, $body);
                }
            }else{
                $body = $entity->getNewsletter()->getBody();
                $this->get('core.mailer')->sendShipping($emailArray, $entity->getType(), $body);
            }
            $em->persist($entity);
            $em->flush();

            //if come from popup
            if ($request->isXMLHttpRequest()) {         
                return new JsonResponse(array(
                            'id' => $entity->getId()
                        ));
            }
            
            $this->get('session')->getFlashBag()->add('success', 'newsletter.shipping.created');

            return $this->redirect($this->generateUrl('coreextra_newsletter_showshipping', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }