Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
fix resorting to no longer break add and delete, generate document names
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Mar 13, 2013
1 parent be0c58d commit 84e2de9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
45 changes: 40 additions & 5 deletions Admin/MinimalSlideshowAdmin.php
Expand Up @@ -4,10 +4,13 @@

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

use Doctrine\Common\Util\ClassUtils;
use Doctrine\ODM\PHPCR\ChildrenCollection;
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;

class MinimalSlideshowAdmin extends Admin
{
Expand Down Expand Up @@ -54,12 +57,44 @@ public function onPostBind(FormEvent $event)
$newCollection->clear();

foreach ($event->getForm()->get('children') as $child) {
$newCollection->add($child->getData());
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();
}
}
}

// TODO: Item deletion doesn't work yet -> do we need to manually delete? Or does sonata call a certain method?
// TODO: Add doesn't work yet -> problem related to http://www.doctrine-project.org/jira/browse/PHPCR-98 ?
// TODO: or do we just have to add the name to the item/image?
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();
}
}
2 changes: 1 addition & 1 deletion Document/ContainerBlock.php
Expand Up @@ -15,7 +15,7 @@
class ContainerBlock extends BaseBlock
{
/**
* \Doctrine\Common\Collections\ArrayCollection
* ChildrenCollection
* @PHPCRODM\Children
*/
protected $children;
Expand Down

0 comments on commit 84e2de9

Please sign in to comment.