nineskeletor/src/Controller/CronController.php

188 lines
6.1 KiB
PHP
Executable File

<?php
namespace App\Controller;
use App\Form\CronType as Form;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
class CronController extends AbstractController
{
private $data = 'cron';
private $entity = "App\Entity\Cron";
private $twig = 'Cron/';
private $route = 'app_admin_cron';
public function list($access): Response
{
return $this->render($this->twig.'list.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => true,
'access' => $access,
]);
}
public function tablelist(Request $request, ManagerRegistry $em): Response
{
$query = $request->query->all();
$start = $query['start'];
$length = $query['length'];
$search = $query['search'];
$draw = $query['draw'];
$ordercolumn = $query['order'][0]['column'];
$orderdir = $query['order'][0]['dir'];
// Nombre total d'enregistrement
$total = $em->getManager()->createQueryBuilder()->select('COUNT(entity)')->from($this->entity, 'entity')->getQuery()->getSingleScalarResult();
// Nombre d'enregistrement filtré
if (!$search || '' == $search['value']) {
$totalf = $total;
} else {
$totalf = $em->getManager()->createQueryBuilder()
->select('COUNT(entity)')
->from($this->entity, 'entity')
->where('entity.command LIKE :value OR entity.description LIKE :value')
->setParameter('value', '%'.$search['value'].'%')
->getQuery()
->getSingleScalarResult();
}
// Construction du tableau de retour
$output = [
'draw' => $draw,
'recordsFiltered' => $totalf,
'recordsTotal' => $total,
'data' => [],
];
// Parcours des Enregistrement
$qb = $em->getManager()->createQueryBuilder();
$qb->select('entity')->from($this->entity, 'entity');
if ($search && '' != $search['value']) {
$qb->andWhere('entity.command LIKE :value OR entity.description LIKE :value')
->setParameter('value', '%'.$search['value'].'%');
}
if ($ordercolumn) {
switch ($ordercolumn) {
case 1:
$qb->orderBy('entity.nextexecdate', $orderdir);
break;
case 2:
$qb->orderBy('entity.command', $orderdir);
break;
}
}
$datas = $qb->setFirstResult($start)->setMaxResults($length)->getQuery()->getResult();
foreach ($datas as $data) {
// Action
$action = '';
$action .= "<a href='".$this->generateUrl($this->route.'_update', ['id' => $data->getId()])."'><i class='fa fa-file fa-fw fa-2x'></i></a>";
$tmp = [];
array_push($tmp, $action);
array_push($tmp, $data->getNextexecdate()->format('d/m/Y H:i'));
array_push($tmp, $data->getCommand());
array_push($tmp, $data->getDescription());
array_push($tmp, $data->getStatutLabel());
array_push($output['data'], $tmp);
}
// Retour
return new JsonResponse($output);
}
public function update($id, $access, Request $request, ManagerRegistry $em): Response
{
// Initialisation de l'enregistrement
$data = $em->getRepository($this->entity)->find($id);
if (!$data) {
throw $this->createNotFoundException('Unable to find entity.');
}
// Création du formulaire
$form = $this->createForm(Form::class, $data, [
'mode' => 'update',
]);
// Récupération des data du formulaire
$form->handleRequest($request);
// Sur validation
if ($form->get('submit')->isClicked() && $form->isValid()) {
$data = $form->getData();
$em->getManager()->flush();
// Retour à la liste
return $this->redirectToRoute($this->route);
}
// Affichage du formulaire
return $this->render($this->twig.'edit.html.twig', [
'useheader' => true,
'usemenu' => false,
'usesidebar' => true,
$this->data => $data,
'mode' => 'update',
'form' => $form->createView(),
'access' => $access,
]);
}
public function log()
{
return $this->render($this->twig.'logs.html.twig', [
'useheader' => true,
'usesidebar' => true,
]);
}
public function getlog(Request $request, $id)
{
$path = $this->getParameter('kernel.project_dir');
if ('dump' == $id) {
$file = $path.'/var/log/'.$this->getParameter('appAlias').'.sql';
} else {
$file = $path.'/var/log/'.$id.'.log';
}
$fs = new Filesystem();
if ($fs->exists($file)) {
$response = new BinaryFileResponse($file);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
return $response;
} else {
return $this->redirectToRoute($this->route.'_log');
}
}
protected function getErrorForm($id, $form, $request, $data, $mode)
{
if ($form->get('submit')->isClicked() && 'delete' == $mode) {
}
if ($form->get('submit')->isClicked() && 'submit' == $mode) {
}
if ($form->get('submit')->isClicked() && !$form->isValid()) {
$errors = $form->getErrors();
foreach ($errors as $error) {
$request->getSession()->getFlashBag()->add('error', $error->getMessage());
}
}
}
}