Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Admin/PostAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ protected function configureFormFields(FormMapper $formMapper)
->add('title')
->add('slug')
->add('text')
->add('tags', 'tags')
;
->add('tags', 'tags');
}

protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('slug')
->add('title')
->add('created')
;
->add('created');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Stfalcon\Bundle\BlogBundle\Bridge\Doctrine\Form\DataTransformer;

use Doctrine\ORM\EntityManager;
use Stfalcon\Bundle\BlogBundle\Entity\TagManager;
use Symfony\Component\Form\DataTransformerInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
Expand All @@ -16,20 +18,22 @@ class EntitiesToStringTransformer implements DataTransformerInterface
{

/**
* @var Doctrine\ORM\EntityManager
* @var EntityManager $em
*/
protected $em;
/** @var string $class */
protected $class;

/**
* Constructor injection. Set entity manager to object
* Constructor injection
*
* @param Doctrine\ORM\EntityManager $em Entity manager object
*
* @return void
* @param EntityManager $em
* @param string $class
*/
public function __construct(\Doctrine\ORM\EntityManager $em)
public function __construct(EntityManager $em, $class)
{
$this->em = $em;
$this->class = $class;
}

/**
Expand All @@ -38,6 +42,8 @@ public function __construct(\Doctrine\ORM\EntityManager $em)
* @param Collection|null $collection A collection of entities or NULL
*
* @return string|null An string of tags or NULL
*
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function transform($collection)
{
Expand All @@ -51,7 +57,7 @@ public function transform($collection)

$array = array();
foreach ($collection as $entity) {
array_push($array, $entity->getText());
$array[] = $entity->getText();
}

return implode(', ', $array);
Expand All @@ -63,6 +69,8 @@ public function transform($collection)
* @param string|null $data Input string data
*
* @return Collection|null
*
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function reverseTransform($data)
{
Expand All @@ -75,12 +83,11 @@ public function reverseTransform($data)
if (!is_string($data)) {
throw new UnexpectedTypeException($data, 'string');
}

$repository = $this->em->getRepository($this->class);
foreach ($this->_stringToArray($data) as $text) {
$tag = $this->em->getRepository("StfalconBlogBundle:Tag")
->findOneBy(array('text' => $text));
$tag = $repository->findOneBy(array('text' => $text));
if (!$tag) {
$tag = new \Stfalcon\Bundle\BlogBundle\Entity\Tag($text);
$tag = new $this->class($text);
$this->em->persist($tag);
}
$collection->add($tag);
Expand All @@ -93,6 +100,8 @@ public function reverseTransform($data)
* Convert string of tags to array
*
* @param string $string
*
* @return array
*/
private function _stringToArray($string)
{
Expand Down
21 changes: 12 additions & 9 deletions Bridge/Doctrine/Form/Type/TagsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Stfalcon\Bundle\BlogBundle\Bridge\Doctrine\Form\Type;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\RegistryInterface;
Expand All @@ -14,19 +15,21 @@
*/
class TagsType extends AbstractType
{

protected $registry;
/** @var EntityManager $em */
protected $em;
/** @var string $class */
protected $class;

/**
* Constructor injection
*
* @param RegistryInterface $registry Doctrine registry object
*
* @return void
* @param EntityManager $em
* @param string $class
*/
public function __construct(RegistryInterface $registry)
public function __construct(EntityManager $em, $class)
{
$this->registry = $registry;
$this->em = $em;
$this->class = $class;
}

/**
Expand All @@ -39,8 +42,8 @@ public function __construct(RegistryInterface $registry)
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->prependClientTransformer(
new EntitiesToStringTransformer($this->registry->getEntityManager())
$builder->addModelTransformer(
new EntitiesToStringTransformer($this->em, $this->class)
);
}

Expand Down
30 changes: 0 additions & 30 deletions Controller/AbstractController.php

This file was deleted.

13 changes: 10 additions & 3 deletions Controller/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Stfalcon\Bundle\BlogBundle\Entity\Post;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Controller for actions with DISQUS comments
Expand All @@ -18,16 +19,22 @@ class CommentController extends Controller
/**
* Synchronization comments count with disqus
*
* @param Post $post Post object
* @param string $slug Slug of post
*
* @return Response
* @Route("/blog/post/{slug}/disqus-sync", name="blog_post_disqus_sync")
*
* @throws NotFoundHttpException
*/
public function disqusSyncAction(Post $post)
public function disqusSyncAction($slug)
{
// @todo. нужно доставать полный список ЗААПРУВЛЕННЫХ комментариев или
// колличество комментариев к записи (если такой метод появится в API disqus)
// после чего обновлять их колличество в БД
$post = $this->get('stfalcon_blog.post.repository')->findOneBy(array('slug' => $slug));
if (!$post) {
throw new NotFoundHttpException();
}

$post->setCommentsCount($post->getCommentsCount() + 1);
$em = $this->get('doctrine.orm.entity_manager');
Expand Down
65 changes: 29 additions & 36 deletions Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,44 @@
namespace Stfalcon\Bundle\BlogBundle\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Stfalcon\Bundle\BlogBundle\Entity\Post;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* PostController
*
* @author Stepan Tanasiychuk <ceo@stfalcon.com>
*/
class PostController extends AbstractController
class PostController extends Controller
{

private function _getRequestArrayWithDisqusShortname($array)
{
$config = $this->container->getParameter('stfalcon_blog.config');
return array_merge(
$array,
array('disqus_shortname' => $config['disqus_shortname'])
);
}

/**
* List of posts for admin
*
* @param int $page Page number
*
* @return array
*
* @Route("/blog/{title}/{page}", name="blog",
* requirements={"page"="\d+", "title"="page"},
* defaults={"page"="1", "title"="page"})
* @Template()
*
* @param int $page Page number
*
* @return array
*/
public function indexAction($page)
{
$allPosts = $this->get('doctrine')->getEntityManager()
->getRepository("StfalconBlogBundle:Post")->getAllPosts();
$posts= $this->get('knp_paginator')->paginate($allPosts, $page, 10);
$allPostsQuery = $this->get('stfalcon_blog.post.repository')->findAllPostsAsQuery();
$posts= $this->get('knp_paginator')->paginate($allPostsQuery, $page, 10);

if ($this->has('application_default.menu.breadcrumbs')) {
$breadcrumbs = $this->get('application_default.menu.breadcrumbs');
$breadcrumbs->addChild('Блог')->setCurrent(true);
}

return $this->_getRequestArrayWithDisqusShortname(array(
'posts' => $posts
));
return array(
'posts' => $posts,
'disqus_shortname' => $this->container->getParameter('stfalcon_blog.disqus_shortname')
);
}

/**
Expand All @@ -59,21 +49,28 @@ public function indexAction($page)
* @Route("/blog/post/{slug}", name="blog_post_view")
* @Template()
*
* @param Post $post
* @param string $slug
*
* @return array
*
* @throws NotFoundHttpException
*/
public function viewAction(Post $post)
public function viewAction($slug)
{
$post = $this->get('stfalcon_blog.post.repository')->findOneBy(array('slug' => $slug));
if (!$post) {
throw new NotFoundHttpException();
}
if ($this->has('application_default.menu.breadcrumbs')) {
$breadcrumbs = $this->get('application_default.menu.breadcrumbs');
$breadcrumbs->addChild('Блог', array('route' => 'blog'));
$breadcrumbs->addChild($post->getTitle())->setCurrent(true);
}

return $this->_getRequestArrayWithDisqusShortname(array(
'post' => $post
));
return array(
'post' => $post,
'disqus_shortname' => $this->container->getParameter('stfalcon_blog.disqus_shortname')
);
}

/**
Expand All @@ -87,14 +84,11 @@ public function rssAction()
{
$feed = new \Zend\Feed\Writer\Feed();

$config = $this->container->getParameter('stfalcon_blog.config');

$feed->setTitle($config['rss']['title']);
$feed->setDescription($config['rss']['description']);
$feed->setTitle($this->container->getParameter('stfalcon_blog.rss.title'));
$feed->setDescription($this->container->getParameter('stfalcon_blog.rss.description'));
$feed->setLink($this->generateUrl('blog_rss', array(), true));

$posts = $this->get('doctrine')->getEntityManager()
->getRepository("StfalconBlogBundle:Post")->getAllPosts();
$posts = $this->get('stfalcon_blog.post.repository')->findAllPosts();
foreach ($posts as $post) {
$entry = new \Zend\Feed\Writer\Entry();
$entry->setTitle($post->getTitle());
Expand All @@ -117,8 +111,7 @@ public function rssAction()
*/
public function lastAction($count = 1)
{
$posts = $this->get('doctrine')->getEntityManager()
->getRepository("StfalconBlogBundle:Post")->getLastPosts($count);
$posts = $this->get('stfalcon_blog.post.repository')->findLastPosts($count);

return array('posts' => $posts);
}
Expand Down
Loading