src/Controller/BusinessContactsController.php line 48

Open in your IDE?
  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\Repository\UserRepository;
  9. use App\Services\BusinessContactsImportService;
  10. use App\Services\UserImportService;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use JeroenDesloovere\VCard\VCard;
  13. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  14. use PhpOffice\PhpSpreadsheet\Writer\Csv;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\StreamedResponse;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\String\Slugger\SluggerInterface;
  22. /**
  23.  * @Route("/businesscontacts")
  24.  */
  25. class BusinessContactsController extends AbstractController
  26. {
  27.     /**
  28.      * @Route("/", name="business_contacts_index", methods={"GET"})
  29.      */
  30.     public function index(BusinessContactsRepository $businessContactsRepositoryBusinessTypesRepository $businessTypesRepository): Response
  31.     {
  32.         $business_contacts $businessContactsRepository->findBy([
  33.             'status' => 'Approved'
  34.         ]);
  35.         if ($this->isGranted('ROLE_ADMIN')) {
  36.             $business_contacts $businessContactsRepository->findAll();
  37.         }
  38.         $business_types = [];
  39.         foreach ($businessTypesRepository->findAll() as $businessTypes) {
  40.             $business_types[] = $businessTypes->getBusinessType();
  41.         }
  42.         $business_types array_unique($business_types);
  43.         return $this->render('business_contacts/index.html.twig', [
  44.             'business_contacts' => $business_contacts,
  45.             'business_types' => $business_types
  46.         ]);
  47.     }
  48.     /**
  49.      * @Route("/new", name="business_contacts_new", methods={"GET", "POST"})
  50.      */
  51.     public function new(Request $requestBusinessContactsRepository $businessContactsRepositoryEntityManagerInterface $entityManager): Response
  52.     {
  53.         $businessContact = new BusinessContacts();
  54.         $form $this->createForm(BusinessContactsType::class, $businessContact);
  55.         $form->handleRequest($request);
  56.         if ($form->isSubmitted() && $form->isValid()) {
  57.             $photo $form['photo']->getData();
  58.             if ($photo) {
  59.                 $files_name = [];
  60.                 $photo_directory $this->getParameter('business_contacts_photos_directory');
  61.                 $fileName pathinfo($photo->getClientOriginalName(), PATHINFO_FILENAME);
  62.                 $file_extension $photo->guessExtension();
  63.                 $newFileName $fileName "." $file_extension;
  64.                 $photo->move($photo_directory$newFileName);
  65.                 $businessContact->setPhoto($newFileName);
  66.             }
  67.             $businessContactsRepository->add($businessContacttrue);
  68.             $firstName $businessContact->getFirstName();
  69.             $lastName $businessContact->getLastName();
  70.             $entityManager->persist($businessContact);
  71.             $entityManager->flush();
  72.             return $this->redirectToRoute('business_contacts_index', [], Response::HTTP_SEE_OTHER);
  73.         }
  74.         return $this->renderForm('business_contacts/new.html.twig', [
  75.             'business_contact' => $businessContact,
  76.             'form' => $form,
  77.         ]);
  78.     }
  79.     /**
  80.      * @Route("/suggestion", name="business_contacts_suggestion", methods={"GET", "POST"})
  81.      */
  82.     public function suggestion(Request $requestBusinessContactsRepository $businessContactsRepository): Response
  83.     {
  84.         $businessContact = new BusinessContacts();
  85.         $form $this->createForm(BusinessContactsType::class, $businessContact);
  86.         $form->handleRequest($request);
  87.         if ($form->isSubmitted() && $form->isValid()) {
  88.             $businessContactsRepository->add($businessContacttrue);
  89.             $businessContact->setStatus('Pending');
  90.             return $this->redirectToRoute('business_contacts_index', [], Response::HTTP_SEE_OTHER);
  91.         }
  92.         return $this->renderForm('business_contacts/new.html.twig', [
  93.             'business_contact' => $businessContact,
  94.             'form' => $form,
  95.         ]);
  96.     }
  97.     /**
  98.      * @Route("/{id}", name="business_contacts_show", methods={"GET"})
  99.      */
  100.     public function show(BusinessContacts $businessContact): Response
  101.     {
  102.         return $this->render('business_contacts/show.html.twig', [
  103.             'business_contact' => $businessContact,
  104.         ]);
  105.     }
  106.     /**
  107.      * @Route("/{id}/edit", name="business_contacts_edit", methods={"GET", "POST"})
  108.      */
  109.     public function edit(Request $requestBusinessContacts $businessContactBusinessContactsRepository $businessContactsRepository): Response
  110.     {
  111.         $form $this->createForm(BusinessContactsType::class, $businessContact);
  112.         $form->handleRequest($request);
  113.         if ($form->isSubmitted() && $form->isValid()) {
  114.             $businessContactsRepository->add($businessContacttrue);
  115.             return $this->redirectToRoute('business_contacts_index', [], Response::HTTP_SEE_OTHER);
  116.         }
  117.         return $this->renderForm('business_contacts/edit.html.twig', [
  118.             'business_contact' => $businessContact,
  119.             'form' => $form,
  120.         ]);
  121.     }
  122.     /**
  123.      * @Route("/{id}", name="business_contacts_delete", methods={"POST"})
  124.      */
  125.     public function delete(Request $requestBusinessContacts $businessContactBusinessContactsRepository $businessContactsRepository): Response
  126.     {
  127.         if ($this->isCsrfTokenValid('delete' $businessContact->getId(), $request->request->get('_token'))) {
  128.             $businessContactsRepository->remove($businessContacttrue);
  129.         }
  130.         return $this->redirectToRoute('business_contacts_index', [], Response::HTTP_SEE_OTHER);
  131.     }
  132.     /**
  133.      * @Route ("/export/business_contacts", name="business_contacts_export" )
  134.      */
  135.     public function businessContactsExport(BusinessContactsRepository $businessContactsRepository)
  136.     {
  137.         $data = [];
  138.         $exported_date = new \DateTime('now');
  139.         $exported_date_formatted $exported_date->format('d-M-Y');
  140.         $exported_date_formatted_for_file $exported_date->format('d-m-Y');
  141.         $fileName 'business_contacts_export'.$exported_date_formatted_for_file.'csv';
  142.         $count 0;
  143.         $business_contact_list $businessContactsRepository->findAll();
  144.         foreach ($business_contact_list as $business_contact) {
  145.             $concatenatedNotes "Exported on: " $exported_date_formatted;
  146.             $data[] = [
  147.                 $business_contact->getPhoto(),
  148.                 $business_contact->getStatus(),
  149.                 $business_contact->getBusinessOrPerson(),
  150.                 $business_contact->getBusinessType(),
  151.                 $business_contact->getCompany(),
  152.                 $business_contact->getFirstName(),
  153.                 $business_contact->getLastName(),
  154.                 $business_contact->getWebsite(),
  155.                 $business_contact->getEmail(),
  156.                 $business_contact->getLandline(),
  157.                 $business_contact->getMobile(),
  158.                 $business_contact->getAddressStreet(),
  159.                 $business_contact->getAddressCity(),
  160.                 $business_contact->getAddressPostCode(),
  161.                 $business_contact->getAddressCountry(),
  162.                 $business_contact->getLocationLongitude(),
  163.                 $business_contact->getLocationLatitude(),
  164.                 $business_contact->getPublicPrivate(),
  165.                 $concatenatedNotes,
  166.                 $business_contact->getId()
  167.             ];
  168.         }
  169.         $spreadsheet = new Spreadsheet();
  170.         $sheet $spreadsheet->getActiveSheet();
  171.         $sheet->setTitle('Business Contacts');
  172.         $sheet->getCell('A1')->setValue('Photo');
  173.         $sheet->getCell('B1')->setValue('Status');
  174.         $sheet->getCell('C1')->setValue('Business Or Person');
  175.         $sheet->getCell('D1')->setValue('Business Type');
  176.         $sheet->getCell('E1')->setValue('Company Name');
  177.         $sheet->getCell('F1')->setValue('First Name');
  178.         $sheet->getCell('G1')->setValue('Last Name');
  179.         $sheet->getCell('H1')->setValue('Website');
  180.         $sheet->getCell('I1')->setValue('Email');
  181.         $sheet->getCell('J1')->setValue('Mobile1');
  182.         $sheet->getCell('K1')->setValue('Landline');
  183.         $sheet->getCell('L1')->setValue('Company');
  184.         $sheet->getCell('M1')->setValue('Address Street');
  185.         $sheet->getCell('N1')->setValue('Address City');
  186.         $sheet->getCell('O1')->setValue('Address Post Code');
  187.         $sheet->getCell('P1')->setValue('Address Country');
  188.         $sheet->getCell('Q1')->setValue('Location Longitude');
  189.         $sheet->getCell('R1')->setValue('Location Latitude');
  190.         $sheet->getCell('S1')->setValue('Public or Private');
  191.         $sheet->getCell('T1')->setValue('Business or Person');
  192.         $sheet->getCell('U1')->setValue('Notes');
  193.         $sheet->getCell('V1')->setValue('Id');
  194.         $sheet->fromArray($datanull'A2'true);
  195.         $total_rows $sheet->getHighestRow();
  196.         for ($i 2$i <= $total_rows$i++) {
  197.             $cell "L" $i;
  198.             $sheet->getCell($cell)->getHyperlink()->setUrl("https://google.com");
  199.         }
  200.         $writer = new Csv($spreadsheet);
  201.         $response = new StreamedResponse(function () use ($writer) {
  202.             $writer->save('php://output');
  203.         });
  204.         $response->headers->set('Content-Type''application/vnd.ms-excel');
  205.         $response->headers->set('Content-Disposition'sprintf('attachment;filename="%s"'$fileName));
  206.         $response->headers->set('Cache-Control''max-age=0');
  207.         return $response;
  208.     }
  209.     /**
  210.      * @Route ("/import/business_contacts", name="business_contacts_import" )
  211.      */
  212.     public function businessContactsImport(Request $requestSluggerInterface $sluggerBusinessContactsRepository $businessContactsRepositoryBusinessContactsImportService $businessContactsImportService): Response
  213.     {
  214.         $form $this->createForm(ImportType::class);
  215.         $form->handleRequest($request);
  216.         if ($form->isSubmitted() && $form->isValid()) {
  217.             $importFile $form->get('File')->getData();
  218.             if ($importFile) {
  219.                 $originalFilename pathinfo($importFile->getClientOriginalName(), PATHINFO_FILENAME);
  220.                 $safeFilename $slugger->slug($originalFilename);
  221.                 $newFilename $safeFilename '.' 'csv';
  222.                 try {
  223.                     $importFile->move(
  224.                         $this->getParameter('business_contact_attachments_directory'),
  225.                         $newFilename
  226.                     );
  227.                 } catch (FileException $e) {
  228.                     die('Import failed');
  229.                 }
  230.                 $businessContactsImportService->importBusinessContacts($newFilename);
  231.                 return $this->redirectToRoute('business_contacts_index');
  232.             }
  233.         }
  234.         return $this->render('business_contacts/import.html.twig', [
  235.             'form' => $form->createView(),
  236.             'heading'=>'Business Contacts Import',
  237.         ]);
  238.     }
  239.     /**
  240.      * @Route("/create/Vcard/{id}", name="create_vcard")
  241.      */
  242.     public function createVcard(int $idBusinessContactsRepository $businessContactsRepository)
  243.     {
  244.         $business_contact $businessContactsRepository->find($id);
  245.         $vcard = new VCard();
  246.         $firstName $business_contact->getFirstName();
  247.         $lastName $business_contact->getLastName();
  248.         $company $business_contact->getCompany();
  249.         $website $business_contact->getWebsite();
  250.         $address $business_contact->getAddressStreet();
  251.         $addressCity $business_contact->getAddressCity();
  252.         $addressPostalCode $business_contact->getAddressPostcode();
  253.         $addressCountry $business_contact->getAddressCountry();
  254.         $notes "GPS location: " $business_contact->getLocationLongitude();
  255.         $vcard->addName($lastName$firstName);
  256.         $vcard->addEmail($business_contact->getEmail())
  257.             ->addPhoneNumber($business_contact->getLandline(), 'work')
  258.             ->addCompany($company)
  259.             ->addURL($website)
  260.             ->addNote($notes)
  261.             ->addAddress($name ''$extended ''$street $address$city $addressCity$region ''$zip $addressPostalCode$country $addressCountry$type 'WORK;POSTAL');
  262.         $vcard->download();
  263.         return new Response(null);
  264.     }
  265.     /**
  266.      * @Route("/gps/GoogleMap/show/{id}", name="show_location_google_maps", methods={"GET"})
  267.      */
  268.     public function showMap(int $idBusinessContactsRepository $businessContactsRepository): Response
  269.     {
  270.         $business_contact $businessContactsRepository->find($id);
  271.         $longitude $business_contact->getLocationLongitude();
  272.         $latitude $business_contact->getLocationLatitude();
  273.         return $this->render('business_contacts/gpsGoogleMaps.html.twig', [
  274.             'latitude' => $latitude,
  275.             'longitude' => $longitude,
  276.             'business' => $business_contact->getCompany()
  277.         ]);
  278.     }
  279.     /**
  280.      * @Route("/update/user/location", name="update_user_location", methods={"POST"})
  281.      */
  282.     public function updateLocation(BusinessContactsRepository $businessContactsRepositoryEntityManagerInterface $manager): Response
  283.     {
  284.         $id $_POST['id'];
  285.         $latitude $_POST['latitude'];
  286.         $longitude $_POST['longitude'];
  287.         $gps $latitude ',' $longitude;
  288.         $business_contact $businessContactsRepository->find($id);
  289.         $business_contact->setGpsLocation($gps);
  290.         $manager->flush();
  291.         return new Response(null);
  292.     }
  293.     /**
  294.      * @Route("/gps_location_clear/{id}", name="tourist_attraction_clear_gps_location")
  295.      */
  296.     public
  297.     function clearGPSLocation(Request $requestTouristAttraction $touristAttractionEntityManagerInterface $entityManager)
  298.     {
  299.         $referer $request->headers->get('referer');
  300.         $touristAttraction->setGpsLocation(null);
  301.         $entityManager->flush();
  302.         return $this->redirect($referer);
  303.     }
  304.     /**
  305.      * @Route("/importContacts", name="importContacts")
  306.      */
  307.     public
  308.     function userImportContacts(Request $requestSluggerInterface $sluggerBusinessContactsImportService $touristAttractionImportOutlookService): Response
  309.     {
  310.         $form $this->createForm(ImportType::class);
  311.         $form->handleRequest($request);
  312.         if ($form->isSubmitted() && $form->isValid()) {
  313.             $importFile $form->get('File')->getData();
  314.             if ($importFile) {
  315.                 $originalFilename pathinfo($importFile->getClientOriginalName(), PATHINFO_FILENAME);
  316.                 $safeFilename $slugger->slug($originalFilename);
  317.                 $newFilename $safeFilename '.' 'csv';
  318.                 try {
  319.                     $importFile->move(
  320.                         $this->getParameter('user_attachments_directory'),
  321.                         $newFilename
  322.                     );
  323.                 } catch (FileException $e) {
  324.                     die('Import failed');
  325.                 }
  326.                 $touristAttractionImportOutlookService->importTouristAttraction($newFilename);
  327.                 return $this->redirectToRoute('tourist_attraction_index');
  328.             }
  329.         }
  330.         return $this->render('admin/import/index.html.twig', [
  331.             'form' => $form->createView(),
  332.             'heading' => 'Cyprus Tourist Attractions'
  333.         ]);
  334.     }
  335. }