From 7a254af1a9f2749c239a893cc036509d1da813c3 Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Wed, 5 Sep 2012 15:14:01 +0200 Subject: [PATCH] Added chunk editor Signed-off-by: Gergely Polonkai (W00d5t0ck) --- .../Version20120905131921.php | 2 +- .../Controller/AdminController.php | 52 +++++++++++++++++++ .../FrontBundle/Form/CodeChunkType.php | 32 ++++++++++++ .../FrontBundle/Form/PostType.php | 2 +- .../Resources/views/Admin/editChunk.html.twig | 14 +++++ .../views/Admin/listCodeChunk.html.twig | 27 ++++++++++ 6 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 src/GergelyPolonkai/FrontBundle/Form/CodeChunkType.php create mode 100644 src/GergelyPolonkai/FrontBundle/Resources/views/Admin/editChunk.html.twig create mode 100644 src/GergelyPolonkai/FrontBundle/Resources/views/Admin/listCodeChunk.html.twig diff --git a/app/DoctrineMigrations/Version20120905131921.php b/app/DoctrineMigrations/Version20120905131921.php index 6307908..6aa560c 100644 --- a/app/DoctrineMigrations/Version20120905131921.php +++ b/app/DoctrineMigrations/Version20120905131921.php @@ -16,7 +16,7 @@ class Version20120905131921 extends AbstractMigration $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); $this->addSql("ALTER TABLE blog_posts ADD draft TINYINT(1) NOT NULL"); - $this->addSql("UPDATE blog_posts SET draft = 0"); + $this->addSql("UPDATE blog_posts SET draft = 0"); } public function down(Schema $schema) diff --git a/src/GergelyPolonkai/FrontBundle/Controller/AdminController.php b/src/GergelyPolonkai/FrontBundle/Controller/AdminController.php index d6f6f49..3827f75 100644 --- a/src/GergelyPolonkai/FrontBundle/Controller/AdminController.php +++ b/src/GergelyPolonkai/FrontBundle/Controller/AdminController.php @@ -7,7 +7,9 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Symfony\Component\Security\Core\SecurityContext; use GergelyPolonkai\FrontBundle\Form\PostType; +use GergelyPolonkai\FrontBundle\Form\CodeChunkType; use GergelyPolonkai\FrontBundle\Entity\Post; +use GergelyPolonkai\FrontBundle\Entity\CodeChunk; /** * Description of AdminController @@ -100,4 +102,54 @@ class AdminController extends Controller 'posts' => $posts, ); } + + /** + * @Route("/code-chunk/", name="GergelyPolonkaiFrontBundle_adminListChunk") + * @Template + */ + public function listCodeChunkAction() + { + $chunkRepo = $this->getDoctrine()->getRepository('GergelyPolonkaiFrontBundle:CodeChunk'); + $chunks = $chunkRepo->findBy(array(), array('title' => 'ASC')); + + return array( + 'chunks' => $chunks, + ); + } + + /** + * @Route("/code-chunk/edit/{id}", name="GergelyPolonkaiFrontBundle_adminEditChunk", defaults={"id": null}) + * @Template + */ + public function editChunkAction($id) + { + if (is_numeric($id)) { + $chunk = $this->getDoctrine()->getRepository('GergelyPolonkaiFrontBundle:CodeChunk')->findOneById($id); + if ($chunk === null) { + throw $this->createNotFoundException(); + } + } else { + $chunk = new CodeChunk(); + } + + $form = $this->createForm(new CodeChunkType(), $chunk); + $request = $this->getRequest(); + $user = $this->get('security.context')->getToken()->getUser(); + + if ($request->getMethod() === 'POST') { + $form->bind($request); + if ($form->isValid()) { + $em = $this->getDoctrine()->getEntityManager(); + $em->persist($chunk); + $em->flush(); + + return $this->redirect($this->generateUrl('GergelyPolonkaiFrontBundle_adminListChunk')); + } + } + + return array( + 'form' => $form->createView(), + 'chunk' => $chunk, + ); + } } diff --git a/src/GergelyPolonkai/FrontBundle/Form/CodeChunkType.php b/src/GergelyPolonkai/FrontBundle/Form/CodeChunkType.php new file mode 100644 index 0000000..fb32d44 --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Form/CodeChunkType.php @@ -0,0 +1,32 @@ +add('language') + ->add('title') + ->add('content') + ->add('description', 'ckeditor') + ; + } + + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'GergelyPolonkai\FrontBundle\Entity\CodeChunk' + )); + } + + public function getName() + { + return 'gergelypolonkai_frontbundle_codechunktype'; + } +} diff --git a/src/GergelyPolonkai/FrontBundle/Form/PostType.php b/src/GergelyPolonkai/FrontBundle/Form/PostType.php index 2e97aac..2f7e7d6 100644 --- a/src/GergelyPolonkai/FrontBundle/Form/PostType.php +++ b/src/GergelyPolonkai/FrontBundle/Form/PostType.php @@ -12,7 +12,7 @@ class PostType extends AbstractType { $builder ->add('title') - ->add('draft') + ->add('draft', null, array('required' => false)) ->add('content', 'ckeditor') ; } diff --git a/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/editChunk.html.twig b/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/editChunk.html.twig new file mode 100644 index 0000000..70ca318 --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/editChunk.html.twig @@ -0,0 +1,14 @@ +{% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %} + +{% block content %} +{% if chunk.id is not null %} +

Edit Code Chunk - {{ chunk.title }}

+{% else %} +

Create Code Chunk

+{% endif %} +
+{{ form_widget(form) }} + + Cancel +
+{% endblock content %} diff --git a/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/listCodeChunk.html.twig b/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/listCodeChunk.html.twig new file mode 100644 index 0000000..7593c9a --- /dev/null +++ b/src/GergelyPolonkai/FrontBundle/Resources/views/Admin/listCodeChunk.html.twig @@ -0,0 +1,27 @@ +{% extends 'GergelyPolonkaiFrontBundle:Default:front_base.html.twig' %} + +{% block content %} +

Code chunks

+New Code Chunk +{% if chunks %} + + + + + + + + + +{% for chunk in chunks %} + + + + + +{% endfor %} + +
TitleLanguageCode
{{ chunk.title }}{{ chunk.language }}
+{% endif %} +New Code Chunk +{% endblock content %}