<?php
namespace App\Controller\Website;
use App\Twig\Components\Common\imageTrait;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sulu\Bundle\MediaBundle\Api\Media;
use Sulu\Bundle\SnippetBundle\Twig\SnippetAreaTwigExtension;
use Sulu\Bundle\WebsiteBundle\Twig\Content\ContentTwigExtensionInterface;
use Sulu\Component\Content\SmartContent\QueryBuilder;
use Sulu\Component\DocumentManager\DocumentManagerInterface;
use Sulu\Component\Rest\AbstractRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use function Aws\filter;
use function Clue\StreamFilter\fun;
/**
* Class of case studies.
*
* @author Michał Wiązek <michal.wiazek@makolab.com>
*
* @Rest\Route("/api/jobs", name="app.jobs.", defaults={ "_format" = "json" })
*/
class JobController extends AbstractRestController
{
use imageTrait;
private DocumentManagerInterface $documentManager;
private ContentTwigExtensionInterface $contentTwigExtension;
private SnippetAreaTwigExtension $snippetAreaTwigExtension;
/**
* Constructor.
*/
public function __construct(
DocumentManagerInterface $documentManager,
ContentTwigExtensionInterface $contentTwigExtension,
ViewHandlerInterface $viewHandler,
?TokenStorageInterface $tokenStorage = null,
SnippetAreaTwigExtension $snippetAreaTwigExtension,
) {
parent::__construct($viewHandler, $tokenStorage);
$this->documentManager = $documentManager;
$this->contentTwigExtension = $contentTwigExtension;
$this->snippetAreaTwigExtension = $snippetAreaTwigExtension;
}
/**
* Index action.
*
* @Rest\Get("")
*
* @return \Symfony\Component\HttpFoundation\Response
*
* @throws \Sulu\Component\DocumentManager\Exception\DocumentManagerException
*/
public function indexAction(Request $request)
{
$category = $request->query->get('filter', '');
$page = $request->query->get('page', 1);
$limit = $request->query->get('limit', 2);
$locale = $request->query->get('locale', 'en');
$cards = $this->snippetAreaTwigExtension->loadByArea('filterJob', null, $locale)['content']['jobs'] ?? [];
if (empty($category)) {
$data = [
'jobs' => $this->convertData($cards),
'page' => $page,
'totalCount' => count($cards),
];
$view = $this->view($data);
return $this->handleView($view);
}
$cards = $this->search($category, $cards);
$total_count = count($cards);
$total_pages = ceil($total_count / $limit);
if ($page > $total_pages) {
$page = $total_pages;
}
if ($page < 1) {
$page = 1;
}
$offset = ($page - 1) * $limit;
$data = array_slice($cards, $offset, $limit);
$data = [
'jobs' => $this->convertData($data),
'page' => $page,
'totalCount' => $total_count,
'totalPages' => $total_pages
];
$view = $this->view($data);
return $this->handleView($view);
}
public function search($category_slug, $cards)
{
return array_filter($cards, function ($item) use ($category_slug) {
foreach ($item['filters'] as $filter) {
if ($filter['slug'] == $category_slug) {
return true;
}
}
return false;
});
}
public function convertData($array)
{
return array_values(array_map(function ($item) {
return [
'url' => $item['url'],
'title' => $item['title'],
'place' => $item['place'],
'image' => [
'name' => $item['image']->getName(),
'lazySrc' => $item['image']->getThumbnails()['full-size'],
'url' => $item['image']->getThumbnails()['full-size'],
],
];
}, $array));
}
/**
* Get image with defined tag.
*
* If many images with the same tag, get first.
* If getAny flag is true and no images with tag, get first of all images.
*
* @param false $getAny
*/
private function getTeaserImage(array $medias, string $tag, $getAny = false): ?Media
{
$images = array_filter($medias, fn($media) => in_array($tag, $media->getTags()));
array_unshift($images);
if ($getAny && empty($images)) {
$images = $medias;
}
return !empty($images) ? $images[0] : null;
}
}