Completed
Pull Request — 2.x (#544)
by Grégoire
02:59 queued 01:29
created

src/Controller/TreeController.php (1 issue)

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Controller;
15
16
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
17
use PHPCR\Util\PathHelper;
18
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
23
/**
24
 * A controller to render the tree block.
25
 */
26
class TreeController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
27
{
28
    /**
29
     * @var string
30
     */
31
    private $template = '@SonataDoctrinePHPCRAdmin/Tree/tree.html.twig';
32
33
    /**
34
     * @var \PHPCR\SessionInterface
35
     */
36
    private $session;
37
    /**
38
     * @var array
39
     */
40
    private $treeConfiguration;
41
42
    /**
43
     * @param string $sessionName
44
     * @param array  $treeConfiguration     same structure as defined in Configuration
45
     * @param string $defaultRepositoryName The name of the default resource repository
46
     * @param string $template
47
     */
48
    public function __construct(
49
        ManagerRegistry $manager,
50
        $sessionName,
51
        array $treeConfiguration,
52
        $defaultRepositoryName,
53
        $template = null
54
    ) {
55
        if ($template) {
56
            $this->template = $template;
57
        }
58
59
        $this->session = $manager->getConnection($sessionName);
60
        if (null === $treeConfiguration['repository_name']) {
61
            $treeConfiguration['repository_name'] = $defaultRepositoryName;
62
        }
63
        $this->treeConfiguration = $treeConfiguration;
64
    }
65
66
    /**
67
     * Renders a tree, passing the routes for each of the admin types (document types)
68
     * to the view.
69
     *
70
     *
71
     * @return Response
72
     */
73
    public function treeAction(Request $request)
74
    {
75
        $root = $request->attributes->get('root');
76
77
        return $this->render($this->template, [
78
            'root_node' => $root,
79
            'routing_defaults' => $this->treeConfiguration['routing_defaults'],
80
            'repository_name' => $this->treeConfiguration['repository_name'],
81
            'reorder' => $this->treeConfiguration['reorder'],
82
            'move' => $this->treeConfiguration['move'],
83
            'sortable_by' => $this->treeConfiguration['sortable_by'],
84
        ]);
85
    }
86
87
    /**
88
     * Reorder $moved (child of $parent) before or after $target.
89
     *
90
     *
91
     * @return Response
92
     */
93
    public function reorderAction(Request $request)
94
    {
95
        $parentPath = $request->get('parent');
96
        $dropedAtPath = $request->get('dropped');
97
        $targetPath = $request->get('target');
98
        $position = $request->get('position');
99
100
        if (null === $parentPath || null === $dropedAtPath || null === $targetPath) {
101
            return new JsonResponse(['Parameters parent, dropped and target has to be set to reorder.'], Response::HTTP_BAD_REQUEST);
102
        }
103
104
        if (\in_array($position, ['over', 'child'], true)) {
105
            return new JsonResponse(['Can not reorder when dropping into a collection.'], Response::HTTP_BAD_REQUEST);
106
        }
107
108
        $before = 'before' === $position;
109
        $parentNode = $this->session->getNode($parentPath);
110
        $targetName = PathHelper::getNodeName($targetPath);
111
        if (!$before) {
112
            $nodesIterator = $parentNode->getNodes();
113
            $nodesIterator->rewind();
114
            while ($nodesIterator->valid()) {
115
                if ($nodesIterator->key() === $targetName) {
116
                    break;
117
                }
118
                $nodesIterator->next();
119
            }
120
            $targetName = null;
121
            if ($nodesIterator->valid()) {
122
                $nodesIterator->next();
123
                if ($nodesIterator->valid()) {
124
                    $targetName = $nodesIterator->key();
125
                }
126
            }
127
        }
128
        $parentNode->orderBefore($targetName, PathHelper::getNodeName($dropedAtPath));
129
        $this->session->save();
130
131
        return new Response('', Response::HTTP_NO_CONTENT);
132
    }
133
}
134