Added blog post editing function

Signed-off-by: Gergely Polonkai (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Gergely Polonkai (W00d5t0ck) 2012-09-05 13:18:18 +02:00
parent a41ddec070
commit 773ea55e3d
3 changed files with 60 additions and 5 deletions

View File

@ -50,12 +50,19 @@ class AdminController extends Controller
} }
/** /**
* @Route("/blog/post", name="GergelyPolonkaiFrontBundle_adminNewBlogPost") * @Route("/blog/post/{id}", name="GergelyPolonkaiFrontBundle_adminEditBlogPost", defaults={"id": null})
* @Template * @Template
*/ */
public function newBlogPostAction() public function newBlogPostAction($id = null)
{ {
$post = new Post(); if (is_numeric($id)) {
$post = $this->getDoctrine()->getRepository('GergelyPolonkaiFrontBundle:Post')->findOneById($id);
if ($post === null) {
throw $this->createNotFoundException();
}
} else {
$post = new Post();
}
$form = $this->createForm(new PostType(), $post); $form = $this->createForm(new PostType(), $post);
$request = $this->getRequest(); $request = $this->getRequest();
$user = $this->get('security.context')->getToken()->getUser(); $user = $this->get('security.context')->getToken()->getUser();
@ -68,12 +75,28 @@ class AdminController extends Controller
$em->persist($post); $em->persist($post);
$em->flush(); $em->flush();
return $this->redirect($this->generateUrl('GergelyPolonkaiFrontBundle_adminNewBlogPost')); return $this->redirect($this->generateUrl('GergelyPolonkaiFrontBundle_adminBlogList'));
} }
} }
return array( return array(
'form' => $form->createView(), 'form' => $form->createView(),
'post' => $post,
);
}
/**
* @Route("/blog/", name="GergelyPolonkaiFrontBundle_adminBlogList")
* @Template
*/
public function listBlogAction()
{
$postRepo = $this->getDoctrine()->getRepository('GergelyPolonkaiFrontBundle:Post');
$posts = $postRepo->findBy(array(), array('createdAt' => 'DESC'));
return array(
'posts' => $posts,
); );
} }
} }

View File

@ -0,0 +1,27 @@
{% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %}
{% block content %}
<h3>All posts</h3>
<a href="{{ path('GergelyPolonkaiFrontBundle_adminEditBlogPost') }}">New post</a>
<table>
<thead>
<tr>
<td>Title (click to edit)</td>
<td>Author</td>
<td>Created at</td>
</tr>
</thead>
{% if posts %}
<tbody>
{% for post in posts %}
<tr>
<td><a href="{{ path('GergelyPolonkaiFrontBundle_adminEditBlogPost', {id: post.id}) }}">{{ post.title }}</a></td>
<td>{{ post.user.name }}</td>
<td>{{ post.createdAt|date('m/d/Y') }}</td>
</tr>
{% endfor %}
</tbody>
{% endif %}
</table>
<a href="{{ path('GergelyPolonkaiFrontBundle_adminEditBlogPost') }}">New post</a>
{% endblock %}

View File

@ -1,8 +1,13 @@
{% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %} {% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %}
{% block content %} {% block content %}
<form method="post" action="{{ path('GergelyPolonkaiFrontBundle_adminNewBlogPost') }}"> {% if post.id is not null %}
<form method="post" action="{{ path('GergelyPolonkaiFrontBundle_adminEditBlogPost', {id: post.id}) }}">
{% else %}
<form method="post" action="{{ path('GergelyPolonkaiFrontBundle_adminEditBlogPost') }}">
{% endif %}
{{ form_widget(form) }} {{ form_widget(form) }}
<button type="submit">Save</button> <button type="submit">Save</button>
<a href="{{ path('GergelyPolonkaiFrontBundle_adminBlogList') }}">Cancel</a>
</form> </form>
{% endblock content %} {% endblock content %}