PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Roddo/EstimateBundle/Listener/EstimateLogoListener.php

https://bitbucket.org/SergeAstapov/roddo
PHP | 118 lines | 52 code | 15 blank | 51 comment | 5 complexity | f7d7a904b123f64cbba305a7481bb0fa MD5 | raw file
  1. <?php
  2. namespace Roddo\EstimateBundle\Listener;
  3. use Doctrine\ORM\Event\LifecycleEventArgs;
  4. use Doctrine\ORM\Events;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. use Roddo\EstimateBundle\Entity\Estimate;
  7. /**
  8. * Listener service that move uploaded logo files from temp directory.
  9. */
  10. class EstimateLogoListener
  11. {
  12. private $request;
  13. private $container;
  14. /**
  15. * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
  16. */
  17. public function __construct(ContainerInterface $container)
  18. {
  19. $this->container = $container;
  20. }
  21. /**
  22. * @param \Doctrine\ORM\Event\LifecycleEventArgs $args
  23. */
  24. public function prePersist(LifecycleEventArgs $args) {
  25. $this->preUpload($args->getEntity());
  26. }
  27. /**
  28. * @param \Doctrine\ORM\Event\LifecycleEventArgs $args
  29. */
  30. public function preUpdate(LifecycleEventArgs $args) {
  31. $this->preUpload($args->getEntity());
  32. }
  33. private function preUpload($entity) {
  34. if (!($entity instanceof EstimateAttribute) || 'logo' != $entity->getAttribute()) {
  35. return;
  36. }
  37. $file = $entity->getValue();
  38. print __FILE__ .':'. __LINE__;
  39. var_dump($file);
  40. exit;
  41. if (null !== $logoAttribute) {
  42. $file = $logoAttribute->getValue();
  43. if (null !== $file) {
  44. $prefix = 'estm_' . $this->container->getParameter('kernel.environment');
  45. $entity->logo_filename = uniqid($prefix) . '.' . $file->guessExtension();
  46. }
  47. }
  48. }
  49. /**
  50. * @param Doctrine\ORM\Event\LifecycleEventArgs $args
  51. */
  52. public function postPersist(LifecycleEventArgs $args) {
  53. $this->preUpload($args->getEntity());
  54. }
  55. /**
  56. * @param Doctrine\ORM\Event\LifecycleEventArgs $args
  57. */
  58. public function postUpdate(LifecycleEventArgs $args) {
  59. $this->upload($args->getEntity());
  60. }
  61. /**
  62. *
  63. * @param Doctrine\ORM\Event\LifecycleEventArgs $args
  64. */
  65. public function upload($entity)
  66. {
  67. // $entity = $args->getEntity();
  68. // if ($entity instanceof Estimate) {
  69. // $file = $entity->getAttribute('logo');
  70. //
  71. // if (null !== $file) {
  72. //// $upload_dir = $this->container->getParameter('roddo_estimate.upload_estimate_logo_dir');
  73. //
  74. // // move takes the target directory and then the target filename to move to
  75. // $file->move($this->getUploadRootDir(), $file->guessExtension());
  76. // $entity->logo_filename = uniqid() . '.' . $file->guessExtension();
  77. // }
  78. // }
  79. }
  80. private function getUploadRootDir() {
  81. return implode('/', array(
  82. $this->request->getBasePath() . '/',
  83. $this->container->getParameter('roddo_estimate.upload_estimate_logo_dir')
  84. ));
  85. }
  86. // private function upload(Estimate $entity) {
  87. // if (null === $this->file) {
  88. // return;
  89. // }
  90. //
  91. // // we use the original file name here but you should
  92. // // sanitize it at least to avoid any security issues
  93. //
  94. // // move takes the target directory and then the target filename to move to
  95. // $this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName());
  96. //
  97. // // set the path property to the filename where you'ved saved the file
  98. // $this->path = $this->file->getClientOriginalName();
  99. //
  100. // // clean up the file property as you won't need it anymore
  101. // $this->file = null;
  102. // }
  103. }