Skip to content

Commit

Permalink
added ability for administrators to schedule feed generation from adm…
Browse files Browse the repository at this point in the history
…inistration
  • Loading branch information
TomasLudvik committed Nov 21, 2023
1 parent 5f1cfa8 commit 7dcd24e
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 7 deletions.
9 changes: 9 additions & 0 deletions UPGRADE-14.0.md
Expand Up @@ -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()`
Expand Down
42 changes: 40 additions & 2 deletions packages/framework/src/Controller/Admin/FeedController.php
Expand Up @@ -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
Expand All @@ -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,
) {
}

Expand Down Expand Up @@ -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/")
*/
Expand All @@ -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(),
];
}
Expand All @@ -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', [
Expand Down
Expand Up @@ -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);
}
Expand Down
11 changes: 11 additions & 0 deletions packages/framework/src/Model/Feed/FeedFacade.php
Expand Up @@ -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
*/
Expand Down
8 changes: 8 additions & 0 deletions packages/framework/src/Model/Feed/FeedModule.php
Expand Up @@ -68,4 +68,12 @@ public function unschedule(): void
{
$this->scheduled = false;
}

/**
* @return bool
*/
public function isScheduled(): bool
{
return $this->scheduled;
}
}
9 changes: 8 additions & 1 deletion packages/framework/src/Model/Feed/FeedModuleRepository.php
Expand Up @@ -6,6 +6,7 @@

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Shopsys\FrameworkBundle\Model\Feed\Exception\FeedNotFoundException;

class FeedModuleRepository
{
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Expand Up @@ -24,7 +24,7 @@
{% endif %}
{% endblock %}

{% block grid_value_cell_id_actions %}
{% block grid_value_cell_id_generate %}
<a href="{{ url(
'admin_feed_generate', {
feedName: row.feedName,
Expand All @@ -33,3 +33,17 @@
) }}" class="btn">{{ 'Generate'|trans }}</a>

{% endblock %}

{% block grid_value_cell_id_schedule %}
{% if value is same as(false) %}
<a href="{{ url(
'admin_feed_schedule', {
feedName: row.feedName,
domainId: row.domainConfig.id
}
) }}" class="btn">{{ 'Schedule'|trans }}</a>
{% else %}
<span style="color: green;"><i class="svg svg-checked"></i> {{ 'Scheduled'|trans }}</span>
{% endif %}

{% endblock %}

0 comments on commit 7dcd24e

Please sign in to comment.