<?php declare(strict_types=1);
namespace BXK_FancyProtect\Controller;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Core\Framework\Context;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\Session;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Symfony\Component\HttpFoundation\Cookie;
class BXK_FancyProtectController extends StorefrontController {
private $config;
private $session;
public function __construct(Session $session, SystemConfigService $systemConfigService) {
$this->session = $session;
$this->config = $systemConfigService;
}
/**
* Fancy Protect Page - GET
*
* @RouteScope(scopes={"storefront"})
* @Route("/entry", name="frontend.bxk_fancyprotect.entry", methods={"GET"})
*/
public function index(Request $request, Context $context) {
$md5Key = $request->cookies->get("entry", null);
$isProtected = true;
if ($md5Key) {
$isProtected = md5($this->getProtectionPassword($request)) != $md5Key;
}
$releaseDate = $this->getConfigValue('unlockTime', $request->attributes->get('sw-sales-channel-id'));
if (isset($releaseDate)) {
if ($this->getProtectionPassword($request) == "" || strtotime($releaseDate) <= strtotime('now')) {
$isProtected = false;
}
}
if ($isProtected) {
// get options for frontend
$params = $this->getFancyParams($request);
return $this->renderStorefront('@BXK_FancyProtect/plugins/BXK_FancyProtect/index.html.twig', $params);
} else {
return $this->redirectToRoute("frontend.home.page");
}
}
public function getFancyParams($request) {
$salesChannelId = $request->attributes->get('sw-sales-channel-id');
$title = ($this->getConfigValue('title', $salesChannelId) == "") ? "COMING SOON" : $this->getConfigValue('title', $salesChannelId);
$content = ($this->getConfigValue('content', $salesChannelId) == "") ? "Stay tuned, we working on our website!" : $this->getConfigValue('content', $salesChannelId);
return [
'title' => $title,
'content' => $content,
'urlBackground' => $this->getConfigValue('urlBackground', $salesChannelId),
'unlockTime' => $this->getConfigValue('unlockTime', $salesChannelId),
'showNewsletter' => $this->getConfigValue('showNewsletter', $salesChannelId),
'socialFacebook' => $this->getConfigValue('socialFacebook', $salesChannelId),
'socialInstagram' => $this->getConfigValue('socialInstagram', $salesChannelId),
'socialTwitter' => $this->getConfigValue('socialTwitter', $salesChannelId),
];
}
/**
* Fancy Protect Early Access Check - POST
*
* @RouteScope(scopes={"storefront"})
* @Route("/entry", name="frontend.bxkfancy.entry.login", methods={"POST"})
*/
public function login(Request $request, Context $context) {
// get password
$key = $request->get('fancyprotect', null);
// get other options for frontend
$params = $this->getFancyParams($request);
if (isset($key) && is_string($key)) {
// Check key
if (md5($key) === md5($this->getProtectionPassword($request))) {
// Forward to homepage
$response = new Response('', 302);
$response->headers->set("location", $this->generateUrl("frontend.home.page"));
$response->headers->setCookie(new Cookie('entry', md5($key),0, '/', null, false, false));
return $response;
}
$params['fancyprotect_error'] = 'fancyprotect.wrongPassword';
}
return $this->renderStorefront('@BXK_FancyProtect/plugins/BXK_FancyProtect/index.html.twig',$params);
}
private function getProtectionPassword($request) {
return $this->getConfigValue('protectionPwd', $request->attributes->get('sw-sales-channel-id'));
}
private function getConfigValue($key, $default = null) {
$configKey = 'BXK_FancyProtect.config.' . $key;
return $this->config->get($configKey, $default);
}
}