BreadcrumbsBuilder.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Admin;
  11. use Knp\Menu\ItemInterface;
  12. /**
  13. * Stateless breadcrumbs builder (each method needs an Admin object).
  14. *
  15. * @author Grégoire Paris <postmaster@greg0ire.fr>
  16. */
  17. final class BreadcrumbsBuilder implements BreadcrumbsBuilderInterface
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function getBreadcrumbs(AdminInterface $admin, $action)
  23. {
  24. $breadcrumbs = array();
  25. if ($admin->isChild()) {
  26. return $this->getBreadcrumbs($admin->getParent(), $action);
  27. }
  28. $menu = $this->buildBreadcrumbs($admin, $action);
  29. do {
  30. $breadcrumbs[] = $menu;
  31. } while ($menu = $menu->getParent());
  32. $breadcrumbs = array_reverse($breadcrumbs);
  33. array_shift($breadcrumbs);
  34. return $breadcrumbs;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. * NEXT_MAJOR : make this method private.
  39. */
  40. public function buildBreadcrumbs(AdminInterface $admin, $action, ItemInterface $menu = null)
  41. {
  42. if (!$menu) {
  43. $menu = $admin->getMenuFactory()->createItem('root');
  44. $menu = $this->createMenuItem(
  45. $admin,
  46. $menu,
  47. 'dashboard',
  48. 'SonataAdminBundle',
  49. array('uri' => $admin->getRouteGenerator()->generate(
  50. 'sonata_admin_dashboard'
  51. ))
  52. );
  53. }
  54. $menu = $this->createMenuItem(
  55. $admin,
  56. $menu,
  57. sprintf('%s_list', $admin->getClassnameLabel()),
  58. null,
  59. array(
  60. 'uri' => $admin->hasRoute('list') && $admin->isGranted('LIST') ?
  61. $admin->generateUrl('list') :
  62. null,
  63. )
  64. );
  65. $childAdmin = $admin->getCurrentChildAdmin();
  66. if ($childAdmin) {
  67. $id = $admin->getRequest()->get($admin->getIdParameter());
  68. $menu = $menu->addChild(
  69. $admin->toString($admin->getSubject()),
  70. array(
  71. 'uri' => $admin->hasRoute('edit') && $admin->isGranted('EDIT') ?
  72. $admin->generateUrl('edit', array('id' => $id)) :
  73. null,
  74. )
  75. );
  76. return $this->buildBreadcrumbs($childAdmin, $action, $menu);
  77. }
  78. if ('list' === $action && $admin->isChild()) {
  79. $menu->setUri(false);
  80. } elseif ('create' !== $action && $admin->hasSubject()) {
  81. $menu = $menu->addChild($admin->toString($admin->getSubject()));
  82. } else {
  83. $menu = $this->createMenuItem(
  84. $admin,
  85. $menu,
  86. sprintf('%s_%s', $admin->getClassnameLabel(), $action)
  87. );
  88. }
  89. return $menu;
  90. }
  91. /**
  92. * Creates a new menu item from a simple name. The name is normalized and
  93. * translated with the specified translation domain.
  94. *
  95. * @param AdminInterface $admin used for translation
  96. * @param ItemInterface $menu will be modified and returned
  97. * @param string $name the source of the final label
  98. * @param string $translationDomain for label translation
  99. * @param array $options menu item options
  100. *
  101. * @return ItemInterface
  102. */
  103. private function createMenuItem(
  104. AdminInterface $admin,
  105. ItemInterface $menu,
  106. $name,
  107. $translationDomain = null,
  108. $options = array()
  109. ) {
  110. return $menu->addChild(
  111. $admin->trans(
  112. $admin->getLabelTranslatorStrategy()->getLabel(
  113. $name,
  114. 'breadcrumb',
  115. 'link'
  116. ),
  117. array(),
  118. $translationDomain
  119. ),
  120. $options
  121. );
  122. }
  123. }