src/Service/AppService.php line 106

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Category;
  4. use App\Entity\History;
  5. use App\Entity\JobOffer;
  6. use App\Entity\LandingSlider;
  7. use App\Entity\Page;
  8. use App\Entity\Post;
  9. use App\Entity\Product;
  10. use App\Entity\Project;
  11. use App\Entity\SubCategory;
  12. use App\Serializer\ProjectNormalizer;
  13. use Symfony\Bridge\Twig\Mime\BodyRenderer;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Psr\Log\LoggerInterface;
  18. use Symfony\Component\Mailer\Mailer;
  19. use Symfony\Component\Mailer\MailerInterface;
  20. use Symfony\Component\Mailer\Transport;
  21. use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
  22. use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
  23. use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
  24. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  25. use Symfony\Component\Security\Core\Security;
  26. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  27. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  28. use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
  29. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  30. use Symfony\Component\Serializer\Serializer;
  31. use \DrewM\MailChimp\MailChimp;
  32. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  33. use Symfony\Contracts\Translation\TranslatorInterface;
  34. use Twig\Environment;
  35. use Twig\Extension\AbstractExtension;
  36. use Twig\Loader\FilesystemLoader;
  37. use Twig\TwigFunction;
  38. class AppService extends AbstractExtension
  39. {
  40.     protected $entityManager;
  41.     protected $router;
  42.     protected $logger;
  43.     protected $security;
  44.     protected $requestStack;
  45.     protected $mailer;
  46.     protected $mailer_user;
  47.     protected $mailer_dsn;
  48.     protected $parameterBag;
  49.     protected $translator;
  50.     public function __construct(RequestStack $requestStackSecurity $securityEntityManagerInterface $entityManagerUrlGeneratorInterface $routerLoggerInterface $logger$mailer_userMailerInterface $mailer$mailer_dsnParameterBagInterface $parameterBagTranslatorInterface $translator)
  51.     {
  52.         $this->em $entityManager;
  53.         $this->logger $logger;
  54.         $this->router $router;
  55.         $this->security $security;
  56.         $this->requestStack $requestStack;
  57.         $this->mailer_user $mailer_user;
  58.         $this->mailer $mailer;
  59.         $this->mailer_dsn $mailer_dsn;
  60.         $this->parameterBag $parameterBag;
  61.         $this->translator $translator;
  62.     }
  63.     public function getFunctions(): array
  64.     {
  65.         return [
  66.             new TwigFunction('getFormattedDate', [$this'getFormattedDate']),
  67.             new TwigFunction('getPage', [$this'getPage']),
  68.             new TwigFunction('getLinksNav', [$this'getLinksNav']),
  69.             new TwigFunction('getPins', [$this'getPins']),
  70.             new TwigFunction('getPins2', [$this'getPins2']),
  71.             new TwigFunction('getHistories', [$this'getHistories']),
  72.             new TwigFunction('getProjectsMap', [$this'getProjectsMap']),
  73.             new TwigFunction('getLandingSliders', [$this'getLandingSliders']),
  74.             new TwigFunction('getSubCategories', [$this'getSubCategories']),
  75.             new TwigFunction('getCategories', [$this'getCategories']),
  76.             new TwigFunction('getSitemapLinks', [$this'getSitemapLinks']),
  77.             new TwigFunction('strEndsWith', [$this'strEndsWith']),
  78.             new TwigFunction('getPropTrans', [$this'getPropTrans']),
  79.             new TwigFunction('isCrudTextEditor', [$this'isCrudTextEditor']),
  80.             new TwigFunction('getProducts', [$this'getProducts']),
  81.             new TwigFunction('getProductUrl', [$this'getProductUrl']),
  82.             new TwigFunction('getMetaColor', [$this'getMetaColor']),
  83.             new TwigFunction('getGoogleAnalytic', [$this'getGoogleAnalytic']),
  84.             new TwigFunction('getGoogleSearchConsole', [$this'getGoogleSearchConsole']),
  85.             new TwigFunction('getCategory', [$this'getCategory']),
  86.         ];
  87.     }
  88.     public function getRoutesControllers()
  89.     {
  90.         $routes $this->getRoutes();
  91.         $controllers = [];
  92. //        $controllers["Aucun"] = null;
  93.         foreach ($routes as $key => $route) {
  94.             $controllers[$route->getPath()] = $route->getDefault('_controller');
  95.         }
  96.         return $controllers;
  97.     }
  98.     public function getPage($controller)
  99.     {
  100.         $repoPage $this->em->getRepository(Page::class);
  101.         $page $repoPage->findOneBy(["controller" => $controller]);
  102.         return $page;
  103.     }
  104.     public function getRoute($controller)
  105.     {
  106.         $routes $this->getRoutes();
  107.         foreach ($routes as $key => $route) {
  108.             if ($route->getDefault('_controller') == $controller) {
  109.                 return $route;
  110.             }
  111.         }
  112.         return null;
  113.     }
  114.     public function getRoutes()
  115.     {
  116.         $router $this->router;
  117.         $routes $router->getRouteCollection()->all();
  118.         foreach ($routes as $key => $route) {
  119.             if (strpos($route->getDefault('_controller'), "App\Controller") === false) {
  120.                 unset($routes[$key]);
  121.             }
  122.         }
  123.         return $routes;
  124.     }
  125.     public function getLandingSliders()
  126.     {
  127.         $repoLandingSlider $this->em->getRepository(LandingSlider::class);
  128.         return $repoLandingSlider->findBy([], ["position" => "asc"]);
  129.     }
  130.     public function getProjectsMap($format null$data = [])
  131.     {
  132.         $encoders = [new XmlEncoder(), new JsonEncoder()];
  133.         $normalizers = [new ProjectNormalizer(new ObjectNormalizer())];
  134. //        $normalizers = [new ObjectNormalizer()];
  135.         $serializer = new Serializer($normalizers$encoders);
  136.         $repoProject $this->em->getRepository(Project::class);
  137.         $projects $repoProject->search($data);
  138.         return $format == "json" $serializer->serialize($projects'json', [AbstractNormalizer::ATTRIBUTES => ['latitude''longitude''id''title']]) : $projects;
  139.     }
  140.     public function suscribeToList($email)
  141.     {
  142.         try {
  143.             $MailChimp = new MailChimp('api_key');
  144. //            dump($MailChimp->get('lists'));
  145. //            exit;
  146.             // prendre l'id de la liste et non le webId
  147.             $list_id "list_id";
  148.             $result $MailChimp->post("lists/$list_id/members", [
  149.                 'email_address' => $email,
  150.                 'status' => 'subscribed',
  151.             ]);
  152.         } catch (\Exception $e) {
  153.         }
  154.     }
  155.     public function getPropertyInfo()
  156.     {
  157.         $phpDocExtractor = new PhpDocExtractor();
  158.         $reflectionExtractor = new ReflectionExtractor();
  159.         $listExtractors = [$reflectionExtractor];
  160.         $typeExtractors = [$phpDocExtractor$reflectionExtractor];
  161.         return new PropertyInfoExtractor(
  162.             $listExtractors,
  163.             $typeExtractors
  164.         );
  165.     }
  166.     public function isCrudTextEditor($name)
  167.     {
  168.         $arr = [
  169.             "description",
  170.             "intro",
  171.             "content",
  172.         ];
  173.         foreach ($arr as $key => $haystack) {
  174.             if (str_contains($haystack$name)) {
  175.                 return true;
  176.             }
  177.         }
  178.         return false;
  179.     }
  180.     public function getCrudProps($namespace)
  181.     {
  182.         $propertyInfo $this->getPropertyInfo();
  183.         $properties = ($propertyInfo->getProperties($namespace) ?? []);
  184.         $props = [];
  185.         $noProp = ["currentLocale""defaultLocale""translatable""locale""empty""id"];
  186.         foreach ($properties as $key => $value) {
  187.             $propertyType $propertyInfo->getTypes($namespace$value)[0];
  188.             if (!in_array($value$noProp) and !str_ends_with($value"FileSize") and !str_ends_with($value"FileUpdatedAt")
  189.                 and !str_contains($value"FileName") and !$propertyType->isCollection()) {
  190.                 $type = ($propertyType->getClassName() ? "ManyToOne" $propertyType->getBuiltinType());
  191.                 $props[] = [
  192.                     "name" => $value,
  193.                     "type" => $type
  194.                 ];
  195.             }
  196.         }
  197.         return $props;
  198.     }
  199.     public function getMetaColor()
  200.     {
  201.         $repoPage $this->em->getRepository(Page::class);
  202.         $page $repoPage->search(["notNull" => ["a.metaColor"], "limit" => 1]);
  203.         return $page $page->getMetaColor() : null;
  204.     }
  205.     public function getGoogleSearchConsole()
  206.     {
  207.         $repoPage $this->em->getRepository(Page::class);
  208.         $page $repoPage->search(["notNull" => ["a.googleSearchConsole"], "limit" => 1]);
  209.         return $page $page->getGoogleSearchConsole() : null;
  210.     }
  211.     public function getGoogleAnalytic()
  212.     {
  213.         $repoPage $this->em->getRepository(Page::class);
  214.         $page $repoPage->search(["notNull" => ["a.googleAnalytic"], "limit" => 1]);
  215.         return $page $page->getGoogleAnalytic() : null;
  216.     }
  217.     public function getProductUrl(Product $product)
  218.     {
  219.         $category $product->getCategory() ?? ($product->getSubCategory() ? $product->getSubCategory()->getCategory() : null);
  220.         $subCategory $product->getSubCategory();
  221.         return $this->router->generate("front_product", ["catSlug" => ($category $category->getSlug() : "0"), "subCatSlug" => ($subCategory $subCategory->getSlug() : "0"), "slug" => $product->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
  222.     }
  223.     public function getCategoryUrl(Category $cat)
  224.     {
  225.         return $this->router->generate("front_products", ["catSlug" => ($cat $cat->getSlug() : "0")], UrlGeneratorInterface::ABSOLUTE_URL);
  226.     }
  227.     public function getSubCategoryUrl(SubCategory $subCat)
  228.     {
  229.         return $this->router->generate("front_products", ["subCatSlug" => ($subCat $subCat->getSlug() : "0"), "catSlug" => $subCat->getCategory()->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
  230.     }
  231.     public function getPropField($type)
  232.     {
  233.         return "Field";
  234.     }
  235.     public function getCategory($categoryId)
  236.     {
  237.         $repoCategory $this->em->getRepository(Category::class);
  238.         $category $repoCategory->find($categoryId);
  239.         return $category;
  240.     }
  241.     public function getSubCategories($categoryId)
  242.     {
  243.         $repoSubCategory $this->em->getRepository(SubCategory::class);
  244.         return $repoSubCategory->findBy(["category" => $categoryId], ["position" => 'asc']);
  245.     }
  246.     public function getProducts(SubCategory $subCategory)
  247.     {
  248.         $repoProduct $this->em->getRepository(Product::class);
  249.         $products $repoProduct->findBy(["subCategory" => $subCategory"hide" => false], ["position" => "asc"]);
  250.         return $products;
  251.     }
  252.     public function getCategories($isChaine null)
  253.     {
  254.         $repoCategory $this->em->getRepository(Category::class);
  255.         return $repoCategory->findBy((is_null($isChaine) ? [] : ["isChaine" => $isChaine]), ["position" => "asc"]);
  256.     }
  257.     public function getHistories()
  258.     {
  259.         $repoHistory $this->em->getRepository(History::class);
  260.         return $repoHistory->findBy([], ["year" => "desc"]);
  261.     }
  262.     public function getFormattedDate(\DateTime $dateTime$days true)
  263.     {
  264.         $translatedDays = [
  265.             => "Lundi",
  266.             => "Mardi",
  267.             => "Mercredi",
  268.             => "Jeudi",
  269.             => "Vendredi",
  270.             => "Samedi",
  271.             => "Dimanche",
  272.         ];
  273.         $translatedMonths = [
  274.             => "Janvier",
  275.             => "Février",
  276.             => "Mars",
  277.             => "Avril",
  278.             => "Mai",
  279.             => "Juin",
  280.             => "Juillet",
  281.             => "Août",
  282.             => "Septembre",
  283.             10 => "Octobre",
  284.             11 => "Novembre",
  285.             12 => "Décembre"
  286.         ];
  287.         return (($days ? ($translatedDays[$dateTime->format("N")] . " ") : "") . $dateTime->format("j") . " " $translatedMonths[$dateTime->format("n")] . " " $dateTime->format("Y"));
  288.     }
  289.     public function getPropTrans($name)
  290.     {
  291.         if ($name == "name") {
  292.             return "Nom";
  293.         } else if ($name == "firstname") {
  294.             return "Prénom";
  295.         } else if ($name == "lastname") {
  296.             return "Nom";
  297.         } else if ($name == "zipCode") {
  298.             return "Code postal";
  299.         } else if ($name == "address") {
  300.             return "Adresse";
  301.         } else if ($name == "city") {
  302.             return "Ville";
  303.         } else if ($name == "title") {
  304.             return "Titre";
  305.         } else if ($name == "category") {
  306.             return "Catégorie";
  307.         } else if ($name == "subCategory") {
  308.             return "Sous-catégorie";
  309.         } else if ($name == "subSubCategory") {
  310.             return "Sous-sous-catégorie";
  311.         } else if ($name == "color") {
  312.             return "Couleur";
  313.         } else if ($name == "content") {
  314.             return "Contenu";
  315.         } else if ($name == "metaTitle") {
  316.             return "Balise titre";
  317.         } else if ($name == "metaDescription") {
  318.             return "Balise description";
  319.         } else if ($name == "paragraphs") {
  320.             return "Paragraphe";
  321.         } else if ($name == "isDeleted") {
  322.             return "Supprimer";
  323.         } else if ($name == "link") {
  324.             return "Lien";
  325.         } else if ($name == "legend") {
  326.             return "Légende";
  327.         } else if ($name == "jobFunction") {
  328.             return "Fonction";
  329.         } else if ($name == "phoneNumber") {
  330.             return "Téléphone";
  331.         } else if ($name == "email") {
  332.             return "Adresse email";
  333.         } else if ($name == "subtitle" or $name == "Subtitle") {
  334.             return "Sous-titre";
  335.         } else if ($name == "createdAt") {
  336.             return "Date de création";
  337.         } else if ($name == "date") {
  338.             return "Date";
  339.         } else if ($name == "product") {
  340.             return "Produit";
  341.         }
  342.         return ucfirst($name);
  343.     }
  344.     public function getSitemapLinks()
  345.     {
  346.         $links = [
  347.             $this->router->generate('front_landing', [], UrlGeneratorInterface::ABSOLUTE_URL),
  348.             $this->router->generate('front_contact', [], UrlGeneratorInterface::ABSOLUTE_URL),
  349.             $this->router->generate('front_posts', [], UrlGeneratorInterface::ABSOLUTE_URL),
  350.             $this->router->generate('front_presentation', [], UrlGeneratorInterface::ABSOLUTE_URL),
  351.         ];
  352.         $repoCategories $this->em->getRepository(Category::class);
  353.         /** @var Category $cat */
  354.         foreach ($repoCategories->findAll() as $cat) {
  355.             if ($cat->getIsChaine()) {
  356.                 foreach ($cat->getSubCategories() as $subCat) {
  357.                     $links[] = $this->getSubCategoryUrl($subCat);
  358.                 }
  359.             } else {
  360.                 $links[] = $this->getCategoryUrl($cat);
  361.             }
  362.         }
  363.         $repoProduct $this->em->getRepository(Product::class);
  364.         foreach ($repoProduct->findBy(["hide" => false]) as $key => $product) {
  365.             $links[] = $this->getProductUrl($product);
  366.         }
  367.         $repoPost $this->em->getRepository(Post::class);
  368.         foreach ($repoPost->findAll() as $key => $post) {
  369.             $links[] = $this->router->generate('front_post', ["slug" => $post->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
  370.         }
  371.         return $links;
  372.     }
  373.     public function strEndsWith($haystack$needle)
  374.     {
  375.         return str_ends_with($haystack$needle);
  376.     }
  377.     public function getPins() {
  378.         return [
  379.                 [
  380.                 "top" => "41.2",
  381.                 "left" => "16.3",
  382.             ],
  383.                 [
  384.                 "top" => "52.9",
  385.                 "left" => "24.6",
  386.             ],
  387.                 [
  388.                 "top" => "74",
  389.                 "left" => "84.1",
  390.             ],
  391.                 [
  392.                 "top" => "70",
  393.                 "left" => "57.8",
  394.             ],
  395.                 [
  396.                 "top" => "39.1",
  397.                 "left" => "82.3",
  398.             ],
  399.                 [
  400.                 "top" => "37.9",
  401.                 "left" => "74.2",
  402.             ],
  403.                 [
  404.                 "top" => "28.1",
  405.                 "left" => "67.6",
  406.             ],
  407.                 [
  408.                 "top" => "34.2",
  409.                 "left" => "51.9",
  410.             ],
  411.                 [
  412.                 "top" => "36.7",
  413.                 "left" => "46.8",
  414.             ],
  415.                 [
  416.                 "top" => "42.8",
  417.                 "left" => "43.4",
  418.             ],
  419.                 [
  420.                 "top" => "49.2",
  421.                 "left" => "46.3",
  422.             ],
  423.                 [
  424.                 "top" => "55",
  425.                 "left" => "41.6",
  426.             ],
  427.                 [
  428.                 "top" => "58.1",
  429.                 "left" => "45.2",
  430.             ],
  431.                 [
  432.                 "top" => "64.2",
  433.                 "left" => "50.4",
  434.             ],
  435.                 [
  436.                 "top" => "62.4",
  437.                 "left" => "53.5",
  438.             ],
  439.         ];
  440.     }
  441.     public function getPins2() {
  442.         return [
  443.                 [
  444.                 "top" => "45.6",
  445.                 "left" => "16.6",
  446.             ],
  447.                 [
  448.                 "top" => "62.3",
  449.                 "left" => "27.7",
  450.             ],
  451.                 [
  452.                 "top" => "71.5",
  453.                 "left" => "30.9",
  454.             ],
  455.                 [
  456.                 "top" => "65.4",
  457.                 "left" => "53.5",
  458.             ],
  459.                 [
  460.                 "top" => "67.4",
  461.                 "left" => "51.7",
  462.             ],
  463.                 [
  464.                 "top" => "62.8",
  465.                 "left" => "45.7",
  466.             ],
  467.                 [
  468.                 "top" => "43.2",
  469.                 "left" => "47",
  470.             ],
  471.                 [
  472.                 "top" => "57.8",
  473.                 "left" => "42.4",
  474.             ],
  475.                 [
  476.                 "top" => "51.6",
  477.                 "left" => "47.2",
  478.             ],
  479.                 [
  480.                 "top" => "40.2",
  481.                 "left" => "50",
  482.             ],
  483.                 [
  484.                 "top" => "38.2",
  485.                 "left" => "53.4",
  486.             ],
  487.                 [
  488.                 "top" => "32.9",
  489.                 "left" => "65.2",
  490.             ],
  491.                 [
  492.                 "top" => "40.7",
  493.                 "left" => "77.5",
  494.             ],
  495.                 [
  496.                 "top" => "42.8",
  497.                 "left" => "83.1",
  498.             ],
  499.                 [
  500.                 "top" => "73.8",
  501.                 "left" => "58.8",
  502.             ],
  503.                 [
  504.                 "top" => "78.1",
  505.                 "left" => "84.2",
  506.             ],
  507.         ];
  508.     }
  509.     public function getLinksNav()
  510.     {
  511.         return [
  512. //            "Missions" => $this->router->generate('front_landing'),
  513. //            "Références" => [
  514. //                "Nos projets" => $this->router->generate('front_landing'),
  515. //                "Autres références" => $this->router->generate('front_landing'),
  516. //            ],
  517. //            "Enjeux" => $this->router->generate('front_landing'),
  518. //            "La tribu" => [
  519. //                "Présentation" => $this->router->generate('front_landing'),
  520. //                "Équipe" => $this->router->generate('front_members'),
  521. //            ],
  522. //           ($this->translator->trans("navbar.accessoires")) => $this->router->generate('front_presentation'),
  523.            ($this->translator->trans("navbar.presentation")) => $this->router->generate('front_presentation'),
  524.            ($this->translator->trans("navbar.posts")) => $this->router->generate('front_posts'),
  525.            ($this->translator->trans("navbar.contact")) => $this->router->generate('front_contact'),
  526.         ];
  527.     }
  528. }