src/Controller/ArtistsController.php line 84

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\artists;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Form\Extension\Core\Type\TextType;
  9. use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
  10. use Symfony\Component\Form\Extension\Core\Type\DateType;
  11. use Symfony\Component\Form\Extension\Core\Type\FileType;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  14. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  15. use Symfony\Component\Filesystem\Filesystem;
  16. class ArtistsController extends AbstractController {
  17.     /**
  18.      * @Route("/artists", methods={"GET"}, name="artist_list")
  19.      */
  20.     public function index() {
  21.         $artists $this->getDoctrine()->getRepository(Artists::class)->findAll();
  22.         return $this->render('artists/index.html.twig', array('artists' => $artists));
  23.     }
  24.     /**
  25.      * @Route("/artists/new", methods={"GET", "POST"}, name="new_artist")
  26.      */
  27.     public function new(Request $request) {
  28.         $artists = new Artists();
  29.         $form $this->createFormBuilder($artists)
  30.             ->add('artist'TextType::class, array(
  31.                 'attr' => array('class' => 'form-control col-6'),
  32.                 'row_attr' => array('class' => 'row col-6'),
  33.                 'label_attr' => array('class' => 'col-6'),
  34.             ))
  35.             ->add('email'TextType::class, array(
  36.                 'attr' => array('class' => 'form-control col-6'),
  37.                 'row_attr' => array('class' => 'row col-6'),
  38.                 'label_attr' => array('class' => 'col-6'),
  39.                 'empty_data' => '',
  40.                 'required' => false
  41.             ))
  42.             ->add('streamlink'TextType::class, array(
  43.                 'attr' => array('class' => 'form-control col-6'),
  44.                 'row_attr' => array('class' => 'row col-6'),
  45.                 'label_attr' => array('class' => 'col-6'),
  46.                 'empty_data' => '',
  47.                 'required' => false
  48.             ))
  49.             ->add('save'SubmitType::class, array(
  50.                 'label' => 'Create',
  51.                 'attr' => array('class' => 'btn btn-primary mt-3')
  52.             ))
  53.             ->getForm();
  54.         $form->handleRequest($request);
  55.         if($form->isSubmitted() && $form->isValid()) {
  56.             $battle $form->getData();
  57.             $entityManager $this->getDoctrine()->getManager();
  58.             $entityManager->persist($battle);
  59.             $entityManager->flush();
  60.             return $this->redirectToRoute('artist_list');
  61.         }
  62.         return $this->render('battles/new.html.twig', array(
  63.             'form' => $form->createView()
  64.         ));
  65.     }
  66.     /**
  67.      * @Route("/artists/edit/{id}", methods={"GET", "POST"}, name="edit_artist")
  68.      */
  69.     public function edit(Request $request$id) {
  70.         $artist = new Artists();
  71.         $artist $this->getDoctrine()->getRepository(Artists::class)->find($id);
  72.         $old_artist_image $artist->image;
  73.         $form $this->createFormBuilder($artist)
  74.             ->add('artist'TextType::class, array(
  75.                 'attr' => array('class' => 'form-control col-6'),
  76.                 'row_attr' => array('class' => 'row col-6'),
  77.                 'label_attr' => array('class' => 'col-6')
  78.             ))
  79.             ->add('email'TextType::class, array(
  80.                 'attr' => array('class' => 'form-control col-6'),
  81.                 'row_attr' => array('class' => 'row col-6'),
  82.                 'label_attr' => array('class' => 'col-6'),
  83.                 'empty_data' => '',
  84.                 'required' => false
  85.             ))
  86.             ->add('streamlink'TextType::class, array(
  87.                 'attr' => array('class' => 'form-control col-6'),
  88.                 'row_attr' => array('class' => 'row col-6'),
  89.                 'label_attr' => array('class' => 'col-6'),
  90.                 'empty_data' => '',
  91.                 'required' => false
  92.             ))
  93.             ->add('judge'ChoiceType::class, array(
  94.                 'attr' => array('class' => 'form-control col-6'),
  95.                 'row_attr' => array('class' => 'row col-6'),
  96.                 'label_attr' => array('class' => 'col-6'),
  97.                 'choices' => array( 'No' => 0,'Yes' => 1),
  98.                 'expanded' => true,
  99.                 'placeholder' => false,
  100.                 'required' => false,
  101.             ))
  102.             ->add('image',FileType::class,array(
  103.                 'row_attr' => array('class' => 'row col-6'),
  104.                 'label_attr' => array('class' => 'col-6'),
  105.                 'data_class' => null,
  106.                 'empty_data' => '',
  107.                 'attr' => array(
  108.                     'accept' => '*',
  109.                     'class' => 'col-6',
  110.                 ),
  111.                 'required' => false
  112.             ))
  113.             ->add('allow_battle_invites'ChoiceType::class, array(
  114.                 'attr' => array('class' => 'form-control col-6'),
  115.                 'row_attr' => array('class' => 'row col-6'),
  116.                 'label_attr' => array('class' => 'col-6'),
  117.                 'choices' => array('No' => 0'Yes' => 1),
  118.                 'placeholder' => false,
  119.                 'expanded' => true,
  120.                 'required' => false,
  121.             ))
  122.             ->add('save'SubmitType::class, array(
  123.                 'label' => 'Update',
  124.                 'attr' => array('class' => 'btn btn-primary mt-3')
  125.             ))
  126.             ->getForm();
  127.         $form->handleRequest($request);
  128.         if($form->isSubmitted() && $form->isValid()) {
  129.             $file $form->get('image')->getData();
  130.             if($file!== NULL){
  131.                 $fileName 'user_'.$id.'.'.$file->guessExtension();
  132.             }else{
  133.                 $fileName $old_artist_image;
  134.             }
  135.             $filesystem = new Filesystem();
  136.             $file_check $artist->getImage();
  137.             if($filesystem->exists($file_check)) {
  138.                 $file->move(
  139.                     'images/artists/',
  140.                     $fileName
  141.                 );
  142.             }
  143.             $artist->setImage($fileName);
  144.             $entityManager $this->getDoctrine()->getManager();
  145.             $entityManager->flush();
  146.             return $this->redirectToRoute('artist_list');
  147.         }
  148.         return $this->render('artists/edit.html.twig', array(
  149.             'form' => $form->createView()
  150.         ));
  151.     }
  152.     /**
  153.      * @Route("/artists/{id}", name="artist_show")
  154.      */
  155.     public function show($id) {
  156.         $battle $this->getDoctrine()->getRepository(Artists::class)->find($id);
  157.         return $this->render('battles/show.html.twig', array('battle' => $battle));
  158.     }
  159.     /**
  160.      * @Route("/artists/delete/{id}", methods={"DELETE"})
  161.      */
  162.     public function delete(Request $request$id) {
  163.         $battle $this->getDoctrine()->getRepository(Artists::class)->find($id);
  164.         $entityManager $this->getDoctrine()->getManager();
  165.         $entityManager->remove($battle);
  166.         $entityManager->flush();
  167.         $response = new Response();
  168.         $response->send();
  169.     }
  170.     ///**
  171.     // * @Route("/battles/save")
  172.     // */
  173.     //public function save() {
  174.     //    $entityManager = $this->getDoctrine()->getManager();
  175.     //
  176.     //    $battle = new Battles();
  177.     //    $battle->setTitle('Article Two');
  178.     //    $battle->setDate(date('Y-m-d H:i'));
  179.     //
  180.     //    $entityManager->persist($battle);
  181.     //
  182.     //    $entityManager->flush();
  183.     //
  184.     //    return new Response('Saved an article with the id of  '.$battle->getId());
  185.     //}
  186. }