Permalink
Browse files

fix resorting to no longer break add and delete, generate document names

  • Loading branch information...
1 parent be0c58d commit 84e2de90ac6b0f1ebd767bdddc85027cb68d4e86 @dbu dbu committed Mar 13, 2013
Showing with 41 additions and 6 deletions.
  1. +40 −5 Admin/MinimalSlideshowAdmin.php
  2. +1 −1 Document/ContainerBlock.php
@@ -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
{
@@ -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();
+ }
}
@@ -15,7 +15,7 @@
class ContainerBlock extends BaseBlock
{
/**
- * \Doctrine\Common\Collections\ArrayCollection
+ * ChildrenCollection
* @PHPCRODM\Children
*/
protected $children;

0 comments on commit 84e2de9

Please sign in to comment.