kekrozsak/src/KekRozsak/FrontBundle/Controller/DocumentController.php

98 lines
2.6 KiB
PHP
Raw Normal View History

<?php
namespace KekRozsak\FrontBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
2012-07-22 17:38:00 +00:00
use KekRozsak\FrontBundle\Entity\Document;
use KekRozsak\FrontBundle\Form\Type\DocumentType;
use KekRozsak\FrontBundle\Extensions\Slugifier;
class DocumentController extends Controller
{
/**
* @Route("/dokumentum/{slug}", name="KekRozsakFrontBundle_documentView")
* @Template()
* @ParamConverter("document")
*/
public function viewAction(Document $document)
{
return array(
'document' => $document,
);
}
2012-07-22 17:38:00 +00:00
/**
* @Route("/dokumentumok/uj/", name="KekRozsakFrontBundle_documentCreate")
2012-07-22 17:38:00 +00:00
* @Template()
*/
public function createAction()
2012-07-22 17:38:00 +00:00
{
$document = new Document();
$document->setSlug('n-a');
$form = $this->createForm(new DocumentType(), $document);
$request = $this->getRequest();
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
{
/* TODO: move these lines into life cycle events */
2012-07-22 17:38:00 +00:00
$slugifier = new Slugifier();
$document->setSlug($slugifier->slugify($document->getTitle()));
$document->setCreatedAt(new \DateTime('now'));
$document->setCreatedBy($this->get('security.context')->getToken()->getUser());
$em = $this->getDoctrine()->getEntityManager();
$em->persist($document);
$em->flush();
return $this->redirect($this->generateUrl('KekRozsakFrontBundle_documentView', array('slug' => $document->getSlug())));
2012-07-22 17:38:00 +00:00
}
}
return array(
'form' => $form->createView(),
);
}
/**
* @Route("/dokumentum/{slug}/szerkesztes", name="KekRozsakFrontBundle_documentEdit")
2012-07-22 17:38:00 +00:00
* @Template()
* @ParamConverter("document")
2012-07-22 17:38:00 +00:00
*/
public function editAction(Document $document)
2012-07-22 17:38:00 +00:00
{
$form = $this->createForm(new DocumentType(), $document);
$request = $this->getRequest();
if ($request->getMethod() == 'POST')
{
$form->bindRequest($request);
if ($form->isValid())
{
/* TODO: move these lines into life cycle events */
2012-07-22 17:38:00 +00:00
$slugifier = new Slugifier();
$document->setSlug($slugifier->slugify($document->getTitle()));
// TODO: add updatedAt, updatedBy, updateReason, etc.
$em = $this->getDoctrine()->getEntityManager();
$em->persist($document);
$em->flush();
return $this->redirect($this->generateUrl('KekRozsakFrontBundle_documentView', array('slug' => $document->getSlug())));
2012-07-22 17:38:00 +00:00
}
}
return array(
'document' => $document,
'form' => $form->createView(),
);
}
}