<?php
namespace KinuInk\AlumniBundle\Controller;
use KinuInk\AlumniBundle\Entity\Alumni;
use KinuInk\AlumniBundle\Entity\Promotion;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use KinuInk\AlumniBundle\Service\FileUploader;
use Symfony\Component\HttpFoundation\File\File;
/**
* Promotion Controller
* @Route("/")
*/
class AlumniController extends AbstractController
{
/**
* @Route("/", name="alumni_default")
* @Method("GET")
*/
public function homeAction(Request $request)
{
/** @var $session \Symfony\Component\HttpFoundation\Session\Session */
$session = $request->getSession();
$lastUsernameKey = Security::LAST_USERNAME;
// last username entered by the user
$lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);
$csrfToken = $this->has('security.csrf.token_manager')
? $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue()
: null;
return $this->render('@KinuInkAlumni/Alumni/index.html.twig', array(
'last_username' => $lastUsername,
'csrf_token' => $csrfToken
));
}
/**
* Display a profile
* @Route("/profile", name="alumni_profile")
* @Method("GET")
*/
public function indexAction(Request $request)
{
$user = $this->getUser();
if ($user and !$user->hasRole('ROLE_ALUMNI')) {
return $this->redirectToRoute('_slug');
}
//$this->denyAccessUnlessGranted('ROLE_ALUMNI');
//$user = $this->getUser();*
$alumni = $user->getAlumni() ?: [];
if (empty($alumni)){
return $this->redirectToRoute('alumni_add');
}
$promotion = new Promotion();
$form = $this->createForm('KinuInk\AlumniBundle\Form\PromotionType', $promotion);
$form->handleRequest($request);
return $this->render('@KinuInkAlumni/Alumni/profile.html.twig', array(
'alumni' => $alumni,
'form' => $form->createView()
));
}
/**
* Add a details information in profile
* @Route("/add", name="alumni_add")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_ALUMNI');
if ($user = $this->getUser() and !$user->getAlumni())
{
$alumni = new Alumni();
$promotion = new Promotion();
$form = $this->createForm('KinuInk\AlumniBundle\Form\AlumniType', $alumni);
$form->handleRequest($request);
$form_error = [];
if ($form->isSubmitted())
{
if ($form->isValid())
{
$em = $this->getDoctrine()->getManager();
$user->setAlumni($alumni);
$promotion->setAlumni($alumni);
$promotion->setFormation($form['formation']->getData());
$promotion->setAnneePromotion($form['anneeDePromotion']->getData());
$alumni->addFormation($promotion->getFormation());
$em->persist($promotion);
$em->persist($alumni);
$em->flush();
return $this->redirectToRoute('alumni_profile');
}
$form_error = $form->getErrors(true);
}
return $this->render('@KinuInkAlumni/Alumni/new.html.twig', array(
'alumni' => $alumni,
'form' => $form->createView(),
'form_errors' => $form_error,
));
} else {
return $this->redirectToRoute('/');
}
}
/**
* Edit a profile
* @Route("/edit", name="alumni_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_ALUMNI');
if ($user = $this->getUser() and $user->getAlumni())
{
$editForm = $this->createForm('KinuInk\AlumniBundle\Form\AlumniEditType', $user->getAlumni());
$editForm->handleRequest($request);
$form_error = [];
if ($editForm->isSubmitted())
{
if ($editForm->isValid())
{
$file = $user->getAlumni()->getFile();
if ($file instanceof UploadedFile) {
$fileUploader = new FileUploader($this->getParameter('avatars_directory'));
$fileName = $fileUploader->upload($file, $user->getAlumni()->getAvatar());
$user->getAlumni()->setAvatar(
$fileName
);
}
$this->getDoctrine()->getManager()->persist($user->getAlumni());
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('alumni_profile');
}
$form_error = $editForm->getErrors(true);
}
return $this->render('@KinuInkAlumni/Alumni/edit.html.twig', array(
'edit_form' => $editForm->createView(),
'form_errors' => $form_error,
));
} else {
return $this->redirectToRoute('/');
}
}
/**
* @Route("/ajouter-promotion", name="alumni_add_promotion")
* @Method({"POST"})
*/
public function addPromotionAction(Request $request)
{
$this->denyAccessUnlessGranted('ROLE_ALUMNI');
if ($user = $this->getUser() and $user->getAlumni())
{
$promotion = new Promotion();
$form = $this->createForm('KinuInk\AlumniBundle\Form\PromotionType', $promotion);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$em = $this->getDoctrine()->getManager();
$promotion->setAlumni($user->getAlumni());
$user->getAlumni()->addFormation($promotion->getFormation());
$em->persist($promotion);
$em->flush();
return $this->redirectToRoute('alumni_profile');
}
} else {
return $this->redirectToRoute('alumni_profile');
}
}
}