/** * @param string $blockName * @param array $options * @param string $default * @return string */ public function contentBlock($blockName, $options = array(), $default = null) { $em = $this->doctrine->getManager(); $repository = $em->getRepository('GlavwebContentBlockBundle:ContentBlock'); $contentBlock = $repository->findOneByName($blockName); $tag = isset($options['tag']) ? $options['tag'] : 'div'; $attr = isset($options['attr']) ? $options['attr'] : array(); if (isset($options['class'])) { $attr['class'] = $options['class']; } if (isset($options['href'])) { $attr['href'] = $options['href']; } if (!$contentBlock) { $contentBlock = new ContentBlock(); $contentBlock->setName($blockName); $contentBlock->setBody($default ? $default : $blockName); $em->persist($contentBlock); $em->flush(); } $contentEditable = ''; $dataBlockName = ''; $isEditable = $this->request && $this->request->get('contenteditable') && $this->securityContext->isGranted('ROLE_ADMIN'); if ($isEditable) { $contentEditable = ' contenteditable="true"'; $dataBlockName = ' data-block-name="' . $blockName . '"'; $attr['class'] = isset($attr['class']) ? $attr['class'] . ' js-content-block' : 'js-content-block'; } $attrParts = array(); foreach ($attr as $attrName => $value) { $attrParts[] = sprintf('%s="%s"', $attrName, $value); } return '<' . $tag . ' ' . implode(' ', $attrParts) . ' ' . $contentEditable . $dataBlockName . '>' . $contentBlock->getBody() . '</' . $tag . '>'; }
/** * @Route("/api/content-block/{name}", name="content_block_show", requirements={"_method": "GET"}) * * @param string $name * @return JsonResponse */ public function showAction($name) { $editable = false; $user = $this->isAuth(); if ($user) { $editable = true; } $em = $this->getDoctrine()->getManager(); $repository = $em->getRepository('GlavwebContentBlockBundle:ContentBlock'); $contentBlock = $repository->findOneByName($name); if (empty($contentBlock)) { $contentBlock = new ContentBlock(); $contentBlock->setName($name); $contentBlock->setBody($name); $em->persist($contentBlock); $em->flush(); } return new JsonResponse(array('editable' => $editable, 'contentBlock' => $contentBlock->getBody())); }