public function addOption(PersonNotificationOption $option)
 {
     $client = $option->getCategory()->getClient();
     foreach ($this->getClients()->getValues() as $clientSettings) {
         $existingId = $clientSettings->getClient()->getId();
         $newId = $client->getId();
         if ($existingId === $newId) {
             $clientSettings->getOptions()->add($option);
             return true;
         }
     }
     // This is a new client
     $clientSettings = new ClientSettings($client);
     $clientSettings->getOptions()->add($option);
     $this->getClients()->add($clientSettings);
 }
 public function initializeSettings(PersonInterface $person, ClientInterface $client = null)
 {
     $om = $this->om;
     $categoriesRepo = $om->getRepository('PROCERGSLoginCidadaoNotificationBundle:Category');
     $orphanCategories = $categoriesRepo->findUnconfigured($person, $client);
     if (count($orphanCategories) > 0) {
         foreach ($orphanCategories as $category) {
             $config = new PersonNotificationOption();
             $config->setCategory($category)->setPerson($person)->setSendEmail($category->getEmailable())->setSendPush(true);
             $om->persist($config);
         }
         $om->flush();
         return true;
     }
     return false;
 }
 /**
  * @Route("/config", name="lc_not_config")
  * @Template()
  */
 public function configAction(Request $request)
 {
     $id = $request->get('client');
     if (!$id) {
         return $this->gridFullAction();
     }
     $em = $this->getDoctrine()->getManager();
     $resultset = $em->getRepository('PROCERGSLoginCidadaoCoreBundle:Notification\\Category')->createQueryBuilder('cnc')->select('cnc, cnp')->join('PROCERGSOAuthBundle:Client', 'c', 'WITH', 'cnc.client = c')->leftJoin('PROCERGSLoginCidadaoCoreBundle:Notification\\PersonNotificationOption', 'cnp', 'with', 'cnp.category = cnc and cnp.person = :person')->where('c.id = :client ')->setParameter('client', $id)->setParameter('person', $this->getUser())->getQuery()->setFetchMode('PROCERGSLoginCidadaoCoreBundle:Notification\\Category', 'client', ClassMetadata::FETCH_EAGER)->getResult();
     for ($i = 0, $tot = count($resultset); $i < $tot; $i++) {
         if ($i % 2) {
             $c =& $resultset[$i];
             if (!$c) {
                 $res =& $resultset[$i - 1];
                 $a = new PersonNotificationOption();
                 $a->setPerson($this->getUser());
                 $a->setCategory($res);
                 $a->setSendEmail($res->getEmailable());
                 $a->setSendPush(false);
                 $a->setCreatedAt(new \DateTime());
                 $em->persist($a);
                 $c =& $a;
             }
             $formId = 'person-notifcation-category-' . $resultset[$i - 1]->getId();
             $form = $this->createForm(new PersonNotificationOptionFormType(), $c, array('action' => $this->generateUrl('lc_not_config_change'), 'attr' => array('class' => 'form-ajax', 'id' => $formId, 'role' => 'form', 'ajax-target' => 'div:has(#' . $formId . '):last')));
             $forms[$i - 1] = $form->createView();
             unset($resultset[$i]);
         }
     }
     if (isset($a)) {
         $em->flush();
     }
     return array('resultset' => $resultset, 'forms' => $forms);
 }