<?php
namespace App\Service;
use App\Entity\Category;
use App\Entity\History;
use App\Entity\JobOffer;
use App\Entity\LandingSlider;
use App\Entity\Page;
use App\Entity\Post;
use App\Entity\Product;
use App\Entity\Project;
use App\Entity\SubCategory;
use App\Serializer\ProjectNormalizer;
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 Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFunction;
class AppService extends AbstractExtension
{
protected $entityManager;
protected $router;
protected $logger;
protected $security;
protected $requestStack;
protected $mailer;
protected $mailer_user;
protected $mailer_dsn;
protected $parameterBag;
protected $translator;
public function __construct(RequestStack $requestStack, Security $security, EntityManagerInterface $entityManager, UrlGeneratorInterface $router, LoggerInterface $logger, $mailer_user, MailerInterface $mailer, $mailer_dsn, ParameterBagInterface $parameterBag, TranslatorInterface $translator)
{
$this->em = $entityManager;
$this->logger = $logger;
$this->router = $router;
$this->security = $security;
$this->requestStack = $requestStack;
$this->mailer_user = $mailer_user;
$this->mailer = $mailer;
$this->mailer_dsn = $mailer_dsn;
$this->parameterBag = $parameterBag;
$this->translator = $translator;
}
public function getFunctions(): array
{
return [
new TwigFunction('getFormattedDate', [$this, 'getFormattedDate']),
new TwigFunction('getPage', [$this, 'getPage']),
new TwigFunction('getLinksNav', [$this, 'getLinksNav']),
new TwigFunction('getPins', [$this, 'getPins']),
new TwigFunction('getPins2', [$this, 'getPins2']),
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('getPropTrans', [$this, 'getPropTrans']),
new TwigFunction('isCrudTextEditor', [$this, 'isCrudTextEditor']),
new TwigFunction('getProducts', [$this, 'getProducts']),
new TwigFunction('getProductUrl', [$this, 'getProductUrl']),
new TwigFunction('getMetaColor', [$this, 'getMetaColor']),
new TwigFunction('getGoogleAnalytic', [$this, 'getGoogleAnalytic']),
new TwigFunction('getGoogleSearchConsole', [$this, 'getGoogleSearchConsole']),
new TwigFunction('getCategory', [$this, 'getCategory']),
];
}
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 getMetaColor()
{
$repoPage = $this->em->getRepository(Page::class);
$page = $repoPage->search(["notNull" => ["a.metaColor"], "limit" => 1]);
return $page ? $page->getMetaColor() : null;
}
public function getGoogleSearchConsole()
{
$repoPage = $this->em->getRepository(Page::class);
$page = $repoPage->search(["notNull" => ["a.googleSearchConsole"], "limit" => 1]);
return $page ? $page->getGoogleSearchConsole() : null;
}
public function getGoogleAnalytic()
{
$repoPage = $this->em->getRepository(Page::class);
$page = $repoPage->search(["notNull" => ["a.googleAnalytic"], "limit" => 1]);
return $page ? $page->getGoogleAnalytic() : null;
}
public function getProductUrl(Product $product)
{
$category = $product->getCategory() ?? ($product->getSubCategory() ? $product->getSubCategory()->getCategory() : null);
$subCategory = $product->getSubCategory();
return $this->router->generate("front_product", ["catSlug" => ($category ? $category->getSlug() : "0"), "subCatSlug" => ($subCategory ? $subCategory->getSlug() : "0"), "slug" => $product->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
}
public function getCategoryUrl(Category $cat)
{
return $this->router->generate("front_products", ["catSlug" => ($cat ? $cat->getSlug() : "0")], UrlGeneratorInterface::ABSOLUTE_URL);
}
public function getSubCategoryUrl(SubCategory $subCat)
{
return $this->router->generate("front_products", ["subCatSlug" => ($subCat ? $subCat->getSlug() : "0"), "catSlug" => $subCat->getCategory()->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
}
public function getPropField($type)
{
return "Field";
}
public function getCategory($categoryId)
{
$repoCategory = $this->em->getRepository(Category::class);
$category = $repoCategory->find($categoryId);
return $category;
}
public function getSubCategories($categoryId)
{
$repoSubCategory = $this->em->getRepository(SubCategory::class);
return $repoSubCategory->findBy(["category" => $categoryId], ["position" => 'asc']);
}
public function getProducts(SubCategory $subCategory)
{
$repoProduct = $this->em->getRepository(Product::class);
$products = $repoProduct->findBy(["subCategory" => $subCategory, "hide" => false], ["position" => "asc"]);
return $products;
}
public function getCategories($isChaine = null)
{
$repoCategory = $this->em->getRepository(Category::class);
return $repoCategory->findBy((is_null($isChaine) ? [] : ["isChaine" => $isChaine]), ["position" => "asc"]);
}
public function getHistories()
{
$repoHistory = $this->em->getRepository(History::class);
return $repoHistory->findBy([], ["year" => "desc"]);
}
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),
];
$repoCategories = $this->em->getRepository(Category::class);
/** @var Category $cat */
foreach ($repoCategories->findAll() as $cat) {
if ($cat->getIsChaine()) {
foreach ($cat->getSubCategories() as $subCat) {
$links[] = $this->getSubCategoryUrl($subCat);
}
} else {
$links[] = $this->getCategoryUrl($cat);
}
}
$repoProduct = $this->em->getRepository(Product::class);
foreach ($repoProduct->findBy(["hide" => false]) as $key => $product) {
$links[] = $this->getProductUrl($product);
}
$repoPost = $this->em->getRepository(Post::class);
foreach ($repoPost->findAll() as $key => $post) {
$links[] = $this->router->generate('front_post', ["slug" => $post->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
}
return $links;
}
public function strEndsWith($haystack, $needle)
{
return str_ends_with($haystack, $needle);
}
public function getPins() {
return [
[
"top" => "41.2",
"left" => "16.3",
],
[
"top" => "52.9",
"left" => "24.6",
],
[
"top" => "74",
"left" => "84.1",
],
[
"top" => "70",
"left" => "57.8",
],
[
"top" => "39.1",
"left" => "82.3",
],
[
"top" => "37.9",
"left" => "74.2",
],
[
"top" => "28.1",
"left" => "67.6",
],
[
"top" => "34.2",
"left" => "51.9",
],
[
"top" => "36.7",
"left" => "46.8",
],
[
"top" => "42.8",
"left" => "43.4",
],
[
"top" => "49.2",
"left" => "46.3",
],
[
"top" => "55",
"left" => "41.6",
],
[
"top" => "58.1",
"left" => "45.2",
],
[
"top" => "64.2",
"left" => "50.4",
],
[
"top" => "62.4",
"left" => "53.5",
],
];
}
public function getPins2() {
return [
[
"top" => "45.6",
"left" => "16.6",
],
[
"top" => "62.3",
"left" => "27.7",
],
[
"top" => "71.5",
"left" => "30.9",
],
[
"top" => "65.4",
"left" => "53.5",
],
[
"top" => "67.4",
"left" => "51.7",
],
[
"top" => "62.8",
"left" => "45.7",
],
[
"top" => "43.2",
"left" => "47",
],
[
"top" => "57.8",
"left" => "42.4",
],
[
"top" => "51.6",
"left" => "47.2",
],
[
"top" => "40.2",
"left" => "50",
],
[
"top" => "38.2",
"left" => "53.4",
],
[
"top" => "32.9",
"left" => "65.2",
],
[
"top" => "40.7",
"left" => "77.5",
],
[
"top" => "42.8",
"left" => "83.1",
],
[
"top" => "73.8",
"left" => "58.8",
],
[
"top" => "78.1",
"left" => "84.2",
],
];
}
public function getLinksNav()
{
return [
// "Missions" => $this->router->generate('front_landing'),
// "Références" => [
// "Nos projets" => $this->router->generate('front_landing'),
// "Autres références" => $this->router->generate('front_landing'),
// ],
// "Enjeux" => $this->router->generate('front_landing'),
// "La tribu" => [
// "Présentation" => $this->router->generate('front_landing'),
// "Équipe" => $this->router->generate('front_members'),
// ],
// ($this->translator->trans("navbar.accessoires")) => $this->router->generate('front_presentation'),
($this->translator->trans("navbar.presentation")) => $this->router->generate('front_presentation'),
($this->translator->trans("navbar.posts")) => $this->router->generate('front_posts'),
($this->translator->trans("navbar.contact")) => $this->router->generate('front_contact'),
];
}
}