src/Controller/VCards/VcardsFrontController.php line 210

Open in your IDE?
  1. <?php
  2. namespace App\Controller\VCards;
  3. use App\Entity\AccountingFirm;
  4. use App\Entity\Qrcode;
  5. use App\Entity\VCards;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use JeroenDesloovere\VCard\VCard;
  8. use phpDocumentor\Reflection\Types\Void_;
  9. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. class VcardsFrontController extends AbstractController
  17. {
  18.     #[Route(path'/'name'vcard_home'methods: ['GET'])]
  19.     public function index(): Response
  20.     {
  21.         dd('#Error');
  22.     }
  23.     #[Route(path'/{cabinet}/{id}'name'vcard_view_front'methods: ['GET''POST'])]
  24.     public function view(Request $requestAccountingFirm $cabinetVCards $vcard): Response
  25.     {
  26.         if ($vcard->getAccountingFirm() != $cabinet) {
  27.             throw new NotFoundHttpException();
  28.         }
  29.         return $this->render('vcards/view.html.twig', [
  30.             'id' => $vcard->getId(),
  31.             'accounting_firm' => $vcard->getAccountingFirm(),
  32.             'vcard' => $vcard
  33.         ]);
  34.     }
  35.     #[Route(path'/media/{cabinet}/{filename}.{_format}'name'vcard_media')]
  36.     public function media(Request $requeststring $cabinetstring $filename): BinaryFileResponse
  37.     {
  38.         $filePath sprintf('%s/%s/%s/%s',
  39.             $this->getParameter('upload_directory'),
  40.             "vcards",
  41.             $cabinet,
  42.             implode('.', [$filename$request->getRequestFormat()]));
  43.         if (!file_exists($filePath)) {
  44.             return new Response('Media introuvable.'404);
  45.             //throw $this->createNotFoundException();
  46.         }
  47.         return $response = new BinaryFileResponse($filePath);
  48.     }
  49.     #[Route(path'/{cabinet}/{id}/download'name'vcard_add_front'methods: ['GET''POST'])]
  50.     public function add(VCards $vcards): Response
  51.     {
  52.         // Init new vCard
  53.         $vcard = new VCard();
  54.         // Name
  55.         $lastname $vcards->getLastName();
  56.         $firstname $vcards->getFirstName();
  57.         $vcard->addName($lastname$firstname);
  58.         // Work
  59.         if ($vcards->getCabinet()) {
  60.             $vcard->addCompany($vcards->getCabinet());
  61.         }
  62.         if ($vcards->getFonction()) {
  63.             $vcard->addJobtitle($vcards->getFonction());
  64.         }
  65.         if ($vcards->getFonction()) {
  66.             $vcard->addRole($vcards->getFonction());
  67.         }
  68.         // Contact
  69.         if ($vcards->getEmail()) {
  70.             $vcard->addEmail($vcards->getEmail());
  71.         }
  72.         // Contact (legacy)
  73.         if ($vcards->getPhone()) {
  74.             $vcard->addPhoneNumber($vcards->getPhone(), 'PREF;CELL');
  75.         }
  76.         if ($vcards->getPhoneFix()) {
  77.             $vcard->addPhoneNumber($vcards->getPhoneFix(), 'WORK');
  78.         }
  79.         // Contact (phones collection)
  80.         $existingPhones array_filter([
  81.             $vcards->getPhone(),
  82.             $vcards->getPhoneFix(),
  83.         ]);
  84.         foreach ($vcards->getPhones() ?? [] as $p) {
  85.             $number $p->getNumber();
  86.             if (!$number) {
  87.                 continue;
  88.             }
  89.             // évite doublons
  90.             if (in_array($number$existingPhonestrue)) {
  91.                 continue;
  92.             }
  93.             $existingPhones[] = $number;
  94.             // Map label -> type vCard (simple et efficace)
  95.             $label strtolower((string) ($p->getLabel() ?? ''));
  96.             $type 'WORK';
  97.             if (str_contains($label'mobile') || str_contains($label'portable') || str_contains($label'cell')) {
  98.                 $type 'CELL';
  99.             } elseif (str_contains($label'fixe') || str_contains($label'bureau') || str_contains($label'work')) {
  100.                 $type 'WORK';
  101.             } elseif (str_contains($label'home') || str_contains($label'maison')) {
  102.                 $type 'HOME';
  103.             }
  104.             $vcard->addPhoneNumber($number$type);
  105.         }
  106.         // Address (principale)
  107.         $addr $vcards->getAddress() ?: '';
  108.         $zip $vcards->getZipCode() ?: '';
  109.         $city $vcards->getCity() ?: '';
  110.         if ($addr || $zip || $city) {
  111.             $vcard->addAddress(nullnull$addrnullnull$zip$city'WORK');
  112.             $vcard->addLabel(trim($addr ' ' $zip ' ' $city));
  113.         }
  114.         // Address (secondaires)
  115.         $seenAddresses = [];
  116.         $mainKey strtolower(trim($addr.'|'.$zip.'|'.$city));
  117.         if ($mainKey !== '||') {
  118.             $seenAddresses[] = $mainKey;
  119.         }
  120.         foreach ($vcards->getAddresses() ?? [] as $a) {
  121.             $aAddr $a->getAddress();
  122.             $aZip  $a->getZipCode();
  123.             $aCity $a->getCity();
  124.             if (!$aAddr && !$aZip && !$aCity) {
  125.                 continue;
  126.             }
  127.             $key strtolower(trim(($aAddr ?? '').'|'.($aZip ?? '').'|'.($aCity ?? '')));
  128.             if (in_array($key$seenAddressestrue)) {
  129.                 continue;
  130.             }
  131.             $seenAddresses[] = $key;
  132.             $label strtolower((string) ($a->getLabel() ?? ''));
  133.             $type 'WORK';
  134.             if (str_contains($label'home') || str_contains($label'maison')) {
  135.                 $type 'HOME';
  136.             }
  137.             $vcard->addAddress(nullnull$aAddr ?: ''nullnull$aZip ?: ''$aCity ?: ''$type);
  138.             $vcard->addLabel(trim(($aAddr ?: '') . ' ' . ($aZip ?: '') . ' ' . ($aCity ?: '')));
  139.         }
  140.         // Social / Website
  141.         if ($vcards->getWebSite()) {
  142.             $vcard->addURL($vcards->getWebSite());
  143.         }
  144.         $note "";
  145.         if ($vcards->getfacebook()) {
  146.             $note .= 'Facebook : ' $vcards->getfacebook() . "\n";
  147.         }
  148.         if ($vcards->gettwitter()) {
  149.             $note .= 'Twitter : ' $vcards->gettwitter() . "\n";
  150.         }
  151.         if ($vcards->getinstagram()) {
  152.             $note .= 'Instagram : ' $vcards->getinstagram() . "\n";
  153.         }
  154.         if ($vcards->getlinkedin()) {
  155.             $note .= 'LinkedIn : ' $vcards->getlinkedin() . "\n";
  156.         }
  157.         $note rtrim($note"\n");
  158.         $vcard->addNote($note);
  159.         // Picture
  160.         if ($vcards->getPicture()) {
  161.             $accountingFirm $vcards->getAccountingFirm();
  162.             $directory $this->getParameter('upload_directory') . '/vcards/' $accountingFirm->getId();
  163.             $vcard->addPhoto($directory '/' $vcards->getPicture());
  164.         }
  165.         //$vcard->addPhoto($this->getParameter('upload_directory') . '/vcards/blank.jpg');
  166.         // Debug
  167.         // dd($vcard->getOutput());
  168.         // Create custom Response
  169.         $res = new Response(
  170.             $vcard->getOutput(),
  171.             200,
  172.             $vcard->getHeaders(true)
  173.         );
  174.         return $res;
  175.     }
  176.     #[Route(path'/{id}'name'vcard_qrcode_redirect')]
  177.     #[Route(path'/qrcode/{id}'name'vcard_qrcode_redirect_bis')]
  178.     public function qrcodeRedirect(Qrcode $qrcode): RedirectResponse
  179.     {
  180.         return new RedirectResponse($qrcode->getUrl());
  181.     }
  182. }