src/Social/FrontendBundle/Controller/SearchController.php line 34

Open in your IDE?
  1. <?php
  2. namespace Social\FrontendBundle\Controller;
  3. use DateTime;
  4. use Egulias\EmailValidator\EmailValidator;
  5. use Egulias\EmailValidator\Validation\RFCValidation;
  6. use Exception;
  7. use Social\InternalBundle\Entity\EmailListValidationHistory;
  8. use Social\UserBundle\Form\UserType;
  9. use Sentry\Client;
  10. use Social\InternalBundle\Entity\TrafficPoll\CommercialCards;
  11. use Social\InternalBundle\Entity\TrafficPool;
  12. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  13. use Symfony\Component\HttpClient\HttpClient;
  14. use Symfony\Component\HttpFoundation\Cookie;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. /**
  19.  * Class SearchController
  20.  *
  21.  * @package Social\FrontendBundle\Controller
  22.  */
  23. class SearchController extends Controller
  24. {
  25.     /**
  26.      * @return Response
  27.      */
  28.     public function indexAction()
  29.     {
  30.         $form $this->createForm(UserType::class, null, [
  31.             'include_birthday' => true,
  32.             'include_agree'    => true,
  33.         ]);
  34.         return $this->render('SocialFrontendBundle:Search:index.html.twig', ['form' => $form->createView()]);
  35.     }
  36.     public function trafficPoolAction(Request $request)
  37.     {
  38.         try {
  39.             if ($this->getUser() !== null && $this->getUser()->getId()) {
  40.                 return $this->redirectToRoute('social_frontend_homepage_account');
  41.             }
  42.             $trafficReffId $request->get('trafficIdRef');
  43.             $currentPage   $request->get('page');
  44.             /** @var TrafficPool $pollEntity */
  45.             $pollEntity $this->getDoctrine()->getRepository(TrafficPool::class)
  46.                                ->findOneBy(['code' => $trafficReffId]);
  47.             if ($pollEntity) {
  48.                 if (!$pollEntity->isActive()) {
  49.                     return $this->redirectToRoute('social_frontend_search');
  50.                 }
  51.                 $now = new DateTime('now');
  52.                 if ($pollEntity->getActiveUntil() != null && $pollEntity->getActiveUntil() instanceof DateTime) {
  53.                     if ($pollEntity->getActiveUntil() < $now) {
  54.                         return $this->redirectToRoute('social_frontend_search');
  55.                     }
  56.                 }
  57.                 $filters['limit']  = 12;
  58.                 $filters['page']   = $currentPage;
  59.                 $filters['offset'] = ($filters['page'] - 1) * $filters['limit'];
  60.                 $filters['poll']   = $pollEntity->getId();
  61.                 $qb                $this->getDoctrine()->getRepository('SocialUserBundle:User')->getQueryBuilderForUserInPoolsByFilters($filters);
  62.                 $users             $qb->getQuery()->execute();
  63.                 $commercials    $pollEntity->getCommercialCards();
  64.                 $hasCommercials count($commercials) > true false;
  65.                 $remadeUsers = [];
  66.                 if ($hasCommercials) {
  67.                     /** @var CommercialCards $singleCommercial */
  68.                     foreach ($commercials as $singleCommercial) {
  69.                         if ($singleCommercial->getPositions() != null && $singleCommercial->getPositions() != '') {
  70.                             if ($singleCommercial->getActive() == true) {
  71.                                 if ($singleCommercial->getActiveUntil() != null && $singleCommercial->getActiveUntil() instanceof DateTime) {
  72.                                     if ($singleCommercial->getActiveUntil() >= $now) {
  73.                                         $remadeUsers[($singleCommercial->getPositions() - 1)] = $singleCommercial;
  74.                                     }
  75.                                 } else {
  76.                                     $remadeUsers[($singleCommercial->getPositions() - 1)] = $singleCommercial;
  77.                                 }
  78.                             }
  79.                         }
  80.                     }
  81.                 }
  82.                 $lastPosition 0;
  83.                 $countUsers count($users);
  84.                 for ($i 0$i $countUsers$i++) {
  85.                     if (isset($remadeUsers[$i]) && !empty($remadeUsers[$i])) {
  86.                         // Check next until we have a slot available
  87.                         for ($j $i$j count($users) + count($commercials) + 1$j++) {
  88.                             if (!isset($remadeUsers[$j]) || empty($remadeUsers[$j])) {
  89.                                 $lastPosition               $j;
  90.                                 $remadeUsers[$lastPosition] = $users[$i];
  91.                                 break;
  92.                             }
  93.                         }
  94.                     } else {
  95.                         $lastPosition               $i;
  96.                         $remadeUsers[$lastPosition] = $users[$i];
  97.                     }
  98.                 }
  99.                 ksort($remadeUsers);
  100.                 $renderedUsers $this->renderView('SocialFrontendBundle:Search:submit.html.twig',
  101.                     ['users' => $remadeUsers'trafficPollFlag' => true'page' => $currentPage]);
  102.                 $options = [
  103.                     'currentPage' => $filters['page'],
  104.                     'total'       => 5,
  105.                     'lastPage'    => 5,
  106.                 ];
  107.                 $formObject=  $this->createForm(UserType::class,null,[
  108.                     'include_birthday'=> true,
  109.                     'include_agree' => true
  110.                 ]);
  111.                 //$formFactory = $this->container->get('fos_user.registration.form.factory');
  112.                 //$form        = $formFactory->createForm(['include_birthday' => true, 'include_agree' => true]);
  113.                 $form        $formObject->createView();
  114.                 $response $this->render('SocialFrontendBundle:Search:search_traffic_poll.html.twig', [
  115.                     'trafficID'     => $trafficReffId,
  116.                     'renderedUsers' => $renderedUsers,
  117.                     'trafficPoll'   => $pollEntity,
  118.                     'form'          => $form,
  119.                     'pagination'    => $this->renderView(
  120.                         'SocialFrontendBundle:Components:pagination.html.twig',
  121.                         [
  122.                             'filters'   => [],
  123.                             'options'   => $options,
  124.                             'routeName' => 'social_frontend_search_submit',
  125.                             'sort'      => null,
  126.                             'sort_type' => null,
  127.                         ]
  128.                     ),
  129.                 ]);
  130.                 $urlParams $pollEntity->getUrlParameters();
  131.                 foreach ($urlParams as $single) {
  132.                     $value $request->get($single->getKeyValue(), null);
  133.                     if ($value != null) {
  134.                         $cookie = new Cookie('tfpr[' $single->getKeyValue() . ']'$value0'/'nullfalse,
  135.                             false);
  136.                         $response->headers->setCookie($cookie);
  137.                     }
  138.                 }
  139.                 $cookie = new Cookie('tfpr[pollId]'$pollEntity->getId(), 0'/'nullfalsefalse);
  140.                 $response->headers->setCookie($cookie);
  141.                 return $response;
  142.             } else {
  143.                 return $this->redirectToRoute('social_frontend_homepage_account');
  144.             }
  145.         } catch (Exception $exception) {
  146.             /** @var Client $sentry */
  147.             $sentry $this->get('sentry.client');
  148.             $sentry->captureException($exception);
  149.             return $this->redirectToRoute('social_frontend_homepage_account');
  150.         }
  151.     }
  152.     public function addToSentry($exception)
  153.     {
  154.         /** @var Client $sentry */
  155.         $sentry $this->get('sentry.client');
  156.         $sentry->captureException($exception);
  157.         return $this->redirectToRoute('social_frontend_search');
  158.     }
  159.     /**
  160.      * @param Request $request
  161.      *
  162.      * @return JsonResponse|Response
  163.      */
  164.     public function submitAction(Request $request)
  165.     {
  166.         $pageLimit 12;
  167.         $em $this->get('doctrine.orm.entity_manager');
  168.         $filters $request->get('filters');
  169.         $currentPage = !$this->getUser() ? $request->get('page'1);
  170.         $filters['current_user'] = $this->getUser();
  171.         $filters['frontend_search_mode'] = true;
  172.         $filters['loop'] = $request->get('loop') ?: 0;
  173.         if ($this->getUser()) {
  174.             $filters['gender'] = $this->getUser()->getInterestedIn();
  175.         }
  176.         $filters['default_member_profile_username'] = $this->getParameter('default_member_profile')['username'];
  177.         $filters['page'] = $currentPage;
  178.         $filters['boost_limit'] = 3;
  179.         $filters['boost_offset'] = ($filters['page'] - 1) * $filters['boost_limit'];
  180.         $withoutBoosted = !(isset($filters['boost_profiles']) && $filters['boost_profiles'] === 'on');
  181.         $boostedUsers = (isset($filters['loop']) && $filters['loop'] == 0) ? $em->getRepository('SocialUserBundle:User')->getBoostedUsers($filters)->getQuery()->execute() : [];
  182.         $filters['limit'] = $pageLimit count($boostedUsers); //($this->getUser() && $this->getUser()->canDisplayCamsLink()) ? 10 : 12;
  183.         $filters['offset'] = ($filters['page'] - 1) * $filters['limit'];
  184.         $qbUsers $em->getRepository('SocialUserBundle:User')->getQueryBuilderByFilters($filtersnulltrue$withoutBoosted)->getQuery()->execute();
  185.         $total count($qbUsers);
  186. //        if (isset($filters['loop']) && $filters['loop'] == 1) {
  187. //            $allUsersFilter = $filters;
  188. //            $allUsersFilter['allUsers'] = true;
  189. //            $allUsers = $em->getRepository('SocialUserBundle:User')->getQueryBuilderByFilters($allUsersFilter, null, true, true)->getQuery()->execute();
  190. //            $qbUsers = $allUsers;
  191. //        }
  192.         shuffle($qbUsers);
  193.         $users array_merge($boostedUsers$qbUsers);
  194.         $totalPages ceil($total $pageLimit);
  195.         if ($request->isXmlHttpRequest()) {
  196.             return new JsonResponse(
  197.                 [
  198.                     'html' => $this->renderView(
  199.                         'SocialFrontendBundle:Search:submit.html.twig',
  200.                         ['users' => $users'page' => $currentPage'isInLoop' => isset($filters['loop']) && $filters['loop'] ? $filters['loop'] : 0]
  201.                     ),
  202.                     'lastPage' => $totalPages,
  203.                 ]
  204.             );
  205.         } else {
  206.             return $this->render('SocialFrontendBundle:Search:index.html.twig');
  207.         }
  208.     }
  209.     public function checkEmailInNeverbounceAction(Request $request)
  210.     {
  211.         try {
  212.             $em $this->getDoctrine()->getManager();
  213.             $email $request->get('email');
  214.             $result = [];
  215.             if ($email) {
  216.                 $validator = new EmailValidator();
  217.                 if ($validator->isValid($email, new RFCValidation())) {
  218.                     $isExistInHistory $em->getRepository(EmailListValidationHistory::class)->findOneBy(['email' => $email]);
  219.                     if (!$isExistInHistory) {
  220.                         $client HttpClient::create();
  221.                         $apiKey $_ENV['NEVERBOUNCE_API_KEY'];
  222.                         $response $client->request('GET''https://api.neverbounce.com/v4/single/check', [
  223.                             'query' => [
  224.                                 'key' => $apiKey,
  225.                                 'email' => $email
  226.                             ],
  227.                         ]);
  228.                         $result json_decode($response->getContent(), true);
  229.                         $emailListValidationHistory = new EmailListValidationHistory();
  230.                         $emailListValidationHistory->setEmail($email);
  231.                         $emailListValidationHistory->setStatus($result['result']);
  232.                         $em->persist($emailListValidationHistory);
  233.                         $em->flush();
  234.                     } else {
  235.                         $result['result'] = $isExistInHistory->getStatus();
  236.                     }
  237.                 } else {
  238.                     $result['result'] = 'invalid';
  239.                 }
  240.             }
  241.             return new JsonResponse(['result' => $result['result']]);
  242.         } catch (\Exception $exception) {
  243.             $this->get('sentry.client')->captureMessage($exception);
  244.         }
  245.     }
  246. }