| Current Path : /var/www/magento.test.indacotrentino.com/www/app/code/Torresani/QrCodeRedirect/Block/ |
| Current File : /var/www/magento.test.indacotrentino.com/www/app/code/Torresani/QrCodeRedirect/Block/Redirect.php |
<?php
namespace Torresani\QrCodeRedirect\Block;
use \Magento\Framework\View\Element\Template;
class Redirect extends Template
{
protected $_redirect;
protected $_response;
protected $resource;
protected $connection;
protected $redirectFactory;
/**
* Constructor
*
* @param Context $context
* @param array $data
*/
public function __construct(
\Magento\Framework\App\Request\Http $request,
\Magento\Customer\Model\Session $customer,
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\App\Response\RedirectInterface $redirect,
\Magento\Framework\App\ResponseInterface $response,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Framework\App\ResourceConnection $resource,
\Torresani\QrCodeRedirect\Model\RedirectFactory $redirectFactory,
array $data = []
){
$this->request = $request;
$this->customer = $customer;
$this->_redirect = $redirect;
$this->_response = $response;
$this->messageManager = $messageManager;
$this->resource = $resource;
$this->connection = $resource->getConnection();
$this->redirectFactory = $redirectFactory;
parent::__construct($context, $data);
}
/**
* https://indaco2.38121.it/qrcode/?code=aaaa
* https://www.qrcode-monkey.com/
*
* @return Post[]
*/
public function doRedirect()
{
$customer = $this->customer;
$customerEmail = $customer->getCustomer()->getEmail();
$code = $this->request->getParam("code");
$redirectRecord = $this->findRedirectByStoreAndCode(2, $code);
if ($redirectRecord) {
$url = $redirectRecord->getUrl();
if(1==0 && !$customer->isLoggedIn()) { // Disable mandatory login
$this->insertVisitLog(2, $customer->getCustomer()->getId(), $code, $url, 'requestLogin');
$customer->authenticate();
} else {
$this->insertVisitLog(2, $customer->getCustomer()->getId(), $code, $url, 'qrcode');
$this->_redirect->redirect($this->_response, $url);
}
}
}
public function findRedirectByStoreAndCode($storeId, $code) {
$redirectModel = $this->redirectFactory->create();
$redirectCollection = $redirectModel->getCollection();
$redirectCollection->addFieldToFilter('store_id', $storeId);
$redirectCollection->addFieldToFilter('code', $code);
if ($redirectCollection->getSize() > 0) {
$firstRecord = $redirectCollection->getFirstItem();
return $firstRecord;
} else {
return null;
}
}
public function insertVisitLog($storeId, $customerId, $code, $url, $action) {
try {
$ip = null;
if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$operating_system = explode(";",$user_agent)[1]."";
$userAgentParts = explode(" ", $user_agent);
$browser = end($userAgentParts);
$data = [
[
'store_id' => $storeId, 'customer_id' => $customerId, 'code' => $code, 'url' => $url,
'action' => $action, 'ip' => $ip, 'user_agent' =>$_SERVER['HTTP_USER_AGENT'], 'operating_system' =>$operating_system, 'browser' =>$browser,
]
];
$tableName = $this->resource->getTableName('qrcoderedirect_visit');
$this->connection->insertMultiple($tableName, $data);
} catch (\Exception $e) {
$this->messageManager->addException($e, __('Cannot insert data.'));
}
}
}
?>