<?php
namespace App\EventListener\SonataAdmin\Block\VOD;
use App\Entity\ProductTVSeries;
use App\Entity\VOD\Title;
use App\Enum\Common\AdminCodesEnum;
use Sonata\BlockBundle\Event\BlockEvent;
use Sonata\BlockBundle\Model\Block;
class TitleItemNumberListener
{
/**
* Responsible for providing a form modal for modifying the title item numbers association.
*
* @param \Sonata\BlockBundle\Event\BlockEvent $event
* The instance of the dispatched event
*/
public function onShowActionDispatched(BlockEvent $event): void
{
if ((!$admin = $event->getSetting('admin')) || (!$title = $event->getSetting('object'))) {
return;
}
// The sonata.show event is dispatched for every admin, having "Show" action enabled and we don't want
// this block to render the "Update" button for each admin.
if (AdminCodesEnum::VOD_TITLE_RELEASE_DATE !== $admin->getCode()) {
return;
}
// We are interested only in VET's of type product series, because they are the ones that can have
// multiple (different) item numbers for single product (association).
if (!$title instanceof Title || !$title->getProduct() instanceof ProductTVSeries) {
return;
}
// If for some reason we still don't have NAV items available, then there is nothing we can
// edit at the moment.
if (!$title->getItemNumbers()->count()) {
return;
}
$block = new Block();
$block->setId(uniqid('', true));
$block->setSettings($event->getSettings());
$block->setType('app.block.vod.title_item_numbers');
$event->addBlock($block);
}
}