Skip to content

Commit

Permalink
EntradaController optimizando
Browse files Browse the repository at this point in the history
  • Loading branch information
gerMdz committed Mar 27, 2022
1 parent bc055a8 commit 071d3e7
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 143 deletions.
24 changes: 12 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 43 additions & 55 deletions src/Controller/AdminEntradaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Service\UploaderHelper;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\QueryException;
use Doctrine\Persistence\ManagerRegistry;
use Exception;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
Expand All @@ -31,16 +32,22 @@ class AdminEntradaController extends AbstractController
{
private LoggerClient $loggerClient;
private BoleanToDateHelper $boleanToDateHelper;
private ManagerRegistry $managerRegistry;

/**
* NO usado es opcional.
* @param LoggerClient $loggerClient
* @param BoleanToDateHelper $boleanToDateHelper
* @param ManagerRegistry $managerRegistry
*/
public function __construct(LoggerClient $loggerClient, BoleanToDateHelper $boleanToDateHelper)
{
public function __construct(
LoggerClient $loggerClient,
BoleanToDateHelper $boleanToDateHelper,
ManagerRegistry $managerRegistry
) {
$this->loggerClient = $loggerClient;
$this->boleanToDateHelper = $boleanToDateHelper;
$this->managerRegistry = $managerRegistry;
}

/**
Expand All @@ -51,8 +58,11 @@ public function __construct(LoggerClient $loggerClient, BoleanToDateHelper $bole
* @param Request $request
* @return Response
*/
public function index(EntradaRepository $entradaRepository, PaginatorInterface $paginator, Request $request): Response
{
public function index(
EntradaRepository $entradaRepository,
PaginatorInterface $paginator,
Request $request
): Response {
$bus = $request->get('busq');
if ($this->isGranted('ROLE_EDITOR')) {
$entrada = $entradaRepository->queryFindAllEntradas($bus);
Expand Down Expand Up @@ -81,7 +91,8 @@ public function index(EntradaRepository $entradaRepository, PaginatorInterface $
*/
public function listadoPublicado(EntradaRepository $entradaRepository): Response
{
$this->isGranted('ROLE_EDITOR') ? $entrada = $entradaRepository->findAllPublicadosOrderedByPublicacion() : $entrada = $entradaRepository->findAllPublicadosOrderedByPublicacion($this->getUser());
$this->isGranted('ROLE_EDITOR') ? $entrada = $entradaRepository->findAllPublicadosOrderedByPublicacion(
) : $entrada = $entradaRepository->findAllPublicadosOrderedByPublicacion($this->getUser());

return $this->render('admin_entrada/list.html.twig', [
'entradas' => $entrada,
Expand All @@ -93,7 +104,6 @@ public function listadoPublicado(EntradaRepository $entradaRepository): Response
* @param Entrada $entrada
* @param UploaderHelper $uploaderHelper
* @param ObtenerDatosHelper $datosHelper
* @param PrincipalRepository $principalRepository
* @return RedirectResponse
* @throws Exception
* @Route("/admin/entrada/{id}/edit", name="admin_entrada_edit")
Expand All @@ -103,10 +113,8 @@ public function edit(
Request $request,
Entrada $entrada,
UploaderHelper $uploaderHelper,
ObtenerDatosHelper $datosHelper,
PrincipalRepository $principalRepository
): Response
{
ObtenerDatosHelper $datosHelper
): Response {
$form = $this->createForm(EntradaType::class, $entrada);
$form->handleRequest($request);

Expand All @@ -116,7 +124,6 @@ public function edit(
$boolean = $form['publicar']->getData();
$link = $form['linkRoute']->getData();


$publicado = $this->boleanToDateHelper->setDatatimeForTrue($boolean);
$entrada->setPublicadoAt($publicado);

Expand All @@ -127,9 +134,9 @@ public function edit(
$entrada->setImageFilename($newFilename);
}

$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();

$this->loggerClient->logMessage('Se editó la entrada \"' . $entrada->getTitulo() . '\"', '');
$this->loggerClient->logMessage('Se editó la entrada \"'.$entrada->getTitulo().'\"', '');

return $this->redirectToRoute('admin_entrada_index');
}
Expand Down Expand Up @@ -158,10 +165,8 @@ public function editComplex(Request $request, Entrada $entrada): Response
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {

$this->getDoctrine()->getManager()->flush();

$this->loggerClient->logMessage('Se editó la entrada \"' . $entrada->getTitulo() . '\"', '');
$this->managerRegistry->getManager()->flush();
$this->loggerClient->logMessage('Se editó la entrada \"'.$entrada->getTitulo().'\"', '');

return $this->redirectToRoute('admin_entrada_index');
}
Expand Down Expand Up @@ -202,13 +207,12 @@ public function new(EntityManagerInterface $em, Request $request, UploaderHelper

$titulo = $form['titulo']->getData();

if(!$link and $titulo) {
$link = $this->limpiaLink(null,$titulo);
}else{
if (!$link and $titulo) {
$link = $this->limpiaLink($titulo);
} else {
$link = $link->getlinkRoute();
}


$entrada->setLinkRoute($link);

if ($uploadedFile) {
Expand All @@ -218,7 +222,6 @@ public function new(EntityManagerInterface $em, Request $request, UploaderHelper

$boolean = $form['publicar']->getData();


$publicado = $this->boleanToDateHelper->setDatatimeForTrue($boolean);
$entrada->setPublicadoAt($publicado);

Expand All @@ -235,6 +238,7 @@ public function new(EntityManagerInterface $em, Request $request, UploaderHelper
'entrada' => $entrada,
]);
}

/**
* @Route("/admin/new/step1", name="admin_entrada_new_step1", methods={"GET","POST"})
* @param Request $request
Expand All @@ -251,12 +255,11 @@ public function newStepOne(Request $request): Response
if ($form->isSubmitted() && $form->isValid()) {
$section = $form['section']->getData();
$entrada->addSection($section);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($entrada);
$entityManager->flush();
$this->managerRegistry->getManager()->persist($entrada);
$this->managerRegistry->getManager()->flush();

return $this->redirectToRoute('admin_entrada_new_step2', [
'id' => $entrada->getId()
'id' => $entrada->getId(),
]);
}

Expand All @@ -266,7 +269,6 @@ public function newStepOne(Request $request): Response
]);
}


/**
* @Route("/admin/new/step2/{id}", name="admin_entrada_new_step2", methods={"GET","POST"})
* @param Request $request
Expand All @@ -276,16 +278,15 @@ public function newStepOne(Request $request): Response
*/
public function newStepTwo(Request $request, Entrada $entrada): Response
{

$form = $this->createForm(StepTwoType::class, $entrada);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {

$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();

return $this->redirectToRoute('admin_entrada_new_step3', [
'id' => $entrada->getId()
'id' => $entrada->getId(),
]);
}

Expand All @@ -311,12 +312,11 @@ public function newStepThree(Request $request): Response
if ($form->isSubmitted() && $form->isValid()) {
$section = $form['section']->getData();
$entrada->addSection($section);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($entrada);
$entityManager->flush();
$this->managerRegistry->getManager()->persist($entrada);
$this->managerRegistry->getManager()->flush();

return $this->redirectToRoute('admin_entrada_index', [
'id' => $entrada->getId()
'id' => $entrada->getId(),
]);
}

Expand All @@ -326,7 +326,6 @@ public function newStepThree(Request $request): Response
]);
}


/**
* @Route("/admin/entrada/{linkRoute}", name="entrada_admin_link")
*
Expand All @@ -347,9 +346,6 @@ public function link(Entrada $entrada): Response
*/
public function show(Entrada $entrada): Response
{
if (!$entrada) {
throw $this->createNotFoundException(sprintf('No se encontró la entrada "%s"', $entrada));
}

return $this->render('entrada/show.html.twig', [
'entrada' => $entrada,
Expand All @@ -364,40 +360,32 @@ public function show(Entrada $entrada): Response
*/
public function delete(Request $request, Entrada $entrada): Response
{
if ($this->isCsrfTokenValid('delete' . $entrada->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($entrada);
$entityManager->flush();
if ($this->isCsrfTokenValid('delete'.$entrada->getId(), $request->request->get('_token'))) {
$this->managerRegistry->getManager()->remove($entrada);
$this->managerRegistry->getManager()->flush();
}

return $this->redirectToRoute('admin_entrada_index');
}

/**
* @param null|string $link
* @param string $titulo
* @return string
*/
private function limpiaLink(?string $link , string $titulo): string
private function limpiaLink(string $titulo): string
{
if ('' != $link) {
$link = strtolower(str_replace(' ', '-', trim($link)));
} else {
$link = strtolower(str_replace(' ', '-', trim($titulo)));
$link = strtolower(str_replace('<p>', '', trim($link)));
$link = strtolower(str_replace('</p>', '', trim($link)));
}
return $link;
$link = strtolower(str_replace(' ', '-', trim($titulo)));
$link = strtolower(str_replace('<p>', '', trim($link)));
return strtolower(str_replace('</p>', '', trim($link)));
}


/**
* @param Principal|null $principal
* @return string|null
*/
private function getLinkRoute(?Principal $principal): ?string
{
if(null === $principal){
if (null === $principal) {
return null;
}
return $principal->getLinkRoute();
Expand All @@ -410,7 +398,7 @@ private function getLinkRoute(?Principal $principal): ?string
*/
private function getPrincipal(?string $linkRoute, PrincipalRepository $principalRepository): ?Principal
{
if(null === $linkRoute){
if (null === $linkRoute) {
return null;
}

Expand Down
Loading

0 comments on commit 071d3e7

Please sign in to comment.