Revised Document stuff (Controller and templates)

Signed-off-by: Polonkai Gergely <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely 2012-07-31 09:47:50 +02:00
parent 067a9fae97
commit a43f2d6017
4 changed files with 18 additions and 30 deletions

View File

@ -5,6 +5,7 @@ namespace KekRozsak\FrontBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use KekRozsak\FrontBundle\Entity\Document; use KekRozsak\FrontBundle\Entity\Document;
use KekRozsak\FrontBundle\Form\Type\DocumentType; use KekRozsak\FrontBundle\Form\Type\DocumentType;
@ -13,30 +14,23 @@ use KekRozsak\FrontBundle\Extensions\Slugifier;
class DocumentController extends Controller class DocumentController extends Controller
{ {
/** /**
* @Route("/dokumentum/{documentSlug}", name="KekRozsakFrontBundle_documentView") * @Route("/dokumentum/{slug}", name="KekRozsakFrontBundle_documentView")
* @Template() * @Template()
* @ParamConverter("document")
*/ */
public function viewAction($documentSlug) public function viewAction(Document $document)
{ {
$docRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:Document');
if (!($document = $docRepo->findOneBySlug($documentSlug)))
throw $this->createNotFoundException('A kért dokumentum nem létezik!');
return array( return array(
'document' => $document, 'document' => $document,
); );
} }
/** /**
* @Route("/dokumentumok/uj/{groupSlug}", name="KekRozsakFrontBundle_documentCreate", defaults={"groupslug"=""}) * @Route("/dokumentumok/uj/", name="KekRozsakFrontBundle_documentCreate")
* @Template() * @Template()
*/ */
public function createAction($groupSlug = '') public function createAction()
{ {
$groupRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:Group');
$group = $groupRepo->findOneBySlug($groupSlug);
/* TODO: make group fully optional */
$document = new Document(); $document = new Document();
$document->setSlug('n-a'); $document->setSlug('n-a');
$form = $this->createForm(new DocumentType(), $document); $form = $this->createForm(new DocumentType(), $document);
@ -48,41 +42,32 @@ class DocumentController extends Controller
if ($form->isValid()) if ($form->isValid())
{ {
/* TODO: move these lines into life cycle events */
$slugifier = new Slugifier(); $slugifier = new Slugifier();
$document->setSlug($slugifier->slugify($document->getTitle())); $document->setSlug($slugifier->slugify($document->getTitle()));
$document->setCreatedAt(new \DateTime('now')); $document->setCreatedAt(new \DateTime('now'));
$document->setCreatedBy($this->get('security.context')->getToken()->getUser()); $document->setCreatedBy($this->get('security.context')->getToken()->getUser());
if ($group)
{
$group->addDocument($document);
$document->addGroup($group);
}
$em = $this->getDoctrine()->getEntityManager(); $em = $this->getDoctrine()->getEntityManager();
$em->persist($document); $em->persist($document);
$em->flush(); $em->flush();
/* TODO: return to group list if groupSlug is empty! */ return $this->redirect($this->generateUrl('KekRozsakFrontBundle_documentView', array('slug' => $document->getSlug())));
return $this->redirect($this->generateUrl('KekRozsakFrontBundle_groupDocuments', array('groupSlug' => $group->getSlug())));
} }
} }
return array( return array(
'defaultGroup' => $groupSlug,
'form' => $form->createView(), 'form' => $form->createView(),
); );
} }
/** /**
* @Route("/dokumentum/{documentSlug}/szerkesztes", name="KekRozsakFrontBundle_documentEdit") * @Route("/dokumentum/{slug}/szerkesztes", name="KekRozsakFrontBundle_documentEdit")
* @Template() * @Template()
* @ParamConverter("document")
*/ */
public function editAction($documentSlug) public function editAction(Document $document)
{ {
$documentRepo = $this->getDoctrine()->getRepository('KekRozsakFrontBundle:Document');
if (!($document = $documentRepo->findOneBySlug($documentSlug)))
throw $this->createNotFoundException('A kért dokumentum nem létezik!');
$form = $this->createForm(new DocumentType(), $document); $form = $this->createForm(new DocumentType(), $document);
$request = $this->getRequest(); $request = $this->getRequest();
@ -91,6 +76,7 @@ class DocumentController extends Controller
$form->bindRequest($request); $form->bindRequest($request);
if ($form->isValid()) if ($form->isValid())
{ {
/* TODO: move these lines into life cycle events */
$slugifier = new Slugifier(); $slugifier = new Slugifier();
$document->setSlug($slugifier->slugify($document->getTitle())); $document->setSlug($slugifier->slugify($document->getTitle()));
// TODO: add updatedAt, updatedBy, updateReason, etc. // TODO: add updatedAt, updatedBy, updateReason, etc.
@ -99,7 +85,7 @@ class DocumentController extends Controller
$em->persist($document); $em->persist($document);
$em->flush(); $em->flush();
return $this->redirect($this->generateUrl('KekRozsakFrontBundle_documentView', array('documentSlug' => $document->getSlug()))); return $this->redirect($this->generateUrl('KekRozsakFrontBundle_documentView', array('slug' => $document->getSlug())));
} }
} }

View File

@ -2,7 +2,7 @@
{% block title %} - Dokumentum létrehozása{% endblock %} {% block title %} - Dokumentum létrehozása{% endblock %}
{% block content %} {% block content %}
<h3>Új dokumentum létrehozása</h3> <h3>Új dokumentum létrehozása</h3>
<form method="post" action="{{ path('KekRozsakFrontBundle_documentCreate', { groupSlug: defaultGroup }) }}"> <form method="post" action="{{ path('KekRozsakFrontBundle_documentCreate') }}">
{{ form_widget(form) }} {{ form_widget(form) }}
<button type="submit">Mentés</button> <button type="submit">Mentés</button>
</form> </form>

View File

@ -1,8 +1,10 @@
{# vim: ft=htmljinja
#}
{% extends '::main_template.html.twig' %} {% extends '::main_template.html.twig' %}
{% block title %} - Dokumentum szerkesztése - {{ document.title }}{% endblock %} {% block title %} - Dokumentum szerkesztése - {{ document.title }}{% endblock %}
{% block content %} {% block content %}
<h3>Dokumentum szerkesztése - {{ document.title }}</h3> <h3>Dokumentum szerkesztése - {{ document.title }}</h3>
<form method="post" action="{{ path('KekRozsakFrontBundle_documentEdit', { documentSlug: document.slug }) }}"> <form method="post" action="{{ path('KekRozsakFrontBundle_documentEdit', {slug: document.slug}) }}">
{{ form_widget(form) }} {{ form_widget(form) }}
<button type="submit">Mentés</button> <button type="submit">Mentés</button>
</form> </form>

View File

@ -2,7 +2,7 @@
{% block title %} - Dokumentum - {{ document.title }}{% endblock %} {% block title %} - Dokumentum - {{ document.title }}{% endblock %}
{% block content %} {% block content %}
<h3> <h3>
{{ document.title }}{% if document.createdBy == app.user %} [ <a href="{{ path('KekRozsakFrontBundle_documentEdit', { documentSlug: document.slug }) }}">Szerkesztés</a> ] {% endif %} {{ document.title }}{% if document.createdBy == app.user %} [ <a href="{{ path('KekRozsakFrontBundle_documentEdit', {slug: document.slug}) }}">Szerkesztés</a> ] {% endif %}
</h3> </h3>
{{ document.content|raw }} {{ document.content|raw }}
<div class="szerzo">{{ document.createdBy.displayName }}</div> <div class="szerzo">{{ document.createdBy.displayName }}</div>