<?php
namespace App\Controller;
use App\Entity\artists;
use App\Entity\battles;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class BattlesController extends AbstractController {
/**
* @Route("/battles", methods={"GET"}, name="battle_list")
*/
public function index() {
$battles= $this->getDoctrine()->getRepository(Battles::class)->findAll();
return $this->render('battles/index.html.twig', array('battles' => $battles));
}
/**
* @Route("/battles/new", methods={"GET", "POST"}, name="new_battle")
*/
public function new(Request $request) {
$battle = new Battles();
$form = $this->createFormBuilder($battle)
->add('title', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6')
))
->add('streamlink', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'empty_data' => '',
'required' => false
))
->add('round_length', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'empty_data' => '',
'required' => true
))
->add('overtime_length', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'empty_data' => '',
'required' => true
))
->add('date', DateTimeType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'empty_data' => '',
'widget' => 'single_text'
))
->add('judge_criteria',ChoiceType::class, array(
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'choices' => array('Performance' => 0, 'Arrangement' => 1, 'Complexity' => 2, 'Execution' => 3, 'Musicality' => 4, 'Technicality' => 5, 'Flow' => 6, 'Originality' => 7, 'Engagement' => 8),
'attr' => array('class' => 'form-control js-example-matcher-criteria col-6'),
'multiple' => true,
'required' => true,
))
->add('judge_all_criteria', ChoiceType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'choices' => array('Yes' => 0, 'No' => 1),
'expanded' => true,
'placeholder' => false,
'required' => false,
))
->add('battle_type', ChoiceType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'choices' => array('AB-AB' => 0, 'AB-BA' => 1, 'A-B' => 2),
'expanded' => true,
'placeholder' => false,
'required' => true,
))
->add('allow_registrations', ChoiceType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'choices' => array('No' => 0, 'Yes' => 1),
'expanded' => true,
'placeholder' => false,
'required' => true,
))
->add('price', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'empty_data' => '',
'required' => false
))
->add('description', TextareaType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6')
))
->add('save', SubmitType::class, array(
'label' => 'Create',
'attr' => array('class' => 'btn btn-primary mt-3')
))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$battle = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($battle);
$entityManager->flush();
return $this->redirectToRoute('battle_list');
}
return $this->render('battles/new.html.twig', array(
'form' => $form->createView()
));
}
/**
* @Route("/battles/edit/{id}", methods={"GET", "POST"}, name="edit_battle")
*/
public function edit(Request $request, $id) {
$battle = new Battles();
$battle = $this->getDoctrine()->getRepository(Battles::class)->find($id);
$em = $this->getDoctrine()->getManager();
$all_artists = $em->getRepository(artists::class)->findByAllowBattleStatus(1);
$all_judges = $em->getRepository(artists::class)->findJudges(1);
if($battle->participant == FALSE){
$battle->participant = array();
}
if($battle->judge_criteria == FALSE){
$battle->judge_criteria = array();
}
if($battle->judges == FALSE){
$battle->judges = array();
}
$form = $this->createFormBuilder($battle)
->add('title', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6')
))
->add('streamlink', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'empty_data' => '',
'required' => false
))
->add('round_length', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'empty_data' => '',
'required' => true
))
->add('overtime_length', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'empty_data' => '',
'required' => true
))
->add('date', DateTimeType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'empty_data' => '',
'widget' => 'single_text'
))
->add('participant', ChoiceType::class, [
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'choices' => $all_artists,
'multiple' => true,
'attr' => array('class' => 'form-control js-matcher-participant col-6'),
'choice_label' => function($category, $key, $index) {
return $category->getArtist();
},
'choice_attr' => function($category, $key, $index)use ($battle) {
if ($this->checkSelection($category,$battle, 'participant') == 'none'){
return ['data-id' => $category->getId()];
}else if($this->checkSelection($category,$battle, 'participant') == 'selected'){
return ['data-id' => $category->getId(), 'selected' => 'selected'];
}
},
])
->add('judges', ChoiceType::class, [
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'choices' => $all_judges,
'multiple' => true,
'attr' => array('class' => 'form-control js-matcher-judges col-6'),
'choice_label' => function($category, $key, $index) {
return $category->getArtist();
},
'choice_attr' => function($category, $key, $index)use ($battle) {
if ($this->checkSelection($category,$battle, 'judges') == 'none'){
return ['data-id' => $category->getId()];
}else if($this->checkSelection($category,$battle, 'judges') == 'selected'){
return ['data-id' => $category->getId(), 'selected' => 'selected'];
}
},
])
->add('judge_criteria',ChoiceType::class, array(
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'choices' => array('Performance' => 0, 'Arrangement' => 1, 'Complexity' => 2, 'Execution' => 3, 'Musicality' => 4, 'Technicality' => 5, 'Flow' => 6, 'Originality' => 7, 'Engagement' => 8),
'attr' => array('class' => 'form-control js-example-matcher-criteria col-6'),
'multiple' => true,
'required' => false
))
/*->add('judge_all_criteria', ChoiceType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'choices' => array('Yes' => 0, 'No' => 1),
'expanded' => true,
'placeholder' => false,
'required' => false,
))*/
->add('sort_judge_criteria', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'empty_data' => '',
'required' => false
))
->add('battle_type', ChoiceType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'choices' => array('AB-AB' => 0, 'AB-BA' => 1, 'A-B' => 2),
'expanded' => true,
'placeholder' => false,
'required' => true,
))
->add('allow_registrations', ChoiceType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'choices' => array('No' => 0, 'Yes' => 1),
'expanded' => true,
'placeholder' => false,
'required' => false,
))
->add('price', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'empty_data' => '',
'required' => false
))
->add('description', TextareaType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6')
))
->add('save', SubmitType::class, array(
'label' => 'Update',
'attr' => array('class' => 'btn btn-primary mt-3')
))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();
return $this->redirectToRoute('battle_list');
}
return $this->render('battles/edit.html.twig', array(
'form' => $form->createView(),
'judges' => $all_judges
));
}
public function checkSelection($category, $battle, $type)
{
$return = 'none';
if($type == 'participant'){
foreach($battle->participant as $parti){
if($category->getId() == $parti->getId()){
$return = 'selected';
}
}
}elseif ($type == 'judges'){
foreach($battle->judges as $judge){
if($category->getId() == $judge->getId()){
$return = 'selected';
}
}
}
return $return;
}
/**
* @Route("/battles/{id}", name="battle_show")
*/
public function show($id) {
$battle = $this->getDoctrine()->getRepository(Battles::class)->find($id);
return $this->render('battles/show.html.twig', array('battle' => $battle));
}
/**
* @Route("/battles/delete/{id}", methods={"DELETE"})
*/
public function delete(Request $request, $id) {
$battle = $this->getDoctrine()->getRepository(Battles::class)->find($id);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($battle);
$entityManager->flush();
$response = new Response();
$response->send();
}
///**
// * @Route("/battles/save")
// */
//public function save() {
// $entityManager = $this->getDoctrine()->getManager();
//
// $battle = new Battles();
// $battle->setTitle('Article Two');
// $battle->setDate(date('Y-m-d H:i'));
//
// $entityManager->persist($battle);
//
// $entityManager->flush();
//
// return new Response('Saved an article with the id of '.$battle->getId());
//}
}