112 lines
4.1KB

  1. <?php
  2. namespace Lc\ShopBundle\Services;
  3. use Cocur\Slugify\Slugify;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigManager;
  6. use Geocoder\Model\Coordinates;
  7. use Geocoder\Provider\Addok\Addok;
  8. use Geocoder\Query\GeocodeQuery;
  9. use Geocoder\Query\ReverseQuery;
  10. use Lc\ShopBundle\Context\ImageInterface;
  11. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  12. use Lc\ShopBundle\Context\PageInterface;
  13. use Lc\ShopBundle\Context\PointSaleInterface;
  14. use Lc\ShopBundle\Context\ProductFamilyInterface;
  15. use Lc\ShopBundle\Context\ProductFamilyUtilsInterface;
  16. use Lc\ShopBundle\Context\ReminderInterface;
  17. use Lc\ShopBundle\Context\SluggableInterface;
  18. use Lc\ShopBundle\Context\TaxRateInterface;
  19. use Lc\ShopBundle\Context\UnitInterface;
  20. use Lc\ShopBundle\Context\UserInterface;
  21. use Lc\ShopBundle\Context\UserPointSaleInterface;
  22. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  23. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  24. use Symfony\Component\HttpClient\HttplugClient;
  25. use Symfony\Component\HttpFoundation\ParameterBag;
  26. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  27. use Symfony\Contracts\Translation\TranslatorInterface;
  28. class UtilsProcess
  29. {
  30. protected $em;
  31. protected $parameterBag;
  32. protected $merchantUtils;
  33. protected $productFamilyUtils;
  34. public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, ProductFamilyUtilsInterface $productFamilyUtils)
  35. {
  36. $this->em = $em;
  37. $this->parameterBag = $parameterBag;
  38. $this->productFamilyUtils = $productFamilyUtils;
  39. }
  40. public function duplicateEntity($entity, $flush = true)
  41. {
  42. $newEntity = clone $entity;
  43. if ($newEntity instanceof ImageInterface) {
  44. $newEntity = $this->duplicateImage($newEntity);
  45. }
  46. if ($newEntity instanceof ProductFamilyInterface) {
  47. $newEntity = $this->productFamilyUtils->processBeforePersistProductFamily($newEntity, false, true);
  48. }
  49. if(method_exists($newEntity, 'getAddress') && is_object($newEntity->getAddress())){
  50. $address = $newEntity->getAddress();
  51. $newAddress = $this->duplicateEntity($address);
  52. $newEntity->setAddress($newAddress);
  53. $this->em->persist($newAddress);
  54. }
  55. if($newEntity instanceof SluggableInterface){
  56. $this->em->persist($newEntity);
  57. if($flush)$this->em->flush();
  58. $newEntity->setSlug(null);
  59. }
  60. $this->em->persist($newEntity);
  61. if($flush)$this->em->flush();
  62. return $newEntity;
  63. }
  64. public function duplicateEntityToOtherHub($entity, $hub){
  65. $newEntity = $this->duplicateEntity($entity);
  66. if ($hub) {
  67. $newEntity->setMerchant($hub);
  68. }
  69. $this->em->persist($newEntity) ;
  70. $this->em->flush() ;
  71. return $newEntity;
  72. }
  73. public function duplicateImage($entity, $folder = false)
  74. {
  75. $basePath = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/images/';
  76. if ($entity->getImage() && file_exists($basePath . $entity->getImage())) {
  77. $extension = (pathinfo($basePath . $entity->getImage(), PATHINFO_EXTENSION));
  78. if ($extension == "jpg" || $extension == "png" || $extension == "gif") {
  79. $newImage = md5(uniqid()) . '.' . $extension;
  80. if ($folder) $newImage = $folder . '/' . $newImage;
  81. copy($basePath . $entity->getImage(), $basePath . $newImage);
  82. $entity->setImage($newImage);
  83. }
  84. } else {
  85. $entity->setImage(null);
  86. }
  87. return $entity;
  88. }
  89. }