<?php
namespace App\Controller;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Form\LoginType;
use Symfony\Component\Form\FormError;
use App\Core\JwtCore;
use App\Entity\Application;
use App\Entity\Mystem;
use App\Form\ApplicationType;
use App\Form\MystemType;
use Symfony\Component\HttpFoundation\RedirectResponse;
use App\Repository\ApplicationRepository;
use App\Service\MenuService;
class DefaultController extends AbstractController
{
/**
*
* @Route("/", name="homepage", defaults={"title": "Homepage"})
*/
public function index(LoggerInterface $logger, Request $request, AuthenticationUtils $auth, ApplicationRepository $applications, RouterInterface $router, MenuService $menu): Response
{
$newApplicationView = $this->getNewApplicationView($request);
if ($newApplicationView instanceof RedirectResponse) {
return $newApplicationView;
}
$mystemView = $this->getMystemView($request);
if ($mystemView instanceof RedirectResponse) {
return $mystemView;
}
return $this->render('index.html.twig', [
'loginForm' => $this->getLoginView($auth),
'newApplicationForm' => $newApplicationView,
'mystemForm' => $mystemView,
'applications' => $applications->findAll(),
'menu' => $menu->getMenu()
]);
}
/**
*
* @Route("/agreement", name="agreement", defaults={"title": "Agreement"})
*/
public function agreement(MenuService $menu): Response
{
return $this->render('agreement.html.twig', [
'menu' => $menu->getMenu()
]);
}
/**
*
* @Route("/privacy", name="privacy", defaults={"title": "Privacy"})
*/
public function privacy(MenuService $menu): Response
{
return $this->render('privacy.html.twig', [
'menu' => $menu->getMenu()
]);
}
/**
*
* @param
* authUtils
*/
private function getLoginView($auth)
{
$error = $auth->getLastAuthenticationError();
$lastUsername = $auth->getLastUsername();
$login = array(
'_username' => $lastUsername
);
$form = $this->createForm(LoginType::class, $login, array(
'action' => $this->generateUrl('app_login')
));
if (null !== $error) {
$form->addError(new FormError($error));
}
$view = $form->createView();
$view->children['_username']->vars['full_name'] = '_username';
$view->children['_password']->vars['full_name'] = '_password';
$view->children['_token']->vars['full_name'] = '_token';
$view->children['login']->vars['full_name'] = 'login';
return $view;
}
/**
*
* @param
* authUtils
*/
private function getNewApplicationView(Request $request)
{
$application = new Application();
$form = $this->createForm(ApplicationType::class, $application);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$application = $form->getData();
$manager = $this->getDoctrine()->getManager();
$manager->persist($application);
$manager->flush();
return $this->redirectToRoute('homepage');
}
$view = $form->createView();
return $view;
}
private function getMystemView($request)
{
$session = $this->container->get('session');
$mystem = new Mystem();
$form = $this->createForm(MystemType::class, $mystem);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$mystem = $form->getData();
$text = "'" . preg_replace('/\s+/', ' ', str_replace("'", '\047', trim($mystem->getText()))) . "'";
$flags = '--format json ';
if ($mystem->getC()) { $flags .= '-c '; }
if ($mystem->getW()) { $flags .= '-w '; }
if ($mystem->getL()) { $flags .= '-l '; }
if ($mystem->getI()) { $flags .= '-i '; }
if ($mystem->getG()) { $flags .= '-g '; }
if ($mystem->getS()) { $flags .= '-s '; }
if ($mystem->getD()) { $flags .= '-d '; }
if ($mystem->getEng()) { $flags .= '--eng-gr '; }
if ($mystem->getAll()) { $flags .= '--generate-all '; }
if ($mystem->getWeight()) { $flags .= '--weight '; }
$command = "echo $text | /home/icon/bin/mystem $flags";
$json = shell_exec($command);
if ($json == null) {
$json = $command;
} else {
$json = json_encode(json_decode($json), JSON_PRETTY_PRINT + JSON_UNESCAPED_UNICODE);
}
$session->set('mystem.c', $mystem->getC());
$session->set('mystem.w', $mystem->getW());
$session->set('mystem.l', $mystem->getL());
$session->set('mystem.i', $mystem->getI());
$session->set('mystem.g', $mystem->getG());
$session->set('mystem.s', $mystem->getS());
$session->set('mystem.d', $mystem->getD());
$session->set('mystem.eng', $mystem->getEng());
$session->set('mystem.all', $mystem->getAll());
$session->set('mystem.weight', $mystem->getWeight());
$session->set('mystem.text', $mystem->getText());
$session->set('mystem.body', $json);
return $this->redirectToRoute('homepage');
}
$mystem = new Mystem();
$mystem->
setC($session->get('mystem.c'))->
setW($session->get('mystem.w'))->
setL($session->get('mystem.l'))->
setI($session->get('mystem.i'))->
setG($session->get('mystem.g'))->
setS($session->get('mystem.s'))->
setD($session->get('mystem.d'))->
setEng($session->get('mystem.eng'))->
setAll($session->get('mystem.all'))->
setWeight($session->get('mystem.weight'))->
setText($session->get('mystem.text'))->
setBody($session->get('mystem.body'));
$form = $this->createForm(MystemType::class, $mystem);
$view = $form->createView();
return $view;
}
/**
*
* @Route("/login", name="app_login")
*/
public function login()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
/**
*
* @Route("/logout", name="app_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}