From 7dcd24e5754111127b2678edbe96f3232366d9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Ludvik?= Date: Tue, 21 Nov 2023 11:38:55 +0100 Subject: [PATCH] added ability for administrators to schedule feed generation from administration --- UPGRADE-14.0.md | 9 ++++ .../src/Controller/Admin/FeedController.php | 42 ++++++++++++++++++- .../Feed/Exception/FeedNotFoundException.php | 14 +++++-- .../framework/src/Model/Feed/FeedFacade.php | 11 +++++ .../framework/src/Model/Feed/FeedModule.php | 8 ++++ .../src/Model/Feed/FeedModuleRepository.php | 9 +++- .../Admin/Content/Feed/listGrid.html.twig | 16 ++++++- 7 files changed, 102 insertions(+), 7 deletions(-) diff --git a/UPGRADE-14.0.md b/UPGRADE-14.0.md index cff2613e0ee..2498e141eff 100644 --- a/UPGRADE-14.0.md +++ b/UPGRADE-14.0.md @@ -167,6 +167,15 @@ Follow the instructions in relevant sections, e.g. `shopsys/coding-standards` or protected readonly GridFactory $gridFactory, protected readonly Domain $domain, + protected readonly FeedRegistry $feedRegistry, + + protected readonly FeedModuleRepository $feedModuleRepository, + ) + ``` + - method `Shopsys\FrameworkBundle\Model\Feed\Exception\FeedNotFoundException__construct` changed its interface: + ```diff + public function __construct( + string $name, + + ?int $domainId = null, + ?Exception $previous = null ) ``` - method `Shopsys\FrameworkBundle\Model\Feed\FeedRegistry::getFeeds()` has been replaced with method `Shopsys\FrameworkBundle\Model\Feed\FeedRegistry::getFeedsForCurrentTime()` diff --git a/packages/framework/src/Controller/Admin/FeedController.php b/packages/framework/src/Controller/Admin/FeedController.php index 6918787bfd5..35862a041ba 100644 --- a/packages/framework/src/Controller/Admin/FeedController.php +++ b/packages/framework/src/Controller/Admin/FeedController.php @@ -10,8 +10,10 @@ use Shopsys\FrameworkBundle\Component\Grid\GridFactory; use Shopsys\FrameworkBundle\Model\Feed\Exception\FeedNotFoundException; use Shopsys\FrameworkBundle\Model\Feed\FeedFacade; +use Shopsys\FrameworkBundle\Model\Feed\FeedModuleRepository; use Shopsys\FrameworkBundle\Model\Feed\FeedRegistry; use Shopsys\FrameworkBundle\Model\Security\Roles; +use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\Annotation\Route; class FeedController extends AdminBaseController @@ -21,12 +23,14 @@ class FeedController extends AdminBaseController * @param \Shopsys\FrameworkBundle\Component\Grid\GridFactory $gridFactory * @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain * @param \Shopsys\FrameworkBundle\Model\Feed\FeedRegistry $feedRegistry + * @param \Shopsys\FrameworkBundle\Model\Feed\FeedModuleRepository $feedModuleRepository */ public function __construct( protected readonly FeedFacade $feedFacade, protected readonly GridFactory $gridFactory, protected readonly Domain $domain, protected readonly FeedRegistry $feedRegistry, + protected readonly FeedModuleRepository $feedModuleRepository, ) { } @@ -60,6 +64,37 @@ public function generateAction($feedName, $domainId) return $this->redirectToRoute('admin_feed_list'); } + /** + * @Route("/feed/schedule/{feedName}/{domainId}", requirements={"domainId" = "\d+"}) + * @param string $feedName + * @param int $domainId + * @return \Symfony\Component\HttpFoundation\RedirectResponse + */ + public function scheduleAction(string $feedName, int $domainId): RedirectResponse + { + try { + $this->feedFacade->scheduleFeedByNameAndDomainId($feedName, $domainId); + + $this->addSuccessFlashTwig( + t('Feed "{{ feedName }}" on domain ID {{ domainId }} successfully scheduled.'), + [ + 'feedName' => $feedName, + 'domainId' => $domainId, + ], + ); + } catch (FeedNotFoundException $ex) { + $this->addErrorFlashTwig( + t('Feed "{{ feedName }}" on domain ID {{ domainId }} not found.'), + [ + 'feedName' => $feedName, + 'domainId' => $domainId, + ], + ); + } + + return $this->redirectToRoute('admin_feed_list'); + } + /** * @Route("/feed/list/") */ @@ -80,7 +115,8 @@ public function listAction() 'domainConfig' => $domainConfig, 'url' => $this->feedFacade->getFeedUrl($feedInfo, $domainConfig), 'created' => $feedTimestamp === null ? null : (new DateTime())->setTimestamp($feedTimestamp), - 'actions' => null, + 'generate' => null, + 'schedule' => $this->feedModuleRepository->getFeedModuleByNameAndDomainId($feedInfo->getName(), $domainId)->isScheduled(), 'additionalInformation' => $feedInfo->getAdditionalInformation(), ]; } @@ -95,9 +131,11 @@ public function listAction() $grid->addColumn('url', 'url', t('Url address')); if ($this->isGranted(Roles::ROLE_SUPER_ADMIN)) { - $grid->addColumn('actions', 'actions', t('Action')); + $grid->addColumn('generate', 'generate', t('Generate')); } + $grid->addColumn('schedule', 'schedule', t('Schedule')); + $grid->setTheme('@ShopsysFramework/Admin/Content/Feed/listGrid.html.twig'); return $this->render('@ShopsysFramework/Admin/Content/Feed/list.html.twig', [ diff --git a/packages/framework/src/Model/Feed/Exception/FeedNotFoundException.php b/packages/framework/src/Model/Feed/Exception/FeedNotFoundException.php index 85dcf1f7d6d..7c541cab802 100644 --- a/packages/framework/src/Model/Feed/Exception/FeedNotFoundException.php +++ b/packages/framework/src/Model/Feed/Exception/FeedNotFoundException.php @@ -10,11 +10,19 @@ class FeedNotFoundException extends Exception implements FeedException { /** * @param string $name + * @param int|null $domainId * @param \Exception|null $previous */ - public function __construct(string $name, ?Exception $previous = null) - { - $message = 'Feed with name "' . $name . ' not found.'; + public function __construct( + string $name, + ?int $domainId = null, + ?Exception $previous = null + ) { + $message = sprintf( + 'Feed with name "%s"%s not found.', + $name, + $domainId !== null ? sprintf(' and domain ID %d', $domainId) : '', + ); parent::__construct($message, 0, $previous); } diff --git a/packages/framework/src/Model/Feed/FeedFacade.php b/packages/framework/src/Model/Feed/FeedFacade.php index 33afb91fcb6..1cd053b12db 100644 --- a/packages/framework/src/Model/Feed/FeedFacade.php +++ b/packages/framework/src/Model/Feed/FeedFacade.php @@ -157,6 +157,17 @@ public function scheduleFeedByName(string $name): void $this->markFeedConfigsForScheduling($feedConfigsToSchedule); } + /** + * @param string $name + * @param int $domainId + */ + public function scheduleFeedByNameAndDomainId(string $name, int $domainId): void + { + $feedModule = $this->feedModuleRepository->getFeedModuleByNameAndDomainId($name, $domainId); + $feedModule->schedule(); + $this->em->flush(); + } + /** * @param \Shopsys\FrameworkBundle\Model\Feed\FeedModule $feedModule */ diff --git a/packages/framework/src/Model/Feed/FeedModule.php b/packages/framework/src/Model/Feed/FeedModule.php index 3e6b271bb7f..bb4909adb7c 100644 --- a/packages/framework/src/Model/Feed/FeedModule.php +++ b/packages/framework/src/Model/Feed/FeedModule.php @@ -68,4 +68,12 @@ public function unschedule(): void { $this->scheduled = false; } + + /** + * @return bool + */ + public function isScheduled(): bool + { + return $this->scheduled; + } } diff --git a/packages/framework/src/Model/Feed/FeedModuleRepository.php b/packages/framework/src/Model/Feed/FeedModuleRepository.php index 5e22775aacb..3c501038790 100644 --- a/packages/framework/src/Model/Feed/FeedModuleRepository.php +++ b/packages/framework/src/Model/Feed/FeedModuleRepository.php @@ -6,6 +6,7 @@ use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; +use Shopsys\FrameworkBundle\Model\Feed\Exception\FeedNotFoundException; class FeedModuleRepository { @@ -66,10 +67,16 @@ public function getFeedModulesByConfig(FeedConfig $feedConfig): array */ public function getFeedModuleByNameAndDomainId(string $name, int $domainId): FeedModule { - return $this->getFeedModuleRepository()->findOneBy([ + $feedModule = $this->getFeedModuleRepository()->findOneBy([ 'name' => $name, 'domainId' => $domainId, ]); + + if ($feedModule === null) { + throw new FeedNotFoundException($name, $domainId); + } + + return $feedModule; } /** diff --git a/packages/framework/src/Resources/views/Admin/Content/Feed/listGrid.html.twig b/packages/framework/src/Resources/views/Admin/Content/Feed/listGrid.html.twig index b7a91eba0d6..e9d4ed451dd 100644 --- a/packages/framework/src/Resources/views/Admin/Content/Feed/listGrid.html.twig +++ b/packages/framework/src/Resources/views/Admin/Content/Feed/listGrid.html.twig @@ -24,7 +24,7 @@ {% endif %} {% endblock %} -{% block grid_value_cell_id_actions %} +{% block grid_value_cell_id_generate %} {{ 'Generate'|trans }} {% endblock %} + +{% block grid_value_cell_id_schedule %} + {% if value is same as(false) %} + {{ 'Schedule'|trans }} + {% else %} + {{ 'Scheduled'|trans }} + {% endif %} + +{% endblock %}