Пример #1
0
 /**
  * Create a new block or edit an existing block.
  *
  * @Route("/edit/{blockEntity}", requirements={"blockEntity" = "^[1-9]\d*$"})
  * @Theme("admin")
  * @param Request $request
  * @param BlockEntity $blockEntity
  * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  */
 public function editAction(Request $request, BlockEntity $blockEntity = null)
 {
     $accessLevelRequired = ACCESS_EDIT;
     if (null === $blockEntity) {
         $bKey = json_decode($request->query->get('bkey'));
         if (empty($bKey)) {
             return $this->redirect('zikulablocksmodule_block_new');
         }
         $blockEntity = new BlockEntity();
         // sets defaults in constructor
         $blockEntity->setBkey($bKey);
         $accessLevelRequired = ACCESS_ADD;
     }
     if (!$this->hasPermission('ZikulaBlocksModule::', $blockEntity->getBlocktype() . ':' . $blockEntity->getTitle() . ':' . $blockEntity->getBid(), $accessLevelRequired)) {
         throw new AccessDeniedException();
     }
     $blockInstance = $this->get('zikula_blocks_module.api.block')->createInstanceFromBKey($blockEntity->getBkey());
     $blockType = $blockEntity->getBlocktype();
     if (empty($blockType)) {
         $blockEntity->setBlocktype($blockInstance->getType());
     }
     $form = $this->createForm('Zikula\\BlocksModule\\Form\\Type\\BlockType', $blockEntity);
     $form->handleRequest($request);
     list($moduleName, $blockFqCn) = explode(':', $blockEntity->getBkey());
     $renderedOutput = $this->getBlockModifyOutput($blockInstance, $blockEntity, $request);
     if ($blockInstance instanceof \Zikula_Controller_AbstractBlock && $blockInstance->info()['form_content']) {
         // @todo @deprecated remove at Core-2.0
         $renderedOutput = $this->formContentModify($request, $blockEntity);
     }
     if ($form->isSubmitted() and $form->get('save')->isClicked() and $form->isValid()) {
         $content = [];
         if ($blockInstance instanceof BlockControllerInterface) {
             $content = $blockInstance->modify($request, $blockEntity->getContent());
         } elseif ($blockInstance instanceof \Zikula_Controller_AbstractBlock) {
             // @todo remove this BC at Core-2.0
             if ($blockInstance->info()['form_content']) {
                 $content = $this->formContentModify($request);
             } else {
                 $blockInfo = call_user_func([$blockInstance, 'update'], ['content' => $blockEntity->getContent()]);
                 $content = $blockInfo['content'];
             }
         }
         $blockEntity->setContent($content);
         // sort Filter array so keys are always sequential.
         $filters = $blockEntity->getFilters();
         sort($filters);
         $blockEntity->setFilters($filters);
         /** @var \Doctrine\ORM\EntityManager $em */
         $em = $this->getDoctrine()->getManager();
         $module = $em->getRepository('ZikulaExtensionsModule:ExtensionEntity')->findOneBy(['name' => $moduleName]);
         $blockEntity->setModule($module);
         $em->persist($blockEntity);
         $em->flush();
         $this->addFlash('status', __('Block saved!'));
         return $this->redirect($this->generateUrl('zikulablocksmodule_admin_view'));
     }
     if ($form->isSubmitted() and $form->get('cancel')->isClicked()) {
         $this->addFlash('status', __('Operation cancelled.'));
         return $this->redirect($this->generateUrl('zikulablocksmodule_admin_view'));
     }
     return $this->render('ZikulaBlocksModule:Admin:edit.html.twig', ['moduleName' => $moduleName, 'renderedOutput' => $renderedOutput, 'form' => $form->createView()]);
 }