<?php
namespace App\Service;
use App\Entity\Application;
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\ProductTable;
use App\Entity\ProductTableColumn;
use App\Entity\ProductTableRow;
use App\Entity\Project;
use App\Entity\SubCategory;
use App\Entity\SubSubCategory;
use App\Entity\SubSubSubCategory;
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\PropertyAccess\PropertyAccess;
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('getHistories', [$this, 'getHistories']),
new TwigFunction('getProjectsMap', [$this, 'getProjectsMap']),
new TwigFunction('getLandingSliders', [$this, 'getLandingSliders']),
new TwigFunction('getSubCategories', [$this, 'getSubCategories']),
new TwigFunction('getSubSubCategories', [$this, 'getSubSubCategories']),
new TwigFunction('getSubSubSubCategories', [$this, 'getSubSubSubCategories']),
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('getApplications', [$this, 'getApplications']),
new TwigFunction('getCategoryMenu', [$this, 'getCategoryMenu']),
new TwigFunction('getClass', [$this, 'getClass']),
new TwigFunction('getSubValues', [$this, 'getSubValues']),
new TwigFunction('getCategoryImage', [$this, 'getCategoryImage']),
new TwigFunction('getValueLevel', [$this, 'getValueLevel']),
new TwigFunction('getValueColor', [$this, 'getValueColor']),
new TwigFunction('getProductValue', [$this, 'getProductValue']),
new TwigFunction('getProductTableColums', [$this, 'getProductTableColums']),
new TwigFunction('getProductTableRows', [$this, 'getProductTableRows']),
new TwigFunction('getSubCategory', [$this, 'getSubCategory']),
new TwigFunction('getProductMenu', [$this, 'getProductMenu']),
];
}
public function getProductTableRows(ProductTableColumn $productTableColumn)
{
$repoProductTableRow = $this->em->getRepository(ProductTableRow::class);
$productTableRows = $repoProductTableRow->findBy(["productTableColumn" => $productTableColumn], ["position" => "asc"]);
return $productTableRows;
}
public function getProductTableColums(ProductTable $productTable)
{
$repoProductTableColumn = $this->em->getRepository(ProductTableColumn::class);
$productTableColumns = $repoProductTableColumn->findBy(["productTable" => $productTable], ["position" => "asc"]);
return $productTableColumns;
}
public function getSubValues($value)
{
$valueClass = $this->getClass($value);
$repoSubValue = $this->em->getRepository(("App\Entity\Sub" . $valueClass));
return $repoSubValue->findBy([(lcfirst($valueClass)) => $value], ["position" => "asc"]);
}
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 getClass($object, $prefix = false)
{
return ($prefix ? "App\Entity\\" : "") . (new \ReflectionClass($object))->getShortName();
}
public function getCategoryMenu(Category $category = null, SubCategory $subCategory = null, SubSubCategory $subSubCategory = null, SubSubSubCategory $subSubSubCategory = null)
{
$menu = [];
if ($category) {
$menu = [$category->getTitle() => $this->getProductUrl(null, true, $category)];
}
if ($subCategory) {
$menu[$subCategory->getTitle()] = $this->getProductUrl(null, true, null, $subCategory);
}
if ($subSubCategory) {
$menu[$subSubCategory->getTitle()] = $this->getProductUrl(null, true, null, null, $subSubCategory);
}
if ($subSubSubCategory) {
$menu[$subSubSubCategory->getTitle()] = $this->getProductUrl(null, true, null, null, null, $subSubSubCategory);
}
return $menu;
}
public function getValueLevel($value)
{
$class = $this->getClass($value, 1);
if ($class == Category::class) {
return 1;
} elseif ($class == SubCategory::class) {
return 2;
} elseif ($class == SubSubCategory::class) {
return 3;
} else {
return 4;
}
}
public function getLevelInfo()
{
return [
1 => [
"underName" => "category"
],
2 => [
"underName" => "sub_category"
],
3 => [
"underName" => "sub_sub_category"
],
4 => [
"underName" => "sub_sub_sub_category"
],
];
}
public function getValueColor($value)
{
$levelInfo = $this->getLevelInfo();
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$level = $this->getValueLevel($value);
$color = null;
while (1) {
if ($level == 1) {
$color = $value->getColor();
}
$level--;
if (!$level) {
break;
} else {
$value = $propertyAccessor->getValue($value, $levelInfo[$level]["underName"]);
}
}
return $color;
}
public function getProductMenu(Product $product)
{
$subSubSubCategory = $product->getSubSubSubCategory();
$subSubCategory = $product->getSubSubCategory();
$subCategory = $product->getSubCategory();
$category = $product->getCategory();
if ($subSubSubCategory) {
$subSubCategory = $subSubSubCategory->getSubSubCategory();
$subCategory = $subSubCategory->getSubCategory();
$category = $subCategory->getCategory();
}
if ($subSubCategory) {
$subCategory = $subSubCategory->getSubCategory();
$category = $subCategory->getCategory();
}
if ($subCategory) {
$category = $subCategory->getCategory();
}
$menu = $this->getCategoryMenu($category, $subCategory, $subSubCategory, $subSubSubCategory);
$menu[$product->getTitle()] = "#";
return $menu;
}
public function getSubCategory(Product $product)
{
$subCategory = null;
if ($product->getSubCategory()) {
$subCategory = $product->getSubCategory();
}
if (!$subCategory) {
$subCategory = $product->getSubSubCategory() ? $product->getSubSubCategory()->getSubCategory() : null;
}
if (!$subCategory) {
$subCategory = $product->getSubSubSubCategory() and $product->getSubSubSubCategory()->getSubSubCategory() ? $product->getSubSubSubCategory()->getSubSubCategory()->getSubCategory() : null;
}
return $subCategory;
}
public function getProductValue(Product $product)
{
if ($product->getSubSubSubCategory()) {
return $product->getSubSubSubCategory();
}
if ($product->getSubSubCategory()) {
return $product->getSubSubCategory();
}
if ($product->getSubCategory()) {
return $product->getSubCategory();
}
if ($product->getCategory()) {
return $product->getCategory();
}
return null;
}
public function getCategoryImage($value, $isIcon = false, $isRecursive = false, $isBg = false)
{
$levelInfo = $this->getLevelInfo();
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$level = $this->getValueLevel($value);
while (1) {
$img = $propertyAccessor->getValue($value, $levelInfo[$level]["underName"] . ($isIcon ? "Icon" : ($isBg ? "Bg" : "")) . '_file_name');
$level--;
if (!$img and $level > 0 and $isRecursive) {
$value = $propertyAccessor->getValue($value, $levelInfo[$level]["underName"]);
} else {
break;
}
}
return $img ? ("/upload/" . lcfirst($this->getClass($value)) . ($isIcon ? "Icon" : ($isBg ? "Bg" : "")) . "/" . $img) : ($isIcon ? "/assets/img/default_cat_icon_img.svg" : ($isBg ? "/assets/img/default_cat_img_bg.png" : "/assets/img/sans-img-1-dmd-inox.jpg"));
}
public
function getProductUrl(Product $product = null, $isCatRoute = false, Category $category = null, SubCategory $subCategory = null, SubSubCategory $subSubCategory = null, SubSubSubCategory $subSubSubCategory = null, $value = null)
{
if ($value) {
if ($this->getClass($value, 1) == Category::class) {
$category = $value;
} elseif ($this->getClass($value, 1) == SubCategory::class) {
$subCategory = $value;
} elseif ($this->getClass($value, 1) == SubSubCategory::class) {
$subSubCategory = $value;
} elseif ($this->getClass($value, 1) == SubSubSubCategory::class) {
$subSubSubCategory = $value;
}
}
$catSlug = $subCatSlug = $subSubCatSlug = $subSubSubCatSlug = "0";
if ($product) {
$subSubSubCategory = $product->getSubSubSubCategory();
if (!$subSubSubCategory) {
$subSubCategory = $product->getSubSubCategory();
if (!$subSubCategory) {
$subCategory = $product->getSubCategory();
if (!$subCategory) {
$category = $product->getCategory();
}
}
}
}
if ($category) {
$catSlug = $category->getSlug();
} elseif ($subCategory) {
$catSlug = $subCategory->getCategory()->getSlug();
$subCatSlug = $subCategory->getSlug();
} elseif ($subSubCategory) {
$catSlug = $subSubCategory->getSubCategory()->getCategory()->getSlug();
$subCatSlug = $subSubCategory->getSubCategory()->getSlug();
$subSubCatSlug = $subSubCategory->getSlug();
} elseif ($subSubSubCategory) {
$catSlug = $subSubSubCategory->getSubSubCategory()->getSubCategory()->getCategory()->getSlug();
$subCatSlug = $subSubSubCategory->getSubSubCategory()->getSubCategory()->getSlug();
$subSubCatSlug = $subSubSubCategory->getSubSubCategory()->getSlug();
$subSubSubCatSlug = $subSubSubCategory->getSlug();
}
if ($product) {
return $this->router->generate("front_product", ["catSlug" => $catSlug, "subCatSlug" => $subCatSlug, "subSubCatSlug" => $subSubCatSlug, "subSubSubCatSlug" => $subSubSubCatSlug, "slug" => $product->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
} else {
$data = ["slug" => $catSlug, "subCatSlug" => $subCatSlug, "subSubCatSlug" => $subSubCatSlug];
if ($isCatRoute and !$subSubSubCategory) {
$route = "front_category";
} else {
$route = "front_products";
$data["subSubSubCatSlug"] = $subSubSubCatSlug;
}
return $this->router->generate($route, $data, UrlGeneratorInterface::ABSOLUTE_URL);
}
}
public
function getPropField($type)
{
return "Field";
}
public
function getSubCategories($categoryId, $limit = null)
{
$repoSubCategory = $this->em->getRepository(SubCategory::class);
return $repoSubCategory->findBy(["category" => $categoryId], ["position" => 'asc'], $limit);
}
public
function getSubSubCategories($subCategoryId)
{
$repoSubSubCategory = $this->em->getRepository(SubSubCategory::class);
return $repoSubSubCategory->findBy(["subCategory" => $subCategoryId], ["position" => 'asc']);
}
public
function getSubSubSubCategories($subSubCategoryId)
{
$repoSubSubSubCategory = $this->em->getRepository(SubSubSubCategory::class);
return $repoSubSubSubCategory->findBy(["subSubCategory" => $subSubCategoryId], ["position" => 'asc']);
}
public
function getProducts(SubCategory $subCategory)
{
$repoProduct = $this->em->getRepository(Product::class);
$products = $repoProduct->findBy(["subCategory" => $subCategory], ["position" => "asc"]);
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 getApplications(Application $application = null)
{
$repoApplication = $this->em->getRepository(Application::class);
if ($application) {
$applications = $repoApplication->search(["orderBys" => ["a.position" => "asc"], "different" => ["a.id" => $application->getId()]]);
} else {
$applications = $repoApplication->findBy([], ["position" => "asc"]);
}
return $applications;
}
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_fabrication', [], UrlGeneratorInterface::ABSOLUTE_URL),
$this->router->generate('front_presentation', [], UrlGeneratorInterface::ABSOLUTE_URL),
$this->router->generate('front_documents', [], 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);
}
$repoCategory = $this->em->getRepository(Category::class);
foreach ($repoCategory->findAll() as $key => $category) {
$links[] = $this->getProductUrl(null, true, $category);
}
$repoSubCategory = $this->em->getRepository(SubCategory::class);
foreach ($repoSubCategory->findAll() as $key => $subCategory) {
$links[] = $this->getProductUrl(null, true, null, $subCategory);
}
$repoSubSubCategory = $this->em->getRepository(SubSubCategory::class);
foreach ($repoSubSubCategory->findAll() as $key => $subSubCategory) {
$links[] = $this->getProductUrl(null, true, null, null, $subSubCategory);
}
$repoSubSubSubCategory = $this->em->getRepository(SubSubSubCategory::class);
foreach ($repoSubSubSubCategory->findAll() as $key => $subSubSubCategory) {
$links[] = $this->getProductUrl(null, true, null, null, null, $subSubSubCategory);
}
$repoApplication = $this->em->getRepository(Application::class);
foreach ($repoApplication->findAll() as $key => $application) {
$links[] = $this->router->generate('front_application', ["slug" => $application->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL);
}
$repoProduct = $this->em->getRepository(Product::class);
foreach ($repoProduct->findAll() as $key => $product) {
$links[] = $this->getProductUrl($product);
}
return $links;
}
public
function strEndsWith($haystack, $needle)
{
return str_ends_with($haystack, $needle);
}
public
function getLinksNav()
{
$applications = $this->getApplications();
foreach ($applications as $key => $application) {
$applicationLinks[$application->getTitle()] = $this->router->generate('front_application', ["slug" => $application->getSlug()]);
}
return [
// ($this->translator->trans("navbar.home")) => $this->router->generate('front_landing'),
// ($this->translator->trans("navbar.skills")) => $skillLinks,
// ($this->translator->trans("navbar.products")) => $categoryLinks,
($this->translator->trans("footer.products.fab")) => $this->router->generate('front_fabrication'),
($this->translator->trans("menu.applications")) => $applicationLinks,
($this->translator->trans("footer.menu.about")) => $this->router->generate('front_presentation'),
($this->translator->trans("footer.menu.posts")) => $this->router->generate('front_posts'),
($this->translator->trans("footer.menu.download")) => $this->router->generate('front_documents'),
($this->translator->trans("footer.menu.contact")) => $this->router->generate('front_contact'),
];
}
}