1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Helper\Menu; |
4
|
|
|
|
5
|
|
|
use Kunstmaan\AdminBundle\Helper\Menu\MenuAdaptorInterface; |
6
|
|
|
use Kunstmaan\AdminBundle\Helper\Menu\MenuBuilder; |
7
|
|
|
use Kunstmaan\AdminBundle\Helper\Menu\MenuItem; |
8
|
|
|
use Kunstmaan\AdminBundle\Helper\Menu\TopMenuItem; |
9
|
|
|
use Kunstmaan\MediaBundle\Entity\Folder; |
10
|
|
|
use Kunstmaan\MediaBundle\Repository\FolderRepository; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The Media Menu Adaptor |
15
|
|
|
*/ |
16
|
|
|
class MediaMenuAdaptor implements MenuAdaptorInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var FolderRepository |
20
|
|
|
*/ |
21
|
|
|
private $repo; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param FolderRepository $repo |
25
|
|
|
*/ |
26
|
|
|
public function __construct($repo) |
27
|
|
|
{ |
28
|
|
|
$this->repo = $repo; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* In this method you can add children for a specific parent, but also remove and change the already created children |
33
|
|
|
* |
34
|
|
|
* @param MenuBuilder $menu The MenuBuilder |
35
|
|
|
* @param MenuItem[] &$children The current children |
36
|
|
|
* @param MenuItem $parent The parent Menu item |
37
|
|
|
* @param Request $request The Request |
38
|
|
|
*/ |
39
|
|
|
public function adaptChildren(MenuBuilder $menu, array &$children, MenuItem $parent = null, Request $request = null) |
40
|
|
|
{ |
41
|
|
|
if (\is_null($parent)) { |
42
|
|
|
// Add menu item for root gallery |
43
|
|
|
$rootFolders = $this->repo->getRootNodes(); |
44
|
|
|
$currentId = $request->get('folderId'); |
|
|
|
|
45
|
|
|
$currentFolder = null; |
46
|
|
|
if (isset($currentId)) { |
47
|
|
|
/* @var Folder $currentFolder */ |
48
|
|
|
$currentFolder = $this->repo->find($currentId); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @var Folder $rootFolder */ |
52
|
|
|
foreach ($rootFolders as $rootFolder) { |
53
|
|
|
$menuItem = new TopMenuItem($menu); |
54
|
|
|
$menuItem |
55
|
|
|
->setRoute('KunstmaanMediaBundle_folder_show') |
56
|
|
|
->setRouteparams(array('folderId' => $rootFolder->getId())) |
57
|
|
|
->setUniqueId('folder-' . $rootFolder->getId()) |
58
|
|
|
->setLabel($rootFolder->getName()) |
59
|
|
|
->setParent(null) |
60
|
|
|
->setRole($rootFolder->getRel()); |
61
|
|
|
|
62
|
|
|
if (!\is_null($currentFolder)) { |
63
|
|
|
$parentIds = $this->repo->getParentIds($currentFolder); |
64
|
|
|
if (\in_array($rootFolder->getId(), $parentIds)) { |
65
|
|
|
$menuItem->setActive(true); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
$children[] = $menuItem; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: