Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions Block/Adminhtml/ReviewPopup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\Community\Block\Adminhtml;

use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Framework\Json\Helper\Data as JsonHelper;
use Magento\Framework\App\Route\Config as RouteConfig;
use Magefan\Community\Model\Config;
class ReviewPopup extends \Magento\Backend\Block\Template
{
private $reviewUrl = null;
private $moduleInfo = null;

/**
* @var \Magefan\Community\Model\GetModuleInfo
*/
private $getModuleInfo;

/**
* @var RouteConfig
*/
private $routeConfig;

/**
* @var Config
*/
private $config;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magefan\Community\Model\GetModuleInfo $getModuleInfo
* @param RouteConfig $routeConfig
* @param Config $config
* @param \Magento\Backend\Model\Auth\Session $authSession
* @param array $data
* @param JsonHelper|null $jsonHelper
* @param DirectoryHelper|null $directoryHelper
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magefan\Community\Model\GetModuleInfo $getModuleInfo,
RouteConfig $routeConfig,
Config $config,
\Magento\Backend\Model\Auth\Session $authSession,
array $data = [],
?JsonHelper $jsonHelper = null,
?DirectoryHelper $directoryHelper = null)
{
$this->getModuleInfo = $getModuleInfo;
$this->routeConfig = $routeConfig;
$this->config = $config;
$this->_authSession = $authSession;
parent::__construct($context, $data, $jsonHelper, $directoryHelper);
}

/**
* Get module name
*
* @return string|null
*/
public function getModuleName() {
$frontModule = $this->routeConfig->getModulesByFrontName($this->getRequest()->getModuleName());

if (!empty($frontModule[0]) && strpos($frontModule[0], 'Magefan_') !== false) {
return $frontModule[0];
}
return null;
}

/**
* Get module review url
*
* @return mixed|null
*/
public function getModuleReviewUrl()
{
if ($this->reviewUrl === null) {
$info = $this->getModuleInfo();

if (!empty($info['review_url'])) {
$this->reviewUrl = $info['review_url'];
}
}

return $this->reviewUrl;
}

/**
* Get the product name
*
* @return string
*/
public function getProductName(): string
{
$info = $this->getModuleInfo();;
if (!empty($info['product_name'])) {
return str_replace('Magento 2', 'Magefan' , $info['product_name']);
}
return '';
}

/**
* Get module info
*
* @return array|\Magento\Framework\DataObject|mixed
*/
private function getModuleInfo() {
if ($this->reviewUrl === null) {
$this->moduleInfo = $this->getModuleInfo->execute($this->getModuleName());
}
return $this->moduleInfo;
}


/**
* Check if we can display block
*
* @return bool
*/
private function canDisplay(): bool
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resourceConnection = $objectManager->get('Magento\Framework\App\ResourceConnection');
$display = true;
$moduleName = $this->getModuleName();
if ($moduleName && strpos($moduleName, '_') !== false) {
$moduleName = explode('_', $moduleName)[1];
$userId = $this->_authSession->getUser()->getId();
$connection = $resourceConnection->getConnection();
$tableName = $resourceConnection->getTableName('mf_review');
$select = $connection->select()
->from($tableName)
->where('user_id = ?', $userId)
->where('module_name = ?', $moduleName)
->limit(1);

$review = $connection->fetchRow($select);

if ($review) {
if ((int)$review['is_reviewed'] === 0) {
$updatedAt = $review['updated_at'] ?? null;
if ($updatedAt) {
try {
$given = new \DateTime($updatedAt);
$threeDaysAgo = new \DateTime('-3 days');
if ($given > $threeDaysAgo) {
$display = false;
}
} catch (\Exception $e) {
// ignore
}
}
} else {
$display = false;
}
}
}
return $this->config->receiveReview()
&& $this->getModuleReviewUrl()
&& $this->getProductName()
&& $display;
}

/**
* Prepare html output
*
* @return string
*/
protected function _toHtml(): string
{
if (!$this->canDisplay()) {
return '';
}
return parent::_toHtml();
}
}
173 changes: 173 additions & 0 deletions Controller/Adminhtml/Review/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
/**
* Copyright © Magefan (support@magefan.com). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\Community\Controller\Adminhtml\Review;

use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\HTTP\Client\Curl;
class Index extends \Magento\Backend\App\Action
{
private const REQUIRED_FIELDS = [
'ratings', 'nickname', 'firstname',
'lastname', 'email', 'title',
'detail'
];

private const RATINGS_OPTION = [
1 => [0 => 16],
2 => [1 => 17],
3 => [2 => 18],
4 => [3 => 19],
5 => [4 => 20]
];

/**
* @var \Magento\Backend\Model\Auth\Session
*/
private $_authSession;

/**
* @var JsonFactory
*/
private $resultJsonFactory;

/**
* @var Curl
*/
private $curl;

/**
* @param Context $context
* @param \Magento\Backend\Model\Auth\Session $authSession
* @param JsonFactory $resultJsonFactory
* @param Curl $curl
*/
public function __construct(
Context $context,
\Magento\Backend\Model\Auth\Session $authSession,
JsonFactory $resultJsonFactory,
Curl $curl
)
{
$this->_authSession = $authSession;
$this->resultJsonFactory = $resultJsonFactory;
$this->curl = $curl;
parent::__construct($context);
}

/**
* Executes the main review processing logic based on request parameters.
*
* @return \Magento\Framework\Controller\Result\Json
*/
public function execute()
{
$reviewData = [];
$result = $this->resultJsonFactory->create();
$moduleName = $this->_request->getParam('module');
if (!$moduleName) {
return $result->setData(['success' => false, 'message' => __('Module name is not specified.')]);
}
$reviewAction = $this->_request->getParam('action');
if (!$reviewAction) {
return $result->setData(['success' => false, 'message' => __('Action is not specified.')]);
}
if ($reviewAction == 'cancel') {
return $this->remindLater($moduleName);
}

foreach (self::REQUIRED_FIELDS as $field) {
if (!$this->_request->getParam($field)) {
return $result->setData(['success' => false,'message' => __('Please fill all required fields.')]);
}
$reviewData[$field] = $this->_request->getParam($field);

if ($field == 'ratings') {
$reviewData[$field] = self::RATINGS_OPTION[$this->_request->getParam($field)];
}
}
try {
$url = $reviewAction;
$postData = $reviewData;

$this->curl->post($url, $postData);

$response = $this->curl->getBody();

$this->setReviewStatus($moduleName);
return $result->setData([
'success' => true,
'response' => $response
]);
} catch (\Exception $e) {
return $result->setData([
'success' => false,
'message' => $e->getMessage()
]);
}
}

/**
* Handle a reminder to delay review prompt for a specific module.
*
* @param string $moduleName
* @return \Magento\Framework\Controller\Result\Json
*/
private function remindLater($moduleName)
{
$result = $this->resultJsonFactory->create();
try {
$this->setReviewStatus($moduleName, false);
return $result->setData(['success' => true]);
} catch (\Exception $e) {
return $result->setData(['success' => false]);
}
}

/**
* Updates or inserts the review status for a given module and user.
*
* @param string $moduleName
* @param bool $status
*
* @return void
*/
private function setReviewStatus($moduleName, $status = true)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resourceConnection = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resourceConnection->getConnection();
$tableName = $resourceConnection->getTableName('mf_review');
$userId = $this->_authSession->getUser()->getId();

$select = $connection->select()
->from($tableName)
->where('user_id = ?', $userId)
->where('module_name = ?', $moduleName);
$existing = $connection->fetchRow($select);

$data = [
'module_name' => $moduleName,
'user_id' => $userId,
'is_reviewed' => $status ? 1 : 0,
'updated_at' => (new \DateTime())->format('Y-m-d H:i:s'),
];

if ($existing) {
$connection->update(
$tableName,
$data,
['id = ?' => $existing['id']]
);
} else {
$data['created_at'] = (new \DateTime())->format('Y-m-d H:i:s');
$connection->insert($tableName, $data);
}
}
}
15 changes: 15 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Config
const XML_PATH_RECEIVE_NEWS = 'mfextension/notification/news';
const XML_PATH_RECEIVE_TIPS_AND_TRICKS = 'mfextension/notification/tip_trick';
const XML_PATH_RECEIVE_GENERAL_INFORMATION = 'mfextension/notification/general';
const XML_PATH_RECEIVE_REVIEW = 'mfextension/notification/review';

/**
* Display Menu
Expand Down Expand Up @@ -110,6 +111,20 @@ public function receiveGeneralInformation($storeId = null)
);
}

/**
* Receive Review
*
* @param null $storeId
* @return bool
*/
public function receiveReview($storeId = null): bool
{
return (bool)$this->getConfig(
self::XML_PATH_RECEIVE_REVIEW,
$storeId
);
}

/**
* Receive Notifications
*
Expand Down
Loading