src/Controller/ProductDetailsController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Pimcore\Model\DataObject;
  5. use Pimcore\Model\DataObject\SpecSheet;
  6. use Pimcore\Model\DataObject\Products;
  7. use Pimcore\Model\DataObject\Fieldcollection\Data\Measurements;
  8. use Pimcore\Model\DataObject\Fieldcollection;
  9. use Pimcore\Model\Element\Tag;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use App\Controller\ProductOptionsController;
  13. class ProductDetailsController extends FrontendController
  14. {
  15.     public function detailAction(Request $request)
  16.     {
  17.         $itm $request->get('itm');
  18.         $lang $request->get('lang');
  19.         if (empty($lang) || $lang == ' '):
  20.             $lang 'en';
  21.         endif;
  22.         if ($itm != 'header'):
  23.             $entries = new DataObject\Products\Listing();
  24.             $entries->setObjectTypes([DataObject::OBJECT_TYPE_VARIANT,DataObject::OBJECT_TYPE_OBJECT]);
  25.             $entries->setCondition("item_nbr LIKE ?", [$itm]); // use prepared statements! Mysqli only supports ? placeholders
  26.             $entries->load();
  27.             $data $entries->getData();
  28.             $productOptionsController = new ProductOptionsController();
  29.             if (!empty($data[0])):
  30.                 $metaTitle = ($data[0]->getMeta_Title() == '') ? $data[0]->getTitle() : $data[0]->getMeta_Title();
  31.                 $metaDescription trim(strip_tags(($data[0]->getMeta_Description() == '') ? $data[0]->getDescription() : $data[0]->getMeta_Description()));
  32.                 $uom $data[0]->getUOM();
  33.                 $upcCode $data[0]->getUPC_CODE();
  34.                 $prodLineName $data[0]->getPROD_LINE_NAME();
  35.                 $productDimentions $data[0]->getProductMeasurements();
  36.                 $productMeasurements null;
  37.                 if ($productDimentions):
  38.                     $productMeasurementsItems $productDimentions->getItems();
  39.                     $productMeasurements = array(
  40.                         'length' => $productMeasurementsItems[0]->getLength() .' ' .$productMeasurementsItems[0]->getLengthUnits(),
  41.                         'height' => $productMeasurementsItems[0]->getHeight() .' ' .$productMeasurementsItems[0]->getHeightUnits(),
  42.                         'width' => $productMeasurementsItems[0]->getWidth() .' ' .$productMeasurementsItems[0]->getWidthUnits(),
  43.                     );
  44.                 endif;
  45.                 $productCollateral $data[0]->getProductCollateral();
  46.                 //print_r($productCollateral);
  47.                 $collateral = array();
  48.                 $pcindex 0;
  49.                 //echo '<pre>';
  50.                 //print_r($productCollateral);
  51.                 //echo '</pre>';
  52.                 //die();
  53.                 if ($productCollateral !== null):
  54.                     foreach($productCollateral->getItems() as $pc):
  55.                         $thisPc $pc->getDocument();
  56.                         $collateral[$pcindex]['title'] = $pc->getDocumentTitle();
  57.                         $collateral[$pcindex]['path'] = 'https://pim.libertyav.io/'.$thisPc->getPath().$thisPc->getFilename();
  58.                         $pcindex++;
  59.                     endforeach;
  60.                 endif;
  61.                 // -- Get parent collateral
  62.                 $parentCollateral $this->getParentField($data[0]->getASSEMBLY_LINE(), 'ProductCollateral');
  63.                 if ($parentCollateral !== null && $data[0]->getASSEMBLY_LINE() != $itm):
  64.                     foreach($parentCollateral->getItems() as $pc):
  65.                         $thisPc $pc->getDocument();
  66.                         $collateral[$pcindex]['title'] = $pc->getDocumentTitle();
  67.                         $collateral[$pcindex]['path'] = 'https://pim.libertyav.io/'.$thisPc->getPath().$thisPc->getFilename();
  68.                         $pcindex++;
  69.                     endforeach;
  70.                 endif;
  71.                 
  72.                 $softwareObj $data[0]->getSoftware();
  73.                 //print_r($productCollateral);
  74.                 $software = array();
  75.                 $si 0;
  76.                 if ($softwareObj !== null):
  77.                     foreach($softwareObj->getItems() as $soft):
  78.                         $thisSoftware $soft->getDocument();
  79.                         $software[$si]['title'] = $soft->getDocumentTitle();
  80.                         $software[$si]['path'] = 'https://pim.libertyav.io/'.$thisSoftware->getPath().$thisSoftware->getFilename();
  81.                         $si++;
  82.                     endforeach;
  83.                 endif;
  84.                 // -- Get parent software
  85.                 $parentSoftware $this->getParentField($data[0]->getASSEMBLY_LINE(), 'Software');
  86.                 if ($parentSoftware !== null && $data[0]->getASSEMBLY_LINE() != $itm):
  87.                     foreach($parentSoftware->getItems() as $soft):
  88.                         $thisSoftware $soft->getDocument();
  89.                         $software[$si]['title'] = $s->getDocumentTitle();
  90.                         $software[$si]['path'] = 'https://pim.libertyav.io/'.$thisSoftware->getPath().$thisSoftware->getFilename();
  91.                         $si++;
  92.                     endforeach;
  93.                 endif;
  94.                 $firmwareObj $data[0]->getFirmware();
  95.                 //print_r($productCollateral);
  96.                 $firmware = array();
  97.                 $fi 0;
  98.                 if ($firmwareObj !== null):
  99.                     foreach($firmwareObj->getItems() as $firm):
  100.                         $thisFirmware $firm->getDocument();
  101.                         $firmware[$fi]['title'] = $firm->getDocumentTitle();
  102.                         $firmware[$fi]['path'] = 'https://pim.libertyav.io/'.$thisFirmware->getPath().$thisFirmware->getFilename();
  103.                         $fi++;
  104.                     endforeach;
  105.                 endif;
  106.                 // -- Get parent software
  107.                 $parentFirmware $this->getParentField($data[0]->getASSEMBLY_LINE(), 'Firmware');
  108.                 if ($parentFirmware !== null && $data[0]->getASSEMBLY_LINE() != $itm):
  109.                     foreach($parentSoftware->getItems() as $firm):
  110.                         $thisFirmware $firm->getDocument();
  111.                         $firmware[$fi]['title'] = $s->getDocumentTitle();
  112.                         $firmware[$fi]['path'] = 'https://pim.libertyav.io/'.$thisFirmware->getPath().$thisFirmware->getFilename();
  113.                         $fi++;
  114.                     endforeach;
  115.                 endif;
  116.                 
  117.                 $diagramsObj $data[0]->getDiagram();
  118.                 //print_r($productCollateral);
  119.                 $diagrams = array();
  120.                 $di 0;
  121.                 if ($diagramsObj !== null):
  122.                     foreach($diagramsObj->getItems() as $dia):
  123.                         $thisDiagram $dia->getDocument();
  124.                         $diagrams[$di]['title'] = $dia->getDocumentTitle();
  125.                         $diagrams[$di]['path'] = 'https://pim.libertyav.io/'.$thisDiagram->getPath().$thisDiagram->getFilename();
  126.                         $di++;
  127.                     endforeach;
  128.                 endif;
  129.                 // -- Get parent software
  130.                 $parentDiagram $this->getParentField($data[0]->getASSEMBLY_LINE(), 'Diagram');
  131.                 if ($parentDiagram !== null && $data[0]->getASSEMBLY_LINE() != $itm):
  132.                     foreach($parentDiagram->getItems() as $dia):
  133.                         $thisDiagram $dia->getDocument();
  134.                         $diagrams[$di]['title'] = $s->getDocumentTitle();
  135.                         $diagrams[$di]['path'] = 'https://pim.libertyav.io/'.$thisDiagram->getPath().$thisDiagram->getFilename();
  136.                         $di++;
  137.                     endforeach;
  138.                 endif;
  139.                 $productImages $data[0]->getProduct_Images();
  140.                 $images = array();
  141.                 $in 0;
  142.                 foreach($productImages as $img):
  143.                     $thisImg $img->getImage();
  144.                     $images[$in] = 'https://pim.libertyav.io/'.$thisImg->getPath().$thisImg->getFilename();
  145.                     $in++;
  146.                 endforeach;
  147.                 $tagsObj Tag::getTagsForElement('object'$data[0]->geto_id());
  148.                 if (empty($tagsObj)):
  149.                     $tagsObj Tag::getTagsForElement('object'$data[0]->getParent()->geto_id());
  150.                 endif;
  151.                 $tags = array();
  152.                 foreach($tagsObj as $t):
  153.                     if ($parent $t->getparent()):
  154.                         $tags[] = $parent->getname();
  155.                     endif;
  156.                     $tags[] = $t->getname();
  157.                 endforeach;
  158.                 $product = array(
  159.                     'localization' => array(
  160.                         'language' => $lang,
  161.                         'title' => $data[0]->getTitle($lang),
  162.                         'description' => $data[0]->getDescription($lang),
  163.                         'featuresAndBenefits' => $data[0]->getFeaturesAndBenefits($lang),
  164.                         'applications' => $data[0]->getApplications($lang),
  165.                     ),
  166.                     'productInformation' => array(
  167.                         'sku' => $data[0]->getITEM_NBR(),
  168.                         'uom' => $uom,
  169.                         'upcCode' =>  $upcCode,
  170.                         'prodLineName' =>  $prodLineName,
  171.                         'assemblyLine' =>  $data[0]->getASSEMBLY_LINE(),
  172.                         'commodCode' => $data[0]->getCOMMOD_CODE(),
  173.                         'productFamily' => $data[0]->getProductFamily(),
  174.                         'weight' => $data[0]->getWeight(),
  175.                         'productDimentions' => $productMeasurements,
  176.                     ),
  177.                     'options' => array(
  178.                         'color' =>  $data[0]->getCOLOR(),
  179.                         'assemblyLength' =>  $data[0]->getASSEMBLY_LENGTH() .' '.$uom,
  180.                         'PACKAGE' => $data[0]->getPACKAGE(),
  181.                     ),
  182.                     'meta' => array(
  183.                         'metaTitle' => $metaTitle,
  184.                         'metaDescription' =>  $metaDescription,
  185.                     ),
  186.                     'package-details' => array(
  187.                         'dimensions' => array(
  188.                             'length' => $data[0]->getShippingLength(),
  189.                             'width' => $data[0]->getShippingWidth(),
  190.                             'height' => $data[0]->getShippingHeight(),
  191.                         ),
  192.                         'weight' => array(
  193.                             'lbs' => ($data[0]->getShipping_Weight_lbs() != '') ? $data[0]->getShipping_Weight_lbs() : 0,
  194.                             'oz' => ($data[0]->getShipping_Weight_oz() != '') ? $data[0]->getShipping_Weight_oz() : 0,
  195.                         ),
  196.                     ),
  197.                     'images' => $images,
  198.                     'OptionsAvailable' => $productOptionsController->getOptions($data[0]->getASSEMBLY_LINE(), 'en'),
  199.                     'tags' => $tags,
  200.                     'collateral' => $collateral,
  201.                     'software' => $software,
  202.                     'firmware' => $firmware,
  203.                     'diagrams' => $diagrams,
  204.                     //'ProductListing' => $this->getProductsFromFamily($data[0]->getASSEMBLY_LINE())
  205.                 );
  206.                 /* echo '<pre>';
  207.                 print_r(Tag::getTagsForElement('object', $data[0]->geto_id()));
  208.                 //print_r($entries);
  209.                 echo '</pre>';
  210.                 die(); */
  211.             else:
  212.                 $product = array('error''No product found');
  213.             endif;
  214.         endif;
  215.         $response = new JsonResponse();
  216.         $response->setData(['productData' => $product]);
  217.         $response->headers->set('Content-Type''application/json');
  218.         return $response;
  219.     }
  220.     public function containerAction(Request $request)
  221.     {
  222.         $itm $request->get('itm');
  223.         $entries = new DataObject\Products\Listing();
  224.         $entries->setCondition("item_nbr LIKE ?", [$itm]); // use prepared statements! Mysqli only supports ? placeholders
  225.         $entries->load();
  226.         $data $entries->getData();
  227.         /* echo '<pre>';
  228.         print_r($data[0]->getITEM_NBR());
  229.         print_r($data[0]->getSpecSheet());
  230.         echo '</pre>';
  231.         die(); */
  232.         $SpecSheetData $data[0]->getSpecSheet();
  233.         $SchematicDrawing $SpecSheetData[0]->getSchematicDrawing();
  234.         $SpecSheet = array(
  235.             'Title' => $SpecSheetData[0]->getTitle(),
  236.             'Subtitle' => $SpecSheetData[0]->getSubtitle(),
  237.             'Description' => $SpecSheetData[0]->getDescription(),
  238.             'SchematicDrawing' => array( 'filename' => $SchematicDrawing->getFilename(), 'customSettings' => $SchematicDrawing->getCustomSettings(), 'path' => $SchematicDrawing->getPath()),
  239.             'TechnicalSpecifications' => $SpecSheetData[0]->getSpecificationGroup(),
  240.             'Footer' => $SpecSheetData[0]->getFooter(),
  241.             'Brand' => $SpecSheetData[0]->getBrand()
  242.         );
  243.         /* echo '<pre>';
  244.         print_r($SpecSheet);
  245.         echo '</pre>';
  246.         die(); */
  247.         // ...
  248.         //return $this->render('documents/specsheet.html.twig', $SpecSheet);
  249.         $html $this->renderView('documents/specsheet.html.twig'$SpecSheet);
  250.             $adapter \Pimcore\Web2Print\Processor::getInstance();
  251.             $params = array();
  252.             //add custom settings if necessary
  253.             if ($adapter instanceof \Pimcore\Web2Print\Processor\HeadlessChrome) {
  254.                 $params['adapterConfig'] = [
  255.                     'landscape' => true,
  256.                     'printBackground' => true,
  257.                     'format' => 'A4',
  258.                     'margin' => [
  259.                         'top' => '36 mm',
  260.                         'bottom' => '30 mm',
  261.                         'right' => '8 mm',
  262.                         'left' => '8 mm',
  263.                     ],
  264.                     'displayHeaderFooter' => false,
  265.                 ];
  266.             } elseif($adapter instanceof \Pimcore\Web2Print\Processor\PdfReactor) {
  267.                 //Config settings -> http://www.pdfreactor.com/product/doc/webservice/php.html#Configuration
  268.                 $params['adapterConfig'] = [
  269.                     'author' => 'Max Mustermann',
  270.                     'title' => 'Custom Title',
  271.                     'javaScriptMode' => 0,
  272.                     'addLinks' => true,
  273.                     'appendLog' => true,
  274.                     'enableDebugMode' => true
  275.                 ];
  276.             }
  277.             /* echo '<pre>';
  278.             print_r($adapter);
  279.             echo '</pre>';
  280.             die(); */
  281.             return new \Symfony\Component\HttpFoundation\Response(
  282.                 $adapter->getPdfFromString($html$params),
  283.                 200,
  284.                 array(
  285.                     'Content-Type' => 'application/pdf',
  286.                     // 'Content-Disposition'   => 'attachment; filename="custom-pdf.pdf"' //direct download
  287.                 )
  288.             );
  289.     }
  290.     private function getParentField($itm$field) {
  291.         $entries = new DataObject\Products\Listing();
  292.         $entries->setObjectTypes([DataObject::OBJECT_TYPE_VARIANT,DataObject::OBJECT_TYPE_OBJECT]);
  293.         $entries->setCondition("item_nbr LIKE ?", [$itm]); // use prepared statements! Mysqli only supports ? placeholders
  294.         $entries->load();
  295.         $data $entries->getData();
  296.         return $data[0]->{'get'.$field}();
  297.     }
  298. }