1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
class InterventionPhotoHandler
{
private $requestStack;
private $form;
private $photosDirectory;
private $currentUser;
private $fileUploader;
private $imageProcessing;
private $formFactory;
private $router;
/**
* Initialize the handler with the form and the request
*/
public function __construct
(
RequestStack $requestStack,
EntityManagerInterface $entityManager,
ContainerInterface $container,
TokenStorageInterface $tokenStorageInterface,
FileUploader $fileUploader,
ImageProcessing $imageProcessing,
FormFactoryInterface $formFactory,
RouterInterface $router
)
{
$this->requestStack = $requestStack;
$this->entityManager = $entityManager;
$this->photosDirectory = $container->getParameter('photos_directory');
$this->currentUser = $tokenStorageInterface->getToken()->getUser();
$this->fileUploader = $fileUploader;
$this->imageProcessing = $imageProcessing;
$this->formFactory = $formFactory;
$this->router = $router;
}
public function init(Intervention $intervention)
{
$interventionPhoto = new InterventionPhoto();
$interventionPhoto->setIntervention($intervention);
$form = $this->formFactory->create(InterventionPhotoType::class, $interventionPhoto, [
'action' => $this->router->generate('admin_' . $intervention->getType() . '_edit', array("intervention_id" => $intervention->getId())),
'method' => 'POST',
'photo_types' => $photo_types
]);
$this->form = $form;
return $this->form;
}
/**
* Process form
*
* @return boolean
*/
public function process()
{
$this->form->handleRequest($this->requestStack->getCurrentRequest());
if ($this->form->isSubmitted() && $this->form->isValid()) {
$this->onSuccess();
return true;
}
return false;
}
public function setForm(Form $form)
{
$this->form = $form;
}
/**
* Get form
*
* @return
*/
public function getForm()
{
return $this->form;
}
/**
* Persist on success
*/
protected function onSuccess()
{
$interventionPhoto = $this->form->getData();
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = $this->form->getData()->getFichier();
$targetDirectory = $this->photosDirectory;
$thumbTargetDirectory = $targetDirectory . '/thumbs';
$fileName = $this->fileUploader->upload($file, $targetDirectory);
if (!$fileName)
return false;
copy($targetDirectory . '/' . $fileName, $thumbTargetDirectory . '/' . $fileName);
$this->imageProcessing->resizeImage($targetDirectory . '/' . $fileName, 1920);
$this->imageProcessing->resizeImage($thumbTargetDirectory . '/' . $fileName, 250);
$interventionPhoto->setNom($fileName);
$interventionPhoto->setFichier($fileName);
$interventionPhoto->setUser($this->currentUser);
// Sauvegarde dans la base de données
$this->entityManager->persist($interventionPhoto);
$this->entityManager->flush();
}
} |
Partager