Skip to content

Commit

Permalink
Adjust bunch of stuff, make forms work and such
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Jun 4, 2011
1 parent 6401be5 commit 0522854
Show file tree
Hide file tree
Showing 20 changed files with 208 additions and 157 deletions.
34 changes: 22 additions & 12 deletions BlogBundle/Controller/AdminPostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
use Symfony\Component\Security\SecurityContext;
use Whitewashing\Blog\Author;
use Whitewashing\Blog\WritePostProcess;
use Whitewashing\BlogBundle\Form\PostType;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
use Whitewashing\BlogBundle\Form\WritePostType;

class AdminPostController extends AbstractBlogController
{
public function indexAction()
{
return $this->render('WhitewashingBlogBundle:AdminPost:dashboard.html.twig', array(
'user' => $this->container->get('security.context')->getUser()
'user' => $this->container->get('security.context')->getToken()
));
}

Expand All @@ -35,40 +39,46 @@ public function manageAction()

public function newAction()
{
$currentUser = $this->container->get('security.context')->getUser();
if (!is_string($currentUser)) { // TODO: This allows anon users, should we?
$currentUser = $currentUser->getUsername();
$securityToken = $this->container->get('security.context')->getToken();
$author = null;
if ($securityToken->getUser() instanceof UserInterface) {
$username = $securityToken->getUser()->getUsername();
$author = $this->container->get('whitewashing.blog.authorservice')->findAuthorForUserAccount($username);
}
$author = $this->container->get('whitewashing.blog.authorservice')->findAuthorForUserAccount($currentUser);

if (!$author) {
throw new AccessDeniedException;
}

$blog = $this->container->get('whitewashing.blog.blogservice')->getCurrentBlog();

$post = new \Whitewashing\Blog\Post($author, $blog);

return $this->handleForm('WhitewashingBlogBundle:AdminPost:new.html.twig', $post);
return $this->handleForm('WhitewashingBlogBundle:AdminPost:new.html.twig', $post, true);
}

public function editAction($id)
{
$post = $this->container->get('whitewashing.blog.postservice')->findPost($id);

return $this->handleForm('WhitewashingBlogBundle:AdminPost:edit.html.twig', $post);
return $this->handleForm('WhitewashingBlogBundle:AdminPost:edit.html.twig', $post, false);
}

/**
* @param string $viewName
* @param Post $post
* @return \Symfony\Component\HttpKernel\Response
*/
private function handleForm($viewName, $post)
private function handleForm($viewName, $post, $allowChangeFormat)
{
$builder = $this->container->get('whitewashing.blog.bundle.formbuilder');
$factory = $this->container->get('form.factory');

$writePost = new WritePostProcess($post);
$form = $builder->createWritePostForm($writePost);
$form = $factory->create(new WritePostType($allowChangeFormat), $writePost, array('allow_change_format' => $allowChangeFormat));

if ($this->getRequest()->getMethod() == 'POST') {

$form->bind($this->getRequest()->get('writepost'), $writePost);
$form->bindRequest($this->getRequest());

if ($form->isValid()) {
$em = $this->container->get('doctrine.orm.default_entity_manager');
Expand All @@ -85,7 +95,7 @@ private function handleForm($viewName, $post)
}

return $this->render($viewName, array(
'writeForm' => $form,
'writeForm' => $form->createView(),
'post' => $post,
));
}
Expand Down
13 changes: 8 additions & 5 deletions BlogBundle/Controller/AuthorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Whitewashing\BlogBundle\Controller;

class AuthorController extends \Symfony\Bundle\FrameworkBundle\Controller\Controller
use Whitewashing\BlogBundle\Form\AuthorType;

class AuthorController extends AbstractBlogController
{
public function listAction()
{
Expand Down Expand Up @@ -47,11 +49,12 @@ public function userAction()
public function newAction()
{
$request = $this->container->get('request');
$newAuthorForm = $this->container->get('whitewashing.blog.bundle.formbuilder')->createNewAuthorForm();

$author = new \Whitewashing\Blog\Author();
$newAuthorForm = $this->container->get('form.factory')->create(new AuthorType(), $author);

if ($request->getMethod() == 'POST') {
$author = new \Whitewashing\Blog\Author();
$newAuthorForm->bind($request, $author);
$newAuthorForm->bindRequest($request);

if ($newAuthorForm->isValid()) {
$em = $this->container->get('doctrine.orm.default_entity_manager');
Expand All @@ -63,7 +66,7 @@ public function newAction()
}

return $this->render('WhitewashingBlogBundle:Author:new.html.twig', array(
'newAuthorForm' => $newAuthorForm
'newAuthorForm' => $newAuthorForm->createView()
));
}
}
4 changes: 4 additions & 0 deletions BlogBundle/DependencyInjection/WhitewashingBlogExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

class WhitewashingBlogExtension extends Extension
{
/**
* @param array $configs
* @param ContainerBuilder $configuration
*/
public function load(array $configs, ContainerBuilder $configuration)
{
foreach ($configs AS $config) {
Expand Down
33 changes: 33 additions & 0 deletions BlogBundle/Form/AuthorType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Whitewashing
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/

namespace Whitewashing\BlogBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Whitewashing\Blog\Post;

class AuthorType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('username', 'text');
$builder->add('email', 'text');
$builder->add('name', 'text');
}

public function getDefaultOptions(array $options)
{
return array();
}
}
77 changes: 0 additions & 77 deletions BlogBundle/Form/BlogFormBuilder.php

This file was deleted.

42 changes: 42 additions & 0 deletions BlogBundle/Form/PostType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Whitewashing
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/

namespace Whitewashing\BlogBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Whitewashing\Blog\Post;

class PostType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
if ($options['allow_change_format'] === true) {
$builder->add('inputFormat', 'choice', array(
'choices' => array(
'rst' => 'ReStructured Text',
'html' => 'HTML',
),
));
}
$builder->add('headline', 'text');
$builder->add('text', 'textarea');
}

public function getDefaultOptions(array $options)
{
return array(
'allow_change_format' => false
);
}
}
44 changes: 44 additions & 0 deletions BlogBundle/Form/WritePostType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Whitewashing
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to kontakt@beberlei.de so I can send you a copy immediately.
*/

namespace Whitewashing\BlogBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Whitewashing\Blog\Post;

class WritePostType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$postBuilder = $builder->create('post', new PostType(), array('allow_change_format' => $options['allow_change_format']));

foreach ($postBuilder->getTypes() AS $type) {
$builder->add('post', $type);
}
$builder->add('publishStatus', 'choice', array(
'choices' => array(
Post::STATUS_DRAFT => 'Draft',
Post::STATUS_PUBLISHED => 'Published',
)
));
$builder->add('tags', 'text');
}

public function getDefaultOptions(array $options)
{
return array(
'allow_change_format' => false
);
}
}
13 changes: 13 additions & 0 deletions BlogBundle/Resources/config/doctrine/User.orm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

<entity name="Whitewashing\Blog\User" table="blog_users">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
</entity>

</doctrine-mapping>

This file was deleted.

2 changes: 1 addition & 1 deletion BlogBundle/Resources/views/AdminPost/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<fieldset>
<legend>Edit Post</legend>
{{ form_field(writeForm) }}
{{ form_widget(writeForm) }}
</fieldset>

<input type="submit" name="submit_thenedit" value="Save" />
Expand Down
2 changes: 1 addition & 1 deletion BlogBundle/Resources/views/AdminPost/new.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{% block content %}
<form id="writepost" class="wwform" method="post" action="{{ path('blog_post_new') }}">
{{ form_field(writeForm) }}
{{ form_widget(writeForm) }}
<input type="submit" name="submit_thenedit" value="Save" />
<input type="submit" name="submit_thenshow" value="Save and Show" />
</form>
Expand Down
2 changes: 1 addition & 1 deletion BlogBundle/Resources/views/Author/new.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ is only a user, he can only edit and delete his own posts. Please look at your S
to learn how to set groups for users.</p>

<form id="add_author" class="wwform" method="post" action="{{ path('blog_new_author') }}">
{{ form_field(newAuthorForm) }}
{{ form_widget(newAuthorForm) }}
<input type="submit" name="submit_add_author" value="Add Author" />
</form>

Expand Down
Loading

0 comments on commit 0522854

Please sign in to comment.