<?php
namespace App\Controller\VCards;
use App\Entity\AccountingFirm;
use App\Entity\Qrcode;
use App\Entity\VCards;
use Doctrine\ORM\EntityManagerInterface;
use JeroenDesloovere\VCard\VCard;
use phpDocumentor\Reflection\Types\Void_;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class VcardsFrontController extends AbstractController
{
#[Route(path: '/', name: 'vcard_home', methods: ['GET'])]
public function index(): Response
{
dd('#Error');
}
#[Route(path: '/{cabinet}/{id}', name: 'vcard_view_front', methods: ['GET', 'POST'])]
public function view(Request $request, AccountingFirm $cabinet, VCards $vcard): Response
{
if ($vcard->getAccountingFirm() != $cabinet) {
throw new NotFoundHttpException();
}
return $this->render('vcards/view.html.twig', [
'id' => $vcard->getId(),
'accounting_firm' => $vcard->getAccountingFirm(),
'vcard' => $vcard
]);
}
#[Route(path: '/media/{cabinet}/{filename}.{_format}', name: 'vcard_media')]
public function media(Request $request, string $cabinet, string $filename): BinaryFileResponse
{
$filePath = sprintf('%s/%s/%s/%s',
$this->getParameter('upload_directory'),
"vcards",
$cabinet,
implode('.', [$filename, $request->getRequestFormat()]));
if (!file_exists($filePath)) {
return new Response('Media introuvable.', 404);
//throw $this->createNotFoundException();
}
return $response = new BinaryFileResponse($filePath);
}
#[Route(path: '/{cabinet}/{id}/download', name: 'vcard_add_front', methods: ['GET', 'POST'])]
public function add(VCards $vcards): Response
{
// Init new vCard
$vcard = new VCard();
// Name
$lastname = $vcards->getLastName();
$firstname = $vcards->getFirstName();
$vcard->addName($lastname, $firstname);
// Work
if ($vcards->getCabinet()) {
$vcard->addCompany($vcards->getCabinet());
}
if ($vcards->getFonction()) {
$vcard->addJobtitle($vcards->getFonction());
}
if ($vcards->getFonction()) {
$vcard->addRole($vcards->getFonction());
}
// Contact
if ($vcards->getEmail()) {
$vcard->addEmail($vcards->getEmail());
}
// Contact (legacy)
if ($vcards->getPhone()) {
$vcard->addPhoneNumber($vcards->getPhone(), 'PREF;CELL');
}
if ($vcards->getPhoneFix()) {
$vcard->addPhoneNumber($vcards->getPhoneFix(), 'WORK');
}
// Contact (phones collection)
$existingPhones = array_filter([
$vcards->getPhone(),
$vcards->getPhoneFix(),
]);
foreach ($vcards->getPhones() ?? [] as $p) {
$number = $p->getNumber();
if (!$number) {
continue;
}
// évite doublons
if (in_array($number, $existingPhones, true)) {
continue;
}
$existingPhones[] = $number;
// Map label -> type vCard (simple et efficace)
$label = strtolower((string) ($p->getLabel() ?? ''));
$type = 'WORK';
if (str_contains($label, 'mobile') || str_contains($label, 'portable') || str_contains($label, 'cell')) {
$type = 'CELL';
} elseif (str_contains($label, 'fixe') || str_contains($label, 'bureau') || str_contains($label, 'work')) {
$type = 'WORK';
} elseif (str_contains($label, 'home') || str_contains($label, 'maison')) {
$type = 'HOME';
}
$vcard->addPhoneNumber($number, $type);
}
// Address (principale)
$addr = $vcards->getAddress() ?: '';
$zip = $vcards->getZipCode() ?: '';
$city = $vcards->getCity() ?: '';
if ($addr || $zip || $city) {
$vcard->addAddress(null, null, $addr, null, null, $zip, $city, 'WORK');
$vcard->addLabel(trim($addr . ' ' . $zip . ' ' . $city));
}
// Address (secondaires)
$seenAddresses = [];
$mainKey = strtolower(trim($addr.'|'.$zip.'|'.$city));
if ($mainKey !== '||') {
$seenAddresses[] = $mainKey;
}
foreach ($vcards->getAddresses() ?? [] as $a) {
$aAddr = $a->getAddress();
$aZip = $a->getZipCode();
$aCity = $a->getCity();
if (!$aAddr && !$aZip && !$aCity) {
continue;
}
$key = strtolower(trim(($aAddr ?? '').'|'.($aZip ?? '').'|'.($aCity ?? '')));
if (in_array($key, $seenAddresses, true)) {
continue;
}
$seenAddresses[] = $key;
$label = strtolower((string) ($a->getLabel() ?? ''));
$type = 'WORK';
if (str_contains($label, 'home') || str_contains($label, 'maison')) {
$type = 'HOME';
}
$vcard->addAddress(null, null, $aAddr ?: '', null, null, $aZip ?: '', $aCity ?: '', $type);
$vcard->addLabel(trim(($aAddr ?: '') . ' ' . ($aZip ?: '') . ' ' . ($aCity ?: '')));
}
// Social / Website
if ($vcards->getWebSite()) {
$vcard->addURL($vcards->getWebSite());
}
$note = "";
if ($vcards->getfacebook()) {
$note .= 'Facebook : ' . $vcards->getfacebook() . "\n";
}
if ($vcards->gettwitter()) {
$note .= 'Twitter : ' . $vcards->gettwitter() . "\n";
}
if ($vcards->getinstagram()) {
$note .= 'Instagram : ' . $vcards->getinstagram() . "\n";
}
if ($vcards->getlinkedin()) {
$note .= 'LinkedIn : ' . $vcards->getlinkedin() . "\n";
}
$note = rtrim($note, "\n");
$vcard->addNote($note);
// Picture
if ($vcards->getPicture()) {
$accountingFirm = $vcards->getAccountingFirm();
$directory = $this->getParameter('upload_directory') . '/vcards/' . $accountingFirm->getId();
$vcard->addPhoto($directory . '/' . $vcards->getPicture());
}
//$vcard->addPhoto($this->getParameter('upload_directory') . '/vcards/blank.jpg');
// Debug
// dd($vcard->getOutput());
// Create custom Response
$res = new Response(
$vcard->getOutput(),
200,
$vcard->getHeaders(true)
);
return $res;
}
#[Route(path: '/{id}', name: 'vcard_qrcode_redirect')]
#[Route(path: '/qrcode/{id}', name: 'vcard_qrcode_redirect_bis')]
public function qrcodeRedirect(Qrcode $qrcode): RedirectResponse
{
return new RedirectResponse($qrcode->getUrl());
}
}