1
1
<?php
2
2
/**
3
- *
4
3
* Copyright © Magento, Inc. All rights reserved.
5
4
* See COPYING.txt for license details.
6
5
*/
7
6
8
7
namespace Magento \Catalog \Model ;
9
8
9
+ use Magento \Catalog \Api \CategoryLinkManagementInterface ;
10
10
use Magento \Catalog \Api \Data \ProductExtension ;
11
11
use Magento \Catalog \Api \Data \ProductInterface ;
12
12
use Magento \Catalog \Model \Product \Gallery \MimeTypeExtensionMap ;
13
+ use Magento \Catalog \Model \ProductRepository \MediaGalleryProcessor ;
13
14
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
14
15
use Magento \Eav \Model \Entity \Attribute \Exception as AttributeException ;
15
- use Magento \Framework \Api \Data \ImageContentInterface ;
16
16
use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
17
17
use Magento \Framework \Api \ImageContentValidatorInterface ;
18
18
use Magento \Framework \Api \ImageProcessorInterface ;
25
25
use Magento \Framework \Exception \InputException ;
26
26
use Magento \Framework \Exception \LocalizedException ;
27
27
use Magento \Framework \Exception \NoSuchEntityException ;
28
- use Magento \Framework \Exception \TemporaryState \CouldNotSaveException as TemporaryCouldNotSaveException ;
29
28
use Magento \Framework \Exception \StateException ;
29
+ use Magento \Framework \Exception \TemporaryState \CouldNotSaveException as TemporaryCouldNotSaveException ;
30
30
use Magento \Framework \Exception \ValidatorException ;
31
31
32
32
/**
@@ -122,11 +122,15 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
122
122
protected $ fileSystem ;
123
123
124
124
/**
125
+ * @deprecated
126
+ *
125
127
* @var ImageContentInterfaceFactory
126
128
*/
127
129
protected $ contentFactory ;
128
130
129
131
/**
132
+ * @deprecated
133
+ *
130
134
* @var ImageProcessorInterface
131
135
*/
132
136
protected $ imageProcessor ;
@@ -137,10 +141,17 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
137
141
protected $ extensionAttributesJoinProcessor ;
138
142
139
143
/**
144
+ * @deprecated
145
+ *
140
146
* @var \Magento\Catalog\Model\Product\Gallery\Processor
141
147
*/
142
148
protected $ mediaGalleryProcessor ;
143
149
150
+ /**
151
+ * @var MediaGalleryProcessor
152
+ */
153
+ private $ mediaProcessor ;
154
+
144
155
/**
145
156
* @var CollectionProcessorInterface
146
157
*/
@@ -161,6 +172,11 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
161
172
*/
162
173
private $ readExtensions ;
163
174
175
+ /**
176
+ * @var CategoryLinkManagementInterface
177
+ */
178
+ private $ linkManagement ;
179
+
164
180
/**
165
181
* ProductRepository constructor.
166
182
* @param ProductFactory $productFactory
@@ -186,7 +202,8 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
186
202
* @param CollectionProcessorInterface $collectionProcessor [optional]
187
203
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
188
204
* @param int $cacheLimit [optional]
189
- * @param ReadExtensions|null $readExtensions
205
+ * @param ReadExtensions $readExtensions
206
+ * @param CategoryLinkManagementInterface $linkManagement
190
207
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
191
208
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
192
209
*/
@@ -214,7 +231,8 @@ public function __construct(
214
231
CollectionProcessorInterface $ collectionProcessor = null ,
215
232
\Magento \Framework \Serialize \Serializer \Json $ serializer = null ,
216
233
$ cacheLimit = 1000 ,
217
- ReadExtensions $ readExtensions = null
234
+ ReadExtensions $ readExtensions = null ,
235
+ CategoryLinkManagementInterface $ linkManagement = null
218
236
) {
219
237
$ this ->productFactory = $ productFactory ;
220
238
$ this ->collectionFactory = $ collectionFactory ;
@@ -239,6 +257,8 @@ public function __construct(
239
257
$ this ->cacheLimit = (int )$ cacheLimit ;
240
258
$ this ->readExtensions = $ readExtensions ?: \Magento \Framework \App \ObjectManager::getInstance ()
241
259
->get (ReadExtensions::class);
260
+ $ this ->linkManagement = $ linkManagement ?: \Magento \Framework \App \ObjectManager::getInstance ()
261
+ ->get (CategoryLinkManagementInterface::class);
242
262
}
243
263
244
264
/**
@@ -381,6 +401,9 @@ private function assignProductToWebsites(\Magento\Catalog\Model\Product $product
381
401
/**
382
402
* Process new gallery media entry.
383
403
*
404
+ * @deprecated
405
+ * @see MediaGalleryProcessor::processNewMediaGalleryEntry()
406
+ *
384
407
* @param ProductInterface $product
385
408
* @param array $newEntry
386
409
* @return $this
@@ -392,40 +415,8 @@ protected function processNewMediaGalleryEntry(
392
415
ProductInterface $ product ,
393
416
array $ newEntry
394
417
) {
395
- /** @var ImageContentInterface $contentDataObject */
396
- $ contentDataObject = $ newEntry ['content ' ];
418
+ $ this ->getMediaGalleryProcessor ()->processNewMediaGalleryEntry ($ product , $ newEntry );
397
419
398
- /** @var \Magento\Catalog\Model\Product\Media\Config $mediaConfig */
399
- $ mediaConfig = $ product ->getMediaConfig ();
400
- $ mediaTmpPath = $ mediaConfig ->getBaseTmpMediaPath ();
401
-
402
- $ relativeFilePath = $ this ->imageProcessor ->processImageContent ($ mediaTmpPath , $ contentDataObject );
403
- $ tmpFilePath = $ mediaConfig ->getTmpMediaShortUrl ($ relativeFilePath );
404
-
405
- if (!$ product ->hasGalleryAttribute ()) {
406
- throw new StateException (
407
- __ ("The product that was requested doesn't exist. Verify the product and try again. " )
408
- );
409
- }
410
-
411
- $ imageFileUri = $ this ->getMediaGalleryProcessor ()->addImage (
412
- $ product ,
413
- $ tmpFilePath ,
414
- isset ($ newEntry ['types ' ]) ? $ newEntry ['types ' ] : [],
415
- true ,
416
- isset ($ newEntry ['disabled ' ]) ? $ newEntry ['disabled ' ] : true
417
- );
418
- // Update additional fields that are still empty after addImage call
419
- $ this ->getMediaGalleryProcessor ()->updateImage (
420
- $ product ,
421
- $ imageFileUri ,
422
- [
423
- 'label ' => $ newEntry ['label ' ],
424
- 'position ' => $ newEntry ['position ' ],
425
- 'disabled ' => $ newEntry ['disabled ' ],
426
- 'media_type ' => $ newEntry ['media_type ' ],
427
- ]
428
- );
429
420
return $ this ;
430
421
}
431
422
@@ -500,68 +491,13 @@ private function processLinks(ProductInterface $product, $newLinks)
500
491
* @return $this
501
492
* @throws InputException
502
493
* @throws StateException
494
+ * @throws LocalizedException
503
495
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
504
496
*/
505
497
protected function processMediaGallery (ProductInterface $ product , $ mediaGalleryEntries )
506
498
{
507
- $ existingMediaGallery = $ product ->getMediaGallery ('images ' );
508
- $ newEntries = [];
509
- $ entriesById = [];
510
- if (!empty ($ existingMediaGallery )) {
511
- foreach ($ mediaGalleryEntries as $ entry ) {
512
- if (isset ($ entry ['value_id ' ])) {
513
- $ entriesById [$ entry ['value_id ' ]] = $ entry ;
514
- } else {
515
- $ newEntries [] = $ entry ;
516
- }
517
- }
518
- foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
519
- if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
520
- $ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
521
- if ($ updatedEntry ['file ' ] === null ) {
522
- unset($ updatedEntry ['file ' ]);
523
- }
524
- $ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
525
- } else {
526
- //set the removed flag
527
- $ existingEntry ['removed ' ] = true ;
528
- }
529
- }
530
- $ product ->setData ('media_gallery ' , ["images " => $ existingMediaGallery ]);
531
- } else {
532
- $ newEntries = $ mediaGalleryEntries ;
533
- }
534
-
535
- $ images = (array )$ product ->getMediaGallery ('images ' );
536
- $ images = $ this ->determineImageRoles ($ product , $ images );
537
-
538
- $ this ->getMediaGalleryProcessor ()->clearMediaAttribute ($ product , array_keys ($ product ->getMediaAttributes ()));
539
-
540
- foreach ($ images as $ image ) {
541
- if (!isset ($ image ['removed ' ]) && !empty ($ image ['types ' ])) {
542
- $ this ->getMediaGalleryProcessor ()->setMediaAttribute ($ product , $ image ['types ' ], $ image ['file ' ]);
543
- }
544
- }
499
+ $ this ->getMediaGalleryProcessor ()->processMediaGallery ($ product , $ mediaGalleryEntries );
545
500
546
- foreach ($ newEntries as $ newEntry ) {
547
- if (!isset ($ newEntry ['content ' ])) {
548
- throw new InputException (__ ('The image content is invalid. Verify the content and try again. ' ));
549
- }
550
- /** @var ImageContentInterface $contentDataObject */
551
- $ contentDataObject = $ this ->contentFactory ->create ()
552
- ->setName ($ newEntry ['content ' ]['data ' ][ImageContentInterface::NAME ])
553
- ->setBase64EncodedData ($ newEntry ['content ' ]['data ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
554
- ->setType ($ newEntry ['content ' ]['data ' ][ImageContentInterface::TYPE ]);
555
- $ newEntry ['content ' ] = $ contentDataObject ;
556
- $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
557
-
558
- $ finalGallery = $ product ->getData ('media_gallery ' );
559
- $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
560
- $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
561
- $ entriesById [$ newEntryId ] = $ newEntry ;
562
- $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
563
- $ product ->setData ('media_gallery ' , $ finalGallery );
564
- }
565
501
return $ this ;
566
502
}
567
503
@@ -572,6 +508,7 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
572
508
*/
573
509
public function save (ProductInterface $ product , $ saveOptions = false )
574
510
{
511
+ $ assignToCategories = false ;
575
512
$ tierPrices = $ product ->getData ('tier_price ' );
576
513
577
514
try {
@@ -589,6 +526,7 @@ public function save(ProductInterface $product, $saveOptions = false)
589
526
$ extensionAttributes = $ product ->getExtensionAttributes ();
590
527
if (empty ($ extensionAttributes ->__toArray ())) {
591
528
$ product ->setExtensionAttributes ($ existingProduct ->getExtensionAttributes ());
529
+ $ assignToCategories = true ;
592
530
}
593
531
} catch (NoSuchEntityException $ e ) {
594
532
$ existingProduct = null ;
@@ -626,6 +564,12 @@ public function save(ProductInterface $product, $saveOptions = false)
626
564
}
627
565
628
566
$ this ->saveProduct ($ product );
567
+ if ($ assignToCategories === true && $ product ->getCategoryIds ()) {
568
+ $ this ->linkManagement ->assignProductToCategories (
569
+ $ product ->getSku (),
570
+ $ product ->getCategoryIds ()
571
+ );
572
+ }
629
573
$ this ->removeProductFromLocalCache ($ product ->getSku ());
630
574
unset($ this ->instancesById [$ product ->getId ()]);
631
575
@@ -763,44 +707,19 @@ public function cleanCache()
763
707
$ this ->instancesById = null ;
764
708
}
765
709
766
- /**
767
- * Ascertain image roles, if they are not set against the gallery entries
768
- *
769
- * @param ProductInterface $product
770
- * @param array $images
771
- * @return array
772
- */
773
- private function determineImageRoles (ProductInterface $ product , array $ images ) : array
774
- {
775
- $ imagesWithRoles = [];
776
- foreach ($ images as $ image ) {
777
- if (!isset ($ image ['types ' ])) {
778
- $ image ['types ' ] = [];
779
- if (isset ($ image ['file ' ])) {
780
- foreach (array_keys ($ product ->getMediaAttributes ()) as $ attribute ) {
781
- if ($ image ['file ' ] == $ product ->getData ($ attribute )) {
782
- $ image ['types ' ][] = $ attribute ;
783
- }
784
- }
785
- }
786
- }
787
- $ imagesWithRoles [] = $ image ;
788
- }
789
- return $ imagesWithRoles ;
790
- }
791
-
792
710
/**
793
711
* Retrieve media gallery processor.
794
712
*
795
- * @return Product\Gallery\Processor
713
+ * @return MediaGalleryProcessor
796
714
*/
797
715
private function getMediaGalleryProcessor ()
798
716
{
799
- if (null === $ this ->mediaGalleryProcessor ) {
800
- $ this ->mediaGalleryProcessor = \Magento \Framework \App \ObjectManager::getInstance ()
801
- ->get (\ Magento \ Catalog \ Model \ Product \ Gallery \Processor ::class);
717
+ if (null === $ this ->mediaProcessor ) {
718
+ $ this ->mediaProcessor = \Magento \Framework \App \ObjectManager::getInstance ()
719
+ ->get (MediaGalleryProcessor ::class);
802
720
}
803
- return $ this ->mediaGalleryProcessor ;
721
+
722
+ return $ this ->mediaProcessor ;
804
723
}
805
724
806
725
/**
@@ -912,6 +831,7 @@ private function saveProduct($product): void
912
831
throw new CouldNotSaveException (__ ($ e ->getMessage ()));
913
832
} catch (LocalizedException $ e ) {
914
833
throw $ e ;
834
+ // phpcs:disable Magento2.Exceptions.ThrowCatch
915
835
} catch (\Exception $ e ) {
916
836
throw new CouldNotSaveException (
917
837
__ ('The product was unable to be saved. Please try again. ' ),
0 commit comments