bundles/App/Twig/Extension/UtilsExtension.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Twig\Extension;
  3. use Pimcore\Model\DataObject\Sponsor;
  4. use Pimcore\Model\Document;
  5. use Pimcore\Twig\Extension\Templating\Navigation;
  6. use Pimcore\Twig\Extension\Templating\Placeholder;
  7. use Twig\TwigFunction;
  8. class UtilsExtension extends \Twig\Extension\AbstractExtension
  9. {
  10. /**
  11. * @var Navigation
  12. */
  13. protected $navigationHelper;
  14. /**
  15. * @var Placeholder
  16. */
  17. protected $placeholderHelper;
  18. protected $breadcrumbActive = '';
  19. public function __construct(Navigation $navigationHelper, Placeholder $placeholderHelper)
  20. {
  21. $this->navigationHelper = $navigationHelper;
  22. $this->placeholderHelper = $placeholderHelper;
  23. }
  24. public function getFunctions()
  25. {
  26. return [
  27. new TwigFunction('json_decode', [$this, 'getJsonDecode']),
  28. new TwigFunction('md5_file', [$this, 'getMd5File']),
  29. new TwigFunction('app_navigation_breadcrumbs', [$this, 'buildBreadcrumbs']),
  30. new TwigFunction('renderImageClass', [$this, 'renderImageClass']),
  31. new TwigFunction('sponsorList', [$this, 'sponsorList']),
  32. ];
  33. }
  34. public function sponsorList($maxItems = 5)
  35. {
  36. $list = new Sponsor\Listing();
  37. $list->addConditionParam('active', true);
  38. if ($list->count() > $maxItems) {
  39. // 5 zufällige auswählen
  40. $items = [];
  41. while (count($items) < $maxItems) {
  42. foreach ($list as $sponsor) {
  43. if ((bool) random_int(0, 1)) {
  44. $items[$sponsor->getId()] = $sponsor;
  45. }
  46. }
  47. }
  48. return $items;
  49. }
  50. return $list;
  51. }
  52. public function renderImageClass(string $content)
  53. {
  54. $content = preg_replace('/(style=".*float:right.*")/', '$1 class="imageRight"', $content);
  55. $content = preg_replace('/(style=".*float:left.*")/', '$1 class="imageLeft"', $content);
  56. $content = preg_replace('/(src="fileadmin)/', 'src="/fileadmin', $content);
  57. return $content;
  58. }
  59. public function getJsonDecode($jsonString)
  60. {
  61. return json_decode($jsonString);
  62. }
  63. public function getMd5File($file)
  64. {
  65. if (file_exists(PIMCORE_WEB_ROOT . $file)) {
  66. return md5_file(PIMCORE_WEB_ROOT . $file);
  67. }
  68. return time();
  69. }
  70. public function buildBreadcrumbs(Document $document)
  71. {
  72. $breadCrumbs = [];
  73. $additionalBreadCrumbs = $this->placeholderHelper->__invoke('addBreadcrumb');
  74. if (count($additionalBreadCrumbs->getArrayCopy())) {
  75. $breadCrumbs = array_reverse($additionalBreadCrumbs->getArrayCopy());
  76. foreach ($breadCrumbs as $index => $breadcrumbItem) {
  77. $breadCrumbs[$index]['active'] = 0 === $index;
  78. }
  79. } else {
  80. $breadCrumbs[] = [
  81. 'id' => $document->getId(),
  82. 'url' => $document->getRealFullPath(),
  83. 'label' => $document->hasProperty('navigation_name') && '' !== trim($document->getProperty('navigation_name')) ? $document->getProperty('navigation_name') : $document->getTittle(),
  84. 'active' => true,
  85. ];
  86. }
  87. $parent = $document->getParent();
  88. while ($parent) {
  89. $breadCrumbs[] = [
  90. 'id' => $parent->getId(),
  91. 'url' => $parent->getRealFullPath(),
  92. 'label' => $parent->hasProperty('navigation_name') && '' !== trim($parent->getProperty('navigation_name')) ? $parent->getProperty('navigation_name') : $parent->getTitle(),
  93. 'active' => false,
  94. ];
  95. $parent = $parent->getParent();
  96. }
  97. $breadCrumbs = array_reverse($breadCrumbs);
  98. return $breadCrumbs;
  99. }
  100. }