public function onAppSaveData(PackageSaveAppEvent $event) { $em = $this->container->get('doctrine')->getManager(); $packageManager = $this->container->get('keosu_core.packagemanager'); $app = $event->getApp(); $appPrivate = !!$app->getConfigPackages()[KeosuGadgetAuthenticationGadgetBundle::PACKAGE_NAME]['privateApp']; // if the app is private we have to add an authentication page $authenticationPage = $em->getRepository('KeosuCoreBundle:Page')->findOneBy(array('appId' => $app->getId(), 'templateId' => KeosuGadgetAuthenticationGadgetBundle::AUTHENTICATION_TEMPLATE_ID, 'name' => KeosuGadgetAuthenticationGadgetBundle::AUTHENTICATION_PAGE_NAME)); if ($authenticationPage == null && $appPrivate) { // remove other main $pagesMain = $em->getRepository('KeosuCoreBundle:Page')->findBy(array('appId' => $app->getId(), 'isMain' => true)); foreach ($pagesMain as $pageMain) { $pageMain->setIsMain(false); $em->persist($pageMain); } $page = new Page(); $page->setIcon('glyphicon-lock'); $page->setTemplateId(KeosuGadgetAuthenticationGadgetBundle::AUTHENTICATION_TEMPLATE_ID); $page->setName(KeosuGadgetAuthenticationGadgetBundle::AUTHENTICATION_PAGE_NAME); $page->setAppId($app->getId()); // the authentication become main $page->setIsMain(true); $em->persist($page); $gadget = new Gadget(); $gadget->setShared(false); $listTemplate = $packageManager->getListTemplateForGadget(KeosuGadgetAuthenticationGadgetBundle::PACKAGE_NAME); $keys = array_keys($listTemplate); $gadget->setName(KeosuGadgetAuthenticationGadgetBundle::PACKAGE_NAME); $gadget->setTemplate($listTemplate[$keys[0]]); // 0 by default $gadget->setPage($page); // there is only one zone on this template $templateHtml = file_get_contents(TemplateUtil::getPageTemplateAbsolutePath() . KeosuGadgetAuthenticationGadgetBundle::AUTHENTICATION_TEMPLATE_ID); //Get all the elements of class "zone" in template dom $crawler = new Crawler($templateHtml); $zones = $crawler->filter('.zone')->extract(array('id')); $gadget->setZone($zones[0]); $em->persist($gadget); } else { if (!$appPrivate && $authenticationPage != null) { $gadgets = $em->getRepository('KeosuCoreBundle:Gadget')->findByPage($authenticationPage->getId()); //First delete manually all its gadget foreach ($gadgets as $gadget) { $em->remove($gadget); } $em->remove($authenticationPage); } } $em->flush(); }
/** * Add a new Gadget in a zone * @param $pageId where we want to add the gadget * @param $zoneName where we want to add the gadget * @param $gadgetName name of the gadget */ public function addAction($pageId, $zoneName, $gadgetName) { $em = $this->get('doctrine')->getManager(); $dispatch = $this->get('event_dispatcher'); // search if there is allready a gadget $gadget = $em->getRepository('KeosuCoreBundle:Gadget')->findOneBy(array('zone' => $zoneName, 'page' => $pageId)); if ($gadget == null) { $gadget = new Gadget(); $page = $em->getRepository('KeosuCoreBundle:Page')->find($pageId); $gadget->setPage($page); $gadget->setZone($zoneName); } if (!$this->get('keosu_core.packagemanager')->isGadgetExist($gadgetName)) { throw new \LogicException("Gadget : " . $gadgetName . " Not Found"); } $gadget->setConfig(array()); // old gadget if ($gadget->getName() !== null) { $event = new GadgetActionEvent($pageId, $zoneName, $gadget); $dispatch->dispatch(KeosuEvents::GADGET_ADD_OLD . $gadget->getName(), $event); if ($event->getResponse() !== null) { return $event->getResponse(); } } $gadget->setName($gadgetName); // add a new $event = new GadgetActionEvent($pageId, $zoneName, $gadget); $dispatch->dispatch(KeosuEvents::GADGET_ADD . $gadget->getName(), $event); if ($event->getResponse() !== null) { return $event->getResponse(); } return $this->formGadget($gadget); }