Changer l'etat en cliquant [symfony]

Par Douiri Ali, il y a 10 ans


Bonsoir mes amis ,

j'aimerais bien faire un telechargement dynamique , je m'explique les utilisateur d'une application X partage des fichiers, ces fichiers sont téléchargés puis traité par des personnels pour cela j'ai crée Fichier_Decode qui contient un chmaps de type boolean vaut 1 ou 0 nommé "Etat" càd est ce que ce fichier il est en cours de traitement "quand le fichier est téléchargé par le personnel" ou il a été traité "upload du fichier par le personnel" . et voila je voudrais bien interprété ca en code j'ai essayé mais comme je suis debutant j'aurais besoin encore d'un coup de pouce .

voici ce que j'ai fait :

Mon controller :

<?php namespace CUDS\DecodageBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Doctrine\ORM\EntityRepository; use CUDS\DecodageBundle\Entity\Fichier_Decode; use CUDS\DecodageBundle\Form\Fichier_DecodeType; class DecodageController extends Controller { public function decodeAction(Request $request) { $all_info_fichiers_plats = $this -> getDoctrine() -> getEntityManager() -> getRepository('CUDSPlatformBundle:Fichier_Recu') -> findAll(); return $this->render('CUDSDecodageBundle:Decodage:all_fichiers_recus.html.twig', array('all_info_fichiers_plats' => $all_info_fichiers_plats )); } public function downloadAction($id=null) { $em = $this -> getDoctrine() -> getEntityManager(); $file = $em -> find('CUDSPlatformBundle:Fichier_Recu',$id) -> getPath(); $response = new Response(); // chemin relatif du fichier a télécharger $path = __DIR__.'/../../../../web/uploads/documents/'.$file; // récupéré l'extension du fichier a télécharger $format = pathinfo($path, PATHINFO_EXTENSION); $response -> setStatusCode(200); $response -> headers -> set('Content-Type', "application/$format"); $response -> headers -> set('Content-Disposition', sprintf('filename="%s"', $file, $format)); $response -> setContent(file_get_contents($path)); $response -> setCharset('UTF-8'); // prints the HTTP headers followed by the content $response -> send(); return $response; } public function uploadAction(Request $request){ $fichier_decode = new Fichier_Decode(); $form = $this->get('form.factory')->create(new Fichier_DecodeType(), $fichier_decode); if ($form->handleRequest($request)->isValid()) { $em = $this->getDoctrine()->getManager(); $fichier_decode->upload(); $em->persist($fichier_decode); $em->flush(); $request->getSession()->getFlashBag()->add('notice', 'Ficher décodé bien enregistrée.'); //return $this->redirect($this->generateUrl('oc_platform_view', array('id' => $advert->getId()))); } return $this->render('CUDSDecodageBundle:Decodage:upload_fichier_decode.html.twig', array( 'form' => $form->createView(), )); } public function etatAction(Request $request){ if (! $request->isXmlHttpRequest()) { throw new NotFoundHttpException(); } $fichier_decode = new Fichier_Decode(); // Get the province ID $id = $request->query->get('id'); // Return a list of cities, based on the selected province $em = $this->getDoctrine()->getManager(); $fichier_decode = $em -> getRepository() -> findById($id); $fichier_decode.setEtat(1); $em->persist($fichier_decode); $em->flush(); if ($request->isMethod('POST')) { $request->getSession()->getFlashBag()->add('notice', 'Téléchargement effectué.'); return $this->redirect($this->generateUrl('cuds_decodage_all_fichiers')); } return $this->render('CUDSDecodageBundle:Decodage:all_fichiers_recus.html.twig'); } }

Fichier_Decode.php

<?php namespace CUDS\DecodageBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * Fichier_Decode * * @ORM\Table() * @ORM\Entity(repositoryClass="CUDS\DecodageBundle\Entity\Fichier_DecodeRepository") */ class Fichier_Decode { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="path", type="string", length=255) */ private $path; /** * @var \DateTime * * @ORM\Column(name="date_validation", type="datetime") */ private $dateValidation; /** * @var \etat * * @ORM\Column(name="etat", type="boolean", nullable=true) */ private $etat; /** * @Assert\File(maxSize="6000000") */ private $file; public function __construct(){ $this -> dateValidation = new \Datetime(); } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set path * * @param string $path * @return Fichier_Decode */ public function setPath($path) { $this->path = $path; return $this; } /** * Get path * * @return string */ public function getPath() { return $this->path; } /** * Set dateValidation * * @param \DateTime $dateValidation * @return Fichier_Decode */ public function setDateValidation($dateValidation) { $this->dateValidation = $dateValidation; return $this; } /** * Get dateValidation * * @return \DateTime */ public function getDateValidation() { return $this->dateValidation; } public function getFile() { return $this->file; } public function setFile($file) { $this->file = $file; return $this; } public function getAbsolutePath() { return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path; } public function getWebPath() { return null === $this->path ? null : $this->getUploadDir().'/'.$this->path; } protected function getUploadRootDir() { // le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés return __DIR__.'/../../../../web/'.$this->getUploadDir(); } protected function getUploadDir() { // on se débarrasse de « __DIR__ » afin de ne pas avoir de problème lorsqu'on affiche // le document/image dans la vue. return 'uploads/fichiers_decodes'; } public function upload() { // la propriété « file » peut être vide si le champ n'est pas requis if (null === $this->file) { return; } // utilisez le nom de fichier original ici mais // vous devriez « l'assainir » pour au moins éviter // quelconques problèmes de sécurité // la méthode « move » prend comme arguments le répertoire cible et // le nom de fichier cible où le fichier doit être déplacé $this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName()); // définit la propriété « path » comme étant le nom de fichier où vous // avez stocké le fichier $this->path = $this->file->getClientOriginalName(); // « nettoie » la propriété « file » comme vous n'en aurez plus besoin $this->file = null; } /** * Set etat * * @param boolean $etat * @return Fichier_Decode */ public function setEtat($etat) { $this->etat = $etat; return $this; } /** * Get etat * * @return boolean */ public function getEtat() { return $this->etat; } }

all_fichiers_recus.html.twig

{% extends "CUDSPlatformBundle::layout.html.twig" %} {% block content %} <!-- link table references --> {% block stylesheettable %} <link rel="stylesheet" type="text/css" href="{{ asset('bundles/cudsplatform/bower_components/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.css') }}" /> <link rel="stylesheet" type="text/css" href="{{asset('bundles/cudsplatform/bower_components/datatables-responsive/css/dataTables.responsive.css') }}" /> {% endblock %} <!-- end link table references --> <div class="row"> <div class="col-lg-12"> <div class="panel panel-default"> <div class="panel-heading"> DataTables Advanced Tables </div> <!-- /.panel-heading --> <div class="panel-body"> <div class="dataTable_wrapper"> <table class="table table-striped table-bordered table-hover" id="dataTables-example"> <thead> <tr> <th class="text-center">Num Fichier</th> <th class="text-center">Nom Fichier</th> <th class="text-center">Universite</th> <th class="text-center">Date Recéption</th> <th class="text-center">Etat</th> <th class="text-center">Téléchargement</th> </tr> </thead> <tbody> {% for fichier_plat_info in all_info_fichiers_plats %} <tr class="odd gradeX"> <td class="text-center" id="id_">{{fichier_plat_info.getId()}}</td> <td class="text-center">{{fichier_plat_info.getPath()}}</td> <td class="text-center">{{fichier_plat_info.getUniversite()}}</td> <td class="text-center">{{ fichier_plat_info.getDateRecep()|date("d/m/Y") }}</td> <td class="text-center" id="etat">Rien</td> <td class="text-center"><a href="{{ path('cuds_decodage_download_fichier_plat', {'id': fichier_plat_info.getId()}) }}" class="download"> <i class="glyphicon glyphicon-download-alt" ></i> </a> </td> </tr> {% endfor %} </tbody> </table> </div> </div> <!-- /.panel-body --> </div> <!-- /.panel --> </div> <!-- script table references --> {% block javascripttable %} <script type="text/javascript" src="{{ asset('bundles/cudsplatform/bower_components/datatables/media/js/jquery.dataTables.min.js') }}"></script> <script type="text/javascript" src="{{ asset('bundles/cudsplatform/bower_components/datatables-plugins/integration/bootstrap/3/dataTables.bootstrap.min.js') }}"></script> <script> $(document).ready(function() { $('#dataTables-example').DataTable({ responsive: true }); }); </script> <!-- end script table references --> {% endblock %} <script type="text/javascript" src="{{ asset('bundles/cudsplatform/js/jquery-1.11.3.min.js') }}"></script> <script type="text/javascript" src="{{ asset('bundles/cudsplatform/js/jquery-migrate-1.2.1.min.js') }}"></script> <script type="text/javascript"> $(document).ready(function () { $('.download').click(function(){ $('#etat').html('<i class="fa fa-spinner"></i'); var str = $(this).attr("href"); var res = str.split("/"); $.ajax({ type: "POST", url: "{{ url('cuds_decodage_upload_change_etat') }}?id=" + res[6], }); return false; }); }); </script> {% endblock %}

routing.yml

cuds_decodage_homepage: path: /hello/{name} defaults: { _controller: CUDSDecodageBundle:Default:index } cuds_decodage_all_fichiers: path: /all-fichiers defaults: { _controller: CUDSDecodageBundle:Decodage:decode } cuds_decodage_download_fichier_plat: path: /download-fichier-plat/{id} defaults: { _controller: CUDSDecodageBundle:Decodage:download } requirements: id: \d+ cuds_decodage_upload_ficher_decode: path: /upload-fichier-decode defaults: { _controller: CUDSDecodageBundle:Decodage:upload } cuds_decodage_upload_change_etat: path: /change-etat defaults: { _controller: CUDSDecodageBundle:Decodage:etat }

Merci

1 réponse

Azorgh, il y a 10 ans

Hello,

Trop de code,
Rien compris à ton problème >_<