You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.9KB

  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\TaxRateInterface;
  18. use Lc\ShopBundle\Context\UnitInterface;
  19. use Lc\ShopBundle\Context\UserInterface;
  20. use Lc\ShopBundle\Context\UserPointSaleInterface;
  21. use Liip\ImagineBundle\Imagine\Cache\CacheManager;
  22. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  23. use Symfony\Component\HttpClient\HttplugClient;
  24. use Symfony\Component\HttpFoundation\ParameterBag;
  25. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  26. use Symfony\Contracts\Translation\TranslatorInterface;
  27. class UtilsProcess
  28. {
  29. protected $em;
  30. protected $parameterBag;
  31. protected $merchantUtils;
  32. protected $productFamilyUtils;
  33. public function __construct(EntityManagerInterface $em, ParameterBagInterface $parameterBag, ProductFamilyUtilsInterface $productFamilyUtils)
  34. {
  35. $this->em = $em;
  36. $this->parameterBag = $parameterBag;
  37. $this->productFamilyUtils = $productFamilyUtils;
  38. }
  39. public function duplicateEntity($entity, $flush = true)
  40. {
  41. $newEntity = clone $entity;
  42. if ($newEntity instanceof ImageInterface) {
  43. $newEntity = $this->duplicateImage($newEntity);
  44. }
  45. if ($newEntity instanceof ProductFamilyInterface) {
  46. $newEntity = $this->productFamilyUtils->processBeforePersistProductFamily($newEntity, false, true);
  47. }
  48. if(method_exists($newEntity, 'getAddress') && is_object($newEntity->getAddress())){
  49. $address = $newEntity->getAddress();
  50. $newAddress = $this->duplicateEntity($address);
  51. $newEntity->setAddress($newAddress);
  52. $this->em->persist($newAddress);
  53. }
  54. $this->em->persist($newEntity);
  55. if($flush){
  56. $this->em->flush();
  57. }
  58. return $newEntity;
  59. }
  60. public function duplicateEntityToOtherHub($entity, $hub){
  61. $newEntity = $this->duplicateEntity($entity);
  62. if ($hub) {
  63. $newEntity->setMerchant($hub);
  64. }
  65. $this->em->persist($newEntity) ;
  66. $this->em->flush() ;
  67. return $newEntity;
  68. }
  69. public function duplicateImage($entity, $folder = false)
  70. {
  71. $basePath = $this->parameterBag->get('kernel.project_dir') . '/public/uploads/images/';
  72. if ($entity->getImage() && file_exists($basePath . $entity->getImage())) {
  73. $extension = (pathinfo($basePath . $entity->getImage(), PATHINFO_EXTENSION));
  74. if ($extension == "jpg" || $extension == "png" || $extension == "gif") {
  75. $newImage = md5(uniqid()) . '.' . $extension;
  76. if ($folder) $newImage = $folder . '/' . $newImage;
  77. copy($basePath . $entity->getImage(), $basePath . $newImage);
  78. $entity->setImage($newImage);
  79. }
  80. } else {
  81. $entity->setImage(null);
  82. }
  83. return $entity;
  84. }
  85. }