add public feed

This commit is contained in:
Simon Vieille 2020-12-21 17:44:08 +01:00
parent c3473a4045
commit 94a0895d39
Signed by: deblan
GPG Key ID: 03383D15A1D31745
6 changed files with 170 additions and 5 deletions

View File

@ -32,20 +32,22 @@ class MailingListCommand extends Command
{ {
$this $this
->setDescription('List mailings') ->setDescription('List mailings')
->addOption('feed', null, InputOption::VALUE_NONE, 'Show URLs of feeds')
; ;
} }
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
$io = new SymfonyStyle($input, $output); $io = new SymfonyStyle($input, $output);
$showFeed = $input->getOption('feed');
$headers = ['Label', 'ID', 'Feed', 'Created at', 'Updated at']; $headers = ['Label', 'ID', 'Feed', 'Public', 'Created at', 'Updated at'];
$rows = []; $rows = [];
$entities = $this->repo->findAll([], ['createdAt' => 'DEC']); $entities = $this->repo->findAll([], ['createdAt' => 'DEC']);
foreach ($entities as $entity) { foreach ($entities as $entity) {
$rows[] = [ $row = [
$entity->getLabel(), $entity->getLabel(),
$entity->getId(), $entity->getId(),
$this->router->generate( $this->router->generate(
@ -53,9 +55,20 @@ class MailingListCommand extends Command
['mailing' => $entity->getId()], ['mailing' => $entity->getId()],
UrlGeneratorInterface::ABSOLUTE_URL UrlGeneratorInterface::ABSOLUTE_URL
), ),
$entity->getIsPublic() ? 'y' : 'n',
$entity->getCreatedAt()->format('Y-m-d H:i:s'), $entity->getCreatedAt()->format('Y-m-d H:i:s'),
$entity->getUpdatedAt()->format('Y-m-d H:i:s'), $entity->getUpdatedAt()->format('Y-m-d H:i:s'),
]; ];
if (!$showFeed) {
unset($row[2]);
}
$rows[] = $row;
}
if (!$showFeed) {
unset($headers[2]);
} }
$io->table($headers, $rows); $io->table($headers, $rows);

View File

@ -29,6 +29,7 @@ class MailingNewCommand extends Command
$this $this
->setDescription('Create a new mailing') ->setDescription('Create a new mailing')
->addArgument('label', InputArgument::REQUIRED, 'Label of the mailing') ->addArgument('label', InputArgument::REQUIRED, 'Label of the mailing')
->addOption('public', 'p', InputOption::VALUE_NONE, 'Make the mailing public')
; ;
} }
@ -38,7 +39,9 @@ class MailingNewCommand extends Command
$label = $input->getArgument('label'); $label = $input->getArgument('label');
$entity = new Mailing(); $entity = new Mailing();
$entity->setLabel($label); $entity
->setLabel($label)
->setIsPublic($input->getOption('public'));
$this->em->persist($entity); $this->em->persist($entity);
$this->em->flush(); $this->em->flush();

View File

@ -0,0 +1,60 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\MailingRepository;
class MailingPublishCommand extends Command
{
protected static $defaultName = 'mailing:publish';
protected EntityManagerInterface $em;
protected MailingRepository $repo;
public function __construct(EntityManagerInterface $em, MailingRepository $repo)
{
parent::__construct();
$this->em = $em;
$this->repo = $repo;
}
protected function configure()
{
$this
->setDescription('Publish a mailing')
->addArgument('id', InputArgument::OPTIONAL, 'ID of the mailing')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$id = $input->getArgument('id');
$entity = $this->repo->find(['id' => $id]);
if (null === $entity) {
$io->error(sprintf('"%s" is not found!', $id));
return Command::FAILURE;
}
$entity->setIsPublic(true);
$this->em->persist($entity);
$this->em->flush();
$io->success(sprintf('"%s" is now public', $entity->getId()));
return Command::SUCCESS;
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\MailingRepository;
class MailingUnpublishCommand extends Command
{
protected static $defaultName = 'mailing:unpublish';
protected EntityManagerInterface $em;
protected MailingRepository $repo;
public function __construct(EntityManagerInterface $em, MailingRepository $repo)
{
parent::__construct();
$this->em = $em;
$this->repo = $repo;
}
protected function configure()
{
$this
->setDescription('Unpublish a mailing')
->addArgument('id', InputArgument::OPTIONAL, 'ID of the mailing')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$id = $input->getArgument('id');
$entity = $this->repo->find(['id' => $id]);
if (null === $entity) {
$io->error(sprintf('"%s" is not found!', $id));
return Command::FAILURE;
}
$entity->setIsPublic(false);
$this->em->persist($entity);
$this->em->flush();
$io->success(sprintf('"%s" is now private', $entity->getId()));
return Command::SUCCESS;
}
}

View File

@ -5,14 +5,26 @@ namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use App\Repository\MailingRepository;
class AppController extends AbstractController class AppController extends AbstractController
{ {
/** /**
* @Route("/", name="home") * @Route("/", name="home")
*/ */
public function home(): Response public function home(MailingRepository $mailingRepo): Response
{ {
return $this->render('app/home.html.twig'); $mailings = $mailingRepo->findBy(
[
'isPublic' => true,
],
[
'label' => 'ASC',
]
);
return $this->render('app/home.html.twig', [
'mailings' => $mailings,
]);
} }
} }

View File

@ -36,6 +36,11 @@ class Mailing
*/ */
private $mails; private $mails;
/**
* @ORM\Column(type="boolean", options={"default": 0})
*/
private $isPublic = false;
public function __construct() public function __construct()
{ {
$this->mails = new ArrayCollection(); $this->mails = new ArrayCollection();
@ -87,4 +92,16 @@ class Mailing
return $this; return $this;
} }
public function getIsPublic(): ?bool
{
return $this->isPublic;
}
public function setIsPublic(bool $isPublic): self
{
$this->isPublic = $isPublic;
return $this;
}
} }