Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
[ Posted by Miguel ] CourseController.php
<?php
namespace BDA\AdminBundle\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use JMS\SecurityExtraBundle\Annotation\Secure;
use BDA\CourseBundle\Entity\Workshop;
use BDA\CourseBundle\Entity\Course;
use BDA\CourseBundle\Entity\CourseStep;
use BDA\CourseBundle\Entity\CoursePrice;
use BDA\CourseBundle\Form\Type\CourseType;
use BDA\CourseBundle\Entity\CourseWeeklyExercise;
class CourseController extends Controller
{
/**
* @Route("/admin/course/{course}")
* @Method({"GET", "POST"})
* @Secure(roles="ROLE_ADMIN")
* @Template()
*/
public function editAction(Course $course) {
$request = $this->getRequest();
$orm = $this->getDoctrine()->getManager();
if( !count($course->getPrices()) ){
$course->addPrice(new CoursePrice());
}
// create form
$form = $this->createForm(new CourseType($orm), $course, array(
'validation_groups' => array('update'),
));
// handle request
$form->handleRequest($request);
// update
if ($form->get('update')->isClicked()) {
// form is valid, update
if ($form->isValid()) {
// persist
$orm->persist($course);
$orm->flush();
// redirect
return $this->redirect($this->generateUrl('bda_admin_course_edit', array(
'course' => $course->getId()
)));
}
}
// delete
elseif ($form->get('delete')->isClicked()) {
$sort_order = $course->getSortOrder();
// Close space left by course
$query = $orm->createQuery('
UPDATE BDACourseBundle:Course c
SET c.sort_order = c.sort_order - 1
WHERE c.sort_order > :sort_order
AND c.workshop = :workshop')
;
$query->setParameter('sort_order', $sort_order);
$query->setParameter('workshop', $course->getWorkshop());
$query->execute();
// persist
$orm->remove($course);
$orm->flush();
// redirect
return $this->redirect($this->generateUrl('bda_admin_workshop_edit', array(
'workshop' => $course->getWorkshop()->getId()
)));
}
// return the form
return array(
'form' => $form->createView(),
'formAction' => $this->generateUrl('bda_admin_course_edit', array(
'course' => $course->getId()
)),
'course' => $course
);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment