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 |
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
|
|
|
* @return Response |
71
|
|
|
*/ |
72
|
|
|
public function treeAction(Request $request) |
73
|
|
|
{ |
74
|
|
|
$root = $request->attributes->get('root'); |
75
|
|
|
|
76
|
|
|
return $this->render($this->template, [ |
77
|
|
|
'root_node' => $root, |
78
|
|
|
'routing_defaults' => $this->treeConfiguration['routing_defaults'], |
79
|
|
|
'repository_name' => $this->treeConfiguration['repository_name'], |
80
|
|
|
'reorder' => $this->treeConfiguration['reorder'], |
81
|
|
|
'move' => $this->treeConfiguration['move'], |
82
|
|
|
'sortable_by' => $this->treeConfiguration['sortable_by'], |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Reorder $moved (child of $parent) before or after $target. |
88
|
|
|
* |
89
|
|
|
* @return Response |
90
|
|
|
*/ |
91
|
|
|
public function reorderAction(Request $request) |
92
|
|
|
{ |
93
|
|
|
$parentPath = $request->get('parent'); |
94
|
|
|
$dropedAtPath = $request->get('dropped'); |
95
|
|
|
$targetPath = $request->get('target'); |
96
|
|
|
$position = $request->get('position'); |
97
|
|
|
|
98
|
|
|
if (null === $parentPath || null === $dropedAtPath || null === $targetPath) { |
99
|
|
|
return new JsonResponse(['Parameters parent, dropped and target has to be set to reorder.'], Response::HTTP_BAD_REQUEST); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (\in_array($position, ['over', 'child'], true)) { |
103
|
|
|
return new JsonResponse(['Can not reorder when dropping into a collection.'], Response::HTTP_BAD_REQUEST); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$before = 'before' === $position; |
107
|
|
|
$parentNode = $this->session->getNode($parentPath); |
108
|
|
|
$targetName = PathHelper::getNodeName($targetPath); |
109
|
|
|
if (!$before) { |
110
|
|
|
$nodesIterator = $parentNode->getNodes(); |
111
|
|
|
$nodesIterator->rewind(); |
112
|
|
|
while ($nodesIterator->valid()) { |
113
|
|
|
if ($nodesIterator->key() === $targetName) { |
114
|
|
|
break; |
115
|
|
|
} |
116
|
|
|
$nodesIterator->next(); |
117
|
|
|
} |
118
|
|
|
$targetName = null; |
119
|
|
|
if ($nodesIterator->valid()) { |
120
|
|
|
$nodesIterator->next(); |
121
|
|
|
if ($nodesIterator->valid()) { |
122
|
|
|
$targetName = $nodesIterator->key(); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
$parentNode->orderBefore($targetName, PathHelper::getNodeName($dropedAtPath)); |
127
|
|
|
$this->session->save(); |
128
|
|
|
|
129
|
|
|
return new Response('', Response::HTTP_NO_CONTENT); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..