src/Controller/Core/BatchOperationController.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Core;
  3. use App\BatchOperations\BatchOperationRegistry;
  4. use FOS\RestBundle\Controller\Annotations as Rest;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. /**
  11.  * Class BatchOperationController.
  12.  */
  13. class BatchOperationController extends AbstractController
  14. {
  15.     /**
  16.      * @Route("/batchoperation/dump", name="sygefor_core.batch.dump")
  17.      * @Template()
  18.      */
  19.     public function dumpAction()
  20.     {
  21.         $operations $this->get('sygefor_core.batch_operation_registry')->getAll();
  22.         $operations_infos = array();
  23.         foreach ($operations as $operation) {
  24.             $operations_infos [] = array('label' => $operation->getLabel(), 'id' => $operation->getId(), 'ids' => 1);
  25.         }
  26.         return array('operations' => $operations_infos);
  27.     }
  28.     /**
  29.      * @Route("/batchoperation/{id}/execute", name="sygefor_core.batch_operation.execute", options={"expose"=true}, defaults={"_format" = "json"})
  30.      * @Rest\View
  31.      */
  32.     public function executeAction($idBatchOperationRegistry $batchRegRequest $request)
  33.     {
  34.         $ids $request->get('ids');
  35.         $options $request->get('options');
  36.         //we try to read option list as a JSON string (case of multipart form type)
  37.         if (is_string($options)) {
  38.             $decodeOptions json_decode($options$assoc true);
  39.             if (is_array($decodeOptions)) { //if translation succeeded, the result is stored as options array
  40.                 $options $decodeOptions;
  41.             }
  42.         }
  43.         if (count($request->files) > 0) {
  44.             $attachments = array();
  45.             //files are stored in option list using form name as key
  46.             foreach ($request->files as $key => $file) {
  47.                 $attachments[] = $file;
  48.             }
  49.             $options['attachment'] = $attachments;
  50.         }
  51.         //also need to decode id list
  52.         $decodeIds json_decode($ids$assoc true);
  53.         if (is_string($decodeIds)) {
  54.             $ids $decodeIds;
  55.         }
  56.         $ids explode(','$ids);
  57.         //$batchOperation = $this->get('sygefor_core.batch_operation_registry')->get($id);
  58.         $batchOperation $batchReg->getByName($id);
  59.         if (!$batchOperation) {
  60.             throw new NotFoundHttpException('Operation not found : ' $id);
  61.         }
  62.         $options is_array($options) ? $options : array();
  63.         $batchOperation->setOptions($options);
  64.         return $batchOperation->execute($ids$options);
  65.     }
  66.     /**
  67.      * @Route("/batchoperation/modalconfig/{service}", name="sygefor_core.batch_operation.modal_config", options={"expose"=true}, defaults={"_format" = "json"})
  68.      * @Rest\View
  69.      */
  70.     public function modalConfigAction($serviceBatchOperationRegistry $batchRegRequest $request)
  71.     {
  72.         $options $request->get('options');
  73.         //we try to read option list as a JSON string (case of multipart form type)
  74.         if (is_string($options)) {
  75.             $decodeOptions json_decode($options$assoc true);
  76.             if (is_array($decodeOptions)) { //if translation succeeded, the result is stored as options array
  77.                 $options $decodeOptions;
  78.             }
  79.         }
  80.         //$batchOperation = $this->get('sygefor_core.batch_operation_registry')->get($service);
  81.         $batchOperation $batchReg->getByName($service);
  82.         if (method_exists($batchOperation'getModalConfig')) {
  83.             return $batchOperation->getModalConfig($options);
  84.         }
  85.         return array();
  86.     }
  87.     /**
  88.      * sends file.
  89.      *
  90.      * @Route("/batchoperation/{service}/get/{file}/as/{filename}", name="sygefor_core.batch_operation.get_file", options={"expose"=true}, defaults={"_format" = "json", "filename"=null})
  91.      * @Rest\View
  92.      */
  93.     public function fileDownloadAction(Request $request$serviceBatchOperationRegistry $batchReg$file$filename null)
  94.     {
  95.         $pdf = ($request->get('pdf') === 'true') ? true false;
  96.         //$batchOperation = $this->get('sygefor_core.batch_operation_registry')->get($service);
  97.         $batchOperation $batchReg->getByName($service);
  98.         if (method_exists($batchOperation'sendFile')) {
  99.             return $batchOperation->sendFile($file$filename $filename 'publipostage.odt', array('pdf' => $pdf));
  100.         }
  101.         return array();
  102.     }
  103. }