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
171 changes: 171 additions & 0 deletions Block/Adminhtml/ReviewPopup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php
/**
* Copyright © Magefan ([email protected]). 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
{
$display = true;
$moduleName = $this->getModuleName();
if ($moduleName && strpos($moduleName, '_') !== false) {
$moduleName = explode('_', $moduleName)[1];
$extra = $this->_authSession->getUser()->getExtra();
if (!empty($extra)) {
$extra = json_decode($extra, true);
$rev = $extra['mf_review'][$moduleName] ?? null;
if ($rev) {
if ($rev['leave_review'] === false) {
if (!empty($rev['updated_at'])) {
try {
$given = new \DateTime($rev['updated_at']);
$threeDaysAgo = new \DateTime('-3 days');
if ($given > $threeDaysAgo) {
$display = false;
}

} catch (\Exception $e) {
}
}
} 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();
}
}
122 changes: 122 additions & 0 deletions Controller/Adminhtml/Review/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* Copyright © Magefan ([email protected]). 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'
];

/**
* @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);
}

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);
}
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()
]);
}
}

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]);
}
}

private function setReviewStatus($moduleName, $status = true)
{
$user = $this->_authSession->getUser();
$extra = $user->getExtra();
$extraArray = !empty($extra) ? json_decode($extra,true) : [];
$extraArray['mf_review'][$moduleName] = [
'updated_at' => (new \DateTime())->format('Y-m-d H:i:s'),
'leave_review' => $status
];
$user->setExtra(json_encode($extraArray));
$user->save();
}
}
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
6 changes: 5 additions & 1 deletion Model/GetModuleInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ public function execute($moduleName = null)
return $modulesInfo;
}

$moduleKey = explode('_', $moduleName)[1];
$moduleKey = $moduleName;
if (strpos($moduleName, '_') !== false) {
$moduleKey = explode('_', $moduleName)[1];
}

if (!isset($modulesInfo[$moduleKey])) {
$modulesInfo[$moduleKey] = new DataObject();
Expand Down Expand Up @@ -94,6 +97,7 @@ private function load(): array
$data = [];
try {
$url = 'https://mage' . 'fan.com/media/product-versions-extended.json';
$url = 'http://mage' . 'fan.loc/media/product-versions-extended.json';

// Make the request
$this->curl->get($url);
Expand Down
4 changes: 4 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<label>General Information</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="review" translate="label" type="select" sortOrder="60" showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
<label>Review</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
</group>
<group id="menu" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Magefan Menu</label>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<news>1</news>
<tip_trick>1</tip_trick>
<general>1</general>
<review>1</review>
</notification>
<menu>
<display>1</display>
Expand Down
3 changes: 3 additions & 0 deletions view/adminhtml/layout/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
<block class="Magefan\Community\Block\Adminhtml\Linv" name="magefan_community_linv" template="Magefan_Community::linv.phtml" after="header" />
<block class="Magefan\Community\Block\Adminhtml\HyvaThemeChecker" name="magefan.hyva.theme.checker" template="Magefan_Community::hyvathemechecker.phtml" after="header"/>
</referenceContainer>
<referenceContainer name="content">
<block class="Magefan\Community\Block\Adminhtml\ReviewPopup" name="magefan.review.popup" template="Magefan_Community::review-popup.phtml" after="-"/>
</referenceContainer>
</body>
</page>
Loading