src/ActiveWireframeBundle/Listener/DocumentListener.php line 205

Open in your IDE?
  1. <?php
  2. /**
  3.  * Active Publishing - All right reserved
  4.  *   Full copyright and license information is available in
  5.  *   LICENSE.md which is distributed with this source code.
  6.  *
  7.  * @copyright Copyright (c) Active Publishing (https://activepublishing.fr)
  8.  * @license Creative Common CC BY ND 4.0
  9.  * @author Active Publishing <contact@active-publishing.fr>
  10.  */
  11. namespace ActiveWireframeBundle\Listener;
  12. use ActivePublishing\Tool\ApTool;
  13. use ActiveWireframeBundle\ActiveWireframeBundle;
  14. use Pimcore\Event\Model\DocumentEvent;
  15. use Pimcore\Config;
  16. use Pimcore\Model\DataObject;
  17. use Pimcore\Model\Document;
  18. use Pimcore\Web2Print\Processor\WkHtmlToPdf;
  19. use ActiveWireframeBundle\ActiveWireframeBundle as Bundle;
  20. use ActiveWireframeBundle\Handler\Document as HandlerDocument;
  21. use ActiveWireframeBundle\Handler\Web2Print as HandlerW2p;
  22. /**
  23.  * Class DocumentListener
  24.  * @package ActiveWireframeBundle\Listener
  25.  */
  26. class DocumentListener
  27. {
  28.     /**
  29.      * @var HandlerDocument|null
  30.      */
  31.     protected $_documentHandler null;
  32.     /**
  33.      * @var HandlerW2p|null
  34.      */
  35.     protected $_web2PrintHandler null;
  36.     /**
  37.      * DocumentListener constructor.
  38.      * @param $DocHandler
  39.      * @param $web2PrintHandler
  40.      */
  41.     public function __construct(HandlerDocument $DocHandlerHandlerW2p $web2PrintHandler)
  42.     {
  43.         $this->_documentHandler  $DocHandler;
  44.         $this->_web2PrintHandler $web2PrintHandler;
  45.     }
  46.     /**
  47.      * @param DocumentEvent $oEvent
  48.      * @throws \Exception
  49.      */
  50.     public function postAdd(DocumentEvent $oEvent)
  51.     {
  52.         $document $oEvent->getDocument();
  53.         if ($document instanceof Document\Printpage && ($document->getModule() == Bundle::BUNDLE_ID)) {
  54.             $this->_documentHandler->addWireframePageData($document);
  55.             $document->save();
  56.         }
  57.     }
  58.     /**
  59.      * @param DocumentEvent $oEvent
  60.      */
  61.     public function postCopy(DocumentEvent $oEvent)
  62.     {
  63.         $document $oEvent->getDocument();
  64.         $aArg $oEvent->getArguments();
  65.         $baseDocument $aArg["base_element"];
  66.         if (($document instanceof Document\Printcontainer || $document instanceof Document\Printpage)
  67.             && ($baseDocument instanceof Document\Printcontainer || $baseDocument instanceof Document\Printpage)
  68.             && $document->getModule() == Bundle::BUNDLE_ID
  69.         ) {
  70.             $this->_documentHandler->addByCopy($document$baseDocument->getId());
  71.         }
  72.     }
  73.     /**
  74.      * @param DocumentEvent $e
  75.      * @throws \Exception
  76.      */
  77.     public function preUpdate (DocumentEvent $e)
  78.     {
  79.         $document $e->getDocument();
  80.         $namespacePage = [
  81.             "@AP\ActiveWireframeBundle\Controller\PagesController",
  82.             "@ActiveWireframeBundle\Controller\PagesController",
  83.             "Pages"
  84.         ];
  85.         // Initialisation Area Nomenclature
  86.         if (($document instanceof Document\Printpage)
  87.             && (in_array($document->getController(), $namespacePage))
  88.         ) {
  89.             if ($document->getProperty('w2p_nomenclature'))
  90.             {
  91.                 $nomenObject $document->getProperty('w2p_nomenclature_object');
  92.                 $areaName $document->getProperty('w2p_nomenclature_area_id');
  93.                 if ($nomenObject instanceof DataObject\Concrete)
  94.                 {
  95.                     $elements $document->getElements();
  96.                     if (!empty($elements))
  97.                     {
  98.                         $areaNomenExist false;
  99.                         // Check if Areablock or "Nomenclature" area exist
  100.                         foreach ($elements as $elementId => $elementObj)
  101.                         {
  102.                             if ($areaNomenExist === FALSE)
  103.                                 $areaNomenExist strpos($elementId$areaName);
  104.                         }
  105.                         // Area Nomenclature not exist
  106.                         if ($areaNomenExist === FALSE)
  107.                         {
  108.                             $conf = [
  109.                                 'area_id' => $areaName,
  110.                                 'o_id' => $nomenObject->getId()
  111.                             ];
  112.                             // Create Tag
  113.                             $blocArea $document->getElement('AwfAreablock');
  114.                             $indices $blocArea->getValue();
  115.                             $indiceKey = (string)(count($indices) + 1);
  116.                             $indices[] = ['key' => $indiceKey'type' => $conf['area_id']];
  117.                             $blocArea->setDataFromEditmode($indices);
  118.                             $areaName trim($conf['area_id']);
  119.                             $keyElement "AwfAreablock:" $indiceKey "." $areaName;
  120.                             $renderlet = new Document\Tag\Renderlet();
  121.                             $renderlet->setName($keyElement);
  122.                             $renderlet->setDataFromEditmode(
  123.                                 ['id' => $conf['o_id'], 'type' => 'object''subtype' => 'object']
  124.                             );
  125.                             $renderlet->setDocumentId($document->getId());
  126.                             try {
  127.                                 $document->setElement($keyElement$renderlet);
  128.                             } catch (\Exception $ex) {
  129.                                 ApTool::log(ActiveWireframeBundle::BUNDLE_ID$ex->getMessage());
  130.                             }
  131.                         }
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.     }
  137.     /**
  138.      * @param DocumentEvent $oEvent
  139.      */
  140.     public function postUpdate(DocumentEvent $oEvent)
  141.     {
  142.         $document $oEvent->getDocument();
  143.         if ($document instanceof Document\Printcontainer && ($document->getModule() == Bundle::BUNDLE_ID)) {
  144.             $this->_documentHandler->updatePrintcontainer($document);
  145.         } elseif ($document instanceof Document\Printpage && ($document->getModule() == Bundle::BUNDLE_ID)) {
  146.             $this->_documentHandler->updatePrintpage($document);
  147.         }
  148.     }
  149.     /**
  150.      * @param DocumentEvent $oEvent
  151.      */
  152.     public function postDelete(DocumentEvent $oEvent)
  153.     {
  154.         $document $oEvent->getDocument();
  155.         if ($document instanceof Document\Printcontainer && ($document->getModule() == Bundle::BUNDLE_ID)) {
  156.             $this->_documentHandler->deletePrintcontainer($document);
  157.         } elseif ($document instanceof Document\Printpage && ($document->getModule() == Bundle::BUNDLE_ID)) {
  158.             $this->_documentHandler->deletePrintpage($document);
  159.         }
  160.     }
  161.     /**
  162.      * @param DocumentEvent $oEvent
  163.      */
  164.     public function prePdfGeneration(DocumentEvent $oEvent)
  165.     {
  166.         $document $oEvent->getDocument();
  167.         $config Config::getWeb2PrintConfig();
  168.         $aArg $oEvent->getArguments();
  169.         if ($document instanceof Document\Printpage && ($document->getModule() == Bundle::BUNDLE_ID)) {
  170.             if ($config->generalTool == "wkhtmltopdf") {
  171.                 $options $this->_web2PrintHandler->getOptionsCatalog($document->getId());
  172.                 $processor $aArg["processor"];
  173.                 if ($processor instanceof WkHtmlToPdf) {
  174.                     $processor->setOptions($options);
  175.                 }
  176.             }
  177.         }
  178.     }
  179.     /**
  180.      * @param DocumentEvent $oEvent
  181.      *
  182.      * @throws \Exception
  183.      */
  184.     public function postPdfGeneration(DocumentEvent $oEvent)
  185.     {
  186.         $document $oEvent->getDocument();
  187.         if ($document->getModule() == Bundle::BUNDLE_ID) {
  188.             $config Config::getWeb2PrintConfig();
  189.             if ($document instanceof Document\Printcontainer) {
  190.                 switch ($config->generalTool){
  191.                     case "wkhtmltopdf"$this->_web2PrintHandler->generatePdfContainerForWk($document);
  192.                         break;
  193.                     case "pdfreactor"$this->_web2PrintHandler->generatePdfContainerForReactor($document$config);
  194.                         break;
  195.                     default: break;
  196.                 }
  197.             }
  198.             $this->_web2PrintHandler->buildPdfAdvanced($document);
  199.         }
  200.     }
  201. }