first commit symfony 6
This commit is contained in:
184
src/Controller/CronController.php
Normal file
184
src/Controller/CronController.php
Normal file
@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
||||
|
||||
use App\Form\CronType as Form;
|
||||
|
||||
class CronController extends AbstractController
|
||||
{
|
||||
private $data="cron";
|
||||
private $entity="App\Entity\Cron";
|
||||
private $twig="Cron/";
|
||||
private $route="app_admin_cron";
|
||||
|
||||
public function list(): Response
|
||||
{
|
||||
return $this->render($this->twig.'list.html.twig',[
|
||||
"useheader"=>true,
|
||||
"usemenu"=>false,
|
||||
"usesidebar"=>true,
|
||||
]);
|
||||
}
|
||||
|
||||
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 = array(
|
||||
'draw' => $draw,
|
||||
'recordsFiltered' => $totalf,
|
||||
'recordsTotal' => $total,
|
||||
'data' => array(),
|
||||
);
|
||||
|
||||
// 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', array('id'=>$data->getId()))."'><i class='fa fa-file fa-fw fa-2x'></i></a>";
|
||||
|
||||
$tmp=array();
|
||||
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,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,array(
|
||||
"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()
|
||||
]);
|
||||
}
|
||||
|
||||
public function log()
|
||||
{
|
||||
return $this->render($this->render.'logs.html.twig', [
|
||||
'useheader' => true,
|
||||
'usesidebar' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getlog(Request $request, $id)
|
||||
{
|
||||
|
||||
$path = $this->getParameter('kernel.project_dir');
|
||||
if($id=="dump")
|
||||
$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()&&$mode=="delete") {
|
||||
}
|
||||
|
||||
if ($form->get('submit')->isClicked() && $mode=="submit") {
|
||||
}
|
||||
|
||||
if ($form->get('submit')->isClicked() && !$form->isValid()) {
|
||||
$this->get('session')->getFlashBag()->clear();
|
||||
|
||||
$errors = $form->getErrors();
|
||||
foreach( $errors as $error ) {
|
||||
$request->getSession()->getFlashBag()->add("error", $error->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user