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