<?php
namespace App\Controller;
use App\Entity\artists;
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\DateType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Filesystem\Filesystem;
class ArtistsController extends AbstractController {
/**
* @Route("/artists", methods={"GET"}, name="artist_list")
*/
public function index() {
$artists = $this->getDoctrine()->getRepository(Artists::class)->findAll();
return $this->render('artists/index.html.twig', array('artists' => $artists));
}
/**
* @Route("/artists/new", methods={"GET", "POST"}, name="new_artist")
*/
public function new(Request $request) {
$artists = new Artists();
$form = $this->createFormBuilder($artists)
->add('artist', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
))
->add('email', 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('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('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('artist_list');
}
return $this->render('battles/new.html.twig', array(
'form' => $form->createView()
));
}
/**
* @Route("/artists/edit/{id}", methods={"GET", "POST"}, name="edit_artist")
*/
public function edit(Request $request, $id) {
$artist = new Artists();
$artist = $this->getDoctrine()->getRepository(Artists::class)->find($id);
$old_artist_image = $artist->image;
$form = $this->createFormBuilder($artist)
->add('artist', TextType::class, array(
'attr' => array('class' => 'form-control col-6'),
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6')
))
->add('email', 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('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('judge', 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('image',FileType::class,array(
'row_attr' => array('class' => 'row col-6'),
'label_attr' => array('class' => 'col-6'),
'data_class' => null,
'empty_data' => '',
'attr' => array(
'accept' => '*',
'class' => 'col-6',
),
'required' => false
))
->add('allow_battle_invites', 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),
'placeholder' => false,
'expanded' => true,
'required' => false,
))
->add('save', SubmitType::class, array(
'label' => 'Update',
'attr' => array('class' => 'btn btn-primary mt-3')
))
->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
$file = $form->get('image')->getData();
if($file!== NULL){
$fileName = 'user_'.$id.'.'.$file->guessExtension();
}else{
$fileName = $old_artist_image;
}
$filesystem = new Filesystem();
$file_check = $artist->getImage();
if($filesystem->exists($file_check)) {
$file->move(
'images/artists/',
$fileName
);
}
$artist->setImage($fileName);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->flush();
return $this->redirectToRoute('artist_list');
}
return $this->render('artists/edit.html.twig', array(
'form' => $form->createView()
));
}
/**
* @Route("/artists/{id}", name="artist_show")
*/
public function show($id) {
$battle = $this->getDoctrine()->getRepository(Artists::class)->find($id);
return $this->render('battles/show.html.twig', array('battle' => $battle));
}
/**
* @Route("/artists/delete/{id}", methods={"DELETE"})
*/
public function delete(Request $request, $id) {
$battle = $this->getDoctrine()->getRepository(Artists::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());
//}
}