Пример #1
0
 /**
  * Retrieving images from all columns and rows
  *
  * @param $bunch
  * @return array
  */
 protected function _getAllBunchImages($bunch)
 {
     $allImagesFromBunch = [];
     foreach ($bunch as $rowData) {
         $rowData = $this->_customFieldsMapping($rowData);
         foreach ($this->_imagesArrayKeys as $image) {
             if (empty($rowData[$image])) {
                 continue;
             }
             $dispersionPath = \Magento\Framework\File\Uploader::getDispretionPath($rowData[$image]);
             $importImages = explode($this->getMultipleValueSeparator(), $rowData[$image]);
             foreach ($importImages as $importImage) {
                 $imageSting = mb_strtolower($dispersionPath . '/' . preg_replace('/[^a-z0-9\\._-]+/i', '', $importImage));
                 $allImagesFromBunch[$importImage] = $imageSting;
             }
         }
     }
     return $allImagesFromBunch;
 }
Пример #2
0
 /**
  * Check protected/allowed extension
  *
  * @param string $extension
  * @return boolean
  */
 public function checkAllowedExtension($extension)
 {
     //validate with protected file types
     if (!$this->_validator->isValid($extension)) {
         return false;
     }
     return parent::checkAllowedExtension($extension);
 }
Пример #3
0
 /**
  * Retrieving images from all columns and rows
  *
  * @param array $bunch
  * @return array
  */
 protected function getBunchImages($bunch)
 {
     $images = [];
     foreach ($bunch as $row) {
         $row = $this->_customFieldsMapping($row);
         foreach ($this->_imagesArrayKeys as $imageColumn) {
             if (empty($row[$imageColumn])) {
                 continue;
             }
             $rowImages = explode($this->getMultipleValueSeparator(), $row[$imageColumn]);
             foreach ($rowImages as $rowImage) {
                 $destinationPath = str_replace('\\', '/', $rowImage);
                 $destinationPath = explode('/', $destinationPath);
                 $destinationPath = array_pop($destinationPath);
                 $destinationPath = preg_replace('/[^a-z0-9\\._-]+/i', '', $destinationPath);
                 $dispersion = \Magento\Framework\File\Uploader::getDispretionPath($destinationPath);
                 $destinationPath = mb_strtolower($dispersion . '/' . $destinationPath);
                 $images[$rowImage] = $destinationPath;
             }
         }
     }
     return $images;
 }
Пример #4
0
 /**
  * @param \Mirasvit\Blog\Model\Post $post
  * @return $this
  * @throws \Exception
  */
 protected function saveImage($post)
 {
     if (!isset($_FILES['featured_image']) || !$_FILES['featured_image']['name']) {
         return $this;
     }
     $image = $_FILES['featured_image'];
     $ext = pathinfo($image['name'], PATHINFO_EXTENSION);
     $name = pathinfo($image['name'], PATHINFO_FILENAME);
     $oldFileName = $post->getFeaturedImage();
     $newFileName = $name . '-' . $post->getId() . '.' . $ext;
     $allowedFileExtensions = ['png', 'jpeg', 'jpg', 'gif'];
     $ext = pathinfo($image['name'], PATHINFO_EXTENSION);
     if (!in_array($ext, $allowedFileExtensions)) {
         throw new \Exception(__('File type not allowed (only JPG, JPEG, PNG & GIF files are allowed)'));
     }
     $uploader = new FileUploader($_FILES['featured_image']);
     $uploader->setAllowedExtensions($allowedFileExtensions)->setAllowRenameFiles(false)->setFilesDispersion(false)->setAllowCreateFolders(true)->setAllowRenameFiles(false)->setFilesDispersion(false);
     $uploader->save($this->config->getMediaPath(), $newFileName);
     $post->setFeaturedImage($newFileName);
     if ($newFileName != $oldFileName) {
         $this->deleteImage($oldFileName);
     }
     return $this;
 }
Пример #5
0
 /**
  * Create preview image duplicate
  *
  * @param ThemeInterface $theme
  * @return bool
  */
 public function createPreviewImageCopy(ThemeInterface $theme)
 {
     $previewDir = $this->themeImagePath->getImagePreviewDirectory();
     $sourcePath = $theme->getThemeImage()->getPreviewImagePath();
     $sourceRelativePath = $this->rootDirectory->getRelativePath($sourcePath);
     if (!$theme->getPreviewImage() && !$this->mediaDirectory->isExist($sourceRelativePath)) {
         return false;
     }
     $isCopied = false;
     try {
         $destinationFileName = \Magento\Framework\File\Uploader::getNewFileName($sourcePath);
         $targetRelativePath = $this->mediaDirectory->getRelativePath($previewDir . '/' . $destinationFileName);
         $isCopied = $this->rootDirectory->copyFile($sourceRelativePath, $targetRelativePath, $this->mediaDirectory);
         $this->theme->setPreviewImage($destinationFileName);
     } catch (\Magento\Framework\Filesystem\FilesystemException $e) {
         $this->theme->setPreviewImage(null);
         $this->logger->critical($e);
     }
     return $isCopied;
 }
Пример #6
0
 /**
  * Gather and save information about product entities.
  *
  * @return $this
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 protected function _saveProducts()
 {
     /** @var $resource \Magento\CatalogImportExport\Model\Import\Proxy\Product\Resource */
     $resource = $this->_resourceFactory->create();
     $priceIsGlobal = $this->_catalogData->isPriceGlobal();
     $this->_prepareAllMediaFiles();
     $productLimit = null;
     $productsQty = null;
     while ($bunch = $this->_dataSourceModel->getNextBunch()) {
         $entityRowsIn = [];
         $entityRowsUp = [];
         $attributes = [];
         $websites = [];
         $categories = [];
         $tierPrices = [];
         $groupPrices = [];
         $mediaGallery = [];
         $uploadedGalleryFiles = [];
         $previousType = null;
         $prevAttributeSet = null;
         foreach ($bunch as $rowNum => $rowData) {
             if (!$this->validateRow($rowData, $rowNum)) {
                 continue;
             }
             $rowScope = $this->getRowScope($rowData);
             $rowSku = $rowData[self::COL_SKU];
             if (null === $rowSku) {
                 $this->_rowsToSkip[$rowNum] = true;
                 // skip rows when SKU is NULL
                 continue;
             } elseif (self::SCOPE_STORE == $rowScope) {
                 // set necessary data from SCOPE_DEFAULT row
                 $rowData[self::COL_TYPE] = $this->skuProcessor->getNewSku($rowSku)['type_id'];
                 $rowData['attribute_set_id'] = $this->skuProcessor->getNewSku($rowSku)['attr_set_id'];
                 $rowData[self::COL_ATTR_SET] = $this->skuProcessor->getNewSku($rowSku)['attr_set_code'];
             }
             // 1. Entity phase
             if (isset($this->_oldSku[$rowSku])) {
                 // existing row
                 $entityRowsUp[] = ['updated_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT), 'entity_id' => $this->_oldSku[$rowSku]['entity_id']];
             } else {
                 if (!$productLimit || $productsQty < $productLimit) {
                     $entityRowsIn[$rowSku] = ['attribute_set_id' => $this->skuProcessor->getNewSku($rowSku)['attr_set_id'], 'type_id' => $this->skuProcessor->getNewSku($rowSku)['type_id'], 'sku' => $rowSku, 'has_options' => isset($rowData['has_options']) ? $rowData['has_options'] : 0, 'created_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT), 'updated_at' => (new \DateTime())->format(DateTime::DATETIME_PHP_FORMAT)];
                     $productsQty++;
                 } else {
                     $rowSku = null;
                     // sign for child rows to be skipped
                     $this->_rowsToSkip[$rowNum] = true;
                     continue;
                 }
             }
             // 2. Product-to-Website phase
             if (!empty($rowData[self::COL_PRODUCT_WEBSITES])) {
                 $websiteId = $this->storeResolver->getWebsiteCodeToId($rowData[self::COL_PRODUCT_WEBSITES]);
                 $websites[$rowSku][$websiteId] = true;
             }
             // 3. Categories phase
             $categoriesString = empty($rowData[self::COL_CATEGORY]) ? '' : $rowData[self::COL_CATEGORY];
             if (!empty($categoriesString)) {
                 foreach ($this->categoryProcessor->upsertCategories($categoriesString) as $categoryId) {
                     $categories[$rowSku][$categoryId] = true;
                 }
             }
             // 4.1. Tier prices phase
             if (!empty($rowData['_tier_price_website'])) {
                 $tierPrices[$rowSku][] = ['all_groups' => $rowData['_tier_price_customer_group'] == self::VALUE_ALL, 'customer_group_id' => $rowData['_tier_price_customer_group'] == self::VALUE_ALL ? 0 : $rowData['_tier_price_customer_group'], 'qty' => $rowData['_tier_price_qty'], 'value' => $rowData['_tier_price_price'], 'website_id' => self::VALUE_ALL == $rowData['_tier_price_website'] || $priceIsGlobal ? 0 : $this->storeResolver->getWebsiteCodeToId($rowData['_tier_price_website'])];
             }
             // 4.2. Group prices phase
             if (!empty($rowData['_group_price_website'])) {
                 $groupPrices[$rowSku][] = ['all_groups' => $rowData['_group_price_customer_group'] == self::VALUE_ALL, 'customer_group_id' => $rowData['_group_price_customer_group'] == self::VALUE_ALL ? 0 : $rowData['_group_price_customer_group'], 'value' => $rowData['_group_price_price'], 'website_id' => self::VALUE_ALL == $rowData['_group_price_website'] || $priceIsGlobal ? 0 : $this->storeResolver->getWebsiteCodeToId($rowData['_group_price_website'])];
             }
             // 5. Media gallery phase
             $fullDispersionPath = '';
             $imageIsSet = null;
             $imageFromProduct = null;
             $imageInProductIsSet = null;
             if (!empty($rowData[self::COL_MEDIA_IMAGE])) {
                 $dispersionPath = \Magento\Framework\File\Uploader::getDispretionPath($rowData[self::COL_MEDIA_IMAGE]);
                 $imageName = preg_replace('/[^a-z0-9\\._-]+/i', '', $rowData[self::COL_MEDIA_IMAGE]);
                 $fullDispersionPath = $dispersionPath . '/' . $imageName;
                 foreach ($this->cachedImages as $image) {
                     if ($image['sku'] == $rowData[self::COL_SKU] && preg_replace('/_[0-9]+/', '', $image['value']) == $fullDispersionPath) {
                         $imageInProductIsSet = true;
                         $imageFromProduct = preg_replace('/_[0-9]+/', '', $image['value']);
                         break;
                     } elseif (in_array($fullDispersionPath, $image)) {
                         $imageIsSet = true;
                         break;
                     }
                 }
             }
             if ($imageInProductIsSet && $imageFromProduct != $fullDispersionPath || !isset($imageIsSet) && !isset($imageInProductIsSet)) {
                 $mediaGalleryImages = array();
                 $mediaGalleryLabels = array();
                 if (!empty($rowData[self::COL_MEDIA_IMAGE])) {
                     $mediaGalleryImages = explode($this->getMultipleValueSeparator(), $rowData[self::COL_MEDIA_IMAGE]);
                     if (isset($rowData['_media_image_label'])) {
                         $mediaGalleryLabels = explode($this->getMultipleValueSeparator(), $rowData['_media_image_label']);
                     } else {
                         $mediaGalleryLabels = [];
                     }
                     if (count($mediaGalleryLabels) > count($mediaGalleryImages)) {
                         $mediaGalleryLabels = array_slice($mediaGalleryLabels, 0, count($mediaGalleryImages));
                     } elseif (count($mediaGalleryLabels) < count($mediaGalleryImages)) {
                         $mediaGalleryLabels = array_pad($mediaGalleryLabels, count($mediaGalleryImages), '');
                     }
                 }
                 foreach ($this->_imagesArrayKeys as $imageCol) {
                     if (!empty($rowData[$imageCol]) && $imageCol != self::COL_MEDIA_IMAGE && !in_array($rowData[$imageCol], $mediaGalleryImages)) {
                         $mediaGalleryImages[] = $rowData[$imageCol];
                         if (isset($mediaGalleryLabels)) {
                             $mediaGalleryLabels[] = isset($rowData[$imageCol . '_label']);
                         } else {
                             $mediaGalleryLabels[] = '';
                         }
                     }
                 }
                 $rowData[self::COL_MEDIA_IMAGE] = array();
                 foreach ($mediaGalleryImages as $mediaImage) {
                     if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
                         $uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(trim($mediaImage));
                     }
                     $rowData[self::COL_MEDIA_IMAGE][] = $uploadedGalleryFiles[$mediaImage];
                 }
                 foreach ($this->_imagesArrayKeys as $imageCol) {
                     if (!empty($rowData[$imageCol]) && $imageCol != self::COL_MEDIA_IMAGE) {
                         $rowData[$imageCol] = $uploadedGalleryFiles[$rowData[$imageCol]];
                     }
                 }
                 if (!empty($rowData[self::COL_MEDIA_IMAGE]) && is_array($rowData[self::COL_MEDIA_IMAGE])) {
                     $position = 0;
                     foreach ($rowData[self::COL_MEDIA_IMAGE] as $media_image) {
                         $mediaGallery[$rowSku][] = ['attribute_id' => $this->getMediaGalleryAttributeId(), 'label' => isset($mediaGalleryLabels[$position]) ? $mediaGalleryLabels[$position] : '', 'position' => $position++, 'disabled' => '', 'value' => $media_image];
                     }
                 }
             } elseif ($imageInProductIsSet && $imageFromProduct == $fullDispersionPath) {
                 $mediaGalleryImages = array();
                 $mediaGalleryLabels = array();
                 if (!empty($rowData[self::COL_MEDIA_IMAGE])) {
                     $mediaGalleryImages = explode($this->getMultipleValueSeparator(), $rowData[self::COL_MEDIA_IMAGE]);
                     if (isset($rowData['_media_image_label'])) {
                         $mediaGalleryLabels = explode($this->getMultipleValueSeparator(), $rowData['_media_image_label']);
                     } else {
                         $mediaGalleryLabels = array();
                     }
                     if (count($mediaGalleryLabels) > count($mediaGalleryImages)) {
                         $mediaGalleryLabels = array_slice($mediaGalleryLabels, 0, count($mediaGalleryImages));
                     } elseif (count($mediaGalleryLabels) < count($mediaGalleryImages)) {
                         $mediaGalleryLabels = array_pad($mediaGalleryLabels, count($mediaGalleryImages), '');
                     }
                 }
                 foreach ($this->_imagesArrayKeys as $imageCol) {
                     if (!empty($rowData[$imageCol]) && $imageCol != self::COL_MEDIA_IMAGE && !in_array($rowData[$imageCol], $mediaGalleryImages)) {
                         $mediaGalleryImages[] = $rowData[$imageCol];
                         if (isset($rowData[$imageCol . '_label'])) {
                             $mediaGalleryLabels[] = $rowData[$imageCol . '_label'];
                         } else {
                             $mediaGalleryLabels[] = '';
                         }
                     }
                 }
                 $rowData[self::COL_MEDIA_IMAGE] = array();
                 foreach ($mediaGalleryImages as $mediaImage) {
                     if (!array_key_exists($mediaImage, $uploadedGalleryFiles)) {
                         $uploadedGalleryFiles[$mediaImage] = $this->_uploadMediaFiles(trim($mediaImage), true);
                     }
                     $rowData[self::COL_MEDIA_IMAGE][] = $uploadedGalleryFiles[$mediaImage];
                 }
             } else {
                 $this->addRowError(__("Image already exists for '%s'"), $rowNum, self::COL_MEDIA_IMAGE);
             }
             // 6. Attributes phase
             $rowStore = self::SCOPE_STORE == $rowScope ? $this->storeResolver->getStoreCodeToId($rowData[self::COL_STORE]) : 0;
             $productType = isset($rowData[self::COL_TYPE]) ? $rowData[self::COL_TYPE] : null;
             if (!is_null($productType)) {
                 $previousType = $productType;
             }
             if (isset($rowData[self::COL_ATTR_SET])) {
                 $prevAttributeSet = $rowData[self::COL_ATTR_SET];
             }
             if (self::SCOPE_NULL == $rowScope) {
                 // for multiselect attributes only
                 if (!is_null($prevAttributeSet)) {
                     $rowData[self::COL_ATTR_SET] = $prevAttributeSet;
                 }
                 if (is_null($productType) && !is_null($previousType)) {
                     $productType = $previousType;
                 }
                 if (is_null($productType)) {
                     continue;
                 }
             }
             $productTypeModel = $this->_productTypeModels[$productType];
             if (!empty($rowData['tax_class_name'])) {
                 $rowData['tax_class_id'] = $this->taxClassProcessor->upsertTaxClass($rowData['tax_class_name'], $productTypeModel);
             }
             if ($this->getBehavior() == \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND || empty($rowData[self::COL_SKU])) {
                 $rowData = $productTypeModel->clearEmptyData($rowData);
             }
             $rowData = $productTypeModel->prepareAttributesWithDefaultValueForSave($rowData, !isset($this->_oldSku[$rowSku]));
             $product = $this->_proxyProdFactory->create(['data' => $rowData]);
             foreach ($rowData as $attrCode => $attrValue) {
                 if (!isset($this->_attributeCache[$attrCode])) {
                     $this->_attributeCache[$attrCode] = $resource->getAttribute($attrCode);
                 }
                 $attribute = $this->_attributeCache[$attrCode];
                 if ('multiselect' != $attribute->getFrontendInput() && self::SCOPE_NULL == $rowScope) {
                     // skip attribute processing for SCOPE_NULL rows
                     continue;
                 }
                 $attrId = $attribute->getId();
                 $backModel = $attribute->getBackendModel();
                 $attrTable = $attribute->getBackend()->getTable();
                 $storeIds = [0];
                 if ('datetime' == $attribute->getBackendType() && strtotime($attrValue)) {
                     $attrValue = (new \DateTime())->setTimestamp(strtotime($attrValue));
                     $attrValue = $attrValue->format(DateTime::DATETIME_PHP_FORMAT);
                 } elseif ($backModel) {
                     $attribute->getBackend()->beforeSave($product);
                     $attrValue = $product->getData($attribute->getAttributeCode());
                 }
                 if (self::SCOPE_STORE == $rowScope) {
                     if (self::SCOPE_WEBSITE == $attribute->getIsGlobal()) {
                         // check website defaults already set
                         if (!isset($attributes[$attrTable][$rowSku][$attrId][$rowStore])) {
                             $storeIds = $this->storeResolver->getStoreIdToWebsiteStoreIds($rowStore);
                         }
                     } elseif (self::SCOPE_STORE == $attribute->getIsGlobal()) {
                         $storeIds = [$rowStore];
                     }
                     if (!isset($this->_oldSku[$rowSku])) {
                         $storeIds[] = 0;
                     }
                 }
                 foreach ($storeIds as $storeId) {
                     if ('multiselect' == $attribute->getFrontendInput()) {
                         if (!isset($attributes[$attrTable][$rowSku][$attrId][$storeId])) {
                             $attributes[$attrTable][$rowSku][$attrId][$storeId] = '';
                         } else {
                             $attributes[$attrTable][$rowSku][$attrId][$storeId] .= ',';
                         }
                         $attributes[$attrTable][$rowSku][$attrId][$storeId] .= $attrValue;
                     } else {
                         $attributes[$attrTable][$rowSku][$attrId][$storeId] = $attrValue;
                     }
                 }
                 // restore 'backend_model' to avoid 'default' setting
                 $attribute->setBackendModel($backModel);
             }
         }
         $this->_saveProductEntity($entityRowsIn, $entityRowsUp)->_saveProductWebsites($websites)->_saveProductCategories($categories)->_saveProductTierPrices($tierPrices)->_saveProductGroupPrices($groupPrices)->_saveMediaGallery($mediaGallery)->_saveProductAttributes($attributes);
         $this->_eventManager->dispatch('catalog_product_import_bunch_save_after', ['adapter' => $this, 'bunch' => $bunch]);
     }
     return $this;
 }
Пример #7
0
 /**
  * @param string $localFilePath
  * @return string
  */
 protected function appendNewFileName($localFilePath)
 {
     $destinationFile = $this->appendAbsoluteFileSystemPath($localFilePath);
     $fileName = Uploader::getNewFileName($destinationFile);
     $fileInfo = pathinfo($localFilePath);
     return $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $fileName;
 }
Пример #8
0
 /**
  * @param string $fileName
  * @return string
  */
 protected function generateFileNameWithPath($fileName)
 {
     return Uploader::getDispretionPath($fileName) . DIRECTORY_SEPARATOR . $fileName;
 }