6
6
*/
7
7
namespace Magento \Catalog \Model ;
8
8
9
- use Magento \Catalog \Api \Data \ProductInterface ;
10
9
use Magento \Catalog \Model \Product \Gallery \MimeTypeExtensionMap ;
11
10
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
12
- use Magento \Framework \Api \Data \ImageContentInterface ;
13
11
use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
14
12
use Magento \Framework \Api \ImageContentValidatorInterface ;
15
13
use Magento \Framework \Api \ImageProcessorInterface ;
18
16
use Magento \Framework \DB \Adapter \DeadlockException ;
19
17
use Magento \Framework \DB \Adapter \LockWaitException ;
20
18
use Magento \Framework \Exception \CouldNotSaveException ;
21
- use Magento \Framework \Exception \InputException ;
22
19
use Magento \Framework \Exception \LocalizedException ;
23
20
use Magento \Framework \Exception \NoSuchEntityException ;
24
- use Magento \Framework \Exception \StateException ;
25
21
use Magento \Framework \Exception \ValidatorException ;
26
22
27
23
/**
@@ -116,11 +112,15 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
116
112
protected $ fileSystem ;
117
113
118
114
/**
115
+ * @deprecated
116
+ * @see \Magento\Catalog\Model\MediaGalleryProcessor
119
117
* @var ImageContentInterfaceFactory
120
118
*/
121
119
protected $ contentFactory ;
122
120
123
121
/**
122
+ * @deprecated
123
+ * @see \Magento\Catalog\Model\MediaGalleryProcessor
124
124
* @var ImageProcessorInterface
125
125
*/
126
126
protected $ imageProcessor ;
@@ -131,7 +131,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
131
131
protected $ extensionAttributesJoinProcessor ;
132
132
133
133
/**
134
- * @var \Magento\Catalog\Model\Product\Gallery\Processor
134
+ * @var ProductRepository\MediaGalleryProcessor
135
135
*/
136
136
protected $ mediaGalleryProcessor ;
137
137
@@ -329,6 +329,9 @@ protected function initializeProductData(array $productData, $createNew)
329
329
unset($ productData ['media_gallery ' ]);
330
330
if ($ createNew ) {
331
331
$ product = $ this ->productFactory ->create ();
332
+ if (isset ($ productData ['price ' ]) && !isset ($ productData ['product_type ' ])) {
333
+ $ product ->setTypeId (Product \Type::TYPE_SIMPLE );
334
+ }
332
335
if ($ this ->storeManager ->hasSingleStore ()) {
333
336
$ product ->setWebsiteIds ([$ this ->storeManager ->getStore (true )->getWebsiteId ()]);
334
337
}
@@ -375,53 +378,6 @@ private function assignProductToWebsites(\Magento\Catalog\Model\Product $product
375
378
$ product ->setWebsiteIds ($ websiteIds );
376
379
}
377
380
378
- /**
379
- * @param ProductInterface $product
380
- * @param array $newEntry
381
- * @return $this
382
- * @throws InputException
383
- * @throws StateException
384
- * @throws \Magento\Framework\Exception\LocalizedException
385
- */
386
- protected function processNewMediaGalleryEntry (
387
- ProductInterface $ product ,
388
- array $ newEntry
389
- ) {
390
- /** @var ImageContentInterface $contentDataObject */
391
- $ contentDataObject = $ newEntry ['content ' ];
392
-
393
- /** @var \Magento\Catalog\Model\Product\Media\Config $mediaConfig */
394
- $ mediaConfig = $ product ->getMediaConfig ();
395
- $ mediaTmpPath = $ mediaConfig ->getBaseTmpMediaPath ();
396
-
397
- $ relativeFilePath = $ this ->imageProcessor ->processImageContent ($ mediaTmpPath , $ contentDataObject );
398
- $ tmpFilePath = $ mediaConfig ->getTmpMediaShortUrl ($ relativeFilePath );
399
-
400
- if (!$ product ->hasGalleryAttribute ()) {
401
- throw new StateException (__ ('Requested product does not support images. ' ));
402
- }
403
-
404
- $ imageFileUri = $ this ->getMediaGalleryProcessor ()->addImage (
405
- $ product ,
406
- $ tmpFilePath ,
407
- isset ($ newEntry ['types ' ]) ? $ newEntry ['types ' ] : [],
408
- true ,
409
- isset ($ newEntry ['disabled ' ]) ? $ newEntry ['disabled ' ] : true
410
- );
411
- // Update additional fields that are still empty after addImage call
412
- $ this ->getMediaGalleryProcessor ()->updateImage (
413
- $ product ,
414
- $ imageFileUri ,
415
- [
416
- 'label ' => $ newEntry ['label ' ],
417
- 'position ' => $ newEntry ['position ' ],
418
- 'disabled ' => $ newEntry ['disabled ' ],
419
- 'media_type ' => $ newEntry ['media_type ' ],
420
- ]
421
- );
422
- return $ this ;
423
- }
424
-
425
381
/**
426
382
* Process product links, creating new links, updating and deleting existing links
427
383
*
@@ -480,67 +436,6 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
480
436
return $ this ;
481
437
}
482
438
483
- /**
484
- * Process Media gallery data before save product.
485
- *
486
- * Compare Media Gallery Entries Data with existing Media Gallery
487
- * * If Media entry has not value_id set it as new
488
- * * If Existing entry 'value_id' absent in Media Gallery set 'removed' flag
489
- * * Merge Existing and new media gallery
490
- *
491
- * @param ProductInterface $product contains only existing media gallery items
492
- * @param array $mediaGalleryEntries array which contains all media gallery items
493
- * @return $this
494
- * @throws InputException
495
- * @throws StateException
496
- * @throws LocalizedException
497
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
498
- */
499
- protected function processMediaGallery (ProductInterface $ product , $ mediaGalleryEntries )
500
- {
501
- $ existingMediaGallery = $ product ->getMediaGallery ('images ' );
502
- $ newEntries = [];
503
- $ entriesById = [];
504
- if (!empty ($ existingMediaGallery )) {
505
- foreach ($ mediaGalleryEntries as $ entry ) {
506
- if (isset ($ entry ['id ' ])) {
507
- $ entriesById [$ entry ['id ' ]] = $ entry ;
508
- } else {
509
- $ newEntries [] = $ entry ;
510
- }
511
- }
512
- foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
513
- if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
514
- $ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
515
- if (array_key_exists ('file ' , $ updatedEntry ) && $ updatedEntry ['file ' ] === null ) {
516
- unset($ updatedEntry ['file ' ]);
517
- }
518
- $ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
519
- } else {
520
- //set the removed flag
521
- $ existingEntry ['removed ' ] = true ;
522
- }
523
- }
524
- unset($ existingEntry );
525
- $ product ->setData ('media_gallery ' , ["images " => $ existingMediaGallery ]);
526
- } else {
527
- $ newEntries = $ mediaGalleryEntries ;
528
- }
529
-
530
- $ this ->getMediaGalleryProcessor ()->clearMediaAttribute ($ product , array_keys ($ product ->getMediaAttributes ()));
531
- $ images = $ product ->getMediaGallery ('images ' );
532
- if ($ images ) {
533
- foreach ($ images as $ image ) {
534
- if (!isset ($ image ['removed ' ]) && !empty ($ image ['types ' ])) {
535
- $ this ->getMediaGalleryProcessor ()->setMediaAttribute ($ product , $ image ['types ' ], $ image ['file ' ]);
536
- }
537
- }
538
- }
539
- $ this ->processEntries ($ product , $ newEntries , $ entriesById );
540
-
541
- return $ this ;
542
- }
543
-
544
439
/**
545
440
* {@inheritdoc}
546
441
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
@@ -577,7 +472,10 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
577
472
578
473
$ this ->processLinks ($ product , $ productLinks );
579
474
if (isset ($ productDataArray ['media_gallery_entries ' ])) {
580
- $ this ->processMediaGallery ($ product , $ productDataArray ['media_gallery_entries ' ]);
475
+ $ this ->getMediaGalleryProcessor ()->processMediaGallery (
476
+ $ product ,
477
+ $ productDataArray ['media_gallery_entries ' ]
478
+ );
581
479
}
582
480
583
481
if (!$ product ->getOptionsReadonly ()) {
@@ -749,13 +647,13 @@ public function cleanCache()
749
647
}
750
648
751
649
/**
752
- * @return Product\Gallery\Processor
650
+ * @return ProductRepository\MediaGalleryProcessor
753
651
*/
754
652
private function getMediaGalleryProcessor ()
755
653
{
756
654
if (null === $ this ->mediaGalleryProcessor ) {
757
655
$ this ->mediaGalleryProcessor = \Magento \Framework \App \ObjectManager::getInstance ()
758
- ->get (\ Magento \ Catalog \ Model \ Product \ Gallery \Processor ::class);
656
+ ->get (ProductRepository \MediaGalleryProcessor ::class);
759
657
}
760
658
return $ this ->mediaGalleryProcessor ;
761
659
}
@@ -775,60 +673,4 @@ private function getCollectionProcessor()
775
673
}
776
674
return $ this ->collectionProcessor ;
777
675
}
778
-
779
- /**
780
- * Convert extension attribute for product media gallery.
781
- *
782
- * @param array $newEntry
783
- * @param array $extensionAttributes
784
- * @return void
785
- */
786
- private function processExtensionAttributes (array &$ newEntry , array $ extensionAttributes )
787
- {
788
- foreach ($ extensionAttributes as $ code => $ value ) {
789
- if (is_array ($ value )) {
790
- $ this ->processExtensionAttributes ($ newEntry , $ value );
791
- } else {
792
- $ newEntry [$ code ] = $ value ;
793
- }
794
- }
795
- unset($ newEntry ['extension_attributes ' ]);
796
- }
797
-
798
- /**
799
- * Convert entries into product media gallery data and set to product.
800
- *
801
- * @param ProductInterface $product
802
- * @param array $newEntries
803
- * @param array $entriesById
804
- * @throws InputException
805
- * @throws LocalizedException
806
- * @throws StateException
807
- * @return void
808
- */
809
- private function processEntries (ProductInterface $ product , array $ newEntries , array $ entriesById )
810
- {
811
- foreach ($ newEntries as $ newEntry ) {
812
- if (!isset ($ newEntry ['content ' ])) {
813
- throw new InputException (__ ('The image content is not valid. ' ));
814
- }
815
- /** @var ImageContentInterface $contentDataObject */
816
- $ contentDataObject = $ this ->contentFactory ->create ()
817
- ->setName ($ newEntry ['content ' ][ImageContentInterface::NAME ])
818
- ->setBase64EncodedData ($ newEntry ['content ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
819
- ->setType ($ newEntry ['content ' ][ImageContentInterface::TYPE ]);
820
- $ newEntry ['content ' ] = $ contentDataObject ;
821
- $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
822
-
823
- $ finalGallery = $ product ->getData ('media_gallery ' );
824
- $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
825
- if (isset ($ newEntry ['extension_attributes ' ])) {
826
- $ this ->processExtensionAttributes ($ newEntry , $ newEntry ['extension_attributes ' ]);
827
- }
828
- $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
829
- $ entriesById [$ newEntryId ] = $ newEntry ;
830
- $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
831
- $ product ->setData ('media_gallery ' , $ finalGallery );
832
- }
833
- }
834
676
}
0 commit comments