<?php
/**
* Base Controller
*
* @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
*/
namespace VisualMedia\LisaBundle\Controller;
use Exception;
use Psr\Container\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Response;
use VisualMedia\FormBundle\Entity\BaseFormReply;
use VisualMedia\FormBundle\Manager\BaseFormReplyManager;
use VisualMedia\LisaBundle\Helper\Helper;
use VisualMedia\LisaBundle\Service\Mailer;
use VisualMedia\LisaBundle\Service\SettingProvider;
use VisualMedia\PageBundle\Manager\BasePageManager;
/**
* Base Controller
*/
class BaseController extends AbstractController
{
public static $controller = false;
private static $staticcontainer = false;
public function __construct()
{
static::$controller = $this;
}
public static function init(ContainerInterface $container = null)
{
if(isset($container) && $container){
static::$staticcontainer = $container;
}
}
public static function getService($id)
{
if(static::$staticcontainer){
$service = static::$staticcontainer->get($id);
return $service;
}
else{
return false;
}
}
public static function getDoctrineObject(){
return static::$controller->getDoctrine();
}
/**
* {@inheritdoc}
*/
public function sessionKey(string $parameter): string
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getMasterRequest();
$matches = null;
if (true === (bool)preg_match('/manage_(?<key>.*)__(?<other>.*)/', $request->get('_route'), $matches)) {
return sprintf('%s.%s', $matches['key'], $parameter);
}
return sprintf('%s.%s', $request->get('_controller'), $parameter);
}
/**
* Helper function for the 3 basic functions of handling forms;
* 1. Add a form reply to the database.
* 2. Send emails to client and website.
* 3. Get or create thanks page
* Returns the redirect response to the thanks page.
*
* @param FormInterface $form
* @param string $data
* @return Response
*/
public function handleForm(FormInterface $form, array $data = []): Response
{
// Exception on empty variables.
if (empty($key = $data['key'] ?? $form->getName())) {
throw new Exception("Empty 'key' (second) argument when handling form.");
}
if (empty($email = $data['email'] ?? ($form->has('email') ? $form->get('email')->getData() : null))) {
throw new Exception("Empty 'email' (third) argument when handling form.");
}
if (empty($contact = $data['contact'] ?? ($form->has('contact') ? $form->get('contact')->getData() : null))) {
throw new Exception("Empty 'contact' (fourth) argument when handling form.");
}
// 1. Add form reply.
$request = $this->get('request_stack')->getMasterRequest();
$reply = new BaseFormReply();
$reply->setForm($form->getName());
$reply->setFormData(json_encode($form->getData()));
$reply->setBrowser($request->server->get('HTTP_USER_AGENT'));
$reply->setUrl("{$request->getScheme()}://{$request->getHost()}{$request->getRequestUri()}");
$formReplyManager = $this->get(BaseFormReplyManager::class);
$formReplyManager->persist($reply);
$formReplyManager->flush();
// 2. Send mail.
$settingProvider = $this->get(SettingProvider::class);
$data = array_merge($form->getData(), [
'mailer_sender_name' => $settingProvider->getValue($reference = 'mailer_sender_name', $domain = 'system') ?? Helper::settingPlaceholder($reference, $domain),
], $data);
$translator = $this->get('translator');
$subjectInternal = $translator->trans("{$key}.email_subject_int", [], 'forms');
$subjectExternal = $translator->trans("{$key}.email_subject_ext", [], 'forms');
$templateInternal = $data['template_int'] ?? "Email/{$key}_int.html.twig";
$templateExternal = $data['template_ext'] ?? "Email/{$key}_ext.html.twig";
$mailer = $this->get(Mailer::class);
$mailer->sendReversed($subjectInternal, $email, $contact, $templateInternal, $data);
$mailer->send($subjectExternal, $email, $contact, $templateExternal, $data);
// 3. Thanks page.
$title = $translator->trans("{$key}_thanks.title", [], 'pages');
$pageManager = $this->get(BasePageManager::class);
$page = $pageManager->getOrCreatePage("{$key}_thanks", [
'title' => $title,
'published' => true,
'indexable' => true,
'browserTitle' => $title,
'metaDescription' => "{$title}.",
'htmlContent' => "{$title}.",
]);
return $this->redirect($page->getUrl());
}
public function hasBundle(string $bundle): bool
{
if(strpos($bundle,"VisualMedia") === false){
$bundle = "VisualMedia".$bundle;
}
global $kernel;
$bundles = $kernel->getBundles();
if (array_key_exists($bundle, $bundles)) {
return true;
}
return false;
}
public function useComponents(): bool
{
$hasBundles = $this->hasBundle("ComponentBundle") && $this->hasBundle("BartPageBundle");
if($hasBundles){
//check settings
$settingProvider = $this->get(SettingProvider::class);
$useComponents = $settingProvider->getValue('componenten_use_componenten', 'componenten');
return (bool)$useComponents;
}
return false;
}
}