/**
  * Called on onConfigure Listener method
  *
  * @param WidgetInstance $widgetInstance
  * @return array    AJAX response
  *
  * @EXT\ParamConverter("authenticatedUser", options={"authenticatedUser" = true})
  */
 public function userStatWidgetConfigureFormAction(WidgetInstance $widgetInstance, Request $request)
 {
     if (!$this->get('security.authorization_checker')->isGranted('edit', $widgetInstance)) {
         throw new AccessDeniedException();
     }
     $exoverrideService = $this->container->get('cpasimusante.exoverride_services');
     $userCanAccessWs = $exoverrideService->userCanAccessWs();
     //block the access to configure for users with no right
     if ($userCanAccessWs == array()) {
         return $this->render('CPASimUSanteExoverrideBundle:Widget:statWidgetConfigure.html.twig', array('userCanAccessWs' => $userCanAccessWs));
     } else {
         $em = $this->get('doctrine.orm.entity_manager');
         $widgetExoverrideRadar = $em->getRepository('CPASimUSanteExoverrideBundle:ExoverrideStatConfig')->findOneByWidgetInstance($widgetInstance);
         if (null === $widgetExoverrideRadar) {
             $widgetExoverrideRadar = new ExoverrideStatConfig();
             $widgetExoverrideRadar->setWidgetInstance($widgetInstance);
         }
         $userlist = $widgetExoverrideRadar->getUserList();
         $resourcelist = $widgetExoverrideRadar->getResourcelist();
         $form = $this->formFactory->create(new ExoverrideStatConfigType(), $widgetExoverrideRadar);
         $form->handleRequest($request);
         if ($form->isValid()) {
             $widgetExoverrideRadar = $form->getData();
             //Need the exercise list corresponding to resource list to persist
             //in order to avoid having a request each time
             $services = $this->container->get('cpasimusante.exoverride_services');
             $exercices = $services->getExoList($widgetExoverrideRadar->getResourcelist());
             $list = array();
             foreach ($exercices as $exos) {
                 $list[] = $exos->getId();
             }
             $exolist = implode(',', $list);
             $widgetExoverrideRadar->setExolist($exolist);
             $em->persist($widgetExoverrideRadar);
             $em->flush();
             return new Response('', Response::HTTP_NO_CONTENT);
         }
         return $this->render('CPASimUSanteExoverrideBundle:Widget:statWidgetConfigure.html.twig', array('form' => $form->createView(), 'widgetInstance' => $widgetInstance, 'userlist' => $userlist, 'resourcelist' => $resourcelist, 'userCanAccessWs' => $userCanAccessWs));
     }
 }
 /**
  * Called on onConfigure Listener method
  *
  * @param WidgetInstance $widgetInstance
  * @return array    AJAX response
  *
  * @EXT\ParamConverter("authenticatedUser", options={"authenticatedUser" = true})
  */
 public function userStatWidgetConfigureFormAction(WidgetInstance $widgetInstance, Request $request)
 {
     if (!$this->get('security.authorization_checker')->isGranted('edit', $widgetInstance)) {
         throw new AccessDeniedException();
     }
     $exoverrideService = $this->container->get('cpasimusante.exoverride_services');
     $userCanAccessWs = $exoverrideService->userCanAccessWs();
     //block the access to configure for users with no right
     if ($userCanAccessWs == array()) {
         return $this->render('CPASimUSanteExoverrideBundle:Widget:statWidgetConfigure.html.twig', array('userCanAccessWs' => $userCanAccessWs));
     } else {
         $em = $this->get('doctrine.orm.entity_manager');
         $widgetExoverride = $em->getRepository('CPASimUSanteExoverrideBundle:ExoverrideStatConfig')->findOneByWidgetInstance($widgetInstance);
         // Create an ArrayCollection of the current Data objects in the database
         $originalDatas = new ArrayCollection();
         $resourcelist = array();
         //first run
         if (null === $widgetExoverride) {
             $widgetExoverride = new ExoverrideStatConfig();
             $widgetExoverride->setWidgetInstance($widgetInstance);
         } else {
             foreach ($widgetExoverride->getDatas() as $data) {
                 $originalDatas->add($data);
             }
             foreach ($originalDatas as $data) {
                 $resourcelist[] = $data->getResourcelist();
             }
         }
         $form = $this->formFactory->create(new ExoverrideStatConfigType(), $widgetExoverride);
         $form->handleRequest($request);
         $userlist = $widgetExoverride->getUserList();
         if ($form->isValid()) {
             //Need the exercise list corresponding to resource list to persist
             //in order to avoid having a request each time
             $services = $this->container->get('cpasimusante.exoverride_services');
             $widgetExoverride = $form->getData();
             // remove the relationship between the data and the ExoverrideStatConfig
             foreach ($originalDatas as $data) {
                 //echo 'data->getResourcelist()<pre>';var_dump($data->getResourcelist());echo '</pre>';
                 /*  if (false === $widgetExoverride->getDatas()->contains($data)) {
                         // in a a many-to-one relationship, remove the relationship
                         $data->setExoverrideStatConfig(null);
                         $em->persist($data);
                         // to delete the Item entirely, you can also do that
                         $em->remove($data);
                     }*/
             }
             $em->persist($widgetExoverride);
             $em->flush();
             foreach ($widgetExoverride->getDatas() as $data) {
                 $resourcelist[] = $data->getResourcelist();
                 $exercices = $services->getExoList($data->getResourcelist());
                 $list = array();
                 foreach ($exercices as $exos) {
                     $list[] = $exos->getId();
                 }
                 $exolist = implode(',', $list);
                 $data->setExolist($exolist);
             }
             $em->persist($widgetExoverride);
             $em->flush();
             return new Response('', Response::HTTP_NO_CONTENT);
         }
         return $this->render('CPASimUSanteExoverrideBundle:Widget:statWidgetConfigure.html.twig', array('form' => $form->createView(), 'widgetInstance' => $widgetInstance, 'userlist' => $userlist, 'resourcelist' => json_encode($resourcelist), 'userCanAccessWs' => $userCanAccessWs));
     }
 }