Commit af42297d by halweg

feat: admin 操作 reviews

parent a9ab7d14
......@@ -108,7 +108,7 @@ class Images extends \Magento\Framework\View\Element\Template
*/
public function getResizedImagePath($item): string
{
return $this->imageHelper->resize($item->getPath(), self::REVIEW_COVER_WIDTH * 2);
return $this->imageHelper->resize($item->getPath(), self::REVIEW_COVER_WIDTH);
}
/**
......
<?php
namespace Joshine\Review\Data\Form\Element;
/**
* Class Image - add multiple file upload
*/
class Image extends \Magento\Framework\Data\Form\Element\Image
{
/**
* @return string
*/
public function getElementHtml()
{
$html = parent::getElementHtml();
$html = str_replace('type="file"', 'type="file" multiple accept="image/*" ', $html);
return $html;
}
}
<?php
namespace Joshine\Review\Model\Repository;
use Joshine\Review\Block\Images;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\Framework\Exception\CouldNotSaveException;
use Joshine\Review\Model\ResourceModel;
use Joshine\Review\Model\ImagesFactory;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Ui\Api\Data\BookmarkSearchResultsInterfaceFactory;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Filesystem\DirectoryList;
use Joshine\Review\Helper\ImageHelper;
class ImagesRepository
{
/**
* @var array
*/
private $images = [];
/**
* @var ResourceModel\Images
*/
private $imagesResource;
/**
* @var ImagesFactory
*/
private $imagesFactory;
/**
* @var ResourceModel\Images\CollectionFactory
*/
private $imagesCollectionFactory;
/**
* @var \Magento\Framework\Filesystem\Io\File
*/
private $ioFile;
/**
* @var null|string
*/
private $folderPath = null;
/**
* @var \Magento\Framework\Filesystem
*/
private $filesystem;
public function __construct(
\Joshine\Review\Model\ResourceModel\Images $imagesResource,
\Joshine\Review\Model\ImagesFactory $imagesFactory,
\Joshine\Review\Model\ResourceModel\Images\CollectionFactory $imagesCollectionFactory,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Filesystem\Io\File $ioFile
) {
$this->imagesResource = $imagesResource;
$this->imagesFactory = $imagesFactory;
$this->imagesCollectionFactory = $imagesCollectionFactory;
$this->ioFile = $ioFile;
$this->filesystem = $filesystem;
}
/**
* {@inheritdoc}
*/
public function get($imageId)
{
if (!isset($this->images[$imageId])) {
/** @var \Joshine\Review\Model\Images $image */
$image = $this->imagesFactory->create();
$this->imagesResource->load($image, $imageId);
if (!$image->getImageId()) {
throw new NoSuchEntityException(__('Rule with specified ID "%1" not found.', $imageId));
}
$this->images[$imageId] = $image;
}
return $this->images[$imageId];
}
/**
* @return string
*/
private function getFolderPath()
{
if ($this->folderPath === null) {
$this->folderPath = $this->filesystem->getDirectoryRead(
DirectoryList::MEDIA
)->getAbsolutePath(
ImageHelper::IMAGE_PATH
);
}
return $this->folderPath;
}
/**
* {@inheritdoc}
*/
public function delete( $image)
{
try {
$this->ioFile->rm($this->getFolderPath() . $image->getPath());
$this->ioFile->rm(
$this->getFolderPath() . 'resized/' . Images::REVIEW_COVER_WIDTH
);
$this->imagesResource->delete($image);
unset($this->images[$image->getId()]);
} catch (\Exception $e) {
if ($image->getImageId()) {
throw new CouldNotDeleteException(
__('Unable to remove image with ID %1. Error: %2', [$image->getImageId(), $e->getMessage()])
);
}
throw new CouldNotDeleteException(__('Unable to remove image. Error: %1', $e->getMessage()));
}
return true;
}
/**
* {@inheritdoc}
*/
public function deleteById($imageId)
{
$model = $this->get($imageId);
$this->delete($model);
return true;
}
}
<?php
namespace Joshine\Review\Plugin\Review\Block\Adminhtml\Edit;
use Joshine\Review\Helper\BlockHelper as BlockHelper;
use Joshine\Review\Model\Repository\VoteRepository;
use Magento\Backend\Block\Widget\Form\Generic as MagentoForm;
class Form
{
/**
* @var \Magento\Framework\Registry
*/
private $registry;
/**
* @var VoteRepository
*/
private $voteRepository;
/**
* @var BlockHelper
*/
private $blockHelper;
/**
* @var \Magento\Framework\DataObjectFactory
*/
private $dataObjectFactory;
public function __construct(
BlockHelper $blockHelper,
\Magento\Framework\Registry $registry,
VoteRepository $voteRepository,
\Magento\Framework\DataObjectFactory $dataObjectFactory
) {
$this->registry = $registry;
$this->voteRepository = $voteRepository;
$this->blockHelper = $blockHelper;
$this->dataObjectFactory = $dataObjectFactory;
}
/**
* @param MagentoForm $subject
* @param \Magento\Framework\Data\Form $form
* @return array
*/
public function beforeSetForm(
MagentoForm $subject,
\Magento\Framework\Data\Form $form
) {
$fieldset = $form->getElement('review_details') ?: $form->getElement('add_review_form');
$review = $this->registry->registry('review_data') ?: $this->dataObjectFactory->create();
$fieldset->addField(
'size_fits',
'select',
[
'label' =>__('Size Fits'),
'required' => false,
'values' => [
['value' => 0, 'label'=> __('---select---')],
['value' => 1, 'label'=> __('Small')],
['value' => 2, 'label'=> __('True to Size')],
['value' => 3, 'label'=> __('Large')],
] ,
'name' => 'size_fits',
'value' => (int)$review->getData('size_fits')
]
);
$form->setData('enctype', 'multipart/form-data');
$imageHtml = trim($this->blockHelper->getReviewImagesHtml($review->getId(), 8575));
$addImageTitle = __('Customer Images');
if ($imageHtml) {
$fieldset->addField(
'added_images',
'note',
[
'label' => __('Customer Images'),
'required' => false,
'name' => 'added_images',
'after_element_html' => $imageHtml
]
);
$addImageTitle = '';
}
$fieldset->addField(
'review_images',
\Joshine\Review\Data\Form\Element\Image::class,
[
'label' => $addImageTitle,
'name' => 'review_images[]',
'before_element_html' => __('Add new images')
]
);
return [$form];
}
/**
* @param $review
* @return \Magento\Framework\Phrase
*/
private function getReviewHelpfulness($review)
{
$vote = $this->voteRepository->getVotesCount($review->getId());
return __('%1 people found this helpful; %2 found this unhelpful.', $vote['plus'], $vote['minus']);
}
}
<?php
namespace Joshine\Review\Plugin\Review\Controller\Adminhtml\Product\Review;
use Magento\Review\Controller\Adminhtml\Product\Save as MagentoReview;
class Save
{
private $imagesRepository;
public function __construct(
\Joshine\Review\Model\Repository\ImagesRepository $imagesRepository
) {
$this->imagesRepository = $imagesRepository;
}
/**
* @param MagentoReview $subject
* @param $result
* @return mixed
*/
public function afterExecute(
MagentoReview $subject,
$result
) {
$this->removeImages($subject);
return $result;
}
/**
* @param MagentoReview $subject
*/
private function removeImages(MagentoReview $subject)
{
$images = $subject->getRequest()->getParam('review_remove_image', []);
if (is_array($images)) {
foreach ($images as $id => $image) {
$this->imagesRepository->deleteById($id);
}
}
}
}
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Review\Block\Adminhtml\Edit\Form">
<plugin name="Joshine_Review::add-answer-field"
type="Joshine\Review\Plugin\Review\Block\Adminhtml\Edit\Form"/>
</type>
<type name="Magento\Review\Block\Adminhtml\Add\Form">
<plugin name="Joshine_Review::add-module-fields"
type="Joshine\Review\Plugin\Review\Block\Adminhtml\Edit\Form"/>
</type>
<type name="Magento\Review\Controller\Adminhtml\Product\Save">
<plugin name="Joshine_Review::add-module-fields::remove-image"
type="Joshine\Review\Plugin\Review\Controller\Adminhtml\Product\Review\Save"/>
</type>
</config>
<?php
/** @var \Joshine\Review\Block\Images $block */
$collection = $block->getCollection();
// phpcs:ignoreFile
?>
<style>
.review-images {
display: flex;
flex-wrap: wrap;
}
.review-slider-item {
position: relative;
flex-shrink: 0;
width: 150px;
height: 150px;
line-height: 150px;
overflow: hidden;
margin-right: 5px;
margin-bottom: 0.2em;
vertical-align: middle;
border: 1px solid #9E9E9E;
}
.review-slider-item img {
width: 100%;
height: 100%;
object-fit: cover;
}
.review-remove-image {
line-height: normal;
position: absolute;
top: .3rem;
left: .3rem;
color: white;
font-weight: lighter;
padding: .3em;
background-color:rgba(0,0,0,0.5);
}
</style>
<?php if ($collection->getSize()) : ?>
<div id="review-id-<?= $block->escapeHtmlAttr($block->getReviewId()); ?>"
class="review-images">
<?php foreach ($collection as $item) : ?>
<div class="review-slider-item" data-review-js="slider-item">
<a href="<?= $block->getFullImagePath($item); ?>" target="_blank" class="review-image-link" data-review-js="review-image">
<img class="review-image"
src="<?= /* @noEscape */ $block->getResizedImagePath($item);?>"
title="<?= $block->escapeHtml(__('Review image')); ?>"
alt="<?= $block->escapeHtml(__('Review image')); ?>"
/>
</a>
<label class="review-remove-image" title="<?= $block->escapeHtml(__('Remove image')); ?>">
<input class="review-checkbox"
value="1"
type="checkbox"
name="review_remove_image[<?= /* @noEscape */ (int)$item->getId();?>]"
style="margin-top: 0;"
>
<?= $block->escapeHtml(__('Remove'));?>
</label>
</div>
<?php endforeach; ?>
</div>
<?php endif;?>
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