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\Controller; |
13
|
|
|
|
14
|
|
|
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry; |
15
|
|
|
use PHPCR\Util\PathHelper; |
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
17
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A controller to render the tree block. |
23
|
|
|
*/ |
24
|
|
|
class TreeController extends Controller |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $repositoryName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $template = 'SonataDoctrinePHPCRAdminBundle:Tree:tree.html.twig'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $defaults; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private $confirmMove = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var \PHPCR\SessionInterface |
48
|
|
|
*/ |
49
|
|
|
private $session; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ManagerRegistry $manager |
53
|
|
|
* @param string $sessionName |
54
|
|
|
* @param string $repositoryName |
55
|
|
|
* @param string $template |
56
|
|
|
* @param array $defaults |
57
|
|
|
* @param bool $confirmMove |
58
|
|
|
*/ |
59
|
|
|
public function __construct( |
60
|
|
|
ManagerRegistry $manager, |
61
|
|
|
$sessionName, |
62
|
|
|
$repositoryName = 'default', |
63
|
|
|
$template = null, |
64
|
|
|
array $defaults = array(), |
65
|
|
|
$confirmMove = false |
66
|
|
|
) { |
67
|
|
|
$this->repositoryName = $repositoryName; |
68
|
|
|
if ($template) { |
|
|
|
|
69
|
|
|
$this->template = $template; |
70
|
|
|
} |
71
|
|
|
$this->defaults = $defaults; |
72
|
|
|
|
73
|
|
|
$this->confirmMove = $confirmMove; |
74
|
|
|
|
75
|
|
|
$this->session = $manager->getConnection($sessionName); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Renders a tree, passing the routes for each of the admin types (document types) |
80
|
|
|
* to the view. |
81
|
|
|
* |
82
|
|
|
* @param Request $request |
83
|
|
|
* |
84
|
|
|
* @return Response |
85
|
|
|
*/ |
86
|
|
|
public function treeAction(Request $request) |
87
|
|
|
{ |
88
|
|
|
$createInOverlay = $request->attributes->get('create_in_overlay'); |
|
|
|
|
89
|
|
|
$editInOverlay = $request->attributes->get('edit_in_overlay'); |
|
|
|
|
90
|
|
|
$deleteInOverlay = $request->attributes->get('delete_in_overlay'); |
|
|
|
|
91
|
|
|
|
92
|
|
|
$root = $request->attributes->get('root'); |
93
|
|
|
$selected = $request->attributes->get('selected') ?: $root; |
|
|
|
|
94
|
|
|
|
95
|
|
|
return $this->render($this->template, array( |
96
|
|
|
'repository_name' => $this->repositoryName, |
97
|
|
|
'root_node' => $root, |
98
|
|
|
'routing_defaults' => $this->defaults, |
99
|
|
|
//'confirm_move' => $this->confirmMove, |
100
|
|
|
//'create_in_overlay' => $createInOverlay ? $createInOverlay : false, |
101
|
|
|
//'edit_in_overlay' => $editInOverlay ? $editInOverlay : false, |
102
|
|
|
//'delete_in_overlay' => $deleteInOverlay ? $deleteInOverlay : false, |
103
|
|
|
)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Reorder $moved (child of $parent) before or after $target. |
108
|
|
|
* |
109
|
|
|
* @param Request $request |
110
|
|
|
* |
111
|
|
|
* @return Response |
112
|
|
|
*/ |
113
|
|
|
public function reorderAction(Request $request) |
114
|
|
|
{ |
115
|
|
|
$parent = $request->get('parent'); |
116
|
|
|
$moved = $request->get('dropped'); |
117
|
|
|
$target = $request->get('target'); |
118
|
|
|
$position = $request->get('position'); |
119
|
|
|
|
120
|
|
|
if (null === $parent || null === $moved || null === $target) { |
121
|
|
|
return new JsonResponse(['Parameters parent, dropped and target has to be set to reorder.'], Response::HTTP_BAD_REQUEST); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$before = 'before' == $position; |
125
|
|
|
$parentNode = $this->session->getNode($parent); |
126
|
|
|
$targetName = PathHelper::getNodeName($target); |
127
|
|
|
if (!$before) { |
128
|
|
|
$nodesIterator = $parentNode->getNodes(); |
129
|
|
|
$nodesIterator->rewind(); |
130
|
|
|
while ($nodesIterator->valid()) { |
131
|
|
|
if ($nodesIterator->key() == $targetName) { |
132
|
|
|
break; |
133
|
|
|
} |
134
|
|
|
$nodesIterator->next(); |
135
|
|
|
} |
136
|
|
|
$targetName = null; |
137
|
|
|
if ($nodesIterator->valid()) { |
138
|
|
|
$nodesIterator->next(); |
139
|
|
|
if ($nodesIterator->valid()) { |
140
|
|
|
$targetName = $nodesIterator->key(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
$parentNode->orderBefore(PathHelper::getNodeName($moved), $targetName); |
145
|
|
|
$this->session->save(); |
146
|
|
|
|
147
|
|
|
return new Response('', Response::HTTP_NO_CONTENT); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: