src/Controller/AdminController.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Contracts\Translation\TranslatorInterface;
  9. use Psr\Log\LoggerInterface;
  10. use App\Util\PMA;
  11. use App\Form\CampaignForm;
  12. use App\Form\VariableForm;
  13. use App\Form\PMAVariableForm;
  14. use App\Entity\VariableDefinition;
  15. use App\Entity\CampaignMailing;
  16. use App\Repository\VariableDefinitionRepository;
  17. use App\Repository\OrganizationRepository;
  18. use App\Repository\CampaignMailingRepository;
  19. use App\Util\AC;
  20. use Firebase\JWT\JWT;
  21. use Firebase\JWT\Key;
  22. class AdminController extends AbstractController
  23. {
  24.     private $requestStack;
  25.     private $logger;
  26.     private $translator;
  27.     private $organizationRepository;
  28.     public function __construct(
  29.         RequestStack $requestStackLoggerInterface $loggerTranslatorInterface $translator,
  30.         OrganizationRepository $organizationRepository
  31.         )
  32.     {
  33.         $this->requestStack $requestStack;
  34.         $this->logger $logger;
  35.         $this->translator $translator;
  36.         $this->organizationRepository $organizationRepository;
  37.     }
  38.     /**
  39.      * @Route("/admin", name="app_admin")
  40.      */
  41.     public function index(): Response
  42.     {
  43.         return $this->redirectToRoute("campaigns_list");
  44.     }
  45.     /**
  46.      * @Route("/admin/campaigns/list", name="campaigns_list")
  47.      */
  48.     public function campaignsList(Request $request): Response
  49.     {
  50.         $jwt $this->_authenticate();
  51.         $pma = new PMA($this->logger$jwt);
  52.         $customerId $this->_getCustomerId($jwt);
  53.         $pma->setCustomerId($customerId);
  54.         
  55.         try {
  56.             $campaigns $pma->getCampaigns($request->query->getInt('page'0), $request->query->getInt('size'20));
  57.         }
  58.         catch(\Exception $ex) {
  59.             $this->addFlash('error'$ex->getMessage());
  60.             return $this->redirectToRoute("show_error");
  61.         }
  62.         return $this->render('admin/campaigns_list.html.twig', [
  63.             'campaigns' => $campaigns,
  64.         ]);
  65.     }
  66.     /**
  67.      * @Route("/admin/campaigns/new", name="campaigns_new")
  68.      */
  69.     public function newCampaign(Request $requestCampaignMailingRepository $campaignMailingRepository): Response
  70.     {
  71.         $form $this->createForm(CampaignForm::class);
  72.         $form->handleRequest($request);
  73.         $jwt $this->_authenticate();
  74.         
  75.         $pma = new PMA($this->logger$jwt);
  76.         $customerId $this->_getCustomerId($jwt);
  77.         $pma->setCustomerId($customerId);
  78.         
  79.         try {
  80.             $products $pma->getProducts();
  81.         }
  82.         catch(\Exception $ex) {
  83.             $this->addFlash('error'$ex->getMessage());
  84.             return $this->redirectToRoute("show_error");
  85.         }
  86.         
  87.         if($form->isSubmitted() && $form->isValid()) {
  88.             try {
  89.                 $productId $form->get('productId')->getData();
  90.                 if(is_null($productId) || $productId == '') {
  91.                     $productId 0;
  92.                 }
  93.                 $deliveryChecked false;
  94.                 if($form->get('deliveryCheckSelected')->getData() == true) {
  95.                     $deliveryChecked true;
  96.                 }
  97.                 $ret $pma->createCampaign($form->get('campaignName')->getData(), $form->get('campaignIdExt')->getData(), $productId$form->get('sendingReasonId')->getData(), $deliveryChecked);
  98.                 $campaignId $ret['id'];
  99.                 $ret $pma->createMailing($campaignId);
  100.                 // save mailing ID
  101.                 $cm = new CampaignMailing();
  102.                 $cm->setCampaignId($campaignId);
  103.                 $cm->setMailingId($ret['id']);
  104.                 $campaignMailingRepository->add($cmtrue);
  105.                 $this->addFlash('success'$this->translator->trans('success message - new campaign added'));
  106.                 $this->logger->info('New campaign added', ['name' => $form->get('campaignName')->getData(), 'customerId' => $customerId'user' => $this->getUser()->getUserIdentifier()]);
  107.                 return $this->redirectToRoute("variables_list", ['mailingId' => $ret['id']]);
  108.             }
  109.             catch(\Exception $ex) {
  110.                 $this->addFlash('error'$this->translator->trans('error message - saving campaign error'));
  111.                 goto newCampaignRender;
  112.             }
  113.         }
  114.         elseif($form->isSubmitted() && !$form->isValid()) {
  115.             $this->addFlash('error'$this->translator->trans('error message - new campaign not added'));
  116.         }
  117.         newCampaignRender:
  118.         return $this->render('admin/campaigns_new.html.twig', [
  119.             'form' => $form->createView(),
  120.             'products' => $products,
  121.             'productTypeId' => 0,
  122.             'mailingId' => 0,
  123.             'isEdit' => false,
  124.         ]);
  125.     }
  126.     /**
  127.      * @Route("/admin/campaigns/edit/{campaignId}", name="campaigns_edit")
  128.      */
  129.     public function editCampaign(int $campaignIdRequest $requestCampaignMailingRepository $campaignMailingRepository): Response
  130.     {
  131.         $form $this->createForm(CampaignForm::class);
  132.         $form->handleRequest($request);
  133.         $jwt $this->_authenticate();
  134.         
  135.         $pma = new PMA($this->logger$jwt);
  136.         $customerId $this->_getCustomerId($jwt);
  137.         $pma->setCustomerId($customerId);
  138.         try {
  139.             $campaign $pma->getCampaign($campaignId);
  140.             $products $pma->getProducts();
  141.         }
  142.         catch(\Exception $ex) {
  143.             $this->addFlash('error'$ex->getMessage());
  144.             return $this->redirectToRoute("show_error");
  145.         }
  146.         
  147.         $productTypeId 0;
  148.         $cm $campaignMailingRepository->findOneByCampaignId($campaignId);
  149.         $mailingId 0;
  150.         if(!is_null($cm)) {
  151.             $mailingId $cm->getMailingId();
  152.         }
  153.         if(array_key_exists('product'$campaign) && !is_null($campaign['product'])) {
  154.             $productTypeId $campaign['product']['productTypeId'];
  155.         }
  156.         
  157.         if(!$form->isSubmitted()) {
  158.             $form->get('campaignName')->setData($campaign['campaignName']);
  159.             $form->get('campaignIdExt')->setData($campaign['campaignIdExt']);
  160.             $form->get('sendingReasonId')->setData($campaign['sendingReasonId']);
  161.             $form->get('deliveryCheckSelected')->setData($campaign['deliveryCheckSelected']);
  162.             $form->get('productId')->setData($campaign['productId']);
  163.         }
  164.         else {
  165.             if($form->isSubmitted() && $form->isValid()) {
  166.                 try {
  167.                     $productId $form->get('productId')->getData();
  168.                     $data = [];
  169.                     if(!is_null($productId) && $productId != '') {
  170.                         if($productId != $campaign['productId']) {
  171.                             $data['productId'] = $productId;
  172.                         }
  173.                     }
  174.                     if($form->get('campaignName')->getData() != $campaign['campaignName']) {
  175.                         $data['campaignName'] = $form->get('campaignName')->getData();
  176.                     }
  177.                     
  178.                     if($form->get('sendingReasonId')->getData() != $campaign['sendingReasonId']) {
  179.                         $data['sendingReasonId'] = $form->get('sendingReasonId')->getData();
  180.                     }
  181.                     
  182.                     if($form->get('deliveryCheckSelected')->getData() != $campaign['deliveryCheckSelected']) {
  183.                         $data['deliveryCheckSelected'] = $form->get('deliveryCheckSelected')->getData();
  184.                     }
  185.                     
  186.                     $pma->updateCampaign($campaignId$data);
  187.                 }
  188.                 catch(\Exception $ex) {
  189.                     $this->addFlash('error'$ex->getMessage());
  190.                     goto editCampaignRender;
  191.                 }
  192.                 $this->addFlash('success'$this->translator->trans('success message - editing campaign OK'));
  193.                 $this->logger->info('Campaign edited', ['name' => $form->get('campaignName')->getData(), 'customerId' => $customerId'user' => $this->getUser()->getUserIdentifier()]);
  194.             }
  195.             elseif($form->isSubmitted() && !$form->isValid()) {
  196.                 $this->addFlash('error'$this->translator->trans('error message - editing campaign not ok'));
  197.             }
  198.         }
  199.         editCampaignRender:
  200.         return $this->render('admin/campaigns_new.html.twig', [
  201.             'form' => $form->createView(),
  202.             'products' => $products,
  203.             'productTypeId' => $productTypeId,
  204.             'mailingId' => $mailingId,
  205.             'isEdit' => true,
  206.         ]);
  207.     }
  208.     /**
  209.      * @Route("/admin/campaigns/del/{campaignId}", name="campaigns_del")
  210.      */
  211.     public function delCampaign(int $campaignIdRequest $requestCampaignMailingRepository $campaignMailingRepository): Response
  212.     {
  213.         $jwt $this->_authenticate();
  214.         $pma = new PMA($this->logger$jwt);
  215.         $customerId $this->_getCustomerId($jwt);
  216.         $pma->setCustomerId($customerId);
  217.         try {
  218.             $pma->delCampaign($campaignId);
  219.         }
  220.         catch(\Exception $ex) {
  221.             $this->addFlash('error'$this->translator->trans('error message - error deleting campaign') . ': ' $ex->getMessage());
  222.         }
  223.         
  224.         $this->addFlash('success'$this->translator->trans('success message - campaign deleted'));
  225.         $this->logger->info('Campaign deleted', ['campaignId' => $campaignId'customerId' => $customerId'user' => $this->getUser()->getUserIdentifier()]);
  226.         // TODO - usuniÄ™cie CampaignMailing 
  227.         return $this->redirectToRoute("campaigns_list");
  228.     }
  229.         
  230.     /**
  231.      * shows all internal variables and allows sending them into PMA
  232.      * @Route("/admin/variables/list/{mailingId}", name="variables_list")
  233.      */
  234.     public function variablesList(int $mailingIdRequest $requestVariableDefinitionRepository $variableRepositoryCampaignMailingRepository $campaignMailingRepository): Response
  235.     {
  236.         // form for creating new local variables
  237.         $form $this->createForm(VariableForm::class);
  238.         $form->handleRequest($request);
  239.         // form for sending variables to PMA
  240.         $pmaForm $this->createForm(PMAVariableForm::class, null, [
  241.             'action' => $this->generateUrl('variables_send')
  242.         ]);
  243.         $jwt $this->_authenticate();
  244.         $pma = new PMA($this->logger$jwt);
  245.         $customerId $this->_getCustomerId($jwt);
  246.         $pma->setCustomerId($customerId);
  247.         $internalVariables $variableRepository->findAllForCustomerId($customerId);
  248.         try {
  249.             $pmaVariables $pma->getVariables($mailingId);
  250.         }
  251.         catch(\Exception $ex) {
  252.             $this->addFlash('error'$ex->getMessage());
  253.             return $this->redirectToRoute("show_error");
  254.         }
  255.         if($form->isSubmitted() && $form->isValid()) {
  256.             try {
  257.                 $localVar = new VariableDefinition();
  258.                 $localVar->setLabel($form->get('label')->getData());
  259.                 $localVar->setDataTypeId($form->get('dataTypeId')->getData());
  260.                 $localVar->setCustomerId($customerId);
  261.                 $variableRepository->add($localVartrue);
  262.                 $internalVariables $variableRepository->findAllForCustomerId($customerId);
  263.             }
  264.             catch(\Exception $ex) {
  265.                 $this->addFlash('error'$this->translator->trans('error message - saving variable error'));
  266.                 goto newVariableRender;
  267.             }
  268.             $this->addFlash('success'$this->translator->trans('success message - new variable added'));
  269.         }
  270.         elseif($form->isSubmitted() && !$form->isValid()) {
  271.             $this->addFlash('error'$this->translator->trans('error message - new variable not added'));
  272.         }
  273.         $campaignId 0;
  274.         $cm $campaignMailingRepository->findOneByMailingId($mailingId);
  275.         
  276.         if(!is_null($cm)) {
  277.             $campaignId $cm->getCampaignId();
  278.         }
  279.         newVariableRender:
  280.         return $this->render('admin/variables_list.html.twig', [
  281.             'form' => $form->createView(),
  282.             'pmaform' => $pmaForm->createView(),
  283.             'internalVariables' => $internalVariables,
  284.             'pmaVariables' => $pmaVariables['elements'],
  285.             'mailingId' => $mailingId,
  286.             'campaignId' => $campaignId,
  287.         ]);
  288.     }
  289.     /**
  290.      * fetches all custom fields from AC and save them as local variables
  291.      * @Route("/admin/variables/fetch-from-ac/{mailingId}", name="variables_fetch")
  292.      */
  293.     public function fetchVariablesFromAC(int $mailingIdVariableDefinitionRepository $variableRepository)
  294.     {
  295.         $jwt $this->_authenticate();
  296.         $customerId $this->_getCustomerId($jwt);
  297.         $user $this->getUser();
  298.         $organization $this->organizationRepository->find($user->getOrganizationId());
  299.         $ac = new AC($organization->getAcUrl(), $organization->getAcKey());
  300.         $list $ac->listAllCustomFields();
  301.         foreach($list as $v) {
  302.             try {
  303.                 if(is_null($variableRepository->findOneByLabelAndCustomerId($v['title'], $customerId))) {
  304.                     $localVar = new VariableDefinition();
  305.                     $localVar->setLabel($v['title']);
  306.                     $localVar->setDataTypeId(PMA::MapAcFieldTypeToPma($v['type']));
  307.                     $localVar->setCustomerId($customerId);
  308.                     $variableRepository->add($localVartrue);
  309.                 }
  310.             }
  311.             catch(\Exception $ex) {
  312.                 $this->addFlash('error'$this->translator->trans('error message - fetching variables error'));
  313.             }
  314.         }
  315.         
  316.         return $this->redirectToRoute('variables_list', ['mailingId' => $mailingId]);
  317.     }
  318.     /**
  319.      * deletes internal variable
  320.      * @Route("/admin/variables/del/{variableId}/{mailingId}", name="variables_del")
  321.      */
  322.     public function delVariable(int $variableIdint $mailingIdRequest $requestVariableDefinitionRepository $variableRepository) : Response
  323.     {
  324.         $jwt $this->_authenticate();
  325.         $var $variableRepository->find($variableId);
  326.         if($var->getCustomerId() == $this->_getCustomerId($jwt)) {
  327.             $variableRepository->remove($vartrue);
  328.         }
  329.         else {
  330.             $this->logger->error("Trying to delete variable of another customer. Var id: " $var->getId());
  331.             throw new \Exception("Trying to delete variable of another customer"403);
  332.         }
  333.         
  334.         return $this->redirectToRoute("variables_list", ['mailingId' => $mailingId]);
  335.     }
  336.     /**
  337.      * saves defined variables into PMA mailing
  338.      * @Route("/admin/variables/send", name="variables_send")
  339.      */
  340.     public function sendVariableDefinitions(Request $requestVariableDefinitionRepository $variableRepository) : Response
  341.     {
  342.         $form $this->createForm(PMAVariableForm::class);
  343.         $form->handleRequest($request);
  344.         
  345.         if($form->isSubmitted() && $form->isValid()) {
  346.             $jwt $this->_authenticate();
  347.             $pma = new PMA($this->logger$jwt);
  348.             $customerId $this->_getCustomerId($jwt);
  349.             $pma->setCustomerId($customerId);
  350.             $variables = [];
  351.             $sortOrder 0;
  352.             $mailingId $form->get('mailingId')->getData();
  353.             // check if there are already defined variables in PMA
  354.             try {
  355.                 $ret $pma->getVariables($mailingId);
  356.             }
  357.             catch(\Exception $ex) {
  358.                 $this->addFlash('error'$ex->getMessage());
  359.                 return $this->redirectToRoute("show_error");
  360.             }
  361.             
  362.             $pmaVariables $ret['elements'];
  363.             if(!is_null($form->get('variableIds')->getData())) {
  364.                 foreach(explode(','$form->get('variableIds')->getData()) as $varId) {
  365.                     $localVar $variableRepository->find($varId);
  366.                     $arrItem = [];
  367.                     $arrItem['label'] = $localVar->getLabel();
  368.                     $arrItem['dataTypeId'] = $localVar->getDataTypeId();
  369.                     $arrItem['sortOrder'] = $sortOrder;
  370.                     $variables[] = $arrItem;
  371.                     $sortOrder $sortOrder 1;
  372.                 }
  373.             }
  374.             
  375.             if(count($pmaVariables) > 0) {
  376.                 // do we need to delete some variables?
  377.                 $variablesForDeletion explode(','$form->get('deletedPmaVariableIds')->getData());
  378.                 foreach($pmaVariables as $v) {
  379.                     if(in_array($v['id'], $variablesForDeletion)) {
  380.                         continue;
  381.                     }
  382.                     $arrItem = [];
  383.                     $arrItem['id'] = $v['id'];
  384.                     $arrItem['label'] = $v['label'];
  385.                     $arrItem['dataTypeId'] = $v['dataType']['id'];
  386.                     $arrItem['sortOrder'] = $v['sortOrder'];
  387.                     $variables[] = $arrItem;
  388.                 }
  389.                 try {
  390.                     $pma->updateVariables($mailingId$variables);
  391.                     $this->logger->info('Variables updated in mailing', ['mailingId' => $mailingId'customerId' => $customerId'user' => $this->getUser()->getUserIdentifier()]);
  392.                 }
  393.                 catch(\Exception $ex) {
  394.                     $this->addFlash('error'$ex->getMessage());
  395.                     return $this->redirectToRoute("show_error");
  396.                 }
  397.             }
  398.             else {
  399.                 try {
  400.                     $pma->createVariables($mailingId$variables);
  401.                     $this->logger->info('Variables added to mailing', ['mailingId' => $mailingId'customerId' => $customerId'user' => $this->getUser()->getUserIdentifier()]);
  402.                 }
  403.                 catch(\Exception $ex) {
  404.                     $this->addFlash('error'$ex->getMessage());
  405.                     return $this->redirectToRoute("show_error");
  406.                 }
  407.             }
  408.             $this->addFlash('success'$this->translator->trans('success message - variables added to mailing'));
  409.             
  410.         }
  411.         elseif($form->isSubmitted() && !$form->isValid()) {
  412.             $this->addFlash('error'$this->translator->trans('error message - new variable not added'));
  413.         }
  414.         return $this->redirectToRoute("variables_list", ['mailingId' => $mailingId]);
  415.     }
  416.     /**
  417.      * shows campaign summary
  418.      * @Route("/admin/campaigns/show/{campaignId}", name="campaigns_show")
  419.      */
  420.     public function showCampaign(int $campaignId) : Response
  421.     {
  422.         $jwt $this->_authenticate();        
  423.         $pma = new PMA($this->logger$jwt);
  424.         $customerId $this->_getCustomerId($jwt);
  425.         $pma->setCustomerId($customerId);
  426.         try {
  427.             $campaign $pma->getCampaign($campaignId);
  428.             $mailings $pma->getMailings($campaignId);
  429.             $lookups PMA::campaignLookups();
  430.         }
  431.         catch(\Exception $ex) {
  432.             $this->addFlash('error'$ex->getMessage());
  433.             return $this->redirectToRoute("show_error");
  434.         }
  435.         $variables = [];
  436.         
  437.         foreach($mailings['elements'] as $m) {
  438.             $variables array_merge($variables$pma->getVariables($m['id']));
  439.         }
  440.         $campaignSendingReasonLabel '';
  441.         foreach($lookups['SendingReason'] as $e) {
  442.             if($e['id'] == $campaign['sendingReasonId']) {
  443.                 $campaignSendingReasonLabel $e['label'];
  444.             }
  445.         }
  446.         $campaignStateLabel '';
  447.         foreach($lookups['CampaignState'] as $e) {
  448.             if($e['id'] == $campaign['stateId']) {
  449.                 $campaignStateLabel $e['label'];
  450.             }
  451.         }
  452.         
  453.         return $this->render('admin/campaigns_show_summary.html.twig', [
  454.             'campaign' => $campaign,
  455.             'mailings' => $mailings['elements'],
  456.             'variables' => $variables['elements'],
  457.             'requiredActions' => $campaign['requiredActions'],
  458.             'mailingId' => $mailings['elements'][0]['id'],
  459.             'campaignStateLabel' => $campaignStateLabel,
  460.             'campaignSendingReasonLabel' => $campaignSendingReasonLabel,
  461.         ]);
  462.     }
  463.     /**
  464.      * shows error message
  465.      * @Route("/admin/error", name="show_error")
  466.      */
  467.     public function showError() : Response
  468.     {
  469.         return $this->render('admin/show_error.html.twig');
  470.     }
  471.     /**
  472.      * shows campaign summary
  473.      * @Route("/admin/campaigns/show-report/{campaignId}", name="campaigns_show_report")
  474.      */
  475.     public function showCampaignReport(int $campaignId) : Response
  476.     {
  477.         $jwt $this->_authenticate();        
  478.         $pma = new PMA($this->logger$jwt);
  479.         $customerId $this->_getCustomerId($jwt);
  480.         $pma->setCustomerId($customerId);
  481.         try {
  482.             $campaign $pma->getCampaign($campaignId);
  483.             $report $pma->getReportOverview($campaignId);
  484.         }
  485.         catch(\Exception $ex) {
  486.             $this->addFlash('error'$ex->getMessage());
  487.             return $this->redirectToRoute("show_error");
  488.         }
  489.         return $this->render('admin/campaigns_show_report.html.twig', [
  490.             'campaign' => $campaign,
  491.             'report' => $report,
  492.         ]);
  493.     }
  494.     /**
  495.      * shows campaign summary
  496.      * @Route("/admin/campaigns/download-report/{campaignId}/{date}", name="campaigns_download_report")
  497.      */
  498.     public function downloadCampaignReport(int $campaignIdstring $date) : Response
  499.     {
  500.         $jwt $this->_authenticate();        
  501.         $pma = new PMA($this->logger$jwt);
  502.         $customerId $this->_getCustomerId($jwt);
  503.         $pma->setCustomerId($customerId);
  504.         try {
  505.             $report $pma->getReportDetailed($campaignId$date);
  506.         }
  507.         catch(\Exception $ex) {
  508.             $this->addFlash('error'$ex->getMessage());
  509.             return $this->redirectToRoute("show_error");
  510.         }
  511.         $response = new Response();
  512.         $response->headers->set('Cache-Control''private');
  513.         $response->headers->set('Content-type''text/csv');
  514.         $response->headers->set('Content-Disposition''attachment; filename="PMA_report_' $campaignId '_' $date '.csv";');
  515.         $response->headers->set('Content-length',  strlen($report));
  516.         // Send headers before outputting anything
  517.         $response->sendHeaders();
  518.         $response->setContent($report);
  519.         return $response;
  520.         // return $this->file($report, $date . '.csv');
  521.     }
  522.     /**
  523.      * shows campaign summary
  524.      * @Route("/admin/settings", name="app_settings")
  525.      */
  526.     public function settings() : Response
  527.     {
  528.         $user $this->getUser();
  529.         $organization $this->organizationRepository->find($user->getOrganizationId());
  530.         return $this->render('admin/settings.html.twig', [
  531.             'organization' => $organization,
  532.             'user' => $user,
  533.         ]);
  534.     }
  535.     /**
  536.      * logs in into PMA system
  537.      * @Route("/admin/pma/login/{campaignId}", name="pma_login")
  538.      */
  539.     public function loginToPMA($campaignId=0) : Response
  540.     {
  541.         $user $this->getUser();
  542.         $organization $this->organizationRepository->find($user->getOrganizationId());
  543.         $token PMA::createJWT(
  544.             $user->getEmail(),
  545.             $user->getFirstname(),
  546.             $user->getLastname(),
  547.             (int) $this->getParameter('app.partner_system_id_ext'),
  548.             $organization->getPartnerSystemCustomerIdExt(),
  549.             $this->getParameter('app.sso_token_key')
  550.         );
  551.         if(!array_key_exists('APP_GUI_URL'$_SERVER)) {
  552.             $this->logger->critical("no APP_GUI_URL set");
  553.             throw new \Exception("no APP_GUI_URL set"500);
  554.         }
  555.         $url $_SERVER['APP_GUI_URL'] . '?partnersystem=' $token;
  556.         if($campaignId != 0) {
  557.             $url .= '&campaignid=' $campaignId;
  558.         }
  559.         return $this->redirect($url);
  560.     }
  561.     /**
  562.      * returns already generated token or tries to get authenticated
  563.      */
  564.     private function _authenticate() : string
  565.     {
  566.         // first get token from session
  567.         $session $this->requestStack->getSession();
  568.         $jwt $session->get('pmajwttoken');
  569.         // check if still valid
  570.         if(!is_null($jwt) && PMA::checkIfTokenValid($jwt)) {
  571.             return $jwt;
  572.         }
  573.         $user $this->getUser();
  574.         $organization $this->organizationRepository->find($user->getOrganizationId());
  575.         if(is_null($organization)) {
  576.             $this->logger->debug('CXController::_authenticate() no organization for user ' $user->getUserIdentifier());
  577.             $this->logger->error('CXController::_authenticate() no organization for user ' $user->getUserIdentifier());
  578.             $this->addFlash('error'$this->translator->trans('error message - no organization set'));
  579.             return $this->redirectToRoute("show_error");
  580.         }
  581.         try {
  582.             $jwt PMA::authenticate(
  583.                 (int) $this->getParameter('app.partner_system_id_ext'),
  584.                 $organization->getPartnerSystemCustomerIdExt(),
  585.                 $this->getParameter('app.auth_secret')
  586.             );
  587.         }
  588.         catch(\Exception $ex) {
  589.             $this->logger->debug('AdminController::_authenticate() error set: ' $ex->getMessage() . ', errno: ' $ex->getCode());
  590.             $this->logger->error('AdminController::_authenticate() error set');
  591.             $this->addFlash('error'$this->translator->trans('error message - authentication error'));
  592.             return $this->redirectToRoute("show_error");
  593.         }
  594.         $session->set('pmajwttoken'$jwt);
  595.         return $jwt;
  596.     }
  597.     private function _getCustomerId($jwt) : int
  598.     {
  599.         $customerIds PMA::getCustomerIdsFromToken($jwt);
  600.         return $customerIds[0];
  601.     }
  602.     
  603. }