Permalink
Browse files

Merge pull request #32 from symfony-cmf/slideshow

SlideshowBlock
  • Loading branch information...
2 parents 14fdc6f + 183bc29 commit 4d65f262288924b536ebce53c4c54039c88dcbfe @dbu dbu committed Mar 15, 2013
@@ -30,8 +30,8 @@ protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('form.group_general')
- ->add('parentDocument', 'doctrine_phpcr_odm_tree', array('root_node' => $this->contentRoot, 'choice_list' => array(), 'select_root_node' => true))
- ->add('name', 'text')
+ ->add('parentDocument', 'doctrine_phpcr_odm_tree', array('root_node' => $this->contentRoot, 'choice_list' => array(), 'select_root_node' => true))
+ ->add('name', 'text')
->end()
;
}
@@ -0,0 +1,27 @@
+<?php
+
+namespace Symfony\Cmf\Bundle\BlockBundle\Admin\Imagine;
+
+use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
+use Sonata\AdminBundle\Form\FormMapper;
+
+class MinimalImagineBlockAdmin extends Admin
+{
+ protected $translationDomain = 'SymfonyCmfBlockBundle';
+
+ protected function configureFormFields(FormMapper $formMapper)
+ {
+ parent::configureFormFields($formMapper);
+
+ // image is only required when creating a new item
+ // TODO: sonata is not using one admin instance per object, so this doesnt really work - fix it
+ $imageRequired = ($this->getSubject() && $this->getSubject()->getParent()) ? false : true;
+
+ $formMapper
+ ->with('form.group_general')
+ ->add('label', 'text', array('required' => false))
+ ->add('image', 'phpcr_odm_image', array('required' => $imageRequired, 'label' => 'Slide Image', 'data_class' => 'Doctrine\ODM\PHPCR\Document\Image'))
+ ->add('position', 'hidden', array('mapped' => false))
+ ->end();
+ }
+}
@@ -0,0 +1,101 @@
+<?php
+
+namespace Symfony\Cmf\Bundle\BlockBundle\Admin\Imagine;
+
+use Symfony\Component\Form\FormEvent;
+use Symfony\Component\Form\FormEvents;
+
+use Doctrine\Common\Util\ClassUtils;
+use Doctrine\ODM\PHPCR\ChildrenCollection;
+
+use Sonata\AdminBundle\Form\FormMapper;
+use Sonata\AdminBundle\Datagrid\ListMapper;
+use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
+
+class MinimalSlideshowBlockAdmin extends Admin
+{
+
+ protected $baseRouteName = 'symfony_cmf_block.imagine.minimal_slideshow_admin';
+ protected $baseRoutePattern = 'symfony_cmf/block/imagineMinimalSlideshow';
+ protected $translationDomain = 'SymfonyCmfBlockBundle';
+
+ protected function configureListFields(ListMapper $listMapper)
+ {
+ parent::configureListFields($listMapper);
+ $listMapper
+ ->addIdentifier('id', 'text')
+ ->add('title', 'text');
+ }
+
+ protected function configureFormFields(FormMapper $formMapper)
+ {
+ parent::configureFormFields($formMapper);
+ $formMapper
+ ->with('form.group_general')
+ ->add('title', 'text')
+ ->with('Items')
+ ->add('children', 'sonata_type_collection',
+ array(
+ 'by_reference' => false,
+ ),
+ array(
+ 'edit' => 'inline',
+ 'inline' => 'table',
+ 'admin_code' => 'symfony_cmf_block.imagine.minimal_imagine_admin',
+ 'sortable' => 'position',
+ ))
+ ->end();
+
+ $formBuilder = $formMapper->getFormBuilder();
+ $formBuilder->addEventListener(FormEvents::POST_BIND, array($this, 'onPostBind'));
+ }
+
+ // reorder children according to the form data
+ public function onPostBind(FormEvent $event)
+ {
+ /** @var $newCollection ChildrenCollection */
+ $newCollection = $event->getData()->getChildren();
+ $newCollection->clear();
+
+ foreach ($event->getForm()->get('children') as $child) {
+ if ($child->get('_delete')->getData()) {
+ // do not re-add a deleted child
+ continue;
+ }
+ if ($child->getName()) {
+ // keep key in collection
+ $newCollection[$child->getName()] = $child->getData();
+ } else {
+ $newCollection[] = $child->getData();
+ }
+ }
+ }
+
+ public function prePersist($slideshow)
+ {
+ foreach($slideshow->getChildren() as $child) {
+ $child->setName($this->generateName());
+ }
+ }
+
+ public function preUpdate($slideshow)
+ {
+ foreach($slideshow->getChildren() as $child) {
+ if (! $this->modelManager->getNormalizedIdentifier($child)) {
+ $child->setName($this->generateName());
+ }
+ }
+ }
+
+ /**
+ * Generate a most likely unique name
+ *
+ * TODO: have child documents use the autoname annotation once this is done: http://www.doctrine-project.org/jira/browse/PHPCR-103
+ *
+ * @return string
+ */
+ private function generateName()
+ {
+ return 'child_' . time() . '_' . rand();
+ }
+}
@@ -0,0 +1,34 @@
+<?php
+
+namespace Symfony\Cmf\Bundle\BlockBundle\Admin\Imagine;
+
+use Sonata\AdminBundle\Form\FormMapper;
+
+class SlideshowBlockAdmin extends MinimalSlideshowBlockAdmin
+{
+ protected $baseRouteName = 'symfony_cmf_block.imagine.slideshow_admin';
+ protected $baseRoutePattern = 'symfony_cmf/block/imagineSlideshow';
+ protected $translationDomain = 'SymfonyCmfBlockBundle';
+
+ /**
+ * Path to where new slideshow blocks may be attached
+ *
+ * @var string
+ */
+ protected $blockRoot;
+
+ public function setBlockRoot($blockRoot)
+ {
+ $this->blockRoot = $blockRoot;
+ }
+
+ protected function configureFormFields(FormMapper $formMapper)
+ {
+ parent::configureFormFields($formMapper);
+ $formMapper
+ ->with('form.group_general')
+ ->add('parentDocument', 'doctrine_phpcr_odm_tree', array('root_node' => $this->blockRoot, 'choice_list' => array(), 'select_root_node' => true))
+ ->add('name', 'text')
+ ->end();
+ }
+}
@@ -15,16 +15,23 @@ class ContainerBlockService extends BaseBlockService implements BlockServiceInte
{
protected $blockRenderer;
+ protected $template = 'SymfonyCmfBlockBundle:Block:block_container.html.twig';
/**
* @param string $name
* @param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
* @param \Sonata\BlockBundle\Block\BlockRendererInterface $blockRenderer
+ * @param string|null $template
*/
- public function __construct($name, EngineInterface $templating, BlockRendererInterface $blockRenderer)
+ public function __construct($name, EngineInterface $templating, BlockRendererInterface $blockRenderer, $template = null)
{
parent::__construct($name, $templating);
+
$this->blockRenderer = $blockRenderer;
+
+ if ($template) {
+ $this->template = $template;
+ }
}
/**
@@ -48,7 +55,7 @@ public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
*/
protected function getTemplate()
{
- return 'SymfonyCmfBlockBundle:Block:block_container.html.twig';
+ return $this->template;
}
/**
@@ -28,7 +28,9 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->scalarNode('document_manager_name')->defaultValue('default')->end()
- ->scalarNode('content_basepath')->defaultNull('/cms/content')->end()
+ ->scalarNode('content_basepath')->defaultValue('/cms/content')->end()
+ ->scalarNode('block_basepath')->defaultValue('/cms/content')->end()
+ ->scalarNode('imagine')->defaultValue(false)->end()
->scalarNode('simple_admin_class')->defaultNull()->end()
->scalarNode('simple_document_class')->defaultNull()->end()
->scalarNode('container_admin_class')->defaultNull()->end()
@@ -74,7 +76,6 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
-
->end()
;
@@ -13,6 +13,9 @@ public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration(new Configuration(), $configs);
+ $container->setParameter($this->getAlias() . '.content_basepath', $config['content_basepath']);
+ $container->setParameter($this->getAlias() . '.block_basepath', $config['block_basepath']);
+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
@@ -33,6 +36,10 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter($this->getAlias() . '.multilang.locales', $config['multilang']['locales']);
}
+ if ($config['imagine']) {
+ $loader->load('imagine.xml');
+ }
+
if (isset($config['simple_document_class'])) {
$container->setParameter($this->getAlias() . '.simple_document_class', $config['simple_document_class']);
}
@@ -61,10 +68,6 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter($this->getAlias() . '.' . 'action_admin_class', $config['action_admin_class']);
}
- if (isset($config['action_admin_class'])) {
- $container->setParameter($this->getAlias() . '.' . 'action_admin_class', $config['action_admin_class']);
- }
-
$blockLoader = $container->getDefinition('symfony_cmf.block.service');
$blockLoader->replaceArgument(1, $config['document_manager_name']);
@@ -96,6 +99,10 @@ public function loadSonataAdmin($config, XmlFileLoader $loader, ContainerBuilder
if (isset($config['simple_admin_class'])) {
$container->setParameter($this->getAlias() . '.' . $prefix . 'simple_admin_class', $config['simple_admin_class']);
}
+
+ if (isset($config['imagine']) && $config['imagine']) {
+ $loader->load('imagine.admin.xml');
+ }
}
public function loadSonataCache($config, XmlFileLoader $loader, ContainerBuilder $container)
@@ -8,13 +8,16 @@
use Sonata\BlockBundle\Model\BlockInterface;
/**
- * Block that contains other blocks ...
+ * Block that contains other blocks
*
* @PHPCRODM\Document(referenceable=true)
*/
class ContainerBlock extends BaseBlock
{
- /** @PHPCRODM\Children */
+ /**
+ * @var ChildrenCollection
+ * @PHPCRODM\Children
+ */
protected $children;
public function __construct($name = null)
@@ -44,16 +47,44 @@ public function setChildren(ChildrenCollection $children)
/**
* Add a child to this container
*
- * @param BlockInterface $child
- * @param string $key OPTIONAL
+ * @param BlockInterface $child
+ * @param string $key the collection index name to use in the
+ * child collection if not set, the child will simply be appended at
+ * the end
+ *
* @return boolean
*/
public function addChild(BlockInterface $child, $key = null)
{
if ($key != null) {
+
return $this->children->set($key, $child);
}
return $this->children->add($child);
}
+
+ /**
+ * Alias to addChild to make the form layer happy
+ *
+ * @param BlockInterface $children
+ *
+ * @return boolean
+ */
+ public function addChildren(BlockInterface $children)
+ {
+ return $this->addChild($children);
+ }
+
+ /**
+ * Remove a child from this container
+ *
+ * @param mixed $child
+ * @return void
+ */
+ public function removeChild($child)
+ {
+ $this->children->removeElement($child);
+ }
+
}
Oops, something went wrong.

0 comments on commit 4d65f26

Please sign in to comment.