vendor/isotope/isotope-core/system/modules/isotope/library/Isotope/Module/ProductReader.php line 123

Open in your IDE?
  1. <?php
  2. /*
  3.  * Isotope eCommerce for Contao Open Source CMS
  4.  *
  5.  * Copyright (C) 2009 - 2019 terminal42 gmbh & Isotope eCommerce Workgroup
  6.  *
  7.  * @link       https://isotopeecommerce.org
  8.  * @license    https://opensource.org/licenses/lgpl-3.0.html
  9.  */
  10. namespace Isotope\Module;
  11. use Contao\Controller;
  12. use Contao\CoreBundle\Exception\PageNotFoundException;
  13. use Contao\CoreBundle\Exception\ResponseException;
  14. use Contao\CoreBundle\Routing\ResponseContext\HtmlHeadBag\HtmlHeadBag;
  15. use Contao\Database;
  16. use Contao\Environment;
  17. use Contao\PageModel;
  18. use Contao\StringUtil;
  19. use Contao\System;
  20. use Haste\Input\Input;
  21. use Isotope\Interfaces\IsotopeProduct;
  22. use Isotope\Model\Product;
  23. use Isotope\Model\Product\AbstractProduct;
  24. use Symfony\Component\HttpFoundation\Response;
  25. /**
  26.  * Class ProductReader
  27.  *
  28.  * @property bool   $iso_use_quantity
  29.  * @property bool   $iso_display404Page
  30.  * @property bool   $iso_addProductJumpTo
  31.  * @property string $iso_reader_layout
  32.  * @property int    $iso_gallery
  33.  */
  34. class ProductReader extends Module
  35. {
  36.     /**
  37.      * Template
  38.      * @var string
  39.      */
  40.     protected $strTemplate 'mod_iso_productreader';
  41.     /**
  42.      * Product
  43.      * @var IsotopeProduct
  44.      */
  45.     protected $objProduct;
  46.     /**
  47.      * Display a wildcard in the back end
  48.      * @return string
  49.      */
  50.     public function generate()
  51.     {
  52.         if ('BE' === TL_MODE) {
  53.             return $this->generateWildcard();
  54.         }
  55.         // Return if no product has been specified
  56.         if (Input::getAutoItem('product'falsetrue) == '') {
  57.             if ($this->iso_display404Page) {
  58.                 throw new PageNotFoundException();
  59.             }
  60.             return '';
  61.         }
  62.         return parent::generate();
  63.     }
  64.     /**
  65.      * Generate module
  66.      * @return void
  67.      */
  68.     protected function compile()
  69.     {
  70.         $jumpTo $GLOBALS['objIsotopeListPage'] ?: $GLOBALS['objPage'];
  71.         if ($jumpTo->iso_readerMode === 'none') {
  72.             throw new PageNotFoundException();
  73.         }
  74.         /** @var AbstractProduct $objProduct */
  75.         $objProduct Product::findAvailableByIdOrAlias(Input::getAutoItem('product'));
  76.         if (null === $objProduct) {
  77.             throw new PageNotFoundException();
  78.         }
  79.         $arrConfig = array(
  80.             'module'      => $this,
  81.             'template'    => $this->iso_reader_layout ? : $objProduct->getType()->reader_template,
  82.             'gallery'     => $this->iso_gallery ? : $objProduct->getType()->reader_gallery,
  83.             'buttons'     => $this->iso_buttons,
  84.             'useQuantity' => $this->iso_use_quantity,
  85.             'disableOptions' => $this->iso_disable_options,
  86.             'jumpTo'      => $jumpTo,
  87.         );
  88.         if (Environment::get('isAjaxRequest')
  89.             && Input::post('AJAX_MODULE') == $this->id
  90.             && Input::post('AJAX_PRODUCT') == $objProduct->getProductId()
  91.             && !$this->iso_disable_options
  92.         ) {
  93.             try {
  94.                 $content $objProduct->generate($arrConfig);
  95.                 $content Controller::replaceInsertTags($contentfalse);
  96.             } catch (\InvalidArgumentException $e) {
  97.                 return;
  98.             }
  99.             throw new ResponseException(new Response($content));
  100.         }
  101.         $this->addMetaTags($objProduct);
  102.         $this->addCanonicalProductUrls($objProduct);
  103.         $this->Template->product       $objProduct->generate($arrConfig);
  104.         $this->Template->product_id    $objProduct->getCssId();
  105.         $this->Template->product_class $objProduct->getCssClass();
  106.         $this->Template->referer       'javascript:history.go(-1)';
  107.         $this->Template->back          $GLOBALS['TL_LANG']['MSC']['goBack'];
  108.     }
  109.     /**
  110.      * Add meta header fields to the current page
  111.      *
  112.      * @param Product $objProduct
  113.      */
  114.     protected function addMetaTags(Product $objProduct)
  115.     {
  116.         $pageTitle $objProduct->meta_title ?: $objProduct->getName();
  117.         /** @noinspection NestedTernaryOperatorInspection */
  118.         $description $objProduct->meta_description ?: ($objProduct->teaser ?: $objProduct->description);
  119.         if ($objProduct->meta_keywords) {
  120.             $GLOBALS['TL_KEYWORDS'] .= ($GLOBALS['TL_KEYWORDS'] != '' ', ' '') . $objProduct->meta_keywords;
  121.         }
  122.         // Support response context in Contao 4.13
  123.         if (System::getContainer()->has('contao.routing.response_context_accessor')) {
  124.             $responseContext System::getContainer()->get('contao.routing.response_context_accessor')->getResponseContext();
  125.             $htmlDecoder System::getContainer()->get('contao.string.html_decoder');
  126.             if ($responseContext && $htmlDecoder && $responseContext->has(HtmlHeadBag::class)) {
  127.                 /** @var HtmlHeadBag $htmlHeadBag */
  128.                 $htmlHeadBag $responseContext->get(HtmlHeadBag::class);
  129.                 $htmlHeadBag->setTitle($htmlDecoder->inputEncodedToPlainText($pageTitle));
  130.                 if ($description) {
  131.                     $htmlHeadBag->setMetaDescription($htmlDecoder->inputEncodedToPlainText($description));
  132.                 }
  133.                 return;
  134.             }
  135.         }
  136.         global $objPage;
  137.         $objPage->pageTitle $this->prepareMetaDescription($pageTitle);
  138.         $objPage->description $this->prepareMetaDescription($description);
  139.     }
  140.     /**
  141.      * Adds canonical product URLs to the document
  142.      *
  143.      * @param Product $objProduct
  144.      */
  145.     protected function addCanonicalProductUrls(Product $objProduct)
  146.     {
  147.         global $objPage;
  148.         $arrPageIds   Database::getInstance()->getChildRecords($objPage->rootIdPageModel::getTable());
  149.         $arrPageIds[] = $objPage->rootId;
  150.         // Find the categories in the current root
  151.         $arrCategories array_intersect($objProduct->getCategories(), $arrPageIds);
  152.         foreach ($arrCategories as $intPage) {
  153.             if (($objJumpTo PageModel::findPublishedById($intPage)) !== null) {
  154.                 // Do not use the index page as canonical link
  155.                 if ('index' === $objJumpTo->alias && \count($arrCategories) > 1) {
  156.                     continue;
  157.                 }
  158.                 $objJumpTo->loadDetails();
  159.                 $href $objProduct->generateUrl($objJumpTotrue);
  160.                 // Canonical links in Contao 4.13
  161.                 if ($objJumpTo->enableCanonical && System::getContainer()->has('contao.routing.response_context_accessor')) {
  162.                     $responseContext System::getContainer()->get('contao.routing.response_context_accessor')->getResponseContext();
  163.                     if ($responseContext && $responseContext->has(HtmlHeadBag::class)) {
  164.                         $responseContext
  165.                             ->get(HtmlHeadBag::class)
  166.                             ->setCanonicalUri($href)
  167.                         ;
  168.                         break;
  169.                     }
  170.                 }
  171.                 $GLOBALS['TL_HEAD'][] = '<link rel="canonical" href="' $href '">';
  172.                 break;
  173.             }
  174.         }
  175.     }
  176.     /**
  177.      * Gets the CSS ID for this product
  178.      *
  179.      * @param Product $objProduct
  180.      *
  181.      * @return string|null
  182.      *
  183.      * @deprecated Use AbstractProduct::getCssId()
  184.      */
  185.     protected function getCssId(Product $objProduct)
  186.     {
  187.         $css StringUtil::deserialize($objProduct->cssIDtrue);
  188.         return $css[0] ? ' id="' $css[0] . '"' null;
  189.     }
  190.     /**
  191.      * Gets the CSS classes for this product
  192.      *
  193.      * @param Product $objProduct
  194.      *
  195.      * @return string
  196.      *
  197.      * @deprecated Use AbstractProduct::getCssClass()
  198.      */
  199.     protected function getCssClass(Product $objProduct)
  200.     {
  201.         if ($objProduct instanceof AbstractProduct) {
  202.             return $objProduct->getCssClass();
  203.         }
  204.         $classes = ['product'];
  205.         if ($objProduct->isNew()) {
  206.             $classes[] = 'new';
  207.         }
  208.         $arrCSS StringUtil::deserialize($objProduct->cssIDtrue);
  209.         if ('' !== (string) $arrCSS[1]) {
  210.             $classes[] = (string) $arrCSS[1];
  211.         }
  212.         return implode(' '$classes);
  213.     }
  214. }