<?php
namespace App\Service;
use App\Entity\Category;
use App\Entity\History;
use App\Entity\JobOffer;
use App\Entity\LandingPage;
use App\Entity\LandingSlider;
use App\Entity\Page;
use App\Entity\PhaseEvolution;
use App\Entity\Post;
use App\Entity\Product;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Entity\ProductBrochure;
use App\Entity\ProductBrochurePdf;
use App\Entity\ProductHardwareProductAddon;
use App\Entity\ProductKeyFeature;
use App\Entity\ProductParagraph;
use App\Entity\ProductProduct;
use App\Entity\ProductProductBrochure;
use App\Entity\ProductProductMedia;
use App\Entity\ProductPublication;
use App\Entity\ProductSoftwareProductAddon;
use App\Entity\ProductSubCategory;
use App\Entity\Project;
use App\Entity\Publication;
use App\Entity\SubCategory;
use App\Serializer\ProjectNormalizer;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use \DrewM\MailChimp\MailChimp;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFunction;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
class AppService extends AbstractExtension
{
protected $entityManager;
protected $router;
protected $logger;
protected $security;
protected $requestStack;
protected $mailer;
protected $mailer_user;
protected $mailer_dsn;
protected $parameterBag;
private $translator;
public function __construct(RequestStack $requestStack, Security $security, TranslatorInterface $translator, EntityManagerInterface $entityManager, UrlGeneratorInterface $router, LoggerInterface $logger, $mailer_user, MailerInterface $mailer, $mailer_dsn, ParameterBagInterface $parameterBag, UploaderHelper $helper)
{
$this->em = $entityManager;
$this->logger = $logger;
$this->router = $router;
$this->translator = $translator;
$this->security = $security;
$this->requestStack = $requestStack;
$this->mailer_user = $mailer_user;
$this->mailer = $mailer;
$this->mailer_dsn = $mailer_dsn;
$this->parameterBag = $parameterBag;
$this->helper = $helper;
}
public function getFunctions(): array
{
return [
new TwigFunction('getFormattedDate', [$this, 'getFormattedDate']),
new TwigFunction('getPage', [$this, 'getPage']),
new TwigFunction('getLinksNav', [$this, 'getLinksNav']),
new TwigFunction('getLinksFooter', [$this, 'getLinksFooter']),
new TwigFunction('getHistories', [$this, 'getHistories']),
new TwigFunction('getProjectsMap', [$this, 'getProjectsMap']),
new TwigFunction('getLandingSliders', [$this, 'getLandingSliders']),
new TwigFunction('getSubCategories', [$this, 'getSubCategories']),
new TwigFunction('getCategories', [$this, 'getCategories']),
new TwigFunction('getSitemapLinks', [$this, 'getSitemapLinks']),
new TwigFunction('strEndsWith', [$this, 'strEndsWith']),
new TwigFunction('getApplicationsBloc', [$this, 'getApplicationsBloc']),
new TwigFunction('getPropTrans', [$this, 'getPropTrans']),
new TwigFunction('isCrudTextEditor', [$this, 'isCrudTextEditor']),
new TwigFunction('getProducts', [$this, 'getProducts']),
new TwigFunction('getProductUrl', [$this, 'getProductUrl']),
new TwigFunction('getProductPhaseEvolutions', [$this, 'getProductPhaseEvolutions']),
new TwigFunction('getProductParagraphs', [$this, 'getProductParagraphs']),
new TwigFunction('getProductKeyFeatures', [$this, 'getProductKeyFeatures']),
new TwigFunction('getProductSoftwareProductAddons', [$this, 'getProductSoftwareProductAddons']),
new TwigFunction('getProductHardwareProductAddons', [$this, 'getProductHardwareProductAddons']),
new TwigFunction('getProductBrochurePdf', [$this, 'getProductBrochurePdf']),
new TwigFunction('getProductProductBrochures', [$this, 'getProductProductBrochures']),
new TwigFunction('getProductPublications', [$this, 'getProductPublications']),
new TwigFunction('getProductProductMedias', [$this, 'getProductProductMedias']),
new TwigFunction('getProductProducts', [$this, 'getProductProducts']),
new TwigFunction('getSubCategoryParagraphs', [$this, 'getSubCategoryParagraphs']),
new TwigFunction('getSubCategoryProducts', [$this, 'getSubCategoryProducts']),
new TwigFunction('getPublicationPhaseEvolutions', [$this, 'getPublicationPhaseEvolutions']),
new TwigFunction('getPhaseEvolutionFile', [$this, 'getPhaseEvolutionFile']),
new TwigFunction('getPresentationBloc', [$this, 'getPresentationBloc']),
new TwigFunction('getLandingPageParagraphs', [$this, 'getLandingPageParagraphs']),
];
}
public function getPhaseEvolutionFile(PhaseEvolution $phaseEvolution, Product $product = null, Category $category = null)
{
if ($phaseEvolution->getCategoryIcon()) {
if ($product) {
$subCategory = $product->getProductSubCategories()->first() ? $product->getProductSubCategories()->first()->getSubCategory() : null;
$category = $subCategory ? $subCategory->getCategory() : null;
}
if ($category and $category->getCategoryIconFileName()) {
return "/" . $this->helper->asset($category, 'categoryIconFile');
}
} else {
return "/" . $this->helper->asset($phaseEvolution, 'phaseEvolutionFile');
}
return null;
}
public function getSubCategoryProducts(SubCategory $subCategory, $isHardware = null)
{
$products = [];
$repoProductSubCategory = $this->em->getRepository(ProductSubCategory::class);
$productSubCategories = $repoProductSubCategory->findBy(["subCategory" => $subCategory], ["position" => "asc"]);
foreach ($productSubCategories as $key => $productSubCategory) {
$product = $productSubCategory->getProduct();
if ($product and !$product->getHide() and (is_null($isHardware) or ($isHardware and $product->getIsHardware()) or (!$isHardware and !$product->getIsHardware()))) {
$products[] = $product;
}
}
return $products;
}
public function getRoutesControllers()
{
$routes = $this->getRoutes();
$controllers = [];
// $controllers["Aucun"] = null;
foreach ($routes as $key => $route) {
$controllers[$route->getPath()] = $route->getDefault('_controller');
}
return $controllers;
}
public function getPage($controller)
{
$repoPage = $this->em->getRepository(Page::class);
$page = $repoPage->findOneBy(["controller" => $controller]);
return $page;
}
public function getRoute($controller)
{
$routes = $this->getRoutes();
foreach ($routes as $key => $route) {
if ($route->getDefault('_controller') == $controller) {
return $route;
}
}
return null;
}
public function getRoutes()
{
$router = $this->router;
$routes = $router->getRouteCollection()->all();
foreach ($routes as $key => $route) {
if (strpos($route->getDefault('_controller'), "App\Controller") === false) {
unset($routes[$key]);
}
}
return $routes;
}
public function getLandingSliders()
{
$repoLandingSlider = $this->em->getRepository(LandingSlider::class);
return $repoLandingSlider->findBy([], ["position" => "asc"]);
}
public function getProjectsMap($format = null, $data = [])
{
$encoders = [new XmlEncoder(), new JsonEncoder()];
$normalizers = [new ProjectNormalizer(new ObjectNormalizer())];
// $normalizers = [new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$repoProject = $this->em->getRepository(Project::class);
$projects = $repoProject->search($data);
return $format == "json" ? $serializer->serialize($projects, 'json', [AbstractNormalizer::ATTRIBUTES => ['latitude', 'longitude', 'id', 'title']]) : $projects;
}
public function suscribeToList($email)
{
try {
$MailChimp = new MailChimp('api_key');
// dump($MailChimp->get('lists'));
// exit;
// prendre l'id de la liste et non le webId
$list_id = "list_id";
$result = $MailChimp->post("lists/$list_id/members", [
'email_address' => $email,
'status' => 'subscribed',
]);
} catch (\Exception $e) {
}
}
public function getPropertyInfo()
{
$phpDocExtractor = new PhpDocExtractor();
$reflectionExtractor = new ReflectionExtractor();
$listExtractors = [$reflectionExtractor];
$typeExtractors = [$phpDocExtractor, $reflectionExtractor];
return new PropertyInfoExtractor(
$listExtractors,
$typeExtractors
);
}
public function isCrudTextEditor($name)
{
$arr = [
"description",
"intro",
"content",
];
foreach ($arr as $key => $haystack) {
if (str_contains($haystack, $name)) {
return true;
}
}
return false;
}
public function getCrudProps($namespace)
{
$propertyInfo = $this->getPropertyInfo();
$properties = ($propertyInfo->getProperties($namespace) ?? []);
$props = [];
$noProp = ["currentLocale", "defaultLocale", "translatable", "locale", "empty", "id"];
foreach ($properties as $key => $value) {
$propertyType = $propertyInfo->getTypes($namespace, $value)[0];
if (!in_array($value, $noProp) and !str_ends_with($value, "FileSize") and !str_ends_with($value, "FileUpdatedAt")
and !str_contains($value, "FileName") and !$propertyType->isCollection()) {
$type = ($propertyType->getClassName() ? "ManyToOne" : $propertyType->getBuiltinType());
$props[] = [
"name" => $value,
"type" => $type
];
}
}
return $props;
}
public function getProductUrl(Product $product, SubCategory $subCategory = null)
{
$subCategory = $subCategory ?? ($product->getProductSubCategories()->first() ? $product->getProductSubCategories()->first()->getSubCategory() : null);
if (!$subCategory) {
return "#";
}
$category = $subCategory->getCategory();
return $this->router->generate("front_product", ["catSlug" => $category->getSlug(), "subCatSlug" => $subCategory->getSlug(), "slug" => $product->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
}
public function getPropField($type)
{
return "Field";
}
public function getSubCategories($categoryId, $exceptId = null)
{
$repoSubCategory = $this->em->getRepository(SubCategory::class);
$data = [
"category" => $categoryId,
"hide" => false,
"orderBys" => ["a.position" => "asc"]
];
if ($exceptId) {
$data["different"] = ["a.id" => $exceptId];
}
return $repoSubCategory->search($data);
}
public function getProductParagraphs(Product $product)
{
$repoProductParagraph = $this->em->getRepository(ProductParagraph::class);
$productParagraphs = $repoProductParagraph->findBy(["product" => $product], ["position" => "asc"]);
return $productParagraphs;
}
public function getLandingPageParagraphs(LandingPage $landingPage)
{
$repoProductParagraph = $this->em->getRepository(ProductParagraph::class);
$productParagraphs = $repoProductParagraph->findBy(["landingPage" => $landingPage], ["position" => "asc"]);
return $productParagraphs;
}
public function getSubCategoryParagraphs(SubCategory $subCategory)
{
$repoSubCategoryParagraph = $this->em->getRepository(ProductParagraph::class);
$subCategoryParagraphs = $repoSubCategoryParagraph->findBy(["subCategory" => $subCategory], ["position" => "asc"]);
return $subCategoryParagraphs;
}
public function getProductHardwareProductAddons(Product $product)
{
$repoProductHardwareProductAddon = $this->em->getRepository(ProductHardwareProductAddon::class);
$productHardwareProductAddons = $repoProductHardwareProductAddon->findBy(["product" => $product], ["position" => "asc"]);
return $productHardwareProductAddons;
}
public function getProductProductBrochures(Product $product)
{
$repoProductProductBrochure = $this->em->getRepository(ProductProductBrochure::class);
$productProductBrochures = $repoProductProductBrochure->findBy(["product" => $product], ["position" => "asc"]);
return $productProductBrochures;
}
public function getProductProductMedias(Product $product)
{
$repoProductProductMedia = $this->em->getRepository(ProductProductMedia::class);
$productProductMedias = $repoProductProductMedia->findBy(["product" => $product], ["position" => "asc"]);
return $productProductMedias;
}
public function getProductProducts(Product $product)
{
$repoProductProduct = $this->em->getRepository(ProductProduct::class);
$productProducts = $repoProductProduct->findBy(["product" => $product], ["position" => "asc"]);
$products = [];
foreach ($productProducts as $key => $productProduct) {
if ($productProduct->getRelatedProduct()) {
$products[] = $productProduct->getRelatedProduct();
}
}
return $products;
}
public function getProductPublications(Product $product)
{
$repoProductPublication = $this->em->getRepository(ProductPublication::class);
$productPublications = $repoProductPublication->findBy(["product" => $product], ["position" => "asc"]);
return $productPublications;
}
public function getProductBrochurePdf(ProductBrochure $productBrochure)
{
$locale = $this->requestStack->getCurrentRequest()->getLocale();
$repoProductBrochurePdf = $this->em->getRepository(ProductBrochurePdf::class);
$productBrochurePdf = $repoProductBrochurePdf->findOneBy(["locale" => $locale, "productBrochure" => $productBrochure]);
if (!$productBrochurePdf and $locale != "en") {
$productBrochurePdf = $repoProductBrochurePdf->findOneBy(["locale" => "en", "productBrochure" => $productBrochure]);
}
return $productBrochurePdf;
}
public function getProductSoftwareProductAddons(Product $product)
{
$repoProductSoftwareProductAddon = $this->em->getRepository(ProductSoftwareProductAddon::class);
$productSoftwareProductAddons = $repoProductSoftwareProductAddon->findBy(["product" => $product], ["position" => "asc"]);
return $productSoftwareProductAddons;
}
public function getProductKeyFeatures(Product $product)
{
$repoProductKeyFeature = $this->em->getRepository(ProductKeyFeature::class);
$productKeyFeatures = $repoProductKeyFeature->findBy(["product" => $product], ["position" => "asc"]);
return $productKeyFeatures;
}
public function getProductPhaseEvolutions(Product $product)
{
$repoPhaseEvolution = $this->em->getRepository(PhaseEvolution::class);
$phaseEvolutions = $repoPhaseEvolution->search(["product" => $product, "orderBy" => ["a.position", "asc"]]);
return $phaseEvolutions;
}
public function getProducts(SubCategory $subCategory)
{
$repoProductSubCategory = $this->em->getRepository(ProductSubCategory::class);
$productSubCategories = $repoProductSubCategory->findBy(["subCategory" => $subCategory], ["position" => "asc"]);
$products = [];
foreach ($productSubCategories as $key => $productSubCategory) {
if ($productSubCategory->getProduct() and !$productSubCategory->getProduct()->getHide()) {
$products[] = $productSubCategory->getProduct();
}
}
return $products;
}
public function getCategories()
{
$repoCategory = $this->em->getRepository(Category::class);
return $repoCategory->findBy([], ["position" => "asc"]);
}
public function getHistories()
{
$repoHistory = $this->em->getRepository(History::class);
return $repoHistory->findBy([], ["year" => "asc"]);
}
public function getFormattedDate(\DateTime $dateTime, $days = true)
{
$translatedDays = [
1 => "Lundi",
2 => "Mardi",
3 => "Mercredi",
4 => "Jeudi",
5 => "Vendredi",
6 => "Samedi",
7 => "Dimanche",
];
$translatedMonths = [
1 => "Janvier",
2 => "Février",
3 => "Mars",
4 => "Avril",
5 => "Mai",
6 => "Juin",
7 => "Juillet",
8 => "Août",
9 => "Septembre",
10 => "Octobre",
11 => "Novembre",
12 => "Décembre"
];
return (($days ? ($translatedDays[$dateTime->format("N")] . " ") : "") . $dateTime->format("j") . " " . $translatedMonths[$dateTime->format("n")] . " " . $dateTime->format("Y"));
}
public function getPropTrans($name)
{
if ($name == "name") {
return "Nom";
} else if ($name == "firstname") {
return "Prénom";
} else if ($name == "lastname") {
return "Nom";
} else if ($name == "zipCode") {
return "Code postal";
} else if ($name == "address") {
return "Adresse";
} else if ($name == "city") {
return "Ville";
} else if ($name == "title") {
return "Titre";
} else if ($name == "category") {
return "Catégorie";
} else if ($name == "subCategory") {
return "Sous-catégorie";
} else if ($name == "subSubCategory") {
return "Sous-sous-catégorie";
} else if ($name == "color") {
return "Couleur";
} else if ($name == "content") {
return "Contenu";
} else if ($name == "metaTitle") {
return "Balise titre";
} else if ($name == "metaDescription") {
return "Balise description";
} else if ($name == "paragraphs") {
return "Paragraphe";
} else if ($name == "isDeleted") {
return "Supprimer";
} else if ($name == "link") {
return "Lien";
} else if ($name == "legend") {
return "Légende";
} else if ($name == "jobFunction") {
return "Fonction";
} else if ($name == "phoneNumber") {
return "Téléphone";
} else if ($name == "email") {
return "Adresse email";
} else if ($name == "subtitle" or $name == "Subtitle") {
return "Sous-titre";
} else if ($name == "createdAt") {
return "Date de création";
} else if ($name == "date") {
return "Date";
} else if ($name == "product") {
return "Produit";
}
return ucfirst($name);
}
public function getSitemapLinks()
{
$links = [
$this->router->generate('front_landing', [], UrlGeneratorInterface::ABSOLUTE_URL),
$this->router->generate('front_contact', [], UrlGeneratorInterface::ABSOLUTE_URL),
$this->router->generate('front_posts', [], UrlGeneratorInterface::ABSOLUTE_URL),
$this->router->generate('front_presentation', [], UrlGeneratorInterface::ABSOLUTE_URL),
$this->router->generate('front_support', [], UrlGeneratorInterface::ABSOLUTE_URL),
];
$repoPost = $this->em->getRepository(Post::class);
foreach ($repoPost->findAll() as $key => $post) {
$links[] = $this->router->generate('front_post', ["slug" => $post->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
}
$repoLandingPage = $this->em->getRepository(LandingPage::class);
foreach ($repoLandingPage->findAll() as $key => $landingPage) {
$links[] = $this->router->generate('front_landing_page', ["slug" => $landingPage->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
}
$repoProduct = $this->em->getRepository(Product::class);
foreach ($repoProduct->findBy(["hide" => false]) as $key => $product) {
$links[] = $this->getProductUrl($product);
}
$repoSubCategory = $this->em->getRepository(SubCategory::class);
foreach ($repoSubCategory->findBy(["hide" => false]) as $key => $subCategory) {
$links[] = $this->router->generate('front_sub_category', ["slug" => $subCategory->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
}
$repoCategory = $this->em->getRepository(Category::class);
foreach ($repoCategory->findAll() as $key => $category) {
$links[] = $this->router->generate('front_products', ["catSlug" => $category->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
$links[] = $this->router->generate('front_publications', ["slug" => $category->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
}
return $links;
}
public function getPublicationPhaseEvolutions(Publication $publication)
{
$phaseEvolutions = new ArrayCollection();
foreach ($publication->getProductPublications() as $key => $productPublication) {
$product = $productPublication->getProduct();
foreach ($product->getPhaseEvolutions() as $key => $phaseEvolution) {
if (!$phaseEvolutions->contains($phaseEvolution)) {
$phaseEvolutions->add($phaseEvolution);
}
}
}
return $phaseEvolutions;
}
public function strEndsWith($haystack, $needle)
{
return str_ends_with($haystack, $needle);
}
public function getLinksNav()
{
// $products= $this->appService->getProductsCatalogue();
// foreach ($products as $key => $product){
// $skillLinks[$skill->getTitle()]=$this->router->generate('front_product',["slug"=>$product->getSlug()]);
// }
return [
($this->translator->trans("navbar.home")) => $this->router->generate('front_landing'),
($this->translator->trans("navbar.products")) => "#",
($this->translator->trans("navbar.about")) => $this->router->generate('front_presentation'),
$this->translator->trans("navbar.publications") => "#",
// ($this->translator->trans("navbar.distributeurs")) => $this->router->generate('ront_distributeurs'),
($this->translator->trans("navbar.posts")) => $this->router->generate('front_posts'),
// ($this->translator->trans("navbar.contact")) => $this->router->generate('front_contact'),
($this->translator->trans("navbar.support")) => $this->router->generate('front_support'),
// "Contact" => $this->router->generate('front_contact'),
];
}
public function getLinksFooter()
{
return [
$this->translator->trans("footer.about") => $this->router->generate('front_presentation'),
$this->translator->trans("footer.blog") => $this->router->generate('front_posts'),
// $this->translator->trans("footer.distributeur") => $this->router->generate('front_distributeurs'),
$this->translator->trans("footer.support") => $this->router->generate('front_support'),
$this->translator->trans("footer.contact") => $this->router->generate('front_contact'),
];
}
public function getApplicationsBloc()
{
return [
$this->translator->trans("application.sticky.about") => "#presentation2",
$this->translator->trans("application.sticky.hardware") => "#hardwareProductsApp",
$this->translator->trans("application.sticky.software") => "#softwareProductsApp",
$this->translator->trans("application.sticky.applications") => "#relatedApp",
];
}
public function getPresentationBloc()
{
return [
$this->translator->trans("presentation.sticky.histories") => "#histories",
$this->translator->trans("presentation.sticky.values") => "#values",
$this->translator->trans("presentation.sticky.team") => "#team",
$this->translator->trans("presentation.sticky.partners") => "#partners",
$this->translator->trans("presentation.sticky.distributors") => "#distributors",
$this->translator->trans("presentation.sticky.world") => "#world",
$this->translator->trans("presentation.sticky.iso") => "#iso",
$this->translator->trans("presentation.sticky.join") => "#joinUs",
];
}
}