create visualisation service rers #268

This commit is contained in:
Julien Fastré 2014-11-10 12:15:10 +01:00
parent 16308d0139
commit 28862bbdb9
9 changed files with 293 additions and 6 deletions

View File

@ -242,18 +242,28 @@ class CustomFieldsGroupController extends Controller
} }
$form = $this->createForm('custom_field', null, array('group' => $entity)); $form = $this->createForm('custom_field', null, array('group' => $entity));
$form->add('submit', 'submit', array('label' => 'POST')); $form->add('submit_dump', 'submit', array('label' => 'POST AND DUMP'));
$form->add('submit_render','submit', array('label' => 'POST AND RENDER'));
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted()) {
var_dump($form->getData());
var_dump(json_encode($form->getData()));
}
$this->get('twig.loader') $this->get('twig.loader')
->addPath(__DIR__.'/../Tests/Fixtures/App/app/Resources/views/', ->addPath(__DIR__.'/../Tests/Fixtures/App/app/Resources/views/',
$namespace = 'test'); $namespace = 'test');
if ($form->isSubmitted()) {
if ($form->get('submit_render')->isClicked()) {
return $this->render('ChillCustomFieldsBundle:CustomFieldsGroup:render.html.twig', array(
'fields' => $form->getData(),
'customFieldsGroup' => $entity
));
}
var_dump($form->getData());
var_dump(json_encode($form->getData()));
}
return $this return $this
->render('@test/CustomField/simple_form_render.html.twig', array( ->render('@test/CustomField/simple_form_render.html.twig', array(
'form' => $form->createView() 'form' => $form->createView()

View File

@ -8,6 +8,7 @@ use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
use Chill\CustomFieldsBundle\Entity\CustomField; use Chill\CustomFieldsBundle\Entity\CustomField;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bundle\TwigBundle\TwigEngine;
/** /**
* *
@ -19,9 +20,17 @@ class CustomFieldText implements CustomFieldInterface
private $requestStack; private $requestStack;
public function __construct(RequestStack $requestStack) /**
*
* @var TwigEngine
*/
private $templating;
public function __construct(RequestStack $requestStack, TwigEngine $templating)
{ {
$this->requestStack = $requestStack; $this->requestStack = $requestStack;
$this->templating = $templating;
} }
const MAX_LENGTH = 'maxLength'; const MAX_LENGTH = 'maxLength';
@ -47,7 +56,8 @@ class CustomFieldText implements CustomFieldInterface
public function render($value, CustomField $customField) public function render($value, CustomField $customField)
{ {
return $this->templating
->render('ChillCustomFieldsBundle:CustomFieldsRendering:text.html.twig', array('text' => $value));
} }
public function serialize($value, CustomField $customField) public function serialize($value, CustomField $customField)

View File

@ -39,6 +39,7 @@ services:
class: Chill\CustomFieldsBundle\CustomFields\CustomFieldText class: Chill\CustomFieldsBundle\CustomFields\CustomFieldText
arguments: arguments:
- "@request_stack" - "@request_stack"
- "@templating"
tags: tags:
- { name: 'chill.custom_field', type: 'text' } - { name: 'chill.custom_field', type: 'text' }
@ -68,3 +69,16 @@ services:
- "@request_stack" - "@request_stack"
tags: tags:
- { name: 'chill.custom_field', type: 'title' } - { name: 'chill.custom_field', type: 'title' }
chill.custom_field.helper:
class: Chill\CustomFieldsBundle\Service\CustomFieldsHelper
arguments:
- "@doctrine.orm.entity_manager"
- "@chill.custom_field.provider"
chill.custom_field.twig.custom_fields_rendering:
class: Chill\CustomFieldsBundle\Templating\Twig\CustomFieldRenderingTwig
calls:
- [setContainer, ["@service_container"]]
tags:
- { name: twig.extension }

View File

@ -0,0 +1 @@
{{ customField.name(app.request.locale) }}

View File

@ -0,0 +1,7 @@
<dl class="custom_fields_group_rendering">
<span class="custom_fields_group_name">{{ customFieldsGroup.name(app.request.locale) }}</span>
{% for customField in customFieldsGroup.customFields %}
<dt>{{ chill_custom_field_label(customField) }}</dt>
<dd>{{ chill_custom_field_widget(fields, customField) }}</dd>
{% endfor %}
</dl>

View File

@ -0,0 +1 @@
{{ text }}

View File

@ -0,0 +1,126 @@
<?php
namespace Chill\CustomFieldsBundle\Service;
use Doctrine\ORM\EntityManagerInterface;
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Symfony\Bundle\TwigBundle\TwigEngine;
/**
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*
*/
class CustomFieldsHelper
{
/**
*
* @var EntityManagerInterface
*/
private $em;
/**
*
* @var CustomFieldProvider
*/
private $provider;
/**
*
* @var array
*/
private $cache = array();
public function __construct(EntityManagerInterface $em, CustomFieldProvider $provider)
{
$this->em = $em;
$this->provider = $provider;
}
/**
*
* @param object|string $class
*/
private function _cacheCustomFields($class)
{
$customFieldsGroups = $this->em->getRepository('ChillCustomFieldsBundle:CustomFieldsGroup')
->findBy(array('entity' => (is_string($class)) ? $class : get_class($class)));
if (!$customFieldsGroups) {
throw CustomFieldsHelperException::customFieldsGroupNotFound((is_string($class)) ? $class : get_class($class));
}
foreach ($customFieldsGroup as $cfGroup) {
$this->_cacheCustomFields($cfGroup);
}
}
/**
*
* @param CustomFieldsGroup $group
*/
private function _cacheCustomFieldsGroup(CustomFieldsGroup $group)
{
foreach ($group->getCustomFields() as $field) {
$this->cache[$group->getEntity()][$field->getSlug()] = $field;
}
}
/**
*
* @param object|string $class
* @param string $slug If the slug is null, throw a proper CustomFieldsHelperException
* @return CustomField
* @throws CustomFieldsHelperException if $slug is null
*/
public function getCustomField($class, $slug = null)
{
if (!$slug) {
throw CustomFieldsHelperException::slugIsMissing();
}
$resolveClass = (is_string($class)) ? $class : get_class($class);
if (!$this->cache[$resolveClass][$slug]) {
$this->_cacheCustomFields($resolveClass);
}
return $this->cache[$resolveClass][$slug];
}
/**
*
* @param array $fields
* @param object|string $class
* @param string $slug
* @return mixed|null null if the slug is not recorded
*/
private function getCustomFieldValue(array $fields, $class, $slug)
{
return (isset($fields[$slug])) ? $this->provider
->getCustomFieldByType($this->getCustomField($class, $slug)->getType())
->deserialize($fields[$slug])
: null;
}
/**
*
* @param array $fields the **raw** array, as stored in the db
* @param CustomField|object|string $classOrCustomField the object OR the get_class($object) string OR The CustomField
* @param string slug The Slug to render, if CustomField is not Given
* @param string $slug the slug you want to render. The html is be safe.
* @throws CustomFieldsHelperException if slug is missing
*/
public function renderCustomField(array $fields, $classOrCustomField, $slug = null)
{
$customField = ($classOrCustomField instanceof CustomField) ? $classOrCustomField : $this->getCustomField($classOrCustomField, $slug);
$slug = $customField->getSlug();
$rawValue = (isset($fields[$slug])) ? $fields[$slug] : null;
return $this->provider->getCustomFieldByType($customField->getType())
->render($rawValue, $customField);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace Chill\CustomFieldsBundle\Service;
class CustomFieldsHelperException extends \Exception
{
public static function customFieldsGroupNotFound($entity)
{
return new CustomFieldsRenderingException("The customFieldsGroups associated with $entity are not found");
}
public static function slugIsMissing()
{
return new CustomFieldsRenderingException("The slug is missing");
}
}

View File

@ -0,0 +1,102 @@
<?php
namespace Chill\CustomFieldsBundle\Templating\Twig;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Chill\CustomFieldsBundle\Service\CustomFieldsHelper;
use Symfony\Component\DependencyInjection\Container;
use Chill\CustomFieldsBundle\Entity\CustomField;
/**
*
* Add three functions to Twig:
* * chill_custom_field_widget : render the value of the custom field,
* * chill_custom_field_label : render the label of the custom field,
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*
*/
class CustomFieldRenderingTwig extends \Twig_Extension implements ContainerAwareInterface
{
/**
*
* @var Container
*/
private $container;
private $defaultParams = array(
'label_layout' => 'ChillCustomFieldsBundle:CustomField:render_label.html.twig'
);
/*
* (non-PHPdoc)
* @see \Symfony\Component\DependencyInjection\ContainerAwareInterface::setContainer()
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/*
* (non-PHPdoc)
* @see Twig_Extension::getFunctions()
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction('chill_custom_field_widget', array(
$this,
'renderWidget'
), array(
'is_safe' => array(
'html'
)
)),
new \Twig_SimpleFunction('chill_custom_field_label', array(
$this,
'renderLabel'
), array(
'is_safe' => array(
'html'
)
))
];
}
/* (non-PHPdoc)
* @see Twig_ExtensionInterface::getName()
*/
public function getName()
{
return 'chill_custom_fields_rendering';
}
/**
*
* @param CustomField|object|string $customFieldOrClass
* @param string $slug
* @param array $params the parameters for rendering
*/
public function renderLabel($customFieldOrClass, $slug = null, array $params = array())
{
$resolvedParams = array_merge($this->defaultParams, $params);
$customField = ($customFieldOrClass instanceof CustomField)
? $customFieldOrClass : $this->container->get('chill.custom_field.provider')
->getCustomField($customFieldOrClass, $slug);
return $this->container->get('templating')
->render($resolvedParams['label_layout'], array('customField' => $customField));
}
public function renderWidget(array $fields, $customFieldOrClass, $slug = null)
{
return $this->container->get('chill.custom_field.helper')
->renderCustomField($fields, $customFieldOrClass, $slug);
}
}