src/Bidcoz/Bundle/CoreBundle/Controller/CoreController.php line 106

Open in your IDE?
  1. <?php
  2. namespace Bidcoz\Bundle\CoreBundle\Controller;
  3. use Bidcoz\Bundle\CoreBundle\BidcozCoreServices;
  4. use Bidcoz\Bundle\ManageBundle\Form\Type\FastListFilterConfig;
  5. use Bidcoz\Bundle\ManageBundle\Form\Type\FastListFilterType;
  6. use Doctrine\ORM\QueryBuilder;
  7. use Doctrine\ORM\Tools\Pagination\Paginator;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Form\FormError;
  10. use Symfony\Component\Form\FormErrorIterator;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Validator\ConstraintViolation;
  17. abstract class CoreController extends AbstractController
  18. {
  19.     use BidcozCoreServices;
  20.     protected array $hostRedirectMap = [
  21.         'nvidia.causepilot.com' => '/nvidia/nvpride2025',
  22.         'chg.causepilot.com'    => '/chg/madw2024',
  23.     ];
  24.     protected function returnJson($data$convert false$status 200)
  25.     {
  26.         // TODO: Fix and refactor calls to \Bidcoz\Bundle\CoreBundle\Controller\CoreController::returnJson
  27.         // Fixes calls with wrong set of parameters such as: $this->returnJson($payload, 400)
  28.         if (is_numeric($convert)) {
  29.             $status  $convert;
  30.             $convert false;
  31.         }
  32.         $data $convert
  33.             json_decode($data)
  34.             : $data;
  35.         return new JsonResponse($data$status);
  36.     }
  37.     protected function returnResponse(string $data$status 200)
  38.     {
  39.         return new Response($data$status);
  40.     }
  41.     protected function returnRedirect(string $url)
  42.     {
  43.         return new RedirectResponse($url);
  44.     }
  45.     protected function returnJsonSuccess($message 'success', array $data = [])
  46.     {
  47.         return $this->returnJson(array_merge($data, [
  48.             'status'  => 'ok',
  49.             'message' => $message,
  50.         ]));
  51.     }
  52.     protected function returnJsonError($errors, array $data = [])
  53.     {
  54.         if ($errors instanceof FormErrorIterator) {
  55.             $errorArray array_map(function ($error) {
  56.                 return $error->getMessage();
  57.             }, iterator_to_array($errors));
  58.         } elseif (is_array($errors)) {
  59.             $errorArray $errors;
  60.         } else {
  61.             $errorArray 'Unexpected Error';
  62.         }
  63.         return $this->returnJson(array_merge($data, [
  64.             'status' => 'error',
  65.             'errors' => $errors,
  66.         ]), false400);
  67.     }
  68.     protected function getPagination($paginatable)
  69.     {
  70.         return $this->getPaginator()->paginate(
  71.             $paginatable,
  72.             $this->getRequestStack()->getCurrentRequest()->query->get('page'1),
  73.             24
  74.         );
  75.     }
  76.     protected function applyFilters(Request $requestFormInterface $filterFormQueryBuilder $qb)
  77.     {
  78.         if ($request->query->has($filterForm->getName())) {
  79.             $filterForm->submit($request->query->get($filterForm->getName()));
  80.             $this->getQueryBuilderFilter()->addFilterConditions($filterForm$qb);
  81.             return true;
  82.         }
  83.         return false;
  84.     }
  85.     protected function messageAccessDeniedException($message$type 'info')
  86.     {
  87.         $this->addFlash($type$message);
  88.         throw $this->createAccessDeniedException($message);
  89.     }
  90.     protected function isSuperAdmin(): bool
  91.     {
  92.         return $this
  93.             ->get('security.authorization_checker')
  94.             ->isGranted('ROLE_SUPER_ADMIN');
  95.     }
  96.     protected function getFormErrorMessage(FormError $error): string
  97.     {
  98.         $message $error->getMessage();
  99.         $cause   $error->getCause();
  100.         $path $cause && $cause instanceof ConstraintViolation
  101.             $cause->getPropertyPath()
  102.             : null;
  103.         return $path
  104.             sprintf('%s - %s'$path$message)
  105.             : $message;
  106.     }
  107.     protected function getFastListFilterForm(FastListFilterConfig $config)
  108.     {
  109.         return $this->createForm(FastListFilterType::class, [], [
  110.             'config' => $config,
  111.         ]);
  112.     }
  113.     protected function applyFastListFilters(Request $requestFormInterface $formQueryBuilder $qb)
  114.     {
  115.         if ($request->query->has($form->getName())) {
  116.             $form->submit($request->query->get($form->getName()));
  117.             $data   $form->getData();
  118.             $config $form->getConfig()->getOptions()['config'];
  119.             foreach ($config->all() as $field => $fieldConfig) {
  120.                 if ($data[$field]) {
  121.                     $qb
  122.                         ->andWhere(sprintf('%s = :%s'$fieldConfig['mapping'], $field))
  123.                         ->setParameter($field$data[$field]);
  124.                 }
  125.             }
  126.         }
  127.     }
  128.     protected function checkRedirectToFastListRoute(Request $request)
  129.     {
  130.         $campaign $request->attributes->get('campaign');
  131.         $user     $this->getUser();
  132.         if ($user->getUseFastList() || $campaign->isUseFastList()) {
  133.             // Continue
  134.         } else {
  135.             return;
  136.         }
  137.         $request->attributes->set('_is_fast_list_route'true);
  138.         if ($request->cookies->has('_fast_list_override')) {
  139.             return;
  140.         }
  141.         $url sprintf('%s/fast-list'$request->getPathInfo());
  142.         return $this->returnRedirect($url);
  143.     }
  144.     protected function paginate(Request $requestQueryBuilder $qb): array
  145.     {
  146.         $page     intval($request->query->get('page'1));
  147.         $pageSize intval(min(100$request->query->get('pageSize'25)));
  148.         $query $qb->getQuery();
  149.         $query->setMaxResults($pageSize);
  150.         if ($page 1) {
  151.             $query->setFirstResult($pageSize * ($page 1));
  152.         }
  153.         // The base url is the URL minus the page parameter if present
  154.         $hasParams false;
  155.         $urlParams $request->query->all();
  156.         unset($urlParams['page']);
  157.         $clearUrl $request->getPathInfo();
  158.         if (count($urlParams)) {
  159.             $hasParams true;
  160.             $baseUrl   $clearUrl.'?'.http_build_query($urlParams);
  161.         } else {
  162.             $baseUrl $clearUrl;
  163.         }
  164.         $paginatable = new Paginator($query);
  165.         $totalRecords $paginatable->count();
  166.         $totalPages   ceil($totalRecords $pageSize);
  167.         // Decide what page(s) to show
  168.         $firstPage 1;
  169.         $lastPage  $totalPages;
  170.         if ($totalPages 10) {
  171.             $firstPage $page $page 1;
  172.             $lastPage  min($totalPages$firstPage 10);
  173.         }
  174.         return [
  175.             'paginatable'  => $paginatable,
  176.             'page'         => $page,
  177.             'pageSize'     => $pageSize,
  178.             'totalPages'   => $totalPages,
  179.             'totalRecords' => $totalRecords,
  180.             'firstPage'    => $firstPage,
  181.             'lastPage'     => $lastPage,
  182.             'base_url'     => $baseUrl,
  183.             'has_params'   => $hasParams,
  184.             'clear_url'    => $clearUrl,
  185.         ];
  186.     }
  187. }