Example #1
0
 private function insertMediaGalleries($text, \Ess\M2ePro\Model\Magento\Product $magentoProduct)
 {
     preg_match_all("/#media_gallery\\[(.*?)\\]#/", $text, $matches);
     if (!count($matches[0])) {
         return $text;
     }
     $blockObj = $this->layout->createBlock('Ess\\M2ePro\\Block\\Adminhtml\\Renderer\\Description\\Gallery');
     $search = array();
     $replace = array();
     $attributeCounter = 0;
     foreach ($matches[0] as $key => $match) {
         $tempMediaGalleryAttributes = explode(',', $matches[1][$key]);
         $realMediaGalleryAttributes = array();
         for ($i = 0; $i < 8; $i++) {
             if (!isset($tempMediaGalleryAttributes[$i])) {
                 $realMediaGalleryAttributes[$i] = '';
             } else {
                 $realMediaGalleryAttributes[$i] = $tempMediaGalleryAttributes[$i];
             }
         }
         $imagesQty = (int) $realMediaGalleryAttributes[5];
         if ($imagesQty == self::IMAGES_QTY_ALL) {
             $imagesQty = $realMediaGalleryAttributes[3] == self::IMAGES_MODE_GALLERY ? 100 : 25;
         }
         $galleryImagesLinks = $magentoProduct->getGalleryImagesLinks($imagesQty);
         if (!count($galleryImagesLinks)) {
             $search = $matches[0];
             $replace = '';
             break;
         }
         if (!in_array($realMediaGalleryAttributes[4], array(self::LAYOUT_MODE_ROW, self::LAYOUT_MODE_COLUMN))) {
             $realMediaGalleryAttributes[4] = self::LAYOUT_MODE_ROW;
         }
         $data = array('width' => (int) $realMediaGalleryAttributes[0], 'height' => (int) $realMediaGalleryAttributes[1], 'margin' => (int) $realMediaGalleryAttributes[2], 'linked_mode' => (int) $realMediaGalleryAttributes[3], 'layout' => $realMediaGalleryAttributes[4], 'gallery_hint' => trim($realMediaGalleryAttributes[6], '"'), 'watermark' => (int) $realMediaGalleryAttributes[7], 'images_count' => count($galleryImagesLinks), 'image_counter' => 0);
         $tempHtml = '';
         $attributeCounter++;
         foreach ($galleryImagesLinks as $imageLink) {
             $data['image_counter']++;
             $data['attribute_counter'] = $attributeCounter;
             $data['src'] = $imageLink;
             $tempHtml .= $blockObj->addData($data)->toHtml();
         }
         $search[] = $match;
         $replace[] = preg_replace('/\\s{2,}/', '', $tempHtml);
     }
     $text = str_replace($search, $replace, $text);
     return $text;
 }