src/Controller/Website/JobController.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Website;
  3. use App\Twig\Components\Common\imageTrait;
  4. use FOS\RestBundle\Controller\Annotations as Rest;
  5. use FOS\RestBundle\View\ViewHandlerInterface;
  6. use Sulu\Bundle\MediaBundle\Api\Media;
  7. use Sulu\Bundle\SnippetBundle\Twig\SnippetAreaTwigExtension;
  8. use Sulu\Bundle\WebsiteBundle\Twig\Content\ContentTwigExtensionInterface;
  9. use Sulu\Component\Content\SmartContent\QueryBuilder;
  10. use Sulu\Component\DocumentManager\DocumentManagerInterface;
  11. use Sulu\Component\Rest\AbstractRestController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use function Aws\filter;
  15. use function Clue\StreamFilter\fun;
  16. /**
  17.  * Class of case studies.
  18.  *
  19.  * @author Michał Wiązek <michal.wiazek@makolab.com>
  20.  *
  21.  * @Rest\Route("/api/jobs", name="app.jobs.", defaults={ "_format" = "json" })
  22.  */
  23. class JobController extends AbstractRestController
  24. {
  25.     use imageTrait;
  26.     private DocumentManagerInterface $documentManager;
  27.     private ContentTwigExtensionInterface $contentTwigExtension;
  28.     private SnippetAreaTwigExtension $snippetAreaTwigExtension;
  29.     /**
  30.      * Constructor.
  31.      */
  32.     public function __construct(
  33.         DocumentManagerInterface $documentManager,
  34.         ContentTwigExtensionInterface $contentTwigExtension,
  35.         ViewHandlerInterface $viewHandler,
  36.         ?TokenStorageInterface $tokenStorage null,
  37.         SnippetAreaTwigExtension $snippetAreaTwigExtension,
  38.     ) {
  39.         parent::__construct($viewHandler$tokenStorage);
  40.         $this->documentManager $documentManager;
  41.         $this->contentTwigExtension $contentTwigExtension;
  42.         $this->snippetAreaTwigExtension $snippetAreaTwigExtension;
  43.     }
  44.     /**
  45.      * Index action.
  46.      *
  47.      * @Rest\Get("")
  48.      *
  49.      * @return \Symfony\Component\HttpFoundation\Response
  50.      *
  51.      * @throws \Sulu\Component\DocumentManager\Exception\DocumentManagerException
  52.      */
  53.     public function indexAction(Request $request)
  54.     {
  55.         $category $request->query->get('filter''');
  56.         $page $request->query->get('page'1);
  57.         $limit $request->query->get('limit'2);
  58.         $locale $request->query->get('locale''en');
  59.         $cards $this->snippetAreaTwigExtension->loadByArea('filterJob'null$locale)['content']['jobs'] ?? [];
  60.         if (empty($category)) {
  61.             $data = [
  62.                 'jobs' => $this->convertData($cards),
  63.                 'page' => $page,
  64.                 'totalCount' => count($cards),
  65.             ];
  66.             $view $this->view($data);
  67.             return $this->handleView($view);
  68.         }
  69.         $cards $this->search($category$cards);
  70.         $total_count count($cards);
  71.         $total_pages ceil($total_count $limit);
  72.         if ($page $total_pages) {
  73.             $page $total_pages;
  74.         }
  75.         if ($page 1) {
  76.             $page 1;
  77.         }
  78.         $offset = ($page 1) * $limit;
  79.         $data array_slice($cards$offset$limit);
  80.         $data = [
  81.             'jobs' => $this->convertData($data),
  82.             'page' => $page,
  83.             'totalCount' => $total_count,
  84.             'totalPages' => $total_pages
  85.         ];
  86.         $view $this->view($data);
  87.         return $this->handleView($view);
  88.     }
  89.     public function search($category_slug$cards)
  90.     {
  91.         return array_filter($cards, function ($item) use ($category_slug) {
  92.             foreach ($item['filters'] as $filter) {
  93.                 if ($filter['slug'] == $category_slug) {
  94.                     return true;
  95.                 }
  96.             }
  97.             return false;
  98.         });
  99.     }
  100.     public function convertData($array)
  101.     {
  102.         return array_values(array_map(function ($item) {
  103.             return [
  104.                 'url' => $item['url'],
  105.                 'title' => $item['title'],
  106.                 'place' => $item['place'],
  107.                 'image' => [
  108.                     'name'    => $item['image']->getName(),
  109.                     'lazySrc' => $item['image']->getThumbnails()['full-size'],
  110.                     'url'     => $item['image']->getThumbnails()['full-size'],
  111.                 ],
  112.             ];
  113.         }, $array));
  114.     }
  115.     /**
  116.      * Get image with defined tag.
  117.      *
  118.      * If many images with the same tag, get first.
  119.      * If getAny flag is true and no images with tag, get first of all images.
  120.      *
  121.      * @param false $getAny
  122.      */
  123.     private function getTeaserImage(array $mediasstring $tag$getAny false): ?Media
  124.     {
  125.         $images array_filter($medias, fn($media) => in_array($tag$media->getTags()));
  126.         array_unshift($images);
  127.         if ($getAny && empty($images)) {
  128.             $images $medias;
  129.         }
  130.         return !empty($images) ? $images[0] : null;
  131.     }
  132. }