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