<?php
namespace App\Controller;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\SpecSheet;
use Pimcore\Model\DataObject\Products;
use Pimcore\Model\DataObject\Fieldcollection\Data\Measurements;
use Pimcore\Model\DataObject\Fieldcollection;
use Pimcore\Model\Element\Tag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Controller\ProductOptionsController;
class ProductDetailsController extends FrontendController
{
public function detailAction(Request $request)
{
$itm = $request->get('itm');
$lang = $request->get('lang');
if (empty($lang) || $lang == ' '):
$lang = 'en';
endif;
if ($itm != 'header'):
$entries = new DataObject\Products\Listing();
$entries->setObjectTypes([DataObject::OBJECT_TYPE_VARIANT,DataObject::OBJECT_TYPE_OBJECT]);
$entries->setCondition("item_nbr LIKE ?", [$itm]); // use prepared statements! Mysqli only supports ? placeholders
$entries->load();
$data = $entries->getData();
$productOptionsController = new ProductOptionsController();
if (!empty($data[0])):
$metaTitle = ($data[0]->getMeta_Title() == '') ? $data[0]->getTitle() : $data[0]->getMeta_Title();
$metaDescription = trim(strip_tags(($data[0]->getMeta_Description() == '') ? $data[0]->getDescription() : $data[0]->getMeta_Description()));
$uom = $data[0]->getUOM();
$upcCode = $data[0]->getUPC_CODE();
$prodLineName = $data[0]->getPROD_LINE_NAME();
$productDimentions = $data[0]->getProductMeasurements();
$productMeasurements = null;
if ($productDimentions):
$productMeasurementsItems = $productDimentions->getItems();
$productMeasurements = array(
'length' => $productMeasurementsItems[0]->getLength() .' ' .$productMeasurementsItems[0]->getLengthUnits(),
'height' => $productMeasurementsItems[0]->getHeight() .' ' .$productMeasurementsItems[0]->getHeightUnits(),
'width' => $productMeasurementsItems[0]->getWidth() .' ' .$productMeasurementsItems[0]->getWidthUnits(),
);
endif;
$productCollateral = $data[0]->getProductCollateral();
//print_r($productCollateral);
$collateral = array();
$pcindex = 0;
//echo '<pre>';
//print_r($productCollateral);
//echo '</pre>';
//die();
if ($productCollateral !== null):
foreach($productCollateral->getItems() as $pc):
$thisPc = $pc->getDocument();
$collateral[$pcindex]['title'] = $pc->getDocumentTitle();
$collateral[$pcindex]['path'] = 'https://pim.libertyav.io/'.$thisPc->getPath().$thisPc->getFilename();
$pcindex++;
endforeach;
endif;
// -- Get parent collateral
$parentCollateral = $this->getParentField($data[0]->getASSEMBLY_LINE(), 'ProductCollateral');
if ($parentCollateral !== null && $data[0]->getASSEMBLY_LINE() != $itm):
foreach($parentCollateral->getItems() as $pc):
$thisPc = $pc->getDocument();
$collateral[$pcindex]['title'] = $pc->getDocumentTitle();
$collateral[$pcindex]['path'] = 'https://pim.libertyav.io/'.$thisPc->getPath().$thisPc->getFilename();
$pcindex++;
endforeach;
endif;
$softwareObj = $data[0]->getSoftware();
//print_r($productCollateral);
$software = array();
$si = 0;
if ($softwareObj !== null):
foreach($softwareObj->getItems() as $soft):
$thisSoftware = $soft->getDocument();
$software[$si]['title'] = $soft->getDocumentTitle();
$software[$si]['path'] = 'https://pim.libertyav.io/'.$thisSoftware->getPath().$thisSoftware->getFilename();
$si++;
endforeach;
endif;
// -- Get parent software
$parentSoftware = $this->getParentField($data[0]->getASSEMBLY_LINE(), 'Software');
if ($parentSoftware !== null && $data[0]->getASSEMBLY_LINE() != $itm):
foreach($parentSoftware->getItems() as $soft):
$thisSoftware = $soft->getDocument();
$software[$si]['title'] = $s->getDocumentTitle();
$software[$si]['path'] = 'https://pim.libertyav.io/'.$thisSoftware->getPath().$thisSoftware->getFilename();
$si++;
endforeach;
endif;
$firmwareObj = $data[0]->getFirmware();
//print_r($productCollateral);
$firmware = array();
$fi = 0;
if ($firmwareObj !== null):
foreach($firmwareObj->getItems() as $firm):
$thisFirmware = $firm->getDocument();
$firmware[$fi]['title'] = $firm->getDocumentTitle();
$firmware[$fi]['path'] = 'https://pim.libertyav.io/'.$thisFirmware->getPath().$thisFirmware->getFilename();
$fi++;
endforeach;
endif;
// -- Get parent software
$parentFirmware = $this->getParentField($data[0]->getASSEMBLY_LINE(), 'Firmware');
if ($parentFirmware !== null && $data[0]->getASSEMBLY_LINE() != $itm):
foreach($parentSoftware->getItems() as $firm):
$thisFirmware = $firm->getDocument();
$firmware[$fi]['title'] = $s->getDocumentTitle();
$firmware[$fi]['path'] = 'https://pim.libertyav.io/'.$thisFirmware->getPath().$thisFirmware->getFilename();
$fi++;
endforeach;
endif;
$diagramsObj = $data[0]->getDiagram();
//print_r($productCollateral);
$diagrams = array();
$di = 0;
if ($diagramsObj !== null):
foreach($diagramsObj->getItems() as $dia):
$thisDiagram = $dia->getDocument();
$diagrams[$di]['title'] = $dia->getDocumentTitle();
$diagrams[$di]['path'] = 'https://pim.libertyav.io/'.$thisDiagram->getPath().$thisDiagram->getFilename();
$di++;
endforeach;
endif;
// -- Get parent software
$parentDiagram = $this->getParentField($data[0]->getASSEMBLY_LINE(), 'Diagram');
if ($parentDiagram !== null && $data[0]->getASSEMBLY_LINE() != $itm):
foreach($parentDiagram->getItems() as $dia):
$thisDiagram = $dia->getDocument();
$diagrams[$di]['title'] = $s->getDocumentTitle();
$diagrams[$di]['path'] = 'https://pim.libertyav.io/'.$thisDiagram->getPath().$thisDiagram->getFilename();
$di++;
endforeach;
endif;
$productImages = $data[0]->getProduct_Images();
$images = array();
$in = 0;
foreach($productImages as $img):
$thisImg = $img->getImage();
$images[$in] = 'https://pim.libertyav.io/'.$thisImg->getPath().$thisImg->getFilename();
$in++;
endforeach;
$tagsObj = Tag::getTagsForElement('object', $data[0]->geto_id());
if (empty($tagsObj)):
$tagsObj = Tag::getTagsForElement('object', $data[0]->getParent()->geto_id());
endif;
$tags = array();
foreach($tagsObj as $t):
if ($parent = $t->getparent()):
$tags[] = $parent->getname();
endif;
$tags[] = $t->getname();
endforeach;
$product = array(
'localization' => array(
'language' => $lang,
'title' => $data[0]->getTitle($lang),
'description' => $data[0]->getDescription($lang),
'featuresAndBenefits' => $data[0]->getFeaturesAndBenefits($lang),
'applications' => $data[0]->getApplications($lang),
),
'productInformation' => array(
'sku' => $data[0]->getITEM_NBR(),
'uom' => $uom,
'upcCode' => $upcCode,
'prodLineName' => $prodLineName,
'assemblyLine' => $data[0]->getASSEMBLY_LINE(),
'commodCode' => $data[0]->getCOMMOD_CODE(),
'productFamily' => $data[0]->getProductFamily(),
'weight' => $data[0]->getWeight(),
'productDimentions' => $productMeasurements,
),
'options' => array(
'color' => $data[0]->getCOLOR(),
'assemblyLength' => $data[0]->getASSEMBLY_LENGTH() .' '.$uom,
'PACKAGE' => $data[0]->getPACKAGE(),
),
'meta' => array(
'metaTitle' => $metaTitle,
'metaDescription' => $metaDescription,
),
'package-details' => array(
'dimensions' => array(
'length' => $data[0]->getShippingLength(),
'width' => $data[0]->getShippingWidth(),
'height' => $data[0]->getShippingHeight(),
),
'weight' => array(
'lbs' => ($data[0]->getShipping_Weight_lbs() != '') ? $data[0]->getShipping_Weight_lbs() : 0,
'oz' => ($data[0]->getShipping_Weight_oz() != '') ? $data[0]->getShipping_Weight_oz() : 0,
),
),
'images' => $images,
'OptionsAvailable' => $productOptionsController->getOptions($data[0]->getASSEMBLY_LINE(), 'en'),
'tags' => $tags,
'collateral' => $collateral,
'software' => $software,
'firmware' => $firmware,
'diagrams' => $diagrams,
//'ProductListing' => $this->getProductsFromFamily($data[0]->getASSEMBLY_LINE())
);
/* echo '<pre>';
print_r(Tag::getTagsForElement('object', $data[0]->geto_id()));
//print_r($entries);
echo '</pre>';
die(); */
else:
$product = array('error', 'No product found');
endif;
endif;
$response = new JsonResponse();
$response->setData(['productData' => $product]);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
public function containerAction(Request $request)
{
$itm = $request->get('itm');
$entries = new DataObject\Products\Listing();
$entries->setCondition("item_nbr LIKE ?", [$itm]); // use prepared statements! Mysqli only supports ? placeholders
$entries->load();
$data = $entries->getData();
/* echo '<pre>';
print_r($data[0]->getITEM_NBR());
print_r($data[0]->getSpecSheet());
echo '</pre>';
die(); */
$SpecSheetData = $data[0]->getSpecSheet();
$SchematicDrawing = $SpecSheetData[0]->getSchematicDrawing();
$SpecSheet = array(
'Title' => $SpecSheetData[0]->getTitle(),
'Subtitle' => $SpecSheetData[0]->getSubtitle(),
'Description' => $SpecSheetData[0]->getDescription(),
'SchematicDrawing' => array( 'filename' => $SchematicDrawing->getFilename(), 'customSettings' => $SchematicDrawing->getCustomSettings(), 'path' => $SchematicDrawing->getPath()),
'TechnicalSpecifications' => $SpecSheetData[0]->getSpecificationGroup(),
'Footer' => $SpecSheetData[0]->getFooter(),
'Brand' => $SpecSheetData[0]->getBrand()
);
/* echo '<pre>';
print_r($SpecSheet);
echo '</pre>';
die(); */
// ...
//return $this->render('documents/specsheet.html.twig', $SpecSheet);
$html = $this->renderView('documents/specsheet.html.twig', $SpecSheet);
$adapter = \Pimcore\Web2Print\Processor::getInstance();
$params = array();
//add custom settings if necessary
if ($adapter instanceof \Pimcore\Web2Print\Processor\HeadlessChrome) {
$params['adapterConfig'] = [
'landscape' => true,
'printBackground' => true,
'format' => 'A4',
'margin' => [
'top' => '36 mm',
'bottom' => '30 mm',
'right' => '8 mm',
'left' => '8 mm',
],
'displayHeaderFooter' => false,
];
} elseif($adapter instanceof \Pimcore\Web2Print\Processor\PdfReactor) {
//Config settings -> http://www.pdfreactor.com/product/doc/webservice/php.html#Configuration
$params['adapterConfig'] = [
'author' => 'Max Mustermann',
'title' => 'Custom Title',
'javaScriptMode' => 0,
'addLinks' => true,
'appendLog' => true,
'enableDebugMode' => true
];
}
/* echo '<pre>';
print_r($adapter);
echo '</pre>';
die(); */
return new \Symfony\Component\HttpFoundation\Response(
$adapter->getPdfFromString($html, $params),
200,
array(
'Content-Type' => 'application/pdf',
// 'Content-Disposition' => 'attachment; filename="custom-pdf.pdf"' //direct download
)
);
}
private function getParentField($itm, $field) {
$entries = new DataObject\Products\Listing();
$entries->setObjectTypes([DataObject::OBJECT_TYPE_VARIANT,DataObject::OBJECT_TYPE_OBJECT]);
$entries->setCondition("item_nbr LIKE ?", [$itm]); // use prepared statements! Mysqli only supports ? placeholders
$entries->load();
$data = $entries->getData();
return $data[0]->{'get'.$field}();
}
}