init
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Review\Block\Adminhtml\Review;
|
||||
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class Images extends Template
|
||||
{
|
||||
public function __construct(
|
||||
Template\Context $context,
|
||||
private readonly ResourceConnection $resourceConnection,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
private readonly Registry $registry,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
public function getReviewId(): int
|
||||
{
|
||||
$reviewId = (int)$this->getData('review_id');
|
||||
if ($reviewId > 0) {
|
||||
return $reviewId;
|
||||
}
|
||||
|
||||
$review = $this->registry->registry('review_data');
|
||||
return $review && $review->getId() ? (int)$review->getId() : 0;
|
||||
}
|
||||
|
||||
public function getImages(): array
|
||||
{
|
||||
$reviewId = $this->getReviewId();
|
||||
if ($reviewId <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$rows = $connection->fetchAll(
|
||||
$connection->select()
|
||||
->from($this->resourceConnection->getTableName('esun_review_image'))
|
||||
->where('review_id = ?', $reviewId)
|
||||
->order('sort_order ASC')
|
||||
->order('image_id ASC')
|
||||
);
|
||||
$mediaUrl = $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
|
||||
|
||||
return array_map(static fn (array $row): array => [
|
||||
'url' => $mediaUrl . ltrim((string)$row['image_path'], '/'),
|
||||
'thumb' => $mediaUrl . ltrim((string)($row['thumbnail_path'] ?: $row['image_path']), '/'),
|
||||
'type' => (string)($row['media_type'] ?? 'image'),
|
||||
'status' => (int)$row['status'],
|
||||
], $rows);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Review\Block\Customer;
|
||||
|
||||
use Esun\Review\Model\Order\AwaitingReviewProvider;
|
||||
use Magento\Catalog\Helper\Image;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
use Magento\Review\Model\RatingFactory;
|
||||
use Magento\Sales\Model\ResourceModel\Order\Item\Collection;
|
||||
|
||||
class Awaiting extends Template
|
||||
{
|
||||
private ?Collection $items = null;
|
||||
|
||||
public function __construct(
|
||||
Template\Context $context,
|
||||
private readonly Session $session,
|
||||
private readonly AwaitingReviewProvider $provider,
|
||||
private readonly RatingFactory $ratings,
|
||||
private readonly Image $imageHelper,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
public function getItems(): Collection
|
||||
{
|
||||
return $this->items ??= $this->provider->getCollection((int)$this->session->getCustomerId());
|
||||
}
|
||||
|
||||
public function getRatings(): \Magento\Review\Model\ResourceModel\Rating\Collection
|
||||
{
|
||||
return $this->ratings->create()->getResourceCollection()->addEntityFilter('product')
|
||||
->setPositionOrder()->addRatingPerStoreName((int)$this->_storeManager->getStore()->getId())
|
||||
->setStoreFilter((int)$this->_storeManager->getStore()->getId())->setActiveFilter(true)
|
||||
->load()->addOptionToItems();
|
||||
}
|
||||
|
||||
public function getImageUrl(\Magento\Sales\Model\Order\Item $item): string
|
||||
{
|
||||
return $item->getProduct()
|
||||
? $this->imageHelper->init($item->getProduct(), 'wishlist_thumbnail')->getUrl()
|
||||
: $this->imageHelper->getDefaultPlaceholderUrl('thumbnail');
|
||||
}
|
||||
|
||||
public function getPostUrl(int $productId): string
|
||||
{
|
||||
return $this->getUrl('review/product/post', ['id' => $productId, '_secure' => $this->getRequest()->isSecure()]);
|
||||
}
|
||||
|
||||
protected function _prepareLayout(): self
|
||||
{
|
||||
parent::_prepareLayout();
|
||||
$pager = $this->getLayout()->createBlock(\Magento\Theme\Block\Html\Pager::class, 'esun.review.awaiting.pager')
|
||||
->setLimit(10)
|
||||
->setShowPerPage(false)
|
||||
->setCollection($this->getItems());
|
||||
$this->setChild('pager', $pager);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Review\Block\Order;
|
||||
|
||||
use Esun\Review\Model\Order\ReviewEligibilityService;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
use Magento\Review\Model\RatingFactory;
|
||||
|
||||
class ReviewableItems extends Template
|
||||
{
|
||||
public function __construct(Template\Context $context, private readonly Registry $registry, private readonly Session $session, private readonly ReviewEligibilityService $eligibility, private readonly RatingFactory $ratings, array $data = []) { parent::__construct($context, $data); }
|
||||
public function getOrder(): \Magento\Sales\Model\Order { return $this->registry->registry('current_order'); }
|
||||
public function getItems(): array { return array_values(array_filter($this->getOrder()->getAllVisibleItems(), fn($item) => (float)$item->getQtyOrdered() - (float)$item->getQtyCanceled() > 0)); }
|
||||
public function canReview(\Magento\Sales\Model\Order\Item $item): bool { return $this->eligibility->canReview($item, (int)$this->session->getCustomerId()); }
|
||||
public function hasReviewed(\Magento\Sales\Model\Order\Item $item): bool { return $this->eligibility->hasReviewed((int)$this->session->getCustomerId(), (int)$item->getProductId()); }
|
||||
public function getRatings(): \Magento\Review\Model\ResourceModel\Rating\Collection { return $this->ratings->create()->getResourceCollection()->addEntityFilter('product')->setPositionOrder()->addRatingPerStoreName((int)$this->_storeManager->getStore()->getId())->setStoreFilter((int)$this->_storeManager->getStore()->getId())->setActiveFilter(true)->load()->addOptionToItems(); }
|
||||
public function getPostUrl(int $productId): string { return $this->getUrl('review/product/post', ['id' => $productId, '_secure' => $this->getRequest()->isSecure()]); }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Review\Block\Product;
|
||||
|
||||
use Magento\Customer\Model\Session as CustomerSession;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\View\Element\Template\Context;
|
||||
use Magento\Review\Model\ResourceModel\Review\CollectionFactory;
|
||||
|
||||
class Review extends \Magento\Review\Block\Product\Review
|
||||
{
|
||||
public function __construct(
|
||||
Context $context,
|
||||
Registry $registry,
|
||||
CollectionFactory $collectionFactory,
|
||||
private readonly CustomerSession $customerSession,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $registry, $collectionFactory, $data);
|
||||
}
|
||||
|
||||
public function canShowReviewForm(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Review\Block\Review;
|
||||
|
||||
use Magento\Customer\Model\Session as CustomerSession;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
use Magento\Review\Model\Review;
|
||||
use Magento\Review\Model\ResourceModel\Review\Collection;
|
||||
use Magento\Review\Model\ResourceModel\Review\CollectionFactory;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class Data extends Template
|
||||
{
|
||||
public function __construct(
|
||||
Template\Context $context,
|
||||
private readonly ResourceConnection $resourceConnection,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
private readonly CustomerSession $customerSession,
|
||||
private readonly Registry $registry,
|
||||
private readonly CollectionFactory $reviewCollectionFactory,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
public function getCurrentProductId(?int $fallback = null): int
|
||||
{
|
||||
$product = $this->registry->registry('current_product');
|
||||
if ($product && $product->getId()) {
|
||||
return (int)$product->getId();
|
||||
}
|
||||
|
||||
return (int)$fallback;
|
||||
}
|
||||
|
||||
public function getSummary(int $productId): array
|
||||
{
|
||||
if ($productId <= 0) {
|
||||
return ['score' => 0.0, 'count' => 0, 'percent' => 0];
|
||||
}
|
||||
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$row = $connection->fetchRow(
|
||||
$connection->select()
|
||||
->from(['r' => $this->resourceConnection->getTableName('review')], [
|
||||
'reviews_count' => new \Zend_Db_Expr('COUNT(DISTINCT r.review_id)'),
|
||||
'avg_percent' => new \Zend_Db_Expr('AVG(v.percent)'),
|
||||
])
|
||||
->joinLeft(
|
||||
['v' => $this->resourceConnection->getTableName('rating_option_vote')],
|
||||
'v.review_id = r.review_id',
|
||||
[]
|
||||
)
|
||||
->where('r.entity_pk_value = ?', $productId)
|
||||
->where('r.status_id = ?', Review::STATUS_APPROVED)
|
||||
) ?: [];
|
||||
|
||||
$percent = (float)($row['avg_percent'] ?? 0);
|
||||
return [
|
||||
'score' => $percent > 0 ? round($percent / 20, 1) : 0.0,
|
||||
'count' => (int)($row['reviews_count'] ?? 0),
|
||||
'percent' => (int)round($percent),
|
||||
];
|
||||
}
|
||||
|
||||
public function getReviewsCollection(int $productId, int $pageSize = 10): Collection
|
||||
{
|
||||
$collection = $this->reviewCollectionFactory->create()
|
||||
->addStatusFilter(Review::STATUS_APPROVED)
|
||||
->addEntityFilter('product', $productId)
|
||||
->setDateOrder()
|
||||
->setPageSize($pageSize)
|
||||
->setCurPage((int)$this->getRequest()->getParam('p', 1));
|
||||
|
||||
$collection->load();
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
public function getReviewPercents(array $reviewIds): array
|
||||
{
|
||||
$reviewIds = array_values(array_filter(array_map('intval', $reviewIds)));
|
||||
if (!$reviewIds) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$rows = $connection->fetchPairs(
|
||||
$connection->select()
|
||||
->from($this->resourceConnection->getTableName('rating_option_vote'), [
|
||||
'review_id',
|
||||
'percent' => new \Zend_Db_Expr('ROUND(AVG(percent))'),
|
||||
])
|
||||
->where('review_id IN (?)', $reviewIds)
|
||||
->group('review_id')
|
||||
);
|
||||
|
||||
return array_map('intval', $rows);
|
||||
}
|
||||
|
||||
public function getCustomerNames(array $customerIds): array
|
||||
{
|
||||
$customerIds = array_values(array_filter(array_map('intval', $customerIds)));
|
||||
if (!$customerIds) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$rows = $connection->fetchAll(
|
||||
$connection->select()
|
||||
->from($this->resourceConnection->getTableName('customer_entity'), [
|
||||
'entity_id',
|
||||
'firstname',
|
||||
'lastname',
|
||||
])
|
||||
->where('entity_id IN (?)', $customerIds)
|
||||
);
|
||||
|
||||
$names = [];
|
||||
foreach ($rows as $row) {
|
||||
$name = trim((string)($row['firstname'] ?? '') . ' ' . (string)($row['lastname'] ?? ''));
|
||||
if ($name !== '') {
|
||||
$names[(int)$row['entity_id']] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
return $names;
|
||||
}
|
||||
|
||||
public function getCustomerPhotos(int $productId): array
|
||||
{
|
||||
if ($productId <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$rows = $connection->fetchAll(
|
||||
$connection->select()
|
||||
->from(['i' => $this->resourceConnection->getTableName('esun_review_image')], [
|
||||
'image_path',
|
||||
'thumbnail_path',
|
||||
'review_id',
|
||||
])
|
||||
->join(
|
||||
['r' => $this->resourceConnection->getTableName('review')],
|
||||
'r.review_id = i.review_id',
|
||||
[]
|
||||
)
|
||||
->where('r.entity_pk_value = ?', $productId)
|
||||
->where('r.status_id = ?', Review::STATUS_APPROVED)
|
||||
->where('i.status = ?', 1)
|
||||
->where('i.media_type = ?', 'image')
|
||||
->order('i.created_at DESC')
|
||||
->order('i.image_id DESC')
|
||||
);
|
||||
|
||||
$mediaUrl = $this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA);
|
||||
return array_map(static fn (array $row): array => [
|
||||
'url' => $mediaUrl . ltrim((string)$row['image_path'], '/'),
|
||||
'thumb' => $mediaUrl . ltrim((string)($row['thumbnail_path'] ?: $row['image_path']), '/'),
|
||||
'review_id' => (int)$row['review_id'],
|
||||
], $rows);
|
||||
}
|
||||
|
||||
public function canWriteReview(int $productId): bool
|
||||
{
|
||||
$customerId = $this->getCustomerId();
|
||||
if ($productId <= 0 || $customerId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$reviewId = (int)$connection->fetchOne(
|
||||
$connection->select()
|
||||
->from(['r' => $this->resourceConnection->getTableName('review')], ['review_id'])
|
||||
->join(
|
||||
['d' => $this->resourceConnection->getTableName('review_detail')],
|
||||
'd.review_id = r.review_id',
|
||||
[]
|
||||
)
|
||||
->where('r.entity_pk_value = ?', $productId)
|
||||
->where('d.customer_id = ?', $customerId)
|
||||
->limit(1)
|
||||
);
|
||||
|
||||
return $reviewId <= 0;
|
||||
}
|
||||
|
||||
public function canVote(array $review): bool
|
||||
{
|
||||
$customerId = $this->getCustomerId();
|
||||
return $customerId > 0 && (int)($review['customer_id'] ?? 0) !== $customerId;
|
||||
}
|
||||
|
||||
public function getCustomerId(): int
|
||||
{
|
||||
return $this->customerSession->isLoggedIn() ? (int)$this->customerSession->getCustomerId() : 0;
|
||||
}
|
||||
|
||||
public function getVoteUrl(): string
|
||||
{
|
||||
return $this->getUrl('esun_review/review/vote');
|
||||
}
|
||||
|
||||
public function getVotes(array $reviewIds): array
|
||||
{
|
||||
$reviewIds = array_values(array_filter(array_map('intval', $reviewIds)));
|
||||
if (!$reviewIds) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$rows = $connection->fetchAll(
|
||||
$connection->select()
|
||||
->from($this->resourceConnection->getTableName('esun_review_vote'), [
|
||||
'review_id',
|
||||
'vote_type',
|
||||
'count' => new \Zend_Db_Expr('COUNT(*)'),
|
||||
])
|
||||
->where('review_id IN (?)', $reviewIds)
|
||||
->group(['review_id', 'vote_type'])
|
||||
);
|
||||
|
||||
$result = [];
|
||||
foreach ($reviewIds as $reviewId) {
|
||||
$result[$reviewId] = ['up' => 0, 'down' => 0, 'customer_vote' => 0];
|
||||
}
|
||||
foreach ($rows as $row) {
|
||||
$key = (int)$row['vote_type'] > 0 ? 'up' : 'down';
|
||||
$result[(int)$row['review_id']][$key] = (int)$row['count'];
|
||||
}
|
||||
|
||||
$customerId = $this->getCustomerId();
|
||||
if ($customerId > 0) {
|
||||
$customerRows = $connection->fetchPairs(
|
||||
$connection->select()
|
||||
->from($this->resourceConnection->getTableName('esun_review_vote'), ['review_id', 'vote_type'])
|
||||
->where('review_id IN (?)', $reviewIds)
|
||||
->where('customer_id = ?', $customerId)
|
||||
);
|
||||
foreach ($customerRows as $reviewId => $voteType) {
|
||||
$result[(int)$reviewId]['customer_vote'] = (int)$voteType;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Review\Block\Review;
|
||||
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class Images extends Template
|
||||
{
|
||||
public function __construct(
|
||||
Template\Context $context,
|
||||
private readonly ResourceConnection $resourceConnection,
|
||||
private readonly StoreManagerInterface $storeManager,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $data);
|
||||
}
|
||||
|
||||
public function getImages(int $reviewId): array
|
||||
{
|
||||
if ($reviewId <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$connection = $this->resourceConnection->getConnection();
|
||||
$rows = $connection->fetchAll(
|
||||
$connection->select()
|
||||
->from($this->resourceConnection->getTableName('esun_review_image'))
|
||||
->where('review_id = ?', $reviewId)
|
||||
->where('status = ?', 1)
|
||||
->order('sort_order ASC')
|
||||
);
|
||||
$mediaUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
|
||||
|
||||
return array_map(static fn (array $row): array => [
|
||||
'url' => $mediaUrl . ltrim((string)$row['image_path'], '/'),
|
||||
'thumb' => $mediaUrl . ltrim((string)($row['thumbnail_path'] ?: $row['image_path']), '/'),
|
||||
'type' => (string)($row['media_type'] ?? 'image'),
|
||||
], $rows);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user