vendor/isotope/isotope-core/system/modules/isotope/library/Isotope/Model/Gallery.php line 69

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\Model;
  11. use Contao\PageModel;
  12. use Contao\StringUtil;
  13. use Isotope\Interfaces\IsotopeProduct;
  14. use Isotope\Model\Gallery\Standard as StandardGallery;
  15. /**
  16.  * Gallery is the parent class for all gallery types
  17.  *
  18.  *
  19.  * @property int    $id
  20.  * @property int    $tstamp
  21.  * @property string $name
  22.  * @property string $type
  23.  * @property string $anchor
  24.  * @property string $placeholder
  25.  * @property string $main_size
  26.  * @property string $main_watermark_image
  27.  * @property string $main_watermark_position
  28.  * @property string $gallery_size
  29.  * @property string $gallery_watermark_image
  30.  * @property string $gallery_watermark_position
  31.  * @property string $customTpl
  32.  */
  33. abstract class Gallery extends TypeAgent
  34. {
  35.     /**
  36.      * Table name
  37.      * @var string
  38.      */
  39.     protected static $strTable 'tl_iso_gallery';
  40.     /**
  41.      * Interface to validate shipping method
  42.      * @var string
  43.      */
  44.     protected static $strInterface '\Isotope\Interfaces\IsotopeGallery';
  45.     /**
  46.      * List of types (classes) for this model
  47.      * @var array
  48.      */
  49.     protected static $arrModelTypes = array();
  50.     /**
  51.      * Create a gallery for product, falls back to standard gallery if none is defined
  52.      *
  53.      * @param IsotopeProduct $objProduct
  54.      * @param string         $strAttribute
  55.      * @param array          $arrConfig
  56.      *
  57.      * @return static
  58.      */
  59.     public static function createForProductAttribute(IsotopeProduct $objProduct$strAttribute$arrConfig)
  60.     {
  61.         $objGallery = static::findByPk((int) $arrConfig['gallery']);
  62.         if (null === $objGallery) {
  63.             $objGallery = new StandardGallery();
  64.         } else {
  65.             $objGallery = clone $objGallery;
  66.             $objGallery->preventSaving();
  67.             $objGallery->id = (int) $arrConfig['gallery'];
  68.         }
  69.         $objGallery->setName($objProduct->getFormId() . '_' $strAttribute);
  70.         $objGallery->setFiles(static::mergeMediaData(
  71.             StringUtil::deserialize($objProduct->$strAttributetrue),
  72.             StringUtil::deserialize($objProduct->{$strAttribute '_fallback'}, true)
  73.         ));
  74.         $objGallery->product_id $objProduct->getProductId();
  75.         if (!$arrConfig['jumpTo'] instanceof PageModel || $arrConfig['jumpTo']->iso_readerMode !== 'none') {
  76.             $objGallery->href $objProduct->generateUrl($arrConfig['jumpTo']);
  77.         }
  78.         return $objGallery;
  79.     }
  80.     /**
  81.      * Merge media manager data from fallback and translated product data
  82.      *
  83.      * @param array $arrCurrent
  84.      * @param array $arrParent
  85.      *
  86.      * @return array
  87.      */
  88.     public static function mergeMediaData($arrCurrent$arrParent)
  89.     {
  90.         $arrTranslate = array();
  91.         if (\is_array($arrParent) && !== \count($arrParent)) {
  92.             // Create an array of images where key = image name
  93.             foreach ($arrParent as $image) {
  94.                 if ('all' !== ($image['translate'] ?? null)) {
  95.                     $arrTranslate[$image['src']] = $image;
  96.                 }
  97.             }
  98.         }
  99.         if (\is_array($arrCurrent) && !== \count($arrCurrent)) {
  100.             foreach ($arrCurrent as $i => $image) {
  101.                 if (isset($arrTranslate[$image['src']])) {
  102.                     if ('none' === ($arrTranslate[$image['src']]['translate'] ?? null)) {
  103.                         $arrCurrent[$i] = $arrTranslate[$image['src']];
  104.                     } else {
  105.                         $arrCurrent[$i]['link']      = $arrTranslate[$image['src']]['link'] ?? '';
  106.                         $arrCurrent[$i]['translate'] = $arrTranslate[$image['src']]['translate'] ?? '';
  107.                     }
  108.                     unset($arrTranslate[$image['src']]);
  109.                 } elseif ('all' !== $arrCurrent[$i]['translate']) {
  110.                     unset($arrCurrent[$i]);
  111.                 }
  112.             }
  113.             // Add remaining parent image to the list
  114.             if (!empty($arrTranslate)) {
  115.                 $arrCurrent array_merge($arrCurrentarray_values($arrTranslate));
  116.             }
  117.             $arrCurrent array_values($arrCurrent);
  118.         } else {
  119.             $arrCurrent array_values($arrTranslate);
  120.         }
  121.         return $arrCurrent;
  122.     }
  123. }