src/Controller/BattlesController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\artists;
  4. use App\Entity\battles;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
  11. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  13. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  14. class BattlesController extends AbstractController {
  15.     /**
  16.      * @Route("/battles", methods={"GET"}, name="battle_list")
  17.      */
  18.     public function index() {
  19.         $battles$this->getDoctrine()->getRepository(Battles::class)->findAll();
  20.         return $this->render('battles/index.html.twig', array('battles' => $battles));
  21.     }
  22.     /**
  23.      * @Route("/battles/new", methods={"GET", "POST"}, name="new_battle")
  24.      */
  25.     public function new(Request $request) {
  26.         $battle = new Battles();
  27.         $form $this->createFormBuilder($battle)
  28.             ->add('title'TextType::class, array(
  29.                 'attr' => array('class' => 'form-control col-6'),
  30.                 'row_attr' => array('class' => 'row col-6'),
  31.                 'label_attr' => array('class' => 'col-6')
  32.             ))
  33.             ->add('streamlink'TextType::class, array(
  34.                 'attr' => array('class' => 'form-control col-6'),
  35.                 'row_attr' => array('class' => 'row col-6'),
  36.                 'label_attr' => array('class' => 'col-6'),
  37.                 'empty_data' => '',
  38.                 'required' => false
  39.             ))
  40.             ->add('round_length'TextType::class, array(
  41.                 'attr' => array('class' => 'form-control col-6'),
  42.                 'row_attr' => array('class' => 'row col-6'),
  43.                 'label_attr' => array('class' => 'col-6'),
  44.                 'empty_data' => '',
  45.                 'required' => true
  46.             ))
  47.             ->add('overtime_length'TextType::class, array(
  48.                 'attr' => array('class' => 'form-control col-6'),
  49.                 'row_attr' => array('class' => 'row col-6'),
  50.                 'label_attr' => array('class' => 'col-6'),
  51.                 'empty_data' => '',
  52.                 'required' => true
  53.             ))
  54.             ->add('date'DateTimeType::class, array(
  55.                 'attr' => array('class' => 'form-control col-6'),
  56.                 'row_attr' => array('class' => 'row col-6'),
  57.                 'label_attr' => array('class' => 'col-6'),
  58.                 'empty_data' => '',
  59.                 'widget' => 'single_text'
  60.             ))
  61.             ->add('judge_criteria',ChoiceType::class, array(
  62.                 'row_attr' => array('class' => 'row col-6'),
  63.                 'label_attr' => array('class' => 'col-6'),
  64.                 'choices' => array('Performance' => 0'Arrangement' => 1'Complexity' => 2'Execution' => 3'Musicality' => 4'Technicality' => 5'Flow' => 6'Originality' => 7'Engagement' => 8),
  65.                 'attr' => array('class' => 'form-control js-example-matcher-criteria col-6'),
  66.                 'multiple' => true,
  67.                 'required' => true,
  68.             ))
  69.             ->add('judge_all_criteria'ChoiceType::class, array(
  70.                 'attr' => array('class' => 'form-control col-6'),
  71.                 'row_attr' => array('class' => 'row col-6'),
  72.                 'label_attr' => array('class' => 'col-6'),
  73.                 'choices' => array('Yes' => 0'No' => 1),
  74.                 'expanded' => true,
  75.                 'placeholder' => false,
  76.                 'required' => false,
  77.             ))
  78.             ->add('battle_type'ChoiceType::class, array(
  79.                 'attr' => array('class' => 'form-control col-6'),
  80.                 'row_attr' => array('class' => 'row col-6'),
  81.                 'label_attr' => array('class' => 'col-6'),
  82.                 'choices' => array('AB-AB' => 0'AB-BA' => 1'A-B' => 2),
  83.                 'expanded' => true,
  84.                 'placeholder' => false,
  85.                 'required' => true,
  86.             ))
  87.             ->add('allow_registrations'ChoiceType::class, array(
  88.                 'attr' => array('class' => 'form-control col-6'),
  89.                 'row_attr' => array('class' => 'row col-6'),
  90.                 'label_attr' => array('class' => 'col-6'),
  91.                 'choices' => array('No' => 0'Yes' => 1),
  92.                 'expanded' => true,
  93.                 'placeholder' => false,
  94.                 'required' => true,
  95.             ))
  96.             ->add('price'TextType::class, array(
  97.                 'attr' => array('class' => 'form-control col-6'),
  98.                 'row_attr' => array('class' => 'row col-6'),
  99.                 'label_attr' => array('class' => 'col-6'),
  100.                 'empty_data' => '',
  101.                 'required' => false
  102.             ))
  103.             ->add('description'TextareaType::class, array(
  104.                 'attr' => array('class' => 'form-control col-6'),
  105.                 'row_attr' => array('class' => 'row col-6'),
  106.                 'label_attr' => array('class' => 'col-6')
  107.             ))
  108.             ->add('save'SubmitType::class, array(
  109.                 'label' => 'Create',
  110.                 'attr' => array('class' => 'btn btn-primary mt-3')
  111.             ))
  112.             ->getForm();
  113.         $form->handleRequest($request);
  114.         if($form->isSubmitted() && $form->isValid()) {
  115.             $battle $form->getData();
  116.             $entityManager $this->getDoctrine()->getManager();
  117.             $entityManager->persist($battle);
  118.             $entityManager->flush();
  119.             return $this->redirectToRoute('battle_list');
  120.         }
  121.         return $this->render('battles/new.html.twig', array(
  122.             'form' => $form->createView()
  123.         ));
  124.     }
  125.     /**
  126.      * @Route("/battles/edit/{id}", methods={"GET", "POST"}, name="edit_battle")
  127.      */
  128.     public function edit(Request $request$id) {
  129.         $battle = new Battles();
  130.         $battle $this->getDoctrine()->getRepository(Battles::class)->find($id);
  131.         $em $this->getDoctrine()->getManager();
  132.         $all_artists    $em->getRepository(artists::class)->findByAllowBattleStatus(1);
  133.         $all_judges     $em->getRepository(artists::class)->findJudges(1);
  134.         if($battle->participant == FALSE){
  135.             $battle->participant = array();
  136.         }
  137.         if($battle->judge_criteria == FALSE){
  138.             $battle->judge_criteria = array();
  139.         }
  140.         if($battle->judges == FALSE){
  141.             $battle->judges = array();
  142.         }
  143.         $form $this->createFormBuilder($battle)
  144.             ->add('title'TextType::class, array(
  145.                 'attr' => array('class' => 'form-control col-6'),
  146.                 'row_attr' => array('class' => 'row col-6'),
  147.                 'label_attr' => array('class' => 'col-6')
  148.             ))
  149.             ->add('streamlink'TextType::class, array(
  150.                 'attr' => array('class' => 'form-control col-6'),
  151.                 'row_attr' => array('class' => 'row col-6'),
  152.                 'label_attr' => array('class' => 'col-6'),
  153.                 'empty_data' => '',
  154.                 'required' => false
  155.             ))
  156.             ->add('round_length'TextType::class, array(
  157.                 'attr' => array('class' => 'form-control col-6'),
  158.                 'row_attr' => array('class' => 'row col-6'),
  159.                 'label_attr' => array('class' => 'col-6'),
  160.                 'empty_data' => '',
  161.                 'required' => true
  162.             ))
  163.             ->add('overtime_length'TextType::class, array(
  164.                 'attr' => array('class' => 'form-control col-6'),
  165.                 'row_attr' => array('class' => 'row col-6'),
  166.                 'label_attr' => array('class' => 'col-6'),
  167.                 'empty_data' => '',
  168.                 'required' => true
  169.             ))
  170.             ->add('date'DateTimeType::class, array(
  171.                 'attr' => array('class' => 'form-control col-6'),
  172.                 'row_attr' => array('class' => 'row col-6'),
  173.                 'label_attr' => array('class' => 'col-6'),
  174.                 'empty_data' => '',
  175.                 'widget' => 'single_text'
  176.             ))
  177.             ->add('participant'ChoiceType::class, [
  178.                 'row_attr' => array('class' => 'row col-6'),
  179.                 'label_attr' => array('class' => 'col-6'),
  180.                 'choices' => $all_artists,
  181.                 'multiple' => true,
  182.                 'attr' => array('class' => 'form-control js-matcher-participant col-6'),
  183.                 'choice_label' => function($category$key$index) {
  184.                     return $category->getArtist();
  185.                 },
  186.                 'choice_attr' => function($category$key$index)use ($battle) {
  187.                     if ($this->checkSelection($category,$battle'participant') == 'none'){
  188.                         return ['data-id' => $category->getId()];
  189.                     }else if($this->checkSelection($category,$battle'participant') == 'selected'){
  190.                         return ['data-id' => $category->getId(), 'selected' => 'selected'];
  191.                     }
  192.                 },
  193.             ])
  194.             ->add('judges'ChoiceType::class, [
  195.                 'row_attr' => array('class' => 'row col-6'),
  196.                 'label_attr' => array('class' => 'col-6'),
  197.                 'choices' => $all_judges,
  198.                 'multiple' => true,
  199.                 'attr' => array('class' => 'form-control js-matcher-judges col-6'),
  200.                 'choice_label' => function($category$key$index) {
  201.                     return $category->getArtist();
  202.                 },
  203.                 'choice_attr' => function($category$key$index)use ($battle) {
  204.                     if ($this->checkSelection($category,$battle'judges') == 'none'){
  205.                         return ['data-id' => $category->getId()];
  206.                     }else if($this->checkSelection($category,$battle'judges') == 'selected'){
  207.                         return ['data-id' => $category->getId(), 'selected' => 'selected'];
  208.                     }
  209.                 },
  210.             ])
  211.             ->add('judge_criteria',ChoiceType::class, array(
  212.                 'row_attr' => array('class' => 'row col-6'),
  213.                 'label_attr' => array('class' => 'col-6'),
  214.                 'choices' => array('Performance' => 0'Arrangement' => 1'Complexity' => 2'Execution' => 3'Musicality' => 4'Technicality' => 5'Flow' => 6'Originality' => 7'Engagement' => 8),
  215.                 'attr' => array('class' => 'form-control js-example-matcher-criteria col-6'),
  216.                 'multiple' => true,
  217.                 'required' => false
  218.             ))
  219.             /*->add('judge_all_criteria', ChoiceType::class, array(
  220.                 'attr' => array('class' => 'form-control col-6'),
  221.                 'row_attr' => array('class' => 'row col-6'),
  222.                 'label_attr' => array('class' => 'col-6'),
  223.                 'choices' => array('Yes' => 0, 'No' => 1),
  224.                 'expanded' => true,
  225.                 'placeholder' => false,
  226.                 'required' => false,
  227.             ))*/
  228.             ->add('sort_judge_criteria'TextType::class, array(
  229.                 'attr' => array('class' => 'form-control col-6'),
  230.                 'row_attr' => array('class' => 'row col-6'),
  231.                 'label_attr' => array('class' => 'col-6'),
  232.                 'empty_data' => '',
  233.                 'required' => false
  234.             ))
  235.             ->add('battle_type'ChoiceType::class, array(
  236.                 'attr' => array('class' => 'form-control col-6'),
  237.                 'row_attr' => array('class' => 'row col-6'),
  238.                 'label_attr' => array('class' => 'col-6'),
  239.                 'choices' => array('AB-AB' => 0'AB-BA' => 1'A-B' => 2),
  240.                 'expanded' => true,
  241.                 'placeholder' => false,
  242.                 'required' => true,
  243.             ))
  244.             ->add('allow_registrations'ChoiceType::class, array(
  245.                 'attr' => array('class' => 'form-control col-6'),
  246.                 'row_attr' => array('class' => 'row col-6'),
  247.                 'label_attr' => array('class' => 'col-6'),
  248.                 'choices' => array('No' => 0'Yes' => 1),
  249.                 'expanded' => true,
  250.                 'placeholder' => false,
  251.                 'required' => false,
  252.             ))
  253.             ->add('price'TextType::class, array(
  254.                 'attr' => array('class' => 'form-control col-6'),
  255.                 'row_attr' => array('class' => 'row col-6'),
  256.                 'label_attr' => array('class' => 'col-6'),
  257.                 'empty_data' => '',
  258.                 'required' => false
  259.             ))
  260.             ->add('description'TextareaType::class, array(
  261.                 'attr' => array('class' => 'form-control col-6'),
  262.                 'row_attr' => array('class' => 'row col-6'),
  263.                 'label_attr' => array('class' => 'col-6')
  264.             ))
  265.             ->add('save'SubmitType::class, array(
  266.                 'label' => 'Update',
  267.                 'attr' => array('class' => 'btn btn-primary mt-3')
  268.             ))
  269.             ->getForm();
  270.         $form->handleRequest($request);
  271.         if($form->isSubmitted() && $form->isValid()) {
  272.             $entityManager $this->getDoctrine()->getManager();
  273.             $entityManager->flush();
  274.             return $this->redirectToRoute('battle_list');
  275.         }
  276.         return $this->render('battles/edit.html.twig', array(
  277.             'form' => $form->createView(),
  278.             'judges' => $all_judges
  279.         ));
  280.     }
  281.     public function checkSelection($category$battle$type)
  282.     {
  283.         $return 'none';
  284.         if($type == 'participant'){
  285.             foreach($battle->participant as $parti){
  286.                 if($category->getId() == $parti->getId()){
  287.                     $return 'selected';
  288.                 }
  289.             }
  290.         }elseif ($type == 'judges'){
  291.             foreach($battle->judges as $judge){
  292.                 if($category->getId() == $judge->getId()){
  293.                     $return 'selected';
  294.                 }
  295.             }
  296.         }
  297.         return $return;
  298.     }
  299.     /**
  300.      * @Route("/battles/{id}", name="battle_show")
  301.      */
  302.     public function show($id) {
  303.         $battle $this->getDoctrine()->getRepository(Battles::class)->find($id);
  304.         return $this->render('battles/show.html.twig', array('battle' => $battle));
  305.     }
  306.     /**
  307.      * @Route("/battles/delete/{id}", methods={"DELETE"})
  308.      */
  309.     public function delete(Request $request$id) {
  310.         $battle $this->getDoctrine()->getRepository(Battles::class)->find($id);
  311.         $entityManager $this->getDoctrine()->getManager();
  312.         $entityManager->remove($battle);
  313.         $entityManager->flush();
  314.         $response = new Response();
  315.         $response->send();
  316.     }
  317.     ///**
  318.     // * @Route("/battles/save")
  319.     // */
  320.     //public function save() {
  321.     //    $entityManager = $this->getDoctrine()->getManager();
  322.     //
  323.     //    $battle = new Battles();
  324.     //    $battle->setTitle('Article Two');
  325.     //    $battle->setDate(date('Y-m-d H:i'));
  326.     //
  327.     //    $entityManager->persist($battle);
  328.     //
  329.     //    $entityManager->flush();
  330.     //
  331.     //    return new Response('Saved an article with the id of  '.$battle->getId());
  332.     //}
  333. }