src/Controller/BusinessContactsController.php line 48

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\BusinessContacts;
  4. use App\Form\BusinessContactsType;
  5. use App\Form\ImportType;
  6. use App\Repository\BusinessContactsRepository;
  7. use App\Repository\BusinessTypesRepository;
  8. use App\Services\ImportBusinessContactsService;
  9. use App\Services\CompanyDetailsService;
  10. use App\Services\CountBusinessContactsService;
  11. use App\Services\PhoneAnalyzer;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use JeroenDesloovere\VCard\VCard;
  14. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  15. use PhpOffice\PhpSpreadsheet\Writer\Csv;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  18. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  22. use Symfony\Component\HttpFoundation\StreamedResponse;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. use Symfony\Component\String\Slugger\SluggerInterface;
  25. /**
  26.  * @Route("/businesscontacts")
  27.  */
  28. class BusinessContactsController extends AbstractController
  29. {
  30.     /**
  31.      * @Route("/index", name="business_contacts_index", methods={"GET"})
  32.      */
  33.     public function index(Request $requestBusinessContactsRepository $businessContactsRepositoryBusinessTypesRepository $businessTypesRepositoryCountBusinessContactsService $countBusinessContactsServicePhoneAnalyzer $phoneAnalyzer): Response
  34.     {
  35.         $business_types_all $businessTypesRepository->findAll();
  36.         usort($business_types_all, function($a$b) {
  37.             return strcmp($a->getBusinessType(), $b->getBusinessType());
  38.         });
  39.         $businessTypeName $request->query->get('business_type');
  40.         $adminView $request->query->get('admin_view'); // <-- New
  41.         if ($businessTypeName) {
  42.             // Look up the type entity
  43.             $businessType $businessTypesRepository->findOneBy(['businessType' => $businessTypeName]);
  44.             if ($businessType) {
  45.                 // Filtered: only contacts and types for this type
  46.                 $business_contacts $businessContactsRepository->findBy(
  47.                     ['businessType' => $businessType],
  48.                     ['id' => 'ASC']
  49.                 );
  50.                 $business_types = [$businessType]; // ONLY this type for headings
  51.             } else {
  52.                 // Invalid filter: fallback to all
  53.                 $business_contacts $businessContactsRepository->findAll();
  54.                 $business_types $businessTypesRepository->findBy([], ['ranking' => 'ASC']);
  55.             }
  56.         } else {
  57.             // No filter: show everything
  58.             $business_contacts $businessContactsRepository->findAll();
  59.             $business_types $businessTypesRepository->findBy([], ['ranking' => 'ASC']);
  60.         }
  61.         // Analyze phones
  62.         foreach ($business_contacts as $business_contact) {
  63.             $business_contact->phoneAnalysis = [
  64.                 'mobile' => $business_contact->getMobile() ? $phoneAnalyzer->analyze($business_contact->getMobile()) : null,
  65.                 'landline' => $business_contact->getLandline() ? $phoneAnalyzer->analyze($business_contact->getLandline()) : null,
  66.             ];
  67.         }
  68.         return $this->render('business_contacts/index.html.twig', [
  69.             'business_contacts' => $business_contacts,
  70.             'business_types' => $business_types,
  71.             'business_types_all' => $business_types_all,
  72.             'countBusinessContactsService' => $countBusinessContactsService,
  73.             'list_or_map' => 'list',
  74.             'admin_check' => $adminView === 'Admin' 'Yes' 'No'// <-- Change
  75.             'current_business_type' => $businessTypeName,
  76.         ]);
  77.     }
  78.     /**
  79.      * @Route("/map/{subset}", name="business_contacts_map", methods={"GET"})
  80.      */
  81.     public function map(Request $requeststring $subsetBusinessContactsRepository $businessContactsRepositoryBusinessTypesRepository $businessTypesRepositoryCountBusinessContactsService $countBusinessContacts): Response
  82.     {
  83.         if ($subset == 'All') {
  84.             $business_contacts $businessContactsRepository->findBy([
  85.                 'status' => 'Approved'
  86.             ]);
  87.         }
  88.         if ($subset != 'All') {
  89.             $business_type $businessTypesRepository->findOneBy(['businessType' => $subset]);
  90.             $business_contacts $businessContactsRepository->findBy([
  91.                 'status' => 'Approved',
  92.                 'businessType' => $business_type
  93.             ]);
  94.         }
  95.         $latitude_total 0;
  96.         $longitude_total 0;
  97.         $count 0;
  98.         $latitude_max = -100;
  99.         $latitude_min = +100;
  100.         $longitude_max = -100;
  101.         $longitude_min = +100;
  102.         foreach ($business_contacts as $business_contact) {
  103.             if ($business_contact->getLocationLatitude() != or $business_contact->getLocationLatitude() != null) {
  104.                 $count $count 1;
  105.                 $latitude_total $latitude_total $business_contact->getLocationLatitude();
  106.                 $longitude_total $longitude_total $business_contact->getLocationLongitude();
  107.                 if ($business_contact->getLocationLatitude() > $latitude_max) {
  108.                     $latitude_max $business_contact->getLocationLatitude();
  109.                 }
  110.                 if ($business_contact->getLocationLatitude() < $latitude_min) {
  111.                     $latitude_min $business_contact->getLocationLatitude();
  112.                 }
  113.                 if ($business_contact->getLocationLongitude() > $longitude_max) {
  114.                     $longitude_max $business_contact->getLocationLongitude();
  115.                 }
  116.                 if ($business_contact->getLocationLongitude() < $longitude_min) {
  117.                     $longitude_min $business_contact->getLocationLongitude();
  118.                 }
  119.             }
  120.         }
  121.         if ($count == 0) {
  122.             $latitude_average 'No data';
  123.             $longitude_average 'No data';
  124.         }
  125.         if ($count >= 1) {
  126.             $latitude_average $latitude_total $count;
  127.             $longitude_average $longitude_total $count;
  128.         }
  129.         if ($count 2) {
  130.             $latitude_range "TBD";
  131.             $longitude_range "TBD";
  132.         }
  133.         if ($count 1) {
  134.             $latitude_range $latitude_max $latitude_min;
  135.             $longitude_range $longitude_max $longitude_min;
  136.         }
  137.         $business_types $businessTypesRepository->findBy([], ['ranking' => 'ASC']);
  138.         return $this->render('business_contacts/map_of_business_contacts.html.twig', [
  139.             'business_contacts' => $business_contacts,
  140.             'business_types' => $business_types,
  141.             'subset' => $subset,
  142.             'latitude_max' => $latitude_max,
  143.             'latitude_min' => $latitude_min,
  144.             'latitude_average' => $latitude_average,
  145.             'latitude_range' => $latitude_range,
  146.             'longitude_max' => $longitude_max,
  147.             'longitude_min' => $longitude_min,
  148.             'longitude_average' => $longitude_average,
  149.             'longitude_range' => $longitude_range,
  150.             'count' => $count,
  151.             'list_or_map' => 'map',
  152.             'admin_check' => 'No'
  153.         ]);
  154.     }
  155.     /**
  156.      * @Route("/new/{business_type}", name="business_contacts_new", methods={"GET", "POST"})
  157.      */
  158.     public function new(Request $requeststring $business_typeBusinessContactsRepository $businessContactsRepositoryBusinessTypesRepository $businessTypesRepositoryCompanyDetailsService $companyDetailsEntityManagerInterface $entityManager): Response
  159.     {
  160.         $business_type $businessTypesRepository->find($business_type);
  161.         $default_country $companyDetails->getCompanyDetails()->getCompanyAddressCountry();
  162.         $businessContact = new BusinessContacts();
  163.         $businessContact->setBusinessType($business_type);
  164.         $businessContact->setAddressCountry($default_country);
  165.         $form $this->createForm(BusinessContactsType::class, $businessContact);
  166.         $form->handleRequest($request);
  167.         if ($form->isSubmitted() && $form->isValid()) {
  168.             $photo $form['photo']->getData();
  169.             if ($photo) {
  170.                 $uniqueId uniqid(); // Generates a unique ID
  171.                 $uniqueId3digits substr($uniqueId103); // Extracts the first 3 digits
  172.                 $files_name = [];
  173.                 $photo_directory $this->getParameter('business_contacts_photos_directory');
  174.                 $fileName pathinfo($photo->getClientOriginalName(), PATHINFO_FILENAME);
  175.                 $file_extension $photo->guessExtension();
  176.                 $newFileName $businessContact->getCompany() . "_" $uniqueId3digits "." $file_extension;
  177.                 $photo->move($photo_directory$newFileName);
  178.                 $businessContact->setPhoto($newFileName);
  179.             }
  180.             $file $form['files']->getData();
  181.             if ($file) {
  182.                 $file_name = [];
  183.                 $file_directory $this->getParameter('business_contacts_attachments_directory');
  184.                 $fileName pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  185.                 $file_extension $file->guessExtension();
  186.                 $newFileName $fileName "." $file_extension;
  187.                 $file->move($file_directory$newFileName);
  188.                 $businessContact->setFiles($newFileName);
  189.             }
  190.             $businessContactsRepository->add($businessContacttrue);
  191.             $firstName $businessContact->getFirstName();
  192.             $lastName $businessContact->getLastName();
  193.             $entityManager->persist($businessContact);
  194.             $entityManager->flush();
  195.             return $this->redirectToRoute('business_contacts_index', [], Response::HTTP_SEE_OTHER);
  196.         }
  197.         return $this->renderForm('business_contacts/new.html.twig', [
  198.             'business_contact' => $businessContact,
  199.             'form' => $form,
  200.         ]);
  201.     }
  202.     /**
  203.      * @Route("/suggestion", name="business_contacts_suggestion", methods={"GET", "POST"})
  204.      */
  205.     public function suggestion(Request $requestBusinessContactsRepository $businessContactsRepository): Response
  206.     {
  207.         $businessContact = new BusinessContacts();
  208.         $form $this->createForm(BusinessContactsType::class, $businessContact);
  209.         $form->handleRequest($request);
  210.         if ($form->isSubmitted() && $form->isValid()) {
  211.             $businessContactsRepository->add($businessContacttrue);
  212.             $businessContact->setStatus('Pending');
  213.             return $this->redirectToRoute('business_contacts_index', [], Response::HTTP_SEE_OTHER);
  214.         }
  215.         return $this->renderForm('business_contacts/new.html.twig', [
  216.             'business_contact' => $businessContact,
  217.             'form' => $form,
  218.         ]);
  219.     }
  220.     /**
  221.      * @Route("/show/{id}", name="business_contacts_show", methods={"GET"})
  222.      */
  223.     public function show(BusinessContacts $businessContact): Response
  224.     {
  225.         $longitude $businessContact->getLocationLongitude();
  226.         $latitude $businessContact->getLocationLatitude();
  227.         return $this->render('business_contacts/show.html.twig', [
  228.             'business_contact' => $businessContact,
  229.             'longitude' => $longitude,
  230.             'latitude' => $latitude,
  231.         ]);
  232.     }
  233.     /**
  234.      * @Route("/edit/{id}", name="business_contacts_edit", methods={"GET", "POST"})
  235.      */
  236.     public function edit(Request $requestBusinessContacts $businessContactBusinessContactsRepository $businessContactsRepository): Response
  237.     {
  238.         $form $this->createForm(BusinessContactsType::class, $businessContact);
  239.         $form->handleRequest($request);
  240.         if ($form->isSubmitted() && $form->isValid()) {
  241.             $photo $form['photo']->getData();
  242.             if ($photo) {
  243.                 $files_name = [];
  244.                 $photo_directory $this->getParameter('business_contacts_photos_directory');
  245.                 $fileName pathinfo($photo->getClientOriginalName(), PATHINFO_FILENAME);
  246.                 $file_extension $photo->guessExtension();
  247.                 if ($businessContact->getFirstName() == '') {
  248.                     $newFileName $businessContact->getCompany() . "." $file_extension;
  249.                 } else {
  250.                     $newFileName $businessContact->getCompany() . "_" $businessContact->getFirstName() . "_" $businessContact->getLastName() . "." $file_extension;
  251.                 }
  252.                 $photo->move($photo_directory$newFileName);
  253.                 $businessContact->setPhoto($newFileName);
  254.             }
  255.             $file $form['files']->getData();
  256.             if ($file) {
  257.                 $file_name = [];
  258.                 $file_directory $this->getParameter('business_contacts_attachments_directory');
  259.                 $fileName pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  260.                 $file_extension $file->guessExtension();
  261.                 $newFileName $fileName "." $file_extension;
  262.                 $file->move($file_directory$newFileName);
  263.                 $businessContact->setFiles($newFileName);
  264.             }
  265.             $businessContactsRepository->add($businessContacttrue);
  266.             return $this->redirectToRoute('business_contacts_index', [], Response::HTTP_SEE_OTHER);
  267.         }
  268.         return $this->renderForm('business_contacts/edit.html.twig', [
  269.             'business_contact' => $businessContact,
  270.             'form' => $form,
  271.         ]);
  272.     }
  273.     /**
  274.      * @Route("/delete/{id}", name="business_contacts_delete", methods={"POST"})
  275.      */
  276.     public function delete(Request $requestBusinessContacts $businessContactBusinessContactsRepository $businessContactsRepositoryEntityManagerInterface $entityManager): Response
  277.     {
  278.         $referer $request->headers->get('referer');
  279.         $file_name $businessContact->getFiles();
  280.         if ($file_name) {
  281.             $file $this->getParameter('business_contacts_attachments_directory') . $file_name;
  282.             if (file_exists($file)) {
  283.                 unlink($file);
  284.             }
  285.             $businessContact->setFiles('');
  286.             $entityManager->flush();
  287.         }
  288.         $photo_file_name $businessContact->getPhoto();
  289.         if ($photo_file_name) {
  290.             $photo_file_name $this->getParameter('business_contacts_photos_directory') . $photo_file_name;
  291.             if (file_exists($photo_file_name)) {
  292.                 unlink($photo_file_name);
  293.             }
  294.             $businessContact->setPhoto('');
  295.             $entityManager->flush();
  296.         }
  297.         if ($this->isCsrfTokenValid('delete' $businessContact->getId(), $request->request->get('_token'))) {
  298.             $businessContactsRepository->remove($businessContacttrue);
  299.         }
  300.         return $this->redirect($referer);
  301.     }
  302.     /**
  303.      * @Route("/delete_GPS_location/{id}", name="business_contacts_delete_GPS_location", methods={"GET","POST"})
  304.      */
  305.     public function deleteGPSLocation(Request $requestint $idBusinessContactsRepository $businessContactsRepositoryEntityManagerInterface $entityManager): Response {
  306.         $referer $request->headers->get('referer');
  307.         $businessContact=$businessContactsRepository->find($id);
  308.         // Optional CSRF check
  309. //        if (!$this->isCsrfTokenValid('delete_gps' . $businessContact->getId(), $request->request->get('_token'))) {
  310. //            throw $this->createAccessDeniedException('Invalid CSRF token.');
  311. //        }
  312.         // Delete GPS coords
  313.         $businessContact->setLocationLatitude(null);
  314.         $businessContact->setLocationLongitude(null);
  315.         $entityManager->flush();
  316.         // Redirect back to the page user came from
  317.         return $this->redirect($referer ?: $this->generateUrl('business_contacts_index'));
  318.     }
  319.     /**
  320.      * @Route("/delete_all_GPS_locations", name="business_contacts_delete_all_GPS_locations")
  321.      */
  322.     public function deleteAllBusinessContactsGPSLocations(Request $requestBusinessContactsRepository $businessContactsRepositoryEntityManagerInterface $entityManager): Response
  323.     {
  324.         $referer $request->headers->get('referer');
  325.         $business_contacts $businessContactsRepository->findAll();
  326.         foreach ($business_contacts as $business_contact) {
  327.             $business_contact->setLocationLongitude(null);
  328.             $business_contact->setLocationLatitude(null);
  329.             $entityManager->flush();
  330.         }
  331.         return $this->redirect($referer ?: $this->generateUrl('business_contacts_index'));
  332.     }
  333.     /**
  334.      * @Route("/delete_all", name="business_contacts_delete_all")
  335.      */
  336.     public function deleteAllBusinessContacts(BusinessContactsRepository $businessContactsRepositoryEntityManagerInterface $entityManager): Response
  337.     {
  338.         $business_contacts $businessContactsRepository->findAll();
  339.         foreach ($business_contacts as $business_contact) {
  340.             $entityManager->remove($business_contact);
  341.             $entityManager->flush();
  342.         }
  343.         return $this->redirectToRoute('business_contacts_index', [], Response::HTTP_SEE_OTHER);
  344.     }
  345.     /**
  346.      * @Route("/delete_photo_file/{id}", name="business_contact_delete_photo_file", methods={"POST", "GET"})
  347.      */
  348.     public function deleteBusinessContactPhotoFile(int $idRequest $requestBusinessContacts $businessContactEntityManagerInterface $entityManager)
  349.     {
  350.         $referer $request->headers->get('referer');
  351.         $photo_file_name $businessContact->getPhoto();
  352.         if ($photo_file_name) {
  353.             $photo_file_name $this->getParameter('business_contacts_photos_directory') . "/" $photo_file_name;
  354.             if (file_exists($photo_file_name)) {
  355.                 unlink($photo_file_name);
  356.             }
  357.             $businessContact->setPhoto('');
  358.             $entityManager->flush();
  359.         }
  360.         return $this->redirect($referer);
  361.     }
  362.     /**
  363.      * @Route("/delete_attachment_file/{id}", name="business_contact_delete_attachment_file", methods={"POST", "GET"})
  364.      */
  365.     public function deleteBusinessContactAttachmentFile(int $idRequest $requestBusinessContacts $businessContactEntityManagerInterface $entityManager)
  366.     {
  367.         $referer $request->headers->get('referer');
  368.         $file_name $businessContact->getFiles();
  369.         if ($file_name) {
  370.             $file $this->getParameter('business_contacts_attachments_directory') . "/" $file_name;
  371.             if (file_exists($file)) {
  372.                 unlink($file);
  373.             }
  374.             $businessContact->setFiles('');
  375.             $entityManager->flush();
  376.         }
  377.         return $this->redirect($referer);
  378.     }
  379.     /**
  380.      * @Route ("/view/photo/{id}", name="view_business_contact_photo")
  381.      */
  382.     public function viewBusinessContactPhoto(Request $request$idBusinessContactsRepository $businessContactsRepository)
  383.     {
  384.         $business_contact $businessContactsRepository->find($id);
  385.         return $this->render('business_contacts/view_photo.html.twig', ['business_contact' => $business_contact]);
  386.     }
  387.     /**
  388.      * @Route ("/business_contacts_export", name="business_contacts_export" )
  389.      */
  390.     public function businessContactsExport(BusinessContactsRepository $businessContactsRepository)
  391.     {
  392.         $data = [];
  393.         $exported_date = new \DateTime('now');
  394.         $exported_date_formatted $exported_date->format('d-M-Y');
  395.         $fileName 'business_contacts_export_' $exported_date_formatted '.csv';
  396.         $count 0;
  397.         $business_contact_list $businessContactsRepository->findAll();
  398.         foreach ($business_contact_list as $business_contact) {
  399.             $concatenatedNotes "Exported on: " $exported_date_formatted;
  400.             $data[] = [
  401.                 "BusinessContacts",
  402.                 $business_contact->getStatus(),
  403.                 $business_contact->getBusinessOrPerson(),
  404.                 $business_contact->getBusinessType()->getBusinessType(),
  405.                 $business_contact->getCompany(),
  406.                 $business_contact->getFirstName(),
  407.                 $business_contact->getLastName(),
  408.                 $business_contact->getWebsite(),
  409.                 $business_contact->getEmail(),
  410.                 $business_contact->getLandline(),
  411.                 $business_contact->getMobile(),
  412.                 $business_contact->getAddressStreet(),
  413.                 $business_contact->getAddressCity(),
  414.                 $business_contact->getAddressCounty(),
  415.                 $business_contact->getAddressPostCode(),
  416.                 $business_contact->getAddressCountry(),
  417.                 $business_contact->getLocationLongitude(),
  418.                 $business_contact->getLocationLatitude(),
  419.                 $business_contact->getNotes()
  420.             ];
  421.         }
  422.         $spreadsheet = new Spreadsheet();
  423.         $sheet $spreadsheet->getActiveSheet();
  424.         $sheet->setTitle('Business Contacts');
  425.         $sheet->getCell('A1')->setValue('Entity');
  426.         $sheet->getCell('B1')->setValue('Status');
  427.         $sheet->getCell('C1')->setValue('Business Or Person');
  428.         $sheet->getCell('D1')->setValue('Business Type');
  429.         $sheet->getCell('E1')->setValue('Company');
  430.         $sheet->getCell('F1')->setValue('First Name');
  431.         $sheet->getCell('G1')->setValue('Last Name');
  432.         $sheet->getCell('H1')->setValue('Web Page');
  433.         $sheet->getCell('I1')->setValue('E-mail');
  434.         $sheet->getCell('J1')->setValue('Business Phone');
  435.         $sheet->getCell('K1')->setValue('Mobile Phone');
  436.         $sheet->getCell('L1')->setValue('Business Street');
  437.         $sheet->getCell('M1')->setValue('Business City');
  438.         $sheet->getCell('N1')->setValue('Business County');
  439.         $sheet->getCell('O1')->setValue('Business Postal Code');
  440.         $sheet->getCell('P1')->setValue('Business Country/Region');
  441.         $sheet->getCell('Q1')->setValue('Location Longitude');
  442.         $sheet->getCell('R1')->setValue('Location Latitude');
  443.         $sheet->fromArray($datanull'A2'true);
  444.         $total_rows $sheet->getHighestRow();
  445.         for ($i 2$i <= $total_rows$i++) {
  446.             $cell "L" $i;
  447.             $sheet->getCell($cell)->getHyperlink()->setUrl("https://google.com");
  448.         }
  449.         $writer = new Csv($spreadsheet);
  450.         $response = new StreamedResponse(function () use ($writer) {
  451.             $writer->save('php://output');
  452.         });
  453.         $response->headers->set('Content-Type''application/vnd.ms-excel');
  454.         $response->headers->set('Content-Disposition'sprintf('attachment;filename="%s"'$fileName));
  455.         $response->headers->set('Cache-Control''max-age=0');
  456.         return $response;
  457.     }
  458.     /**
  459.      * @Route ("/export/business_contacts_outlook", name="business_contacts_export_for_outlook" )
  460.      */
  461.     public function businessContactsExportForOutlook(BusinessContactsRepository $businessContactsRepository)
  462.     {
  463.         $data = [];
  464.         $exported_date = new \DateTime('now');
  465.         $exported_date_formatted $exported_date->format('d-M-Y');
  466.         $fileName 'business_contacts_export_for_outlook_' $exported_date_formatted '.csv';
  467.         $count 0;
  468.         $business_contact_list $businessContactsRepository->findAll();
  469.         foreach ($business_contact_list as $business_contact) {
  470.             $concatenatedNotes "Exported on: " $exported_date_formatted;
  471.             $data[] = [
  472.                 $business_contact->getStatus(),
  473.                 $business_contact->getBusinessOrPerson(),
  474.                 $business_contact->getBusinessType()->getBusinessType(),
  475.                 $business_contact->getCompany(),
  476.                 $business_contact->getFirstName(),
  477.                 $business_contact->getLastName(),
  478.                 $business_contact->getWebsite(),
  479.                 $business_contact->getEmail(),
  480.                 $business_contact->getLandline(),
  481.                 $business_contact->getMobile(),
  482.                 $business_contact->getAddressStreet(),
  483.                 $business_contact->getAddressCity(),
  484.                 $business_contact->getAddressCounty(),
  485.                 $business_contact->getAddressPostCode(),
  486.                 $business_contact->getAddressCountry(),
  487.                 $business_contact->getLocationLongitude(),
  488.                 $business_contact->getLocationLatitude(),
  489.                 $concatenatedNotes,
  490.                 $business_contact->getId()
  491.             ];
  492.         }
  493.         $spreadsheet = new Spreadsheet();
  494.         $sheet $spreadsheet->getActiveSheet();
  495.         $sheet->setTitle('Business Contacts');
  496.         $sheet->getCell('A1')->setValue('Status');
  497.         $sheet->getCell('B1')->setValue('Business Or Person');
  498.         $sheet->getCell('C1')->setValue('Business Type');
  499.         $sheet->getCell('D1')->setValue('Company');
  500.         $sheet->getCell('E1')->setValue('First Name');
  501.         $sheet->getCell('F1')->setValue('Last Name');
  502.         $sheet->getCell('G1')->setValue('Web Page');
  503.         $sheet->getCell('H1')->setValue('E-mail');
  504.         $sheet->getCell('I1')->setValue('Business Phone');
  505.         $sheet->getCell('J1')->setValue('Mobile Phone');
  506.         $sheet->getCell('K1')->setValue('Business Street');
  507.         $sheet->getCell('L1')->setValue('Business City');
  508.         $sheet->getCell('M1')->setValue('Business County');
  509.         $sheet->getCell('N1')->setValue('Business Postal Code');
  510.         $sheet->getCell('O1')->setValue('Business Country/Region');
  511.         $sheet->getCell('P1')->setValue('Location Longitude');
  512.         $sheet->getCell('Q1')->setValue('Location Latitude');
  513.         $sheet->getCell('R1')->setValue('Notes');
  514.         $sheet->getCell('S1')->setValue('Id');
  515.         $sheet->fromArray($datanull'A2'true);
  516.         $total_rows $sheet->getHighestRow();
  517.         for ($i 2$i <= $total_rows$i++) {
  518.             $cell "L" $i;
  519.             $sheet->getCell($cell)->getHyperlink()->setUrl("https://google.com");
  520.         }
  521.         $writer = new Csv($spreadsheet);
  522.         $response = new StreamedResponse(function () use ($writer) {
  523.             $writer->save('php://output');
  524.         });
  525.         $response->headers->set('Content-Type''application/vnd.ms-excel');
  526.         $response->headers->set('Content-Disposition'sprintf('attachment;filename="%s"'$fileName));
  527.         $response->headers->set('Cache-Control''max-age=0');
  528.         return $response;
  529.     }
  530.     /**
  531.      * @Route ("/business_contacts_import", name="business_contacts_import" )
  532.      */
  533.     public function businessContactsImport(Request $requestSluggerInterface $sluggerBusinessContactsRepository $businessContactsRepositoryImportBusinessContactsService $businessContactsImportService): Response
  534.     {
  535.         $form $this->createForm(ImportType::class);
  536.         $form->handleRequest($request);
  537.         if ($form->isSubmitted() && $form->isValid()) {
  538.             $importFile $form->get('File')->getData();
  539.             if ($importFile) {
  540.                 $originalFilename pathinfo($importFile->getClientOriginalName(), PATHINFO_FILENAME);
  541.                 $safeFilename $slugger->slug($originalFilename);
  542.                 $newFilename $safeFilename '.' 'csv';
  543.                 try {
  544.                     $importFile->move(
  545.                         $this->getParameter('business_contacts_import_directory'),
  546.                         $newFilename
  547.                     );
  548.                 } catch (FileException $e) {
  549.                     die('Import failed');
  550.                 }
  551.                 $businessContactsImportService->importBusinessContacts($newFilename);
  552.                 return $this->redirectToRoute('business_contacts_index');
  553.             }
  554.         }
  555.         return $this->render('home/import.html.twig', [
  556.             'form' => $form->createView(),
  557.             'heading' => 'Business Contacts Import',
  558.         ]);
  559.     }
  560.     /**
  561.      * @Route("/create/Vcard/{id}", name="create_vcard")
  562.      */
  563.     public function createVcard(int $idBusinessContactsRepository $businessContactsRepository)
  564.     {
  565.         $business_contact $businessContactsRepository->find($id);
  566.         $vcard = new VCard();
  567.         $businessOrPerson $business_contact->getBusinessOrPerson();
  568.         $business_type $business_contact->getBusinessType()->getBusinessType();
  569.         $firstName $business_contact->getFirstName();
  570.         $lastName $business_contact->getLastName();
  571.         $mobile $business_contact->getMobile();
  572.         $landline $business_contact->getLandline();
  573.         $company $business_contact->getCompany();
  574.         $website $business_contact->getWebsite();
  575.         $addressStreet $business_contact->getAddressStreet();
  576.         $addressCity $business_contact->getAddressCity();
  577.         $addressPostCode $business_contact->getAddressPostcode();
  578.         $addressCountry $business_contact->getAddressCountry();
  579.         $longitude $business_contact->getLocationLongitude();
  580.         $latitude $business_contact->getLocationLatitude();
  581.         $notes $business_contact->getNotes();
  582.         if ($businessOrPerson "Business") {
  583.             $firstNameCard $company;
  584.             $lastNameCard $business_type;
  585.             $companyCard = [];
  586.         }
  587.         if ($businessOrPerson "Person") {
  588.             $firstNameCard $firstName;
  589.             $lastNameCard $lastName;
  590.             $companyCard $company;
  591.         }
  592.         $vcard->addName($lastNameCard$firstNameCard);
  593.         $vcard->addEmail($business_contact->getEmail())
  594.             ->addPhoneNumber($landline'work')
  595.             ->addPhoneNumber($mobile'mobile')
  596.             ->addCompany($companyCard)
  597.             ->addURL($website)
  598.             ->addNote($notes)
  599.             ->addAddress($name ''$extended ''$street $addressStreet$city $addressCity$region ''$zip $addressPostCode$country $addressCountry$type 'WORK;POSTAL');
  600.         $vcard->download();
  601.         return new Response(null);
  602.     }
  603.     /**
  604.      * @Route("/update/location", name="update_business_contact_location", methods={"POST"})
  605.      */
  606.     public function updateLocation(BusinessContactsRepository $businessContactsRepositoryEntityManagerInterface $manager): Response
  607.     {
  608.         $id $_POST['id'];
  609.         $latitude $_POST['latitude'];
  610.         $longitude $_POST['longitude'];
  611.         $gps $latitude ',' $longitude;
  612.         $business_contact $businessContactsRepository->find($id);
  613.         $business_contact->setLocationLongitude($longitude)
  614.             ->setLocationLatitude($latitude);
  615.         $manager->flush();
  616.         return new Response(null);
  617.     }
  618.     /**
  619.      * @Route("/gps_location_clear/{id}", name="business_contact_clear_gps_location")
  620.      */
  621.     public
  622.     function clearGPSLocation(Request $requestBusinessContacts $businessContactsEntityManagerInterface $entityManager)
  623.     {
  624.         $referer $request->headers->get('referer');
  625.         $businessContacts->setLocationLongitude(null);
  626.         $businessContacts->setLocationLatitude(null);
  627.         $entityManager->flush();
  628.         return $this->redirect($referer);
  629.     }
  630.     /**
  631.      * @Route("/show_attachment/{id}", name="business_contact_show_attachment")
  632.      */
  633.     public function showAttachmentBusinessContact(int $idBusinessContactsRepository $businessContactsRepository)
  634.     {
  635.         $business_contact $businessContactsRepository->find($id);
  636.         $filename $business_contact->getFiles();
  637.         $filepath $this->getParameter('business_contacts_attachments_directory') . "/" $filename;
  638.         if (file_exists($filepath)) {
  639.             $response = new BinaryFileResponse($filepath);
  640.             $response->setContentDisposition(
  641.                 ResponseHeaderBag::DISPOSITION_INLINE//use ResponseHeaderBag::DISPOSITION_ATTACHMENT to save as an attachment
  642.                 $filename
  643.             );
  644.             return $response;
  645.         } else {
  646.             return new Response("file does not exist");
  647.         }
  648.     }
  649. }