Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

UtilsProcess.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 = strtolower(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. }else{
  84. $entity->setImage(null);
  85. }
  86. } else {
  87. $entity->setImage(null);
  88. }
  89. return $entity;
  90. }
  91. }