<?php
/**
* Active Publishing - All right reserved
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Active Publishing (https://activepublishing.fr)
* @license Creative Common CC BY ND 4.0
* @author Active Publishing <contact@active-publishing.fr>
*/
namespace ActiveWireframeBundle\Listener;
use ActivePublishing\Tool\ApTool;
use ActiveWireframeBundle\ActiveWireframeBundle;
use Pimcore\Event\Model\DocumentEvent;
use Pimcore\Config;
use Pimcore\Model\DataObject;
use Pimcore\Model\Document;
use Pimcore\Web2Print\Processor\WkHtmlToPdf;
use ActiveWireframeBundle\ActiveWireframeBundle as Bundle;
use ActiveWireframeBundle\Handler\Document as HandlerDocument;
use ActiveWireframeBundle\Handler\Web2Print as HandlerW2p;
/**
* Class DocumentListener
* @package ActiveWireframeBundle\Listener
*/
class DocumentListener
{
/**
* @var HandlerDocument|null
*/
protected $_documentHandler = null;
/**
* @var HandlerW2p|null
*/
protected $_web2PrintHandler = null;
/**
* DocumentListener constructor.
* @param $DocHandler
* @param $web2PrintHandler
*/
public function __construct(HandlerDocument $DocHandler, HandlerW2p $web2PrintHandler)
{
$this->_documentHandler = $DocHandler;
$this->_web2PrintHandler = $web2PrintHandler;
}
/**
* @param DocumentEvent $oEvent
* @throws \Exception
*/
public function postAdd(DocumentEvent $oEvent)
{
$document = $oEvent->getDocument();
if ($document instanceof Document\Printpage && ($document->getModule() == Bundle::BUNDLE_ID)) {
$this->_documentHandler->addWireframePageData($document);
$document->save();
}
}
/**
* @param DocumentEvent $oEvent
*/
public function postCopy(DocumentEvent $oEvent)
{
$document = $oEvent->getDocument();
$aArg = $oEvent->getArguments();
$baseDocument = $aArg["base_element"];
if (($document instanceof Document\Printcontainer || $document instanceof Document\Printpage)
&& ($baseDocument instanceof Document\Printcontainer || $baseDocument instanceof Document\Printpage)
&& $document->getModule() == Bundle::BUNDLE_ID
) {
$this->_documentHandler->addByCopy($document, $baseDocument->getId());
}
}
/**
* @param DocumentEvent $e
* @throws \Exception
*/
public function preUpdate (DocumentEvent $e)
{
$document = $e->getDocument();
$namespacePage = [
"@AP\ActiveWireframeBundle\Controller\PagesController",
"@ActiveWireframeBundle\Controller\PagesController",
"Pages"
];
// Initialisation Area Nomenclature
if (($document instanceof Document\Printpage)
&& (in_array($document->getController(), $namespacePage))
) {
if ($document->getProperty('w2p_nomenclature'))
{
$nomenObject = $document->getProperty('w2p_nomenclature_object');
$areaName = $document->getProperty('w2p_nomenclature_area_id');
if ($nomenObject instanceof DataObject\Concrete)
{
$elements = $document->getElements();
if (!empty($elements))
{
$areaNomenExist = false;
// Check if Areablock or "Nomenclature" area exist
foreach ($elements as $elementId => $elementObj)
{
if ($areaNomenExist === FALSE)
$areaNomenExist = strpos($elementId, $areaName);
}
// Area Nomenclature not exist
if ($areaNomenExist === FALSE)
{
$conf = [
'area_id' => $areaName,
'o_id' => $nomenObject->getId()
];
// Create Tag
$blocArea = $document->getElement('AwfAreablock');
$indices = $blocArea->getValue();
$indiceKey = (string)(count($indices) + 1);
$indices[] = ['key' => $indiceKey, 'type' => $conf['area_id']];
$blocArea->setDataFromEditmode($indices);
$areaName = trim($conf['area_id']);
$keyElement = "AwfAreablock:" . $indiceKey . "." . $areaName;
$renderlet = new Document\Tag\Renderlet();
$renderlet->setName($keyElement);
$renderlet->setDataFromEditmode(
['id' => $conf['o_id'], 'type' => 'object', 'subtype' => 'object']
);
$renderlet->setDocumentId($document->getId());
try {
$document->setElement($keyElement, $renderlet);
} catch (\Exception $ex) {
ApTool::log(ActiveWireframeBundle::BUNDLE_ID, $ex->getMessage());
}
}
}
}
}
}
}
/**
* @param DocumentEvent $oEvent
*/
public function postUpdate(DocumentEvent $oEvent)
{
$document = $oEvent->getDocument();
if ($document instanceof Document\Printcontainer && ($document->getModule() == Bundle::BUNDLE_ID)) {
$this->_documentHandler->updatePrintcontainer($document);
} elseif ($document instanceof Document\Printpage && ($document->getModule() == Bundle::BUNDLE_ID)) {
$this->_documentHandler->updatePrintpage($document);
}
}
/**
* @param DocumentEvent $oEvent
*/
public function postDelete(DocumentEvent $oEvent)
{
$document = $oEvent->getDocument();
if ($document instanceof Document\Printcontainer && ($document->getModule() == Bundle::BUNDLE_ID)) {
$this->_documentHandler->deletePrintcontainer($document);
} elseif ($document instanceof Document\Printpage && ($document->getModule() == Bundle::BUNDLE_ID)) {
$this->_documentHandler->deletePrintpage($document);
}
}
/**
* @param DocumentEvent $oEvent
*/
public function prePdfGeneration(DocumentEvent $oEvent)
{
$document = $oEvent->getDocument();
$config = Config::getWeb2PrintConfig();
$aArg = $oEvent->getArguments();
if ($document instanceof Document\Printpage && ($document->getModule() == Bundle::BUNDLE_ID)) {
if ($config->generalTool == "wkhtmltopdf") {
$options = $this->_web2PrintHandler->getOptionsCatalog($document->getId());
$processor = $aArg["processor"];
if ($processor instanceof WkHtmlToPdf) {
$processor->setOptions($options);
}
}
}
}
/**
* @param DocumentEvent $oEvent
*
* @throws \Exception
*/
public function postPdfGeneration(DocumentEvent $oEvent)
{
$document = $oEvent->getDocument();
if ($document->getModule() == Bundle::BUNDLE_ID) {
$config = Config::getWeb2PrintConfig();
if ($document instanceof Document\Printcontainer) {
switch ($config->generalTool){
case "wkhtmltopdf": $this->_web2PrintHandler->generatePdfContainerForWk($document);
break;
case "pdfreactor": $this->_web2PrintHandler->generatePdfContainerForReactor($document, $config);
break;
default: break;
}
}
$this->_web2PrintHandler->buildPdfAdvanced($document);
}
}
}