<?php
namespace App\Controller\Admin;
use App\Entity\Magazine\Magazine;
use App\Entity\Magazine\MagazinePage;
use Doctrine\ORM\EntityManager;
use http\Client\Request;
use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ProductAdminController extends Controller
{
public function mpickerAction(): JsonResponse
{
$request = $this->getRequest();
/** @var EntityManager $em */
$em = $this->get('doctrine.orm.entity_manager');
$magazine_id = $request->get('magazine');
$magazine = $em->getRepository(Magazine::class)->find($magazine_id);
if(!$magazine) {
return $this->json(['message' => 'Magazine not found'], Response::HTTP_NOT_FOUND);
}
$pages = $magazine->getPages();
$pageArr = [];
foreach ($pages as $i => $page) {
$pageArr[] = ['text' => 'Page ' . $page->getPosition(), 'value' => strval($page->getPosition())];
}
return new JsonResponse($pageArr);
}
public function ppickerAction(): Response
{
$request = $this->getRequest();
/** @var EntityManager $em */
$em = $this->get('doctrine.orm.entity_manager');
$magazine_id = $request->get('magazine');
$page_position = $request->get('page');
$magazine = $em->getRepository(Magazine::class)->find($magazine_id);
if(!$magazine) {
return $this->json(['message' => 'Magazine not found'], Response::HTTP_NOT_FOUND);
}
$imgArr = [];
$slug = 'src-' . $magazine->getSlug();
$publicMagDir = $this->getParameter('kernel.project_dir') . '/public/magazine/';
$imageDir = '/magazine/' . $slug . '/assets/images/';
$imgfinder = new Finder();
$imgfinder->files()->name('/item.*pg' . $page_position . '\.png/')->name('/item.*pg' . $page_position . '\.jpg/')->in($publicMagDir . pathinfo($slug, PATHINFO_FILENAME) . '/assets/images/');
if ($imgfinder->hasResults()) {
foreach ($imgfinder as $img) {
$imgArr[] = ['text' => $imageDir . $img->getFilename(), 'value' => $imageDir . $img->getFilename()];
}
}
return new JsonResponse($imgArr);
}
}