src/EventSubscriber/Content/ContentInTextTocSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Content;
  3. use App\Entity\Content\Content;
  4. use App\Entity\Content\Text;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use App\Event\Content\ContentDisplayEvent;
  7. use App\Entity\Menu\Item;
  8. use App\Service\Gatherer;
  9. use App\Text\Aliasifer;
  10. /**
  11.  * @author Przemysław Chrupek <przemyslaw.chrupek@avt.pl>
  12.  */
  13. class ContentInTextTocSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var Content
  17.      */
  18.     private $content;
  19.     /**
  20.      * @var Item
  21.      */
  22.     private $menuItem;
  23.     /**
  24.      * @var Gatherer
  25.      */
  26.     protected $gatherer;
  27.     /**
  28.      * @var Text
  29.      */
  30.     private $text;
  31.     private $breakpoint;
  32.     public function __construct(Gatherer $gatherer)
  33.     {
  34.         $this->gatherer $gatherer;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             ContentDisplayEvent::NAME => [
  40.                 ['preContentDisplay'102],
  41.             ]
  42.         ];
  43.     }
  44.     public function preContentDisplay(ContentDisplayEvent $event)
  45.     {
  46.         $this->content $event->getContent();
  47.         $this->text $this->content->getText();
  48.         $this->menuItem $this->content->getMenuItem();
  49.         if (!$this->text instanceof Text || !$this->menuItem instanceof Item)
  50.             return;
  51.         $n $this->regexPagebreak($this->text->getValue());
  52.         if (empty($this->breakpoint))
  53.             return;
  54.         $this->gatherer->add('contentToc'$this->breakpoint);
  55.     }
  56.     protected function regexPagebreak()
  57.     {
  58.         $n 1;
  59.         $regex "/\<pre.*?\>\{PAGEBREAK\|(.*)}.*?\<\/pre>/";
  60.         $this->text->setValue(preg_replace_callback($regex, [$this'prepareBreakpoint'], $this->text->getValue()));
  61.         if (!empty($this->breakpoint)) {
  62.             foreach ($this->breakpoint as $value) {
  63.                 if ($value['newPage'] == true) {
  64.                     $n++;
  65.                 }
  66.             }
  67.         }
  68.         return $n;
  69.     }
  70.     protected function prepareBreakpoint($matches)
  71.     {
  72.         $data explode('|'$matches[1]);
  73.         $alias Aliasifer::aliasify(html_entity_decode($data[0]));
  74.         $this->breakpoint[$alias]['title'] = html_entity_decode($data[0]);
  75.         $this->breakpoint[$alias]['alias'] = $alias;
  76.         $this->breakpoint[$alias]['newPage'] = isset($data[1]) ? true false;
  77.         $newPage = isset($data[1]) ? ' toc-breakpoint-newpage' '';
  78.         return '<div id="'.$alias.'" class="toc-breakpoint'$newPage .'"></div>';
  79.     }
  80. }