Completed
Push — master ( a33308...36ba79 )
by Maximilian
01:58
created

src/Form/Listener/CollectionOrderListener.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrinePHPCRAdminBundle\Form\Listener;
13
14
use Doctrine\Common\Collections\Collection;
15
use Symfony\Component\Form\FormBuilder;
16
use Symfony\Component\Form\FormEvent;
17
use Symfony\Component\PropertyAccess\PropertyAccess;
18
19
/**
20
 * A listener for the parent form object to reorder a children collection based
21
 * on the order in the form request, which reflects the frontend order. Just
22
 * setting the right order will make PHPCR-ODM persist the reorderings.
23
 *
24
 * @author David Buchmann <[email protected]>
25
 */
26
class CollectionOrderListener
27
{
28
    /**
29
     * @var string
30
     */
31
    private $name;
32
33
    /**
34
     * @param string $name the form field name used for the collection
35
     */
36
    public function __construct($name)
37
    {
38
        $this->name = $name;
39
    }
40
41
    /**
42
     * Reorder the children of the parent form data at $this->name.
43
     *
44
     * For whatever reason we have to go through the parent object, just
45
     * getting the collection from the form event and reordering it does
46
     * not update the stored order.
47
     *
48
     * @param FormEvent $event
49
     */
50
    public function onSubmit(FormEvent $event)
51
    {
52
        $form = $event->getForm()->getParent();
53
        $data = $form->getData();
54
55
        if (!is_object($data)) {
56
            return;
57
        }
58
59
        $accessor = PropertyAccess::getPropertyAccessor(); // use deprecated BC method to support symfony 2.2
0 ignored issues
show
The method getPropertyAccessor() does not seem to exist on object<Symfony\Component...yAccess\PropertyAccess>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
        $newCollection = $accessor->getValue($data, $this->name);
61
        if (!$newCollection instanceof Collection) {
62
            return;
63
        }
64
        /* @var $newCollection Collection */
65
66
        $newCollection->clear();
67
68
        /** @var $item FormBuilder */
69
        foreach ($form->get($this->name) as $key => $item) {
70
            if ($item->get('_delete')->getData()) {
71
                // do not re-add a deleted child
72
                continue;
73
            }
74
            if ($item->getName() && !is_numeric($item->getName())) {
75
                // keep key in collection
76
                $newCollection[$item->getName()] = $item->getData();
77
            } else {
78
                $newCollection[] = $item->getData();
79
            }
80
        }
81
    }
82
}
83