<?php
namespace App\Controller;
use App\Entity\AppSharing;
use App\Entity\Flow;
use App\Entity\Page;
use App\Form\FlowFormType;
use App\Service\AlgorithmService;
use App\Service\FlowService;
use App\Service\PageGeneratorService;
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Client\Browser;
use DeviceDetector\Parser\OperatingSystem;
use Doctrine\Persistence\ManagerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Twig\Environment;
use Symfony\Component\Routing\Annotation\Route;
class WebController extends EasyAdminController {
public function __construct(ManagerRegistry $managerRegistry, PageGeneratorService $pageGeneratorService) {
$this->doctrine = $managerRegistry;
$this->pageGeneratorService = $pageGeneratorService;
}
public function index(Request $request, FlowService $flowService, AlgorithmService $algorithmService )
{
$em = $this->doctrine->getManager();
$form = $this->createForm( FlowFormType::class, null, array('custom'=>1 ) );
$form->handleRequest( $request );
if ($form->isSubmitted()) {
$fuuid = $form['fuuid']->getData();
$query_input_data = $request->query->all();
$post_input_data = $request->request->all();
//$input_data = array('query'=>$query_input_data,'form'=>$post_input_data['flow_form'] );
$input_data = array_merge($post_input_data['flow_form'], $query_input_data);
$Flow = $em->getRepository(Flow::class)->findOneBy(['Fuuid'=>$fuuid]);
$Flow = $flowService->executeFlow($Flow, $input_data,'pl');
return $this->render('website/index.html.twig', [
'form' => $form->createView(),
'fuuid' => $fuuid,
]);
} else {
return $this->render('website/index.html.twig', [
'form' => $form->createView(),
'fuuid'=>null
]);
}
}
/**
* @Route("/r/{appid}/{code}", name="url_redirect")
*/
public function url_redirect(Request $request, $code='', $appid='') {
$data = $request->query->get( 'd' );
$pageid = $request->query->get( 'pageid' );
$uri = $request->getUri();
$em = $this->doctrine->getManager();
$AppSharing = $em->getRepository(AppSharing::class)->findOneBy(array('Code' => $code));
$App = $AppSharing->getApp();
if (!empty( $AppSharing)) {
$showLink = $AppSharing->getShowLink();
$showQr = $AppSharing->getShowQr();
$showAppstores = $AppSharing->getShowAppStores();
$webUrl = $AppSharing->getWebUrl();
$pageId = $AppSharing->getPage()->getId();
$appSettings = $AppSharing->getApp()->getSettings();
$linkCode = $AppSharing->getCode();
$type = $AppSharing->getType();
$redirectMobile = $AppSharing->getRedirectMobile();
$redirectWeb = $AppSharing->getRedirectWeb();
$pages = $AppSharing->getPages();
$childpages = array();
$clientLocale = strtolower(str_split($_SERVER['HTTP_ACCEPT_LANGUAGE'], 2)[0]);
if ($pageid > 0) { // pageid is used in the mobile's page menu to access individual pages of the
$Page = $em->getRepository(Page::class)->findOneBy(array('id'=>$pageid, 'App'=>$App));
}
foreach ($pages as $page) {
$childId = $page['Child'];
$childPage = $em->getRepository(Page::class)->find($childId);
$childpages[$childId] = $childPage->translate($clientLocale)->getName();
}
if (!empty($Page)) {
$pageTitle = $Page->translate($clientLocale)->getName();
$pageBody = $Page->translate($clientLocale)->getBody();
return $this->render('website/redirect.html.twig',
['childpages'=>$childpages,'appid'=>$appid, 'code' => $code, 'data' => $data, 'title' => $pageTitle, 'body' => $pageBody, 'link' => '', 'qr' => '', 'osFamily' => '', 'browser' => '', 'showAppstores' => false, 'urlAppStore' => '', 'urlGooglePlay' => '', 'urlHuaweiAppGallery' => '']);
} else {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$dd = new DeviceDetector($userAgent);
$dd->parse();
$osFamily = OperatingSystem::getOsFamily($dd->getOs('name'));
$browserFamily = Browser::getBrowserFamily($dd->getClient('name'));
$isSmartphone = $dd->isSmartphone();
$pageTitle = $AppSharing->translate($clientLocale)->getName();
$pageBody = $AppSharing->translate($clientLocale)->getBody();
$urlAppStore = (isset($appSettings['urlAppStore'])) ? $appSettings['urlAppStore'] : '';
$urlGooglePlay = (isset($appSettings['urlGooglePlay'])) ? $appSettings['urlGooglePlay'] : '';
$urlHuaweiAppGallery = (isset($appSettings['urlHuaweiAppGallery'])) ? $appSettings['urlHuaweiAppGallery'] : '';
$link = $webUrl . '#/page/' . $this->pageGeneratorService->buildPageId($pageId, 11, null);
if ($type == "db") {
$link .= '?c=' . $code;
} elseif ($type == "qr") {
$link .= '?d=' . $data;
}
if (($dd->isSmartphone()) && ($redirectMobile)) {
return $this->redirect($link);
} elseif ($redirectWeb) {
return $this->redirect($link);
}
if (!$showLink) {
$link = '';
}
if (($showQr) && (!$isSmartphone)) {
$qr = '/qr/' . $linkCode . '.png';
} else {
$qr = '';
}
return $this->render('website/redirect.html.twig',
['childpages'=>$childpages,'appid'=>$appid, 'code' => $code, 'data' => $data, 'title' => $pageTitle, 'body' => $pageBody, 'link' => $link, 'qr' => $qr, 'osFamily' => $osFamily, 'browser' => $browserFamily, 'showAppstores' => $showAppstores, 'urlAppStore' => $urlAppStore, 'urlGooglePlay' => $urlGooglePlay, 'urlHuaweiAppGallery' => $urlHuaweiAppGallery]);
}
}
return $this->render('website/redirect.html.twig',
['childpages'=>'', 'appid'=>$appid, 'code' => '', 'data' => '', 'title' => 'redirect_code_not_found_title', 'body' => 'redirect_code_not_found_full_message', 'link' => '', 'qr' => '', 'osFamily' => '', 'browser' => '', 'showAppstores' =>false, 'urlAppStore' => '', 'urlGooglePlay' => '', 'urlHuaweiAppGallery' => '']);
}
}