src/Controller/DefaultController.php line 164

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  10. use App\Form\LoginType;
  11. use Symfony\Component\Form\FormError;
  12. use App\Core\JwtCore;
  13. use App\Entity\Application;
  14. use App\Entity\Mystem;
  15. use App\Form\ApplicationType;
  16. use App\Form\MystemType;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use App\Repository\ApplicationRepository;
  19. use App\Service\MenuService;
  20. class DefaultController extends AbstractController
  21. {
  22.     /**
  23.      *
  24.      * @Route("/", name="homepage", defaults={"title": "Homepage"})
  25.      */
  26.     public function index(LoggerInterface $loggerRequest $requestAuthenticationUtils $authApplicationRepository $applicationsRouterInterface $routerMenuService $menu): Response
  27.     {
  28.         $newApplicationView $this->getNewApplicationView($request);
  29.         if ($newApplicationView instanceof RedirectResponse) {
  30.             return $newApplicationView;
  31.         }
  32.         $mystemView $this->getMystemView($request);
  33.         if ($mystemView instanceof RedirectResponse) {
  34.             return $mystemView;
  35.         }
  36.         return $this->render('index.html.twig', [
  37.             'loginForm' => $this->getLoginView($auth),
  38.             'newApplicationForm' => $newApplicationView,
  39.             'mystemForm' => $mystemView,
  40.             'applications' => $applications->findAll(),
  41.             'menu' => $menu->getMenu()
  42.         ]);
  43.     }
  44.     /**
  45.      *
  46.      * @Route("/agreement", name="agreement", defaults={"title": "Agreement"})
  47.      */
  48.     public function agreement(MenuService $menu): Response
  49.     {
  50.         return $this->render('agreement.html.twig', [
  51.             'menu' => $menu->getMenu()
  52.         ]);
  53.     }
  54.     /**
  55.      *
  56.      * @Route("/privacy", name="privacy", defaults={"title": "Privacy"})
  57.      */
  58.     public function privacy(MenuService $menu): Response
  59.     {
  60.         return $this->render('privacy.html.twig', [
  61.             'menu' => $menu->getMenu()
  62.         ]);
  63.     }
  64.     /**
  65.      *
  66.      * @param
  67.      *            authUtils
  68.      */
  69.     private function getLoginView($auth)
  70.     {
  71.         $error $auth->getLastAuthenticationError();
  72.         $lastUsername $auth->getLastUsername();
  73.         $login = array(
  74.             '_username' => $lastUsername
  75.         );
  76.         $form $this->createForm(LoginType::class, $login, array(
  77.             'action' => $this->generateUrl('app_login')
  78.         ));
  79.         if (null !== $error) {
  80.             $form->addError(new FormError($error));
  81.         }
  82.         $view $form->createView();
  83.         $view->children['_username']->vars['full_name'] = '_username';
  84.         $view->children['_password']->vars['full_name'] = '_password';
  85.         $view->children['_token']->vars['full_name'] = '_token';
  86.         $view->children['login']->vars['full_name'] = 'login';
  87.         return $view;
  88.     }
  89.     /**
  90.      *
  91.      * @param
  92.      *            authUtils
  93.      */
  94.     private function getNewApplicationView(Request $request)
  95.     {
  96.         $application = new Application();
  97.         $form $this->createForm(ApplicationType::class, $application);
  98.         $form->handleRequest($request);
  99.         if ($form->isSubmitted() && $form->isValid()) {
  100.             $application $form->getData();
  101.             $manager $this->getDoctrine()->getManager();
  102.             $manager->persist($application);
  103.             $manager->flush();
  104.             return $this->redirectToRoute('homepage');
  105.         }
  106.         $view $form->createView();
  107.         return $view;
  108.     }
  109.     private function getMystemView($request)
  110.     {
  111.         $session $this->container->get('session');
  112.         $mystem = new Mystem();
  113.         $form $this->createForm(MystemType::class, $mystem);
  114.         $form->handleRequest($request);
  115.         if ($form->isSubmitted() && $form->isValid()) {
  116.             $mystem $form->getData();
  117.             $text "'" preg_replace('/\s+/'' 'str_replace("'"'\047'trim($mystem->getText()))) . "'";
  118.             $flags '--format json ';
  119.             if ($mystem->getC()) { $flags .= '-c '; }
  120.             if ($mystem->getW()) { $flags .= '-w '; }
  121.             if ($mystem->getL()) { $flags .= '-l '; }
  122.             if ($mystem->getI()) { $flags .= '-i '; }
  123.             if ($mystem->getG()) { $flags .= '-g '; }
  124.             if ($mystem->getS()) { $flags .= '-s '; }
  125.             if ($mystem->getD()) { $flags .= '-d '; }
  126.             if ($mystem->getEng()) { $flags .= '--eng-gr '; }
  127.             if ($mystem->getAll()) { $flags .= '--generate-all '; }
  128.             if ($mystem->getWeight()) { $flags .= '--weight '; }
  129.             $command "echo $text | /home/icon/bin/mystem $flags";
  130.             $json shell_exec($command);
  131.             if ($json == null) {
  132.                 $json $command;
  133.             } else {
  134.                 $json json_encode(json_decode($json), JSON_PRETTY_PRINT JSON_UNESCAPED_UNICODE);
  135.             }
  136.             $session->set('mystem.c'$mystem->getC());
  137.             $session->set('mystem.w'$mystem->getW());
  138.             $session->set('mystem.l'$mystem->getL());
  139.             $session->set('mystem.i'$mystem->getI());
  140.             $session->set('mystem.g'$mystem->getG());
  141.             $session->set('mystem.s'$mystem->getS());
  142.             $session->set('mystem.d'$mystem->getD());
  143.             $session->set('mystem.eng'$mystem->getEng());
  144.             $session->set('mystem.all'$mystem->getAll());
  145.             $session->set('mystem.weight'$mystem->getWeight());
  146.             $session->set('mystem.text'$mystem->getText());
  147.             $session->set('mystem.body'$json);
  148.             return $this->redirectToRoute('homepage');
  149.         }
  150.         $mystem = new Mystem();
  151.         $mystem->
  152.             setC($session->get('mystem.c'))->
  153.             setW($session->get('mystem.w'))->
  154.             setL($session->get('mystem.l'))->
  155.             setI($session->get('mystem.i'))->
  156.             setG($session->get('mystem.g'))->
  157.             setS($session->get('mystem.s'))->
  158.             setD($session->get('mystem.d'))->
  159.             setEng($session->get('mystem.eng'))->
  160.             setAll($session->get('mystem.all'))->
  161.             setWeight($session->get('mystem.weight'))->
  162.             setText($session->get('mystem.text'))->
  163.             setBody($session->get('mystem.body'));
  164.         $form $this->createForm(MystemType::class, $mystem);
  165.         $view $form->createView();
  166.         return $view;
  167.     }
  168.     /**
  169.      *
  170.      * @Route("/login", name="app_login")
  171.      */
  172.     public function login()
  173.     {
  174.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  175.     }
  176.     /**
  177.      *
  178.      * @Route("/logout", name="app_logout")
  179.      */
  180.     public function logout()
  181.     {
  182.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  183.     }
  184. }