Commit eb82d520 authored by Stephane Bouvry's avatar Stephane Bouvry
Browse files

- Modification de la méthode d'envoi du document pour les avenants

parent a2808cf1
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -315,7 +315,7 @@ return array(
        // %s ID de l'activité
        // %s Date de l'avenant (YYYY-MM-DD)
        // %s uniqid
        'avenant_filename' => 'avenant_%s_%s_%s.pdf',
        'avenant_filename' => 'avenant_%s_%s_%s',

        'generated-documents' => [
            'activity' => []
+1 −1
Original line number Diff line number Diff line
@@ -293,7 +293,7 @@ return array(
                // AVENANTS
                [
                    'controller' => 'ActivityAvenants',
                    'action' => ['api'],
                    'action' => ['api', 'download'],
                    'roles' => ['user'],
                ],

+13 −1
Original line number Diff line number Diff line
@@ -766,7 +766,8 @@ member:




################################################################################
# AVENANTS des CONTRACTS
avenant:
  type: literal
  options:
@@ -774,6 +775,8 @@ avenant:
    defaults:
      controller: ActivityAvenants
  child_routes:

    # API [GET,PUT,POST,DELETE]
    api:
      type: segment
      options:
@@ -782,6 +785,15 @@ avenant:
          action: api
      may_terminate: false

    # Téléchargement de l'avenant
    download:
      type: segment
      options:
        route: '/download/:avenant_id'
        defaults:
          action: download
      may_terminate: false


################################################################################
# GESTION des ORGANISATIONS
+90 −4
Original line number Diff line number Diff line
@@ -3,12 +3,16 @@
namespace Oscar\Controller;

use Oscar\Entity\Activity;
use Oscar\Entity\ActivityAvenant;
use Oscar\Exception\OscarException;
use Oscar\Service\ActivityAvenantsService;
use Oscar\Service\ProjectGrantApiService;
use Oscar\Strategy\Upload\FileUploadStandard;
use Oscar\Traits\UseLoggerService;
use Oscar\Traits\UseLoggerServiceTrait;
use Oscar\Traits\UseOscarUserContextService;
use Oscar\Traits\UseOscarUserContextServiceTrait;
use Oscar\Utils\FileSystemUtils;

class ActivityAvenantsController extends AbstractOscarController implements UseLoggerService, UseOscarUserContextService
{
@@ -48,11 +52,10 @@ class ActivityAvenantsController extends AbstractOscarController implements UseL
    {
        $this->getLoggerService()->debug(__METHOD__);

        $idActivity = $this->params()->fromRoute('activity_id');
        try {
            $activity = $this->getEntityManager()->getRepository(Activity::class)->find($idActivity);
            $activity = $this->getActivityFromRoute();
        } catch (\Exception $e) {
            return $this->jsonError("Impossible de charger l'activité $idActivity");
            return $this->jsonError($e->getMessage());
        }

        switch ($this->getHttpXMethod()) {
@@ -72,8 +75,11 @@ class ActivityAvenantsController extends AbstractOscarController implements UseL

            case 'POST':
                // TODO Tester les droits d'accès
                $datas = $this->getJsonREST();
                $datas = $_POST;
                try {
                    $datas['file'] = $this->fileAvenantDrop($activity);
                    $datas['status'] = ActivityAvenant::STATUS_DRAFT;
                    $this->getLoggerService()->debug(print_r($datas, true));
                    $this->getActivityAvenantsService()->createAvenantFromArray($activity, $datas);
                    return $this->getResponseOk("Avenant ajouté");
                } catch (\Exception $e) {
@@ -94,4 +100,84 @@ class ActivityAvenantsController extends AbstractOscarController implements UseL
                return $this->getResponseBadRequest();
        }
    }

    public function downloadAction()
    {
        $this->getLoggerService()->debug(__METHOD__);


        $idAvenant = $this->params()->fromRoute('avenant_id');
        try {
            $avenant = $this->getEntityManager()->getRepository(ActivityAvenant::class)->find($idAvenant);
        } catch (\Exception $e) {
            throw new OscarException("Impossible de charger l'avenant $idAvenant");
        }

        // TODO check privileges
        $activity = $avenant->getActivity();

        try {
            $file_infos = $this->getActivityAvenantsService()->getFileInfos($avenant);
            $this->getLoggerService()->debug(
                "download file (avenant $idAvenant => "
                .$file_infos['path']
                ." --- "
                .$file_infos['typemime']
                .")");
            header('Content-Type: ' . $file_infos['typemime']);
            header('Content-Transfer-Encoding: Binary');
            header('Content-Disposition: attachment; filename="' . $file_infos['filename']);
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . $file_infos['filesize']);
            die($file_infos['content']);
        } catch (\Exception $e) {
            throw new OscarException("Impossible de télécharger l'avenant $idAvenant");
        }
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * @throws OscarException
     */
    protected function getActivityFromRoute( string $paramName = 'activity_id' ) : Activity
    {
        $idActivity = $this->params()->fromRoute($paramName);
        try {
            return $this->getEntityManager()->getRepository(Activity::class)->find($idActivity);
        } catch (\Exception $e) {
            throw new OscarException("Impossible de charger l'activité $idActivity");
        }
    }

    private function fileAvenantDrop( Activity $activity ) :string
    {
        if( !array_key_exists('file', $_FILES) ){
            $this->getLoggerService()->error("Aucun fichier d'avenant envoyé");
            throw new OscarException("Fichier manquant");
        }
        try {
            $uploader = new FileUploadStandard();
            $avenant_directory = $this->getOscarConfigurationService()->getDocumentDropLocation();
            $filename_pattern = $this->getOscarConfigurationService()->getConfiguration('avenant_filename');
            $filename = sprintf(
                $filename_pattern,
                $activity->getId(),
                (new \DateTime())->format('Y-m-d'),
                uniqid()
            );
            $mimes = ["application/pdf" => "pdf"];
            $uploader->setDestination($avenant_directory)
                ->setFilename($filename)
                ->setMimesAllowed($mimes);
            $uploader->updoad($_FILES['file']);
            $this->getLoggerService()->info("Upload ok");
            return $uploader->getUploadName();
        } catch (\Exception $e){
            $this->getLoggerService()->error($e->getMessage());
            throw new OscarException("Impossible de téléverser le fichier de l'avenant : " . $e->getMessage());
        }
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ use Doctrine\ORM\EntityManager;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Oscar\Controller\ActivityAvenantsController;
use Oscar\Service\ActivityAvenantsService;
use Oscar\Service\OscarConfigurationService;
use Oscar\Service\OscarUserContext;
use Oscar\Service\ProjectGrantApiService;
use Psr\Container\ContainerInterface;
@@ -19,6 +20,7 @@ class ActivityAvenantsControllerFactory implements FactoryInterface
        $c->setProjectGrantApiService($container->get(ProjectGrantApiService::class));
        $c->setEntityManager($container->get(EntityManager::class));
        $c->setOscarUserContextService($container->get(OscarUserContext::class));
        $c->setOscarConfigurationService($container->get(OscarConfigurationService::class));
        $c->setLoggerService($container->get('Logger'));
        return $c;
    }
Loading