src/Controller/CompanyDetailsController.php line 160

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CompanyDetails;
  4. use App\Form\CompanyDetailsType;
  5. use App\Repository\CompanyDetailsRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * @Route("/company_details")
  14.  * @Security("is_granted('ROLE_ADMIN')")
  15.  */
  16. class CompanyDetailsController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/index", name="company_details_index", methods={"GET"})
  20.      */
  21.     public function index(CompanyDetailsRepository $companyDetailsRepository): Response
  22.     {
  23.         return $this->render('company_details/index.html.twig', [
  24.             'company_details' => $companyDetailsRepository->findAll()
  25.         ]);
  26.     }
  27.     /**
  28.      * @Route("/new", name="company_details_new", methods={"GET", "POST"})
  29.      */
  30.     public function new(Request $requestCompanyDetailsRepository $companyDetailsRepository): Response
  31.     {
  32.         $companyDetails = new CompanyDetails();
  33.         $form $this->createForm(CompanyDetailsType::class, $companyDetails);
  34.         $form->handleRequest($request);
  35.         if ($form->isSubmitted() && $form->isValid()) {
  36.             $faviconDev $form['faviconDev']->getData();
  37.             $faviconLive $form['faviconLive']->getData();
  38.             $qrCode $form['companyQrCode']->getData();
  39.             if ($faviconDev) {
  40.                 $originalFilename pathinfo($faviconDev->getClientOriginalName(), PATHINFO_FILENAME);
  41.                 $newFilename $companyDetails->getCompanyName() . '_dev.' $faviconDev->guessExtension();
  42.                 $faviconDev->move(
  43.                     $this->getParameter('favicon_directory'),
  44.                     $newFilename
  45.                 );
  46.                 $companyDetails->setFaviconDev($newFilename);
  47.             }
  48.             if ($faviconLive) {
  49.                 $originalFilenameLive pathinfo($faviconLive->getClientOriginalName(), PATHINFO_FILENAME);
  50.                 $newFilenameLive $companyDetails->getCompanyName() . '_live.' $faviconLive->guessExtension();
  51.                 $faviconLive->move(
  52.                     $this->getParameter('favicon_directory'),
  53.                     $newFilenameLive
  54.                 );
  55.                 $companyDetails->setFaviconLive($newFilenameLive);
  56.             }
  57.             if ($qrCode) {
  58.                 $originalFilenameQR pathinfo($qrCode->getClientOriginalName(), PATHINFO_FILENAME);
  59.                 $newFilenameQR $companyDetails->getCompanyName() . '_qr_code.' $qrCode->guessExtension();
  60.                 $qrCode->move(
  61.                     $this->getParameter('favicon_directory'),
  62.                     $newFilenameQR
  63.                 );
  64.                 $companyDetails->setCompanyQrCode($newFilenameQR);
  65.             }
  66.             $companyDetailsRepository->add($companyDetailstrue);
  67.             return $this->redirectToRoute('company_details_index', [], Response::HTTP_SEE_OTHER);
  68.         }
  69.         return $this->render('company_details/new.html.twig', [
  70.             'company_details' => $companyDetails,
  71.             'form' => $form->createView(),
  72.         ]);
  73.     }
  74.     /**
  75.      * @Route("/show/{id}", name="company_details_show", methods={"GET"})
  76.      */
  77.     public function show(CompanyDetails $companyDetails): Response
  78.     {
  79.         return $this->render('company_details/show.html.twig', [
  80.             'company_details' => $companyDetails,
  81.         ]);
  82.     }
  83.     /**
  84.      * @Route("/edit/{id}", name="company_details_edit", methods={"GET", "POST"})
  85.      */
  86.     public function edit(Request $requestCompanyDetails $companyDetailsCompanyDetailsRepository $companyDetailsRepository): Response
  87.     {
  88.         $form $this->createForm(CompanyDetailsType::class, $companyDetails);
  89.         $form->handleRequest($request);
  90.         if ($form->isSubmitted() && $form->isValid()) {
  91.             $faviconDev $form['faviconDev']->getData();
  92.             $faviconLive $form['faviconLive']->getData();
  93.             $qrCode $form['companyQrCode']->getData();
  94.             if ($faviconDev) {
  95.                 $originalFilename pathinfo($faviconDev->getClientOriginalName(), PATHINFO_FILENAME);
  96.                 $newFilename $companyDetails->getCompanyName() . '_dev.' $faviconDev->guessExtension();
  97.                 $faviconDev->move(
  98.                     $this->getParameter('favicon_directory'),
  99.                     $newFilename
  100.                 );
  101.                 $companyDetails->setFaviconDev($newFilename);
  102.             }
  103.             if ($faviconLive) {
  104.                 $originalFilenameLive pathinfo($faviconLive->getClientOriginalName(), PATHINFO_FILENAME);
  105.                 $newFilenameLive $companyDetails->getCompanyName() . '_live.' $faviconLive->guessExtension();
  106.                 $faviconLive->move(
  107.                     $this->getParameter('favicon_directory'),
  108.                     $newFilenameLive
  109.                 );
  110.                 $companyDetails->setFaviconLive($newFilenameLive);
  111.             }
  112.             if ($qrCode) {
  113.                 $originalFilenameQR pathinfo($qrCode->getClientOriginalName(), PATHINFO_FILENAME);
  114.                 $newFilenameQR $companyDetails->getCompanyName() . '_qr_code.' $qrCode->guessExtension();
  115.                 $qrCode->move(
  116.                     $this->getParameter('favicon_directory'),
  117.                     $newFilenameQR
  118.                 );
  119.                 $companyDetails->setCompanyQrCode($newFilenameQR);
  120.             }
  121.             $companyDetailsRepository->add($companyDetailstrue);
  122.             return $this->redirectToRoute('company_details_index', [], Response::HTTP_SEE_OTHER);
  123.         }
  124.         return $this->render('company_details/edit.html.twig', [
  125.             'company_details' => $companyDetails,
  126.             'form' => $form->createView(),
  127.         ]);
  128.     }
  129.     /**
  130.      * @Route("/delete/{id}", name="company_details_delete", methods={"POST"})
  131.      */
  132.     public function delete(Request $requestCompanyDetails $companyDetailsCompanyDetailsRepository $companyDetailsRepository): Response
  133.     {
  134.         if ($this->isCsrfTokenValid('delete' $companyDetails->getId(), $request->request->get('_token'))) {
  135.             $companyDetailsRepository->remove($companyDetailstrue);
  136.         }
  137.         return $this->redirectToRoute('company_details_index', [], Response::HTTP_SEE_OTHER);
  138.     }
  139.     /**
  140.      * @Route("/office_address", name="office_address", methods={"GET"})
  141.      */
  142.     public function officeAddress(CompanyDetailsRepository $companyDetailsRepository): Response
  143.     {
  144.         return $this->render('home/officeAddress.html.twig');
  145.     }
  146.     /**
  147.      * @Route("/delete_favicon/{live_or_dev}/{id}", name="company_details_delete_favicon", methods={"POST", "GET"})
  148.      */
  149.     public function deleteLiveFavicon(Request $requestint $idstring $live_or_devCompanyDetails $companyDetailsEntityManagerInterface $entityManager)
  150.     {
  151.         $referer $request->headers->get('referer');
  152.         if ($live_or_dev == 'live') {
  153.             $companyDetails->setFaviconLive(null);
  154.             $entityManager->flush();
  155.             $files glob($this->getParameter('favicon_directory') . "/*live*");
  156.             foreach ($files as $file) {
  157.                 unlink($file);
  158.             }
  159.         }
  160.         if ($live_or_dev == 'dev') {
  161.             $companyDetails->setFaviconDev(null);
  162.             $entityManager->flush();
  163.             $files glob($this->getParameter('favicon_directory') . "/*dev*");
  164.             foreach ($files as $file) {
  165.                 unlink($file);
  166.             }
  167.         }
  168.         $entityManager->flush();
  169.         return $this->redirect($referer);
  170.     }
  171.     /**
  172.      * @Route("/delete_qr_code/{id}", name="company_details_delete_qr_code", methods={"POST", "GET"})
  173.      */
  174.     public function deleteQRCodeLiveFavicon(Request $requestint $idCompanyDetails $companyDetailsEntityManagerInterface $entityManager)
  175.     {
  176.         $referer $request->headers->get('referer');
  177.         $companyDetails->setCompanyQrCode(null);
  178.         $entityManager->flush();
  179.         $files glob($this->getParameter('favicon_directory') . "/*qr*");
  180.         foreach ($files as $file) {
  181.             unlink($file);
  182.         }
  183.         $entityManager->flush();
  184.         return $this->redirect($referer);
  185.     }
  186.     /**
  187.      * @Route("/export/live/database", name="export_live_database", methods={"POST", "GET"})
  188.      */
  189.     public function exportDatabase(Request $requestEntityManagerInterface $entityManager\App\Services\CompanyDetails $companyDetails)
  190.     {
  191.         $filename $companyDetails->getCompanyDetails()->getSqlDatabase().'.sql';
  192.         $sqlDatabase $companyDetails->getCompanyDetails()->getSqlDatabase();
  193.         $sqlPassword $companyDetails->getCompanyDetails()->getDatabasePassword();
  194.         if( $_ENV['APP_SERVER']=="local"){
  195.             exec('mysqldump --user=root --password= --host=localhost '.$sqlDatabase.'>'.$filename);
  196.         }
  197.         else{
  198.             exec('mysqldump --user=stephen --password='.$sqlPassword.' --host=localhost '.$sqlDatabase.' >'.$filename);
  199.         }
  200.         $file $this->getParameter('public').$filename;
  201.         if(file_exists($file)){
  202.             header('Content-Type: application/octet-stream');
  203.             header("Content-Transfer-Encoding: Binary");
  204.             header("Content-disposition: attachment; filename=\"" basename($filename) . "\"");
  205.             readfile($file);
  206.             unlink($file);
  207.         }
  208.         $referer $request->headers->get('Referer');
  209.         return $this->redirect($referer);
  210.     }
  211. }