Commit 56cdc04b by liumengfei

Merge branch 'developer' into production

parents 030cdb93 15ce2546
<?php
/**
* Product Media Attribute Write Service
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Joshine\Catalog\Api;
/**
* @todo implement this interface as a \Magento\Catalog\Model\Product\Attribute\Media\GalleryManagement.
* Move logic from service there.
* @api
* @since 100.0.2
*/
interface ProductAttributeMediaGalleryManagementInterface
{
/**
* Updates the specified products in item array.
*
* @api
* @param mixed $entry
* @return \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface[]
*/
public function add(
$entry
);
/**
* Create new gallery entry
*
* @param string $sku
* @param \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry
* @return int gallery entry ID
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
*/
public function create(
$sku,
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry
);
/**
* Update gallery entry
*
* @param string $sku
* @param \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry
* @return bool
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
*/
public function update(
$sku,
\Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface $entry
);
/**
* Retrieve the list of gallery entries associated with given product
*
* @param string $sku
* @return \Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface[]
*/
public function getList($sku);
}
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Joshine\Catalog\Api;
/**
* @api
* @since 100.0.2
*/
interface ProductRepositoryInterface
{
/**
* Create product
*
* @api
* @param mixed $product
* @return boolean
*/
public function create($product);
/**
* Create products
*
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @param bool $saveOptions
* @return \Magento\Catalog\Api\Data\ProductInterface
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\StateException
* @throws \Magento\Framework\Exception\CouldNotSaveException
*/
public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveOptions = false);
/**
* Get info about product by product SKU
*
* @param string $sku
* @param bool $editMode
* @param int|null $storeId
* @param bool $forceReload
* @return \Magento\Catalog\Api\Data\ProductInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function get($sku, $editMode = false, $storeId = null, $forceReload = false);
/**
* Get info about product by product id
*
* @param int $productId
* @param bool $editMode
* @param int|null $storeId
* @param bool $forceReload
* @return \Magento\Catalog\Api\Data\ProductInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getById($productId, $editMode = false, $storeId = null, $forceReload = false);
/**
* Delete product
*
* @param \Magento\Catalog\Api\Data\ProductInterface $product
* @return bool Will returned True if deleted
* @throws \Magento\Framework\Exception\StateException
*/
public function delete(\Magento\Catalog\Api\Data\ProductInterface $product);
/**
* @param string $sku
* @return bool Will returned True if deleted
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\StateException
*/
public function deleteById($sku);
/**
* Get product list
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return \Magento\Catalog\Api\Data\ProductSearchResultsInterface
*/
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria);
}
<?php
namespace Joshine\Catalog\Block\Adminhtml\Category\Tab;
use Magento\Catalog\Block\Adminhtml\Category\Tab\Product as JoshineProduct;
class Product extends JoshineProduct
{
public function setCollection($collection){
$collection->addAttributeToSelect('thumbnail');
parent::setCollection($collection);
}
protected function _prepareColumns(){
parent::_prepareColumns();
$this->addColumnAfter('image', array(
'header' => __('image'),
'index' => 'thumbnail',
'renderer' =>'Joshine\Catalog\Block\Adminhtml\Helper\Form\Image'
), 'sku');
$this->sortColumnsByOrder();
return $this;
}
}
<?php
namespace Joshine\Catalog\Block\Adminhtml\Helper\Form;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Framework\DataObject;
class Image extends AbstractRenderer
{
public function render(DataObject $row)
{
$objectManager =\Magento\Framework\App\ObjectManager::getInstance();
$helperImport = $objectManager->get('\Magento\Catalog\Helper\Image');
$imageUrl = $helperImport->init($row, 'thumbnail')
->setImageFile($row->getData('thumbnail')) // image,small_image,thumbnail
->resize(380)
->getUrl();
if (empty($row->getData('thumbnail')) ){
return "<span> No pictures </span>";
}
return "<img src=". $imageUrl ." width='100' >";
}
}
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Joshine\Catalog\Helper;
use Magento\Catalog\Helper\Image;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Gallery\ReadHandler as GalleryReadHandler;
use Magento\Framework\App\Helper\Context;
/**
* Catalog data helper
*
* @api
*
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @since 100.0.2
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
protected $galleryReadHandler;
/**
* Catalog Image Helper
*
* @var \Magento\Catalog\Helper\Image
*/
protected $imageHelper;
public function __construct(
GalleryReadHandler $galleryReadHandler, Context $context, Image $imageHelper)
{
$this->galleryReadHandler = $galleryReadHandler;
$this->imageHelper = $imageHelper;
parent::__construct($context);
}
public function getSecondProductImage(Product $product){
$this->galleryReadHandler->execute($product);
$images = $product->getMediaGalleryImages();
if(count($images)>=2){
$index = 0;
foreach ($images as $image) {
$index++;
if($index==2){
$image->setData(
'second_image',
$this->imageHelper->init($product, 'category_page_list')
->constrainOnly(true)->keepAspectRatio(true)->keepFrame(false)
->setImageFile($image->getFile())
->getUrl()
);
$result = $image;
break;
}
}
return $result;
}else{
return null;
}
}
}
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Joshine\Catalog\Model\Product\Gallery;
use Magento\Catalog\Api\Data\ProductAttributeMediaGalleryEntryInterface;
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\ImageContent;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\Api\ImageContentValidatorInterface;
use Magento\Framework\View\Asset\NotationResolver\Variable;
use Magento\Framework\Webapi\Rest\Request;
/**
* Class GalleryManagement
*
* Provides implementation of api interface ProductAttributeMediaGalleryManagementInterface
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class GalleryManagement implements \Joshine\Catalog\Api\ProductAttributeMediaGalleryManagementInterface
{
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterface
*/
protected $productRepository;
/**
* @var ImageContentValidatorInterface
*/
protected $contentValidator;
/**
* @var ProductInterfaceFactory
*/
private $productInterfaceFactory;
protected $entry;
/**
* @param ProductRepositoryInterface $productRepository
* @param ImageContentValidatorInterface $contentValidator
* @param ProductInterfaceFactory|null $productInterfaceFactory
* @param DeleteValidator|null $deleteValidator
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
ProductRepositoryInterface $productRepository,
ImageContentValidatorInterface $contentValidator,
ProductAttributeMediaGalleryEntryInterface $entry,
?ProductInterfaceFactory $productInterfaceFactory = null
) {
$this->productRepository = $productRepository;
$this->contentValidator = $contentValidator;
$this->productInterfaceFactory = $productInterfaceFactory
?? ObjectManager::getInstance()->get(ProductInterfaceFactory::class);
$this->entry = $entry;
}
/**
* @inheritdoc
*/
public function add($content)
{
if (empty($content)) {
throw new StateException(__("request update date is null ,so failed"));
}
$ids = [];
$sku = $content[0]['sku'];
foreach ($content as $entry){
$entry_id = 0;
if (key_exists("id",$entry)){
$entry_id= (int)$entry["id"];
}
$this->entry->setMediaType($entry['media_type']);
$this->entry->setDisabled($entry['disabled']);
$this->entry->setPosition($entry['position']);
$this->entry->setLabel($entry['label']);
$this->entry->setTypes($entry['types']);
if ($entry_id){
$this->entry->setId($entry_id);
$result = $this->update($sku,$this->entry);
if ($result){
$id = $entry_id;
}else{
throw new StateException(__("$entry_id update failed"));
}
}else{
$this->entry->setFile($entry['file']);
$imageContent = new ImageContent();
$imageContent->setBase64EncodedData($entry['content']['base64_encoded_data']);
$imageContent->setName($entry['content']['name']);
$imageContent->setType($entry['content']['type']);
$this->entry->setContent($imageContent);
$id = $this->create($sku,$this->entry);
}
if (is_numeric((int)$id)){
$ids []= (int)$id;
}
$this->entry->setData([]);
}
if (!$ids){
throw new StateException(__('Batch upload of product images failed'));
}
$list = $this->getList($sku);
foreach ($list as $key => $v) {
if (!in_array($v->getId(),$ids)) {
unset($list[$key]);
}
}
array_multisort($list);
return $list;
}
/**
* @inheritdoc
*/
public function create($sku,ProductAttributeMediaGalleryEntryInterface $entry)
{
/** @var $entry ProductAttributeMediaGalleryEntryInterface */
$entryContent = $entry->getContent();
if (!$this->contentValidator->isValid($entryContent)) {
throw new InputException(__('The image content is invalid. Verify the content and try again.'));
}
$product = $this->productRepository->get($sku, true);
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
$existingEntryIds = [];
if ($existingMediaGalleryEntries == null) {
// set all media types if not specified
if ($entry->getTypes() == null) {
$entry->setTypes(array_keys($product->getMediaAttributes()));
}
$existingMediaGalleryEntries = [$entry];
} else {
foreach ($existingMediaGalleryEntries as $existingEntries) {
$existingEntryIds[$existingEntries->getId()] = $existingEntries->getId();
}
$existingMediaGalleryEntries[] = $entry;
}
$product = $this->productInterfaceFactory->create();
$product->setSku($sku);
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
try {
$product = $this->productRepository->save($product);
} catch (\Exception $e) {
if ($e instanceof InputException) {
throw $e;
} else {
throw new StateException(__("The product can't be saved."));
}
}
foreach ($product->getMediaGalleryEntries() as $entry) {
if (!isset($existingEntryIds[$entry->getId()])) {
return $entry->getId();
}
}
throw new StateException(__('The new media gallery entry failed to save.'));
}
/**
* @inheritdoc
*/
public function update($sku, ProductAttributeMediaGalleryEntryInterface $entry)
{
$product = $this->productRepository->get($sku, true);
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
if ($existingMediaGalleryEntries == null) {
throw new NoSuchEntityException(
__('No image with the provided ID was found. Verify the ID and try again.')
);
}
$found = false;
$entryTypes = (array)$entry->getTypes();
foreach ($existingMediaGalleryEntries as $key => $existingEntry) {
$existingEntryTypes = (array)$existingEntry->getTypes();
$existingEntry->setTypes(array_diff($existingEntryTypes, $entryTypes));
if ($existingEntry->getId() == $entry->getId()) {
$found = true;
$existingMediaGalleryEntries[$key] = $entry;
}
}
if (!$found) {
throw new NoSuchEntityException(
__('No image with the provided ID was found. Verify the ID and try again.')
);
}
$product = $this->productInterfaceFactory->create();
$product->setSku($sku);
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
try {
$this->productRepository->save($product);
} catch (\Exception $exception) {
throw new StateException(__("The product can't be saved."));
}
return true;
}
/**
* @inheritdoc
*/
public function getList($sku)
{
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->productRepository->get($sku);
return $product->getMediaGalleryEntries();
}
}
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Adminhtml\Category\Tab\Product" type="Joshine\Catalog\Block\Adminhtml\Category\Tab\Product" />
<preference for="Joshine\Catalog\Api\ProductAttributeMediaGalleryManagementInterface" type="Joshine\Catalog\Model\Product\Gallery\GalleryManagement" />
<preference for="Joshine\Catalog\Api\ProductRepositoryInterface" type="Joshine\Catalog\Model\ProductRepository" />
</config>
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Joshine_Catalog" setup_version="1.0.0">
</module>
</config>
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
<route url="/V1/products/media" method="POST">
<service class="Joshine\Catalog\Api\ProductAttributeMediaGalleryManagementInterface" method="add"/>
<resources>
<resource ref="Magento_Catalog::catalog"/>
</resources>
</route>
<route url="/V1/product" method="POST">
<service class="Joshine\Catalog\Api\ProductRepositoryInterface" method="create"/>
<resources>
<resource ref="Magento_Catalog::products" />
</resources>
</route>
</routes>
<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Joshine_Catalog',
__DIR__
);
......@@ -14,10 +14,10 @@
var dis = $(this).siblings("ul").css("display");
if (dis == "none"){
$(this).siblings("ul").css("display","block");
$(this).children("span").html('^')
$(this).children("strong").html('^')
}else{
$(this).siblings("ul").css("display","none");
$(this).children("span").html('>')
$(this).children("strong").html('>')
}
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment