src/Controller/SocialLinkController.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\SocialLink;
  4. use App\Form\SocialLinkType;
  5. use Kunstmaan\AdminListBundle\Controller\AdminListController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Validator\Exception\ValidatorException;
  10. class SocialLinkController extends AdminListController
  11. {
  12.     /**
  13.      *  Set urls
  14.      *
  15.      * @Route("/set-urls", methods={"GET"}, name="gcs_admin_social_set_urls")
  16.      *
  17.      */
  18.     public function setUrl()
  19.     {
  20.         return $this->render(
  21.             'SocialLinks/settingsView.html.twig',
  22.             [
  23.                 'instagram' => $this->prepareForm('instagram')->createView(),
  24.                 'facebook' => $this->prepareForm('facebook')->createView(),
  25.                 'youtube' => $this->prepareForm('youtube')->createView(),
  26.                 'twitter' => $this->prepareForm('twitter')->createView(),
  27.                 'rss' => $this->prepareForm('rss')->createView(),
  28.                 'bip' => $this->prepareForm('bip')->createView()
  29.             ]
  30.         );
  31.     }
  32.     /**
  33.      *  Save setting
  34.      *
  35.      * @Route("/set-urls/{type}", methods={"POST"}, name="gcs_admin_social_set_urls_post")
  36.      */
  37.     public function postAction(Request $request$type)
  38.     {
  39.         $form $this->prepareForm($type);
  40.         $form->handleRequest($request);
  41.         if (!$form->isSubmitted() || !$form->isValid()) {
  42.             throw new ValidatorException('form is invalid');
  43.         }
  44.         /** @var SocialLink $socialLink */
  45.         $socialLink $form->getData();
  46.         $em $this->getDoctrine()->getManager();
  47.         $em->persist($socialLink);
  48.         $em->flush();
  49.         return $this->redirectToRoute('gcs_admin_social_set_urls');
  50.     }
  51.     private function prepareForm($type)
  52.     {
  53.         /** @var SocialLink $setting */
  54.         $setting $this->getDoctrine()->getRepository(SocialLink::class)->findOneByType($type);
  55.         if (!$setting) {
  56.             $setting = new SocialLink();
  57.             $setting->setType($type);
  58.         }
  59.         return $this->createForm(
  60.             SocialLinkType::class,
  61.             $setting,
  62.             [
  63.                 'method' => 'POST',
  64.                 'action' => $this->generateUrl('gcs_admin_social_set_urls_post', ['type' => $type]),
  65.             ]
  66.         );
  67.     }
  68.     public function feedAction($type)
  69.     {
  70.         $repository $this->getDoctrine()->getRepository(SocialLink::class);
  71.         /** @var SocialLink $settings */
  72.         $settings $repository->findOneByType($type);
  73.         $url $settings $settings->getLinkUrl() : null;
  74.         return new Response($url);
  75.     }
  76. }