src/Twig/AppRuntime.php line 104

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use App\Application\Sonata\MediaBundle\Entity\Media;
  4. use App\Entity\Boutique\Panier;
  5. use App\Entity\Boutique\Produit;
  6. use App\Entity\Boutique\ProduitDeclinaison;
  7. use App\Entity\Boutique\ProduitReducCe;
  8. use App\Entity\Boutique\Promotion;
  9. use App\Manager\Boutique\ProduitManager;
  10. use App\Manager\Boutique\ProduitStockBalanceManager;
  11. use App\Manager\Categorie\CategorieManager;
  12. use App\Utils\Toolbox;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\HttpKernel\KernelInterface;
  15. use Symfony\Component\Intl\Intl;
  16. use App\Entity\Contenu\Page;
  17. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Liip\ImagineBundle\Service\FilterService;
  21. use Sonata\MediaBundle\Model\MediaInterface;
  22. use Sonata\MediaBundle\Provider\ImageProvider;
  23. class AppRuntime
  24. {
  25.     private $provider;
  26.     private $filterService;
  27.     private $router;
  28.     private $requestStack;
  29.     private $categorieManager;
  30.     private $locales;
  31.     private $defaultLocale;
  32.     private $kernel;
  33.     /**
  34.      * @var ProduitManager
  35.      */
  36.     private $produitManager;
  37.     /**
  38.      * @var ProduitStockBalanceManager
  39.      */
  40.     private $produitStockBalanceManager;
  41.     /**
  42.      * @var EntityManagerInterface
  43.      */
  44.     private $entityManager;
  45.     public function __construct(
  46.         ImageProvider $provider,
  47.         FilterService $filterService,
  48.         UrlGeneratorInterface $router,
  49.         RequestStack $requestStack,
  50.         CategorieManager $categorieManager,
  51.         ProduitManager $produitManager,
  52.         string $locales,
  53.         string $defaultLocale,
  54.         EntityManagerInterface $entityManager,
  55.         KernelInterface $kernel,
  56.         ProduitStockBalanceManager $produitStockBalanceManager
  57.     )
  58.     {
  59.         $this->provider             $provider;
  60.         $this->filterService        $filterService;
  61.         $this->router               $router;
  62.         $this->requestStack         $requestStack;
  63.         $this->categorieManager     $categorieManager;
  64.         $this->locales              $locales;
  65.         $this->defaultLocale        $defaultLocale;
  66.         $this->kernel               $kernel;
  67.         $this->produitManager       $produitManager;
  68.         $this->produitStockBalanceManager $produitStockBalanceManager;
  69.         $this->entityManager $entityManager;
  70.     }
  71.     public function ireplace(string $subject, array $replacement = []): ?string
  72.     {
  73.         return str_ireplace(array_keys($replacement), array_values($replacement), $subject);
  74.     }
  75.     public function fileExists(string $file)
  76.     {
  77.         if (false === preg_match('#^\/#'$file)) {
  78.             $file '/'.$file;
  79.         }
  80.         if (false === preg_match('#^\.#'$file)) {
  81.             $file '.'.$file;
  82.         }
  83.         return file_exists($this->kernel->getProjectDir().$file);
  84.     }
  85.     public function pregReplace(string $patternstring $replacementstring $subject): ?string
  86.     {
  87.         return preg_replace($pattern$replacement$subject);
  88.     }
  89.     public function stringToUrl(string $stringstring $separator '-'bool $isImage false): ?string
  90.     {
  91.         return Toolbox::stringToUrl($string$separator$isImage);
  92.     }
  93.     public function sonataMedia(?MediaInterface $media, ?string $filter null, array $data = []): string
  94.     {
  95.         if (count($data)) {
  96.             $media = new Media();
  97.             foreach ($data as $key => $val) {
  98.                 $method 'set'.ucfirst($key);
  99.                 if (method_exists($media$method)) {
  100.                     $media->$method($val);
  101.                 }
  102.             }
  103.         }
  104.         if (is_null($filter)) {
  105.             $path $this->provider->generatePublicUrl($media'reference');
  106.         } else {
  107.             $path '/media/'.$this->provider->generatePath($media).'/'.$media->getProviderReference();
  108.             try {
  109.                 $path $this->filterService->getUrlOfFilteredImage($path$filter);
  110.             } catch (\Exception $e) {
  111.             }
  112.         }
  113.         return $path;
  114.     }
  115.     public function getLocales(?string $locale null$excludeSpecifiedLocale false): array
  116.     {
  117.         $localeCodes explode('|'$this->locales);
  118.         $locales = [];
  119.         foreach ($localeCodes as $localeCode) {
  120.             $locales[$localeCode] = ['code' => $localeCode'name' => Intl::getLocaleBundle()->getLocaleName($localeCode$localeCode)];
  121.         }
  122.         if (!is_null($locale) && isset($locales[$locale]) && true === $excludeSpecifiedLocale) {
  123.             unset($locales[$locale]);
  124.         }
  125.         if (!is_null($locale) && isset($locales[$locale]) && false === $excludeSpecifiedLocale) {
  126.             return $locales[$locale];
  127.         }
  128.         return $locales;
  129.     }
  130.     public function generateUrl($object): string
  131.     {
  132.         if ($object instanceof Page) {
  133.             try {
  134.                 $parameters $this->router->match('/'.$object->getSlug());
  135.                 if (isset($parameters['_route'])) {
  136.                     return $this->router->generate($parameters['_route']);
  137.                 }
  138.             }
  139.             catch (ResourceNotFoundException $e) {
  140.                 return $this->router->generate('page.detail', [
  141.                     'slug' => $object->getSlug()
  142.                 ]);
  143.             }
  144.         }
  145.         return '';
  146.     }
  147.     public function prettify(?string $string null): ?string
  148.     {
  149.         $pattern '/\*(.*?)\*/';
  150.         $replacement '<span>$1</span>';
  151.         return preg_replace($pattern$replacement$string);
  152.     }
  153.     public function wysiswygFormatter(?string $string null): ?string
  154.     {
  155.         if (is_null($string)) {
  156.             $string '';
  157.         }
  158.         // Tableaux
  159.         $string str_replace('<table''<div class="table-responsive"><table class="table table-hover"'$string);
  160.         $string str_replace('</table>''</table></div>'$string);
  161.         $string preg_replace('/(<[^>]+) cellspacing=".*?"/i''$1'$string);
  162.         $string preg_replace('/(<[^>]+) cellpadding=".*?"/i''$1'$string);
  163.         $string preg_replace('/(<[^>]+) border=".*?"/i''$1'$string);
  164.         // Images
  165.         $string str_replace('<img''<img class="img-wysiwyg"'$string);
  166. //        $string = preg_replace('/(<[^>]+) style=".*?"/i', '$1', $string);
  167.         // Liens
  168.         $string str_replace('.pdf"''.pdf" class="link-pdf"'$string);
  169.         // Nettoyage styles word
  170.         $string preg_replace('/(<[^>]+) color=".*?"/i''$1'$string);
  171.         $string preg_replace('/(<[^>]+) size=".*?"/i''$1'$string);
  172.         $string preg_replace('/(<[^>]+) face=".*?"/i''$1'$string);
  173.         $string preg_replace('/(<[^>]+) width=".*?"/i''$1'$string);
  174.         $string preg_replace('/(<[^>]+) bgcolor=".*?"/i''$1'$string);
  175.         $string preg_replace('/(<[^>]+) cellspacing=".*?"/i''$1'$string);
  176.         $string preg_replace('/(<[^>]+) cellpadding=".*?"/i''$1'$string);
  177.         $string preg_replace('/(<[^>]+) border=".*?"/i''$1'$string);
  178.         return $string;
  179.     }
  180.     public function localeName($locale){
  181.         return Intl::getLocaleBundle()->getLocaleName($locale);
  182.     }
  183.     public function getCategorieChemin($categorie): string
  184.     {
  185.         $className = (new \ReflectionClass($categorie))->getShortName();
  186.         $requestStack $this->requestStack->getCurrentRequest();
  187.         $arborescence array_reverse($this->categorieManager->getRecursiveCategorieParent($categorie));
  188.         $categories = [];
  189.         foreach ($arborescence as $categorie) {
  190.             $url $this->router->generate('admin_app_boutique_'.strtolower($className).'_edit'array_merge(['id' => $categorie->getId()], $requestStack->query->all()));
  191.             $categories[] = '<a href="'.$url.'" class="sonata-link-identifier">'.$categorie->getIntitule().'</a>';
  192.         }
  193.         if (count($categories)) {
  194.             return implode('&nbsp;<span>&gt;</span>&nbsp;'$categories);
  195.         }
  196.         return '';
  197.     }
  198.     public function getCategorieIcone($categorie): string
  199.     {
  200.         $icone $this->categorieManager->getCategorieIcone($categorie);
  201.         return $icone;
  202.     }
  203.     public function getCategorieRootIntitule($categorie)
  204.     {
  205.         $arborescence $this->categorieManager->getRecursiveCategorieParent($categorie);
  206.         if (isset($arborescence[0])) {
  207.             return $arborescence[0]->getIntitule();
  208.         }
  209.         return null;
  210.     }
  211.     public function getMontantPromotionUtilisePanier(Panier $panierPromotion $promotion): string
  212.     {
  213.         $montant 0;
  214.         foreach ($panier->getLignes() as $ligne) {
  215.             if ($ligne->getPromotion() == $promotion) {
  216.                 $entite $ligne->getProduit();
  217.                 if (!is_null($ligne->getProduitDeclinaison())) {
  218.                     $entite $ligne->getProduitDeclinaison();
  219.                 }
  220.                 list($prixVenteTtcAvecRemise$appliquerPromotion) = $this->produitManager->getPrixVenteTtcAvecRemise($entite$promotiontrue);
  221.                 $montantLigne = (($ligne->getMontantTtc() - $prixVenteTtcAvecRemise) * $ligne->getQuantite());
  222.                 if (!is_null($ligne->getRemiseMontantMax()) && $montantLigne $ligne->getRemiseMontantMax()) {
  223.                     $montantLigne $ligne->getRemiseMontantMax();
  224.                 }
  225.                 $montant += $montantLigne;
  226.             }
  227.         }
  228.         if (!is_null($promotion->getPlafondPeriode()) && $montant $promotion->getPlafondPeriode()) {
  229.             $montant $promotion->getPlafondPeriode();
  230.         }
  231.         return $montant;
  232.     }
  233.     public function getMontantPromotionUtiliseApresPanier(Panier $panierPromotion $promotion): ?string
  234.     {
  235.         // Si pas de plafond configuré on affiche pas le montant restant
  236.         if (is_null($promotion->getPlafondPeriode()) && is_null($promotion->getRemisePlafond())) {
  237.             return null;
  238.         }
  239.         list($nombreUtilisationCommande$montantUtilisationCommande) = $this->produitManager->getUtilisateurHistoriquePromotionCommande($promotion);
  240.         $montant $montantUtilisationCommande;
  241.         $montant += $this->getMontantPromotionUtilisePanier($panier$promotion);
  242.         if (!is_null($promotion->getPlafondPeriode()) && $montant $promotion->getPlafondPeriode()) {
  243.             $montant $promotion->getPlafondPeriode();
  244.         }
  245.         $montantRestant $promotion->getPlafondPeriode() - $montant;
  246.         if (!is_null($promotion->getRemisePlafond())) {
  247.             $montantRestant $promotion->getRemisePlafond() + $montantRestant;
  248.         }
  249.         if ($montantRestant 0) {
  250.             return $montantRestant;
  251.         }
  252.         return 0;
  253.     }
  254.     public function produitDefaultImage($produit null$taille '800_533_thumbnail')
  255.     {
  256.         $catalogueId null;
  257.         if (is_object($produit) && !is_null($produit->getProduitReducCe())) {
  258.             $catalogueId $produit->getProduitReducCe()->getCataloguesId();
  259.         }
  260.         if (is_array($produit) && isset($produit['produitReducCe'])) {
  261.             $catalogueId $produit['produitReducCe']['cataloguesId'];
  262.         }
  263.         $miniature 'build/img/ticket-spectacle_'.$taille.'.jpg';
  264.         if (getenv('APP_SITE_SLUG') == 'horizonce' && $catalogueId == ProduitReducCe::CATALOGUE_REDUCTICKET) {
  265.             $miniature 'build/img/ticket-concert_'.$taille.'.jpg';
  266.         }
  267.         return $miniature;
  268.     }
  269.     public function xmlEscape($string)
  270.     {
  271.         return str_replace(array('&''<''>''\'''"'), array('&amp;''&lt;''&gt;''&apos;''&quot;'), $string);
  272.     }
  273.     public function unescape($string)
  274.     {
  275.         return html_entity_decode($stringENT_QUOTES'UTF-8');
  276.     }
  277.     /**
  278.      * @param  Produit|null             $produit
  279.      * @param  ProduitDeclinaison|null  $produitDeclinaison
  280.      *
  281.      * @return int
  282.      */
  283.     public function getStock(?Produit $produit null, ?ProduitDeclinaison $produitDeclinaison null): int
  284.     {
  285.         return $this->produitStockBalanceManager->getStock($produit$produitDeclinaison);
  286.     }
  287.     /**
  288.      * @param  Produit  $produit
  289.      *
  290.      * @return int
  291.      */
  292.     public function getStockTotalProduitDeclinaisons(Produit $produit): int
  293.     {
  294.         $stock 0;
  295.         foreach ($produit->getProduitDeclinaisonsVisibles() as $produitDeclinaison) {
  296.             $stock += $this->produitStockBalanceManager->getStock(null$produitDeclinaison);
  297.         }
  298.         return $stock;
  299.     }
  300.     /**
  301.      * @param  int    $id
  302.      * @param  mixed  $class
  303.      * @param  string  $field
  304.      *
  305.      * @return string
  306.      */
  307.     public function getObjectStringFromId(int $id$classstring $field): string
  308.     {
  309.         $object $this->entityManager->getRepository($class)->find($id);
  310.         $getter 'get'.ucfirst($field);
  311.         return trim($object->$getter());
  312.     }
  313. }