From 870aba4ea938d8bed4491d531334830f824db9ef Mon Sep 17 00:00:00 2001 From: Nek Date: Thu, 12 Jul 2012 14:23:10 +0200 Subject: [PATCH 01/20] Added Tag entities --- Entity/Tag.php | 10 +++++++++ Entity/Tagging.php | 10 +++++++++ Resources/config/doctrine/Tag.orm.xml | 17 +++++++++++++++ Resources/config/doctrine/Tagging.orm.xml | 25 +++++++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 Entity/Tag.php create mode 100644 Entity/Tagging.php create mode 100644 Resources/config/doctrine/Tag.orm.xml create mode 100644 Resources/config/doctrine/Tagging.orm.xml diff --git a/Entity/Tag.php b/Entity/Tag.php new file mode 100644 index 0000000..18af0a6 --- /dev/null +++ b/Entity/Tag.php @@ -0,0 +1,10 @@ + + + + + + + + + + + + + + diff --git a/Resources/config/doctrine/Tagging.orm.xml b/Resources/config/doctrine/Tagging.orm.xml new file mode 100644 index 0000000..b4448be --- /dev/null +++ b/Resources/config/doctrine/Tagging.orm.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + From 2f7cdfafbf9bda474f754219e2cc415501940465 Mon Sep 17 00:00:00 2001 From: Nek Date: Thu, 12 Jul 2012 14:30:06 +0200 Subject: [PATCH 02/20] Fixed tag entities configuration --- Resources/config/doctrine/Tag.orm.xml | 4 ++-- Resources/config/doctrine/Tagging.orm.xml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Resources/config/doctrine/Tag.orm.xml b/Resources/config/doctrine/Tag.orm.xml index 644313d..5c33129 100644 --- a/Resources/config/doctrine/Tag.orm.xml +++ b/Resources/config/doctrine/Tag.orm.xml @@ -4,13 +4,13 @@ xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> - + - + diff --git a/Resources/config/doctrine/Tagging.orm.xml b/Resources/config/doctrine/Tagging.orm.xml index b4448be..33a6479 100644 --- a/Resources/config/doctrine/Tagging.orm.xml +++ b/Resources/config/doctrine/Tagging.orm.xml @@ -4,13 +4,13 @@ xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> - + - + From e2d584c1caa3ae99c1f52305d2a5f405b0f8b52d Mon Sep 17 00:00:00 2001 From: Nek Date: Thu, 12 Jul 2012 15:58:10 +0200 Subject: [PATCH 03/20] Deletion of tag entities. Checkout the add-tag branch --- Entity/Tag.php | 10 --------- Entity/Tagging.php | 10 --------- Resources/config/doctrine/Tag.orm.xml | 17 --------------- Resources/config/doctrine/Tagging.orm.xml | 25 ----------------------- 4 files changed, 62 deletions(-) delete mode 100644 Entity/Tag.php delete mode 100644 Entity/Tagging.php delete mode 100644 Resources/config/doctrine/Tag.orm.xml delete mode 100644 Resources/config/doctrine/Tagging.orm.xml diff --git a/Entity/Tag.php b/Entity/Tag.php deleted file mode 100644 index 18af0a6..0000000 --- a/Entity/Tag.php +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - - - - - diff --git a/Resources/config/doctrine/Tagging.orm.xml b/Resources/config/doctrine/Tagging.orm.xml deleted file mode 100644 index 33a6479..0000000 --- a/Resources/config/doctrine/Tagging.orm.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - From 8c98278a361d47edf47dd98f33d803e1d0a8b3c7 Mon Sep 17 00:00:00 2001 From: Nek Date: Thu, 12 Jul 2012 15:59:50 +0200 Subject: [PATCH 04/20] Added tag entities --- Controller/Tag.php | 10 +++++++++ Controller/Tagging.php | 10 +++++++++ Resources/config/doctrine/Tag.orm.xml | 17 +++++++++++++++ Resources/config/doctrine/Tagging.orm.xml | 25 +++++++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 Controller/Tag.php create mode 100644 Controller/Tagging.php create mode 100644 Resources/config/doctrine/Tag.orm.xml create mode 100644 Resources/config/doctrine/Tagging.orm.xml diff --git a/Controller/Tag.php b/Controller/Tag.php new file mode 100644 index 0000000..18af0a6 --- /dev/null +++ b/Controller/Tag.php @@ -0,0 +1,10 @@ + + + + + + + + + + + + + + diff --git a/Resources/config/doctrine/Tagging.orm.xml b/Resources/config/doctrine/Tagging.orm.xml new file mode 100644 index 0000000..33a6479 --- /dev/null +++ b/Resources/config/doctrine/Tagging.orm.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + From 0744fb66b7495bc1c7b71a8126f38e0f69de5dcd Mon Sep 17 00:00:00 2001 From: Nek Date: Thu, 12 Jul 2012 16:00:55 +0200 Subject: [PATCH 05/20] Fixed bad file localization --- {Controller => Entity}/Tag.php | 0 {Controller => Entity}/Tagging.php | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {Controller => Entity}/Tag.php (100%) rename {Controller => Entity}/Tagging.php (100%) diff --git a/Controller/Tag.php b/Entity/Tag.php similarity index 100% rename from Controller/Tag.php rename to Entity/Tag.php diff --git a/Controller/Tagging.php b/Entity/Tagging.php similarity index 100% rename from Controller/Tagging.php rename to Entity/Tagging.php From 4a7b06e8391bb980c065665d40b2fa50f9040e6a Mon Sep 17 00:00:00 2001 From: Nek Date: Thu, 12 Jul 2012 17:37:09 +0200 Subject: [PATCH 06/20] Added global tag support to the node handler --- Form/Handler/Node.php | 80 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/Form/Handler/Node.php b/Form/Handler/Node.php index aaae3c4..a57f9f6 100644 --- a/Form/Handler/Node.php +++ b/Form/Handler/Node.php @@ -7,7 +7,9 @@ use Symfony\Component\HttpFoundation\Request, Symfony\Component\Form\FormFactory, - Symfony\Component\Form\Form; + Symfony\Component\Form\Form, + FPN\TagBundle\Entity\TagManager, + DoctrineExtensions\Taggable\Taggable; use Soloist\Bundle\CoreBundle\Node\Factory as NodeFactory, Soloist\Bundle\CoreBundle\Entity\Node as NodeEntity, @@ -31,20 +33,40 @@ class Node */ protected $nodeFactory; - public function __construct(NodeFactory $nodeFactory, EntityManager $em, FormFactory $factory) + /** + * @var FPN\TagBundle\Entity\TagManager + */ + protected $tagManager; + + public function __construct(NodeFactory $nodeFactory, EntityManager $em, FormFactory $factory, TagManager $tagManager) { $this->em = $em; $this->factory = $factory; $this->nodeFactory = $nodeFactory; + $this->tagManager = $tagManager; } public function create(Form $form, Request $request) { $form->bindRequest($request); + if ($form->isValid()) { - $this->em->persist($form->getData()); + $data = $form->getData(); + + if ($data instanceof Taggable) { + $tags = explode(',', $data->tagsAsText); + $tags = $this->trimTags($tags); + foreach ($tags as $tag) { + $currentTag = $this->tagManager->loadOrCreateTag($tag); + $this->tagManager->addTag($currentTag, $data); + } + } + + $this->em->persist($data); $this->em->flush(); + $tagManager->saveTagging($data); + return true; } @@ -53,8 +75,38 @@ public function create(Form $form, Request $request) public function update(Form $form, Request $request) { + $data = $form->getData(); + $oldTags = null; + if ($data instanceof Taggable) { + $oldTags = explode(',', $data->tagsAsText); + } + $form->bindRequest($request); if ($form->isValid()) { + + // if entity is taggable, we should remove old tags + // and add new + if ($data instanceof Taggable) { + $tags = explode(',', $data->tagsAsText); + $oldTags = $this->trimTags($oldTags); + $Tags = $this->trimTags($Tags); + + foreach ($tags as $key => $tag) { + if(!in_array($tag, $oldTags)) { + $currentTag = $this->tagManager->loadOrCreateTag($tag); + $this->tagManager->addTag($currentTag, $data); + + } else { + unset($oldTags[$key]); + } + } + + foreach ($oldTags as $tag) { + $currentTag = $this->tagManager->loadOrCreateTag($tag); + $this->tagManager->removeTag($currentTag, $data); + } + } + $this->em->flush(); return true; @@ -66,6 +118,18 @@ public function update(Form $form, Request $request) public function getCreateForm($type, $pageType = null) { $node = $this->nodeFactory->getNode($type, $pageType); + + if ($node instanceof Taggable) { + $text = ''; + foreach($node->getTags() as $tag) { + if($text !== '') { + $text .= ', '; + } + $text .= $tag->getSlug(); + } + $node->tagsAsText = $text; + } + $form = $this->nodeFactory->getFormType($type); return $this->factory->create($form, $node); @@ -77,4 +141,14 @@ public function getUpdateForm(NodeEntity $node) return $this->factory->create($form, $node); } + + public function trimTags() + { + $res = array(); + foreach ($tags as $tag) { + $res = trim($tag); + } + + return $res; + } } From 6f311772d6754cb545e90be579c500743484545b Mon Sep 17 00:00:00 2001 From: Nek Date: Thu, 12 Jul 2012 17:37:24 +0200 Subject: [PATCH 07/20] Added tag support to page --- Entity/Page.php | 45 +++++++++++++++++++++++++- Form/Type/PageType.php | 1 + Resources/config/doctrine/Page.orm.xml | 2 ++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/Entity/Page.php b/Entity/Page.php index 1a4bcb2..7f07110 100644 --- a/Entity/Page.php +++ b/Entity/Page.php @@ -3,8 +3,9 @@ namespace Soloist\Bundle\CoreBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; +use DoctrineExtensions\Taggable\Taggable; -class Page extends Node +class Page extends Node implements Taggable { /** @@ -27,9 +28,22 @@ class Page extends Node */ protected $blocks; + /** + * Tags managed by FPNTagBundle + * @var \Doctrine\Common\Collections\Collection + */ + protected $tags; + + /** + * Tas as text + * @var string + */ + public $tagsAsText; + public function __construct() { $this->blocks = new ArrayCollection; + $this->tags = new ArrayCollection; } /** @@ -113,4 +127,33 @@ public function postLoad() $this->blocks = $blocks; } + /** + * Get tags + * @return ArrayCollection + */ + public function getTags() + { + $this->tags = $this->tags ?: new ArrayCollection(); + + return $this->tags; + } + + /** + * Get tag type + * @return string + */ + public function getTaggableType() + { + return 'soloist_tag'; + } + + /** + * Get id of the current entity + * @return integer + */ + public function getTaggableId() + { + return $this->getId(); + } + } diff --git a/Form/Type/PageType.php b/Form/Type/PageType.php index d31fba4..14954e3 100644 --- a/Form/Type/PageType.php +++ b/Form/Type/PageType.php @@ -37,6 +37,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'choice_list' => new SimpleChoiceList($this->factory->getPageTypes()) )) ->add('blocks', new BlockCollectionType, array('block_factory' => $this->factory)) + ->add('tagsAsText', 'text') ; } diff --git a/Resources/config/doctrine/Page.orm.xml b/Resources/config/doctrine/Page.orm.xml index 2b87a22..a748e01 100644 --- a/Resources/config/doctrine/Page.orm.xml +++ b/Resources/config/doctrine/Page.orm.xml @@ -16,6 +16,8 @@ + + From e454b0638c63a7cbc4ef27523032db53f36c0c72 Mon Sep 17 00:00:00 2001 From: Nek Date: Fri, 13 Jul 2012 09:44:57 +0200 Subject: [PATCH 08/20] Fixed stupid code --- Form/Handler/Node.php | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/Form/Handler/Node.php b/Form/Handler/Node.php index a57f9f6..c09ba3e 100644 --- a/Form/Handler/Node.php +++ b/Form/Handler/Node.php @@ -54,12 +54,11 @@ public function create(Form $form, Request $request) $data = $form->getData(); if ($data instanceof Taggable) { - $tags = explode(',', $data->tagsAsText); - $tags = $this->trimTags($tags); - foreach ($tags as $tag) { - $currentTag = $this->tagManager->loadOrCreateTag($tag); - $this->tagManager->addTag($currentTag, $data); - } + + $tags = $this->tagManager->splitTagNames($data->tagsAsText); + + $tags = $this->tagManager->loadOrCreateTags($tags); + $this->tagManager->addTags($tags, $data); } $this->em->persist($data); @@ -78,7 +77,7 @@ public function update(Form $form, Request $request) $data = $form->getData(); $oldTags = null; if ($data instanceof Taggable) { - $oldTags = explode(',', $data->tagsAsText); + $oldTags = $this->tagManager->splitTagNames($data->tagsAsText); } $form->bindRequest($request); @@ -87,17 +86,19 @@ public function update(Form $form, Request $request) // if entity is taggable, we should remove old tags // and add new if ($data instanceof Taggable) { - $tags = explode(',', $data->tagsAsText); - $oldTags = $this->trimTags($oldTags); - $Tags = $this->trimTags($Tags); + + $tags = $this->tagManager->splitTagNames($data->tagsAsText); foreach ($tags as $key => $tag) { - if(!in_array($tag, $oldTags)) { + if (!in_array($tag, $oldTags)) { $currentTag = $this->tagManager->loadOrCreateTag($tag); $this->tagManager->addTag($currentTag, $data); } else { - unset($oldTags[$key]); + if ($key = array_search($tag, $oldTags)) { + unset($oldTags[$key]); + } + } } @@ -109,6 +110,8 @@ public function update(Form $form, Request $request) $this->em->flush(); + $tagManager->saveTagging($data); + return true; } @@ -120,6 +123,7 @@ public function getCreateForm($type, $pageType = null) $node = $this->nodeFactory->getNode($type, $pageType); if ($node instanceof Taggable) { + $this->tagManager->loadTagging($node); $text = ''; foreach($node->getTags() as $tag) { if($text !== '') { @@ -141,14 +145,4 @@ public function getUpdateForm(NodeEntity $node) return $this->factory->create($form, $node); } - - public function trimTags() - { - $res = array(); - foreach ($tags as $tag) { - $res = trim($tag); - } - - return $res; - } } From 35fdea5702e6dcac55aa12bc130295b3c5e5c55b Mon Sep 17 00:00:00 2001 From: Nek Date: Fri, 13 Jul 2012 09:46:31 +0200 Subject: [PATCH 09/20] Fixed bad service declaration --- Resources/config/services.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/Resources/config/services.xml b/Resources/config/services.xml index d6e4ca5..3497255 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -29,6 +29,7 @@ + From ce5445a2766f078182ebe27e9918d6cb67fb7d14 Mon Sep 17 00:00:00 2001 From: Nek Date: Fri, 13 Jul 2012 09:46:48 +0200 Subject: [PATCH 10/20] Fixed useless constraint --- Resources/config/doctrine/Page.orm.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/Resources/config/doctrine/Page.orm.xml b/Resources/config/doctrine/Page.orm.xml index a748e01..2b87a22 100644 --- a/Resources/config/doctrine/Page.orm.xml +++ b/Resources/config/doctrine/Page.orm.xml @@ -16,8 +16,6 @@ - - From 491dcbb3054bc92956f0f9e6a3aa245f8a51b001 Mon Sep 17 00:00:00 2001 From: Nek Date: Fri, 13 Jul 2012 09:51:52 +0200 Subject: [PATCH 11/20] Stupid code was stupid --- Form/Handler/Node.php | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/Form/Handler/Node.php b/Form/Handler/Node.php index c09ba3e..798a1bc 100644 --- a/Form/Handler/Node.php +++ b/Form/Handler/Node.php @@ -75,10 +75,6 @@ public function create(Form $form, Request $request) public function update(Form $form, Request $request) { $data = $form->getData(); - $oldTags = null; - if ($data instanceof Taggable) { - $oldTags = $this->tagManager->splitTagNames($data->tagsAsText); - } $form->bindRequest($request); if ($form->isValid()) { @@ -89,23 +85,8 @@ public function update(Form $form, Request $request) $tags = $this->tagManager->splitTagNames($data->tagsAsText); - foreach ($tags as $key => $tag) { - if (!in_array($tag, $oldTags)) { - $currentTag = $this->tagManager->loadOrCreateTag($tag); - $this->tagManager->addTag($currentTag, $data); - - } else { - if ($key = array_search($tag, $oldTags)) { - unset($oldTags[$key]); - } - - } - } - - foreach ($oldTags as $tag) { - $currentTag = $this->tagManager->loadOrCreateTag($tag); - $this->tagManager->removeTag($currentTag, $data); - } + $tags = $this->tagManager->loadOrCreateTags($tags); + $this->tagManager->addTags($tags, $data); } $this->em->flush(); From 5d698a193fe3a47a60dd57d262b25c6033a4b235 Mon Sep 17 00:00:00 2001 From: Nek Date: Fri, 13 Jul 2012 15:26:41 +0200 Subject: [PATCH 12/20] Change the entity tag signature --- Entity/Node.php | 11 +++++++++++ Entity/Page.php | 9 --------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Entity/Node.php b/Entity/Node.php index 91c0a23..6ddec82 100644 --- a/Entity/Node.php +++ b/Entity/Node.php @@ -224,4 +224,15 @@ public function getUpdatedAt() public function postLoad() { } + + /** + * Get tag type + * This method is a part of the Taggable interface + * + * @return string + */ + public function getTaggableType() + { + return 'soloist_node'; + } } diff --git a/Entity/Page.php b/Entity/Page.php index 7f07110..4f17061 100644 --- a/Entity/Page.php +++ b/Entity/Page.php @@ -138,15 +138,6 @@ public function getTags() return $this->tags; } - /** - * Get tag type - * @return string - */ - public function getTaggableType() - { - return 'soloist_tag'; - } - /** * Get id of the current entity * @return integer From 17d05796142b57d1d560cd9e5be37b5f08902e4f Mon Sep 17 00:00:00 2001 From: Nek Date: Fri, 13 Jul 2012 18:10:25 +0200 Subject: [PATCH 13/20] Added begin of management of similar content --- Controller/DefaultController.php | 17 ++++++++++++++++- Entity/Repository/Node.php | 15 +++++++++++++++ EventListener/BlockListener.php | 7 +++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php index a66abbb..e3d7b6e 100644 --- a/Controller/DefaultController.php +++ b/Controller/DefaultController.php @@ -2,13 +2,15 @@ namespace Soloist\Bundle\CoreBundle\Controller; -use Doctrine\Common\Util\Inflector; +use Doctrine\Common\Util\Inflector, + DoctrineExtensions\Taggable\Taggable; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Soloist\Bundle\CoreBundle\Entity\Action, Soloist\Bundle\CoreBundle\Entity\Page, Soloist\Bundle\CoreBundle\Entity\Node; use Symfony\Bundle\FrameworkBundle\Controller\Controller; + class DefaultController extends Controller { public function indexAction() @@ -53,4 +55,17 @@ public function pageShortcutAction($page, $path_image, $description) 'description' => $description, ); } + + /** + * @Template() + * @return array + */ + public function similarContentAction($node) + { + if (!$node instanceof Taggable) { + throw new InvalidArgumentException('Sorry ! I need tags to search similar content. Therefore your element must implements "DoctrineExtensions\Taggable\Taggable".') + } + $tagManager = $this->get('fpn_tag.tag_manager'); + $tagManager->loadOrCreateTags($node); + } } diff --git a/Entity/Repository/Node.php b/Entity/Repository/Node.php index 70af8d2..0cf417d 100644 --- a/Entity/Repository/Node.php +++ b/Entity/Repository/Node.php @@ -4,6 +4,7 @@ use Doctrine\ORM\Query\Expr; use Gedmo\Tree\Entity\Repository\NestedTreeRepository; +use DoctrineExtensions\Taggable\Taggable; class Node extends NestedTreeRepository { @@ -27,4 +28,18 @@ public function findRoot() ->getSingleResult() ; } + + /** + * Get similar results + * NEED a taggable node + * Sql draft available here: https://gist.github.com/3105206/70ab5aaa6654890433d8e5d9671f320a695d3627 + * @param Taggable $resource + * @param integer $limit + * @return array + */ + public function getSimilarResults(Taggable $resource, $limit = 5) + { + $qb = $this->createQueryBuilder('n'); + // will be better with a native query + } } diff --git a/EventListener/BlockListener.php b/EventListener/BlockListener.php index 926b18e..c2dde8f 100644 --- a/EventListener/BlockListener.php +++ b/EventListener/BlockListener.php @@ -31,6 +31,13 @@ public function onRequestTypes(RequestTypes $event) 'form' => new PageShortcutType($this->em), 'form_template' => 'SoloistCoreBundle:AdminBlock:configurePageShortcut.html.twig' )) + ->addBlockType('similar_content', array( + 'name' => 'Contenu similaire', + 'action' => 'SoloistCoreBundle:Default:similarContent', + 'settings' => array('type' => 'page'), + 'form' => new SimilarContentType(), + 'form_template' => 'SoloistCoreBundle:AdminBlock:configureSimilarContent.html.twig' + )) ; } } From aa820fbc125b135ffc255681e1e54d3fe8111145 Mon Sep 17 00:00:00 2001 From: Nek Date: Mon, 16 Jul 2012 11:23:07 +0200 Subject: [PATCH 14/20] Updated Node repository to support similar search --- Entity/Repository/Node.php | 50 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/Entity/Repository/Node.php b/Entity/Repository/Node.php index 0cf417d..d38b381 100644 --- a/Entity/Repository/Node.php +++ b/Entity/Repository/Node.php @@ -6,6 +6,8 @@ use Gedmo\Tree\Entity\Repository\NestedTreeRepository; use DoctrineExtensions\Taggable\Taggable; +use Doctrine\ORM\Query\ResultSetMappingBuilder; + class Node extends NestedTreeRepository { public function findAllOrderedByLeft() @@ -39,7 +41,51 @@ public function findRoot() */ public function getSimilarResults(Taggable $resource, $limit = 5) { - $qb = $this->createQueryBuilder('n'); - // will be better with a native query + $tagsList = ''; + $count = $resource->getTags()->count(); + + if ($count < 1) { + return array(); + } + + foreach ($resource->getTags() as $tag) { + if (!empty($tagsList)) { + $tagsList .= ', '; + } + $tagsList .= '\'' . $tag->getSlug() . '\''; + } + + $part = << 0); + + + $sql .= "\n" . 'LIMIT ' . $limit; + + $rsm = new ResultSetMappingBuilder($this->_em); + $rsm->addRootEntityFromClassMetadata('SoloistCoreBundle:Node', 'n'); + + $replacedValues = array($count, $limit); + $query = $this->_em->createNativeQuery($sql, $rsm); + + return $query->getResult(); } } From 5ae4588d6df3149af0388fbe371642e6b07a6055 Mon Sep 17 00:00:00 2001 From: Nek Date: Mon, 16 Jul 2012 11:23:35 +0200 Subject: [PATCH 15/20] Added SimilarContentBlock --- .../Type/BlockSettings/SimilarContentType.php | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Form/Type/BlockSettings/SimilarContentType.php diff --git a/Form/Type/BlockSettings/SimilarContentType.php b/Form/Type/BlockSettings/SimilarContentType.php new file mode 100644 index 0000000..892e238 --- /dev/null +++ b/Form/Type/BlockSettings/SimilarContentType.php @@ -0,0 +1,49 @@ +em = $em; + } + + /** + * Build the form + * @param FormBuilderInterface $builder + * @param array $options + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->appendClientTransformer($this) + ->add('node', 'choice', array( + 'class' => 'SoloistCoreBundle:Page', + 'empty_value' => 'Choisissez un élément', + 'label' => 'Élément' + )) + ; + } +} From 61318eadac92e68c4e7a349cddfd38753b3b1ca2 Mon Sep 17 00:00:00 2001 From: Nek Date: Mon, 16 Jul 2012 16:30:19 +0200 Subject: [PATCH 16/20] Updated default controller to remove useless method --- Controller/DefaultController.php | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php index e3d7b6e..985d06a 100644 --- a/Controller/DefaultController.php +++ b/Controller/DefaultController.php @@ -2,8 +2,7 @@ namespace Soloist\Bundle\CoreBundle\Controller; -use Doctrine\Common\Util\Inflector, - DoctrineExtensions\Taggable\Taggable; +use Doctrine\Common\Util\Inflector; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; use Soloist\Bundle\CoreBundle\Entity\Action, Soloist\Bundle\CoreBundle\Entity\Page, @@ -56,16 +55,4 @@ public function pageShortcutAction($page, $path_image, $description) ); } - /** - * @Template() - * @return array - */ - public function similarContentAction($node) - { - if (!$node instanceof Taggable) { - throw new InvalidArgumentException('Sorry ! I need tags to search similar content. Therefore your element must implements "DoctrineExtensions\Taggable\Taggable".') - } - $tagManager = $this->get('fpn_tag.tag_manager'); - $tagManager->loadOrCreateTags($node); - } } From d976549da73c50d278a33a174fdbcb735c73d6a1 Mon Sep 17 00:00:00 2001 From: Nek Date: Mon, 16 Jul 2012 16:30:55 +0200 Subject: [PATCH 17/20] Updated code to make another type of block --- Controller/NavigationController.php | 33 ++++++++++++- Entity/Repository/Node.php | 9 ++-- EventListener/BlockListener.php | 7 --- .../Type/BlockSettings/SimilarContentType.php | 49 ------------------- .../views/Navigation/Similar/page.html.twig | 7 +++ 5 files changed, 44 insertions(+), 61 deletions(-) delete mode 100644 Form/Type/BlockSettings/SimilarContentType.php create mode 100644 Resources/views/Navigation/Similar/page.html.twig diff --git a/Controller/NavigationController.php b/Controller/NavigationController.php index f5feaa4..683e2a5 100644 --- a/Controller/NavigationController.php +++ b/Controller/NavigationController.php @@ -3,7 +3,8 @@ namespace Soloist\Bundle\CoreBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; -use Soloist\Bundle\CoreBundle\Entity\Node; +use Soloist\Bundle\CoreBundle\Entity\Node, + DoctrineExtensions\Taggable\Taggable; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class NavigationController extends Controller @@ -41,4 +42,34 @@ public function showPartAction($node, $depth = null) 'current' => $node ); } + + /** + * Show similars elements + * + * @param Taggable $node + * @return Respons + */ + public function showSimilarAction(Taggable $node) + { + if (!$node instanceof Taggable) { + throw new InvalidArgumentException('Sorry ! I need tags to search similar content. Therefore your element must implements "DoctrineExtensions\Taggable\Taggable".'); + } + $type = $node->getType(); + + if ($type != 'page') { + throw new InvalidArgumentException('Sorry ! This node is currently not supported.'); + } + + $tagManager = $this->get('fpn_tag.tag_manager'); + $tagManager->loadOrCreateTags($node); + + $em = $this->getDoctrine()->getManager(); + $repo = $em->getRepository('SoloistCoreBundle:Node'); + + $similarElements = $repo->getSimilarResults($node); + + return $this->render('SoloistCoreBundle:Navigation:Similar/'. $type . '.html.twig', array( + 'nodes' => $similarElements + )); + } } diff --git a/Entity/Repository/Node.php b/Entity/Repository/Node.php index d38b381..aaf2df4 100644 --- a/Entity/Repository/Node.php +++ b/Entity/Repository/Node.php @@ -35,6 +35,8 @@ public function findRoot() * Get similar results * NEED a taggable node * Sql draft available here: https://gist.github.com/3105206/70ab5aaa6654890433d8e5d9671f320a695d3627 + * Notice that the only node who will work is tha page + * * @param Taggable $resource * @param integer $limit * @return array @@ -56,15 +58,15 @@ public function getSimilarResults(Taggable $resource, $limit = 5) } $part = <<_em); $rsm->addRootEntityFromClassMetadata('SoloistCoreBundle:Node', 'n'); - $replacedValues = array($count, $limit); $query = $this->_em->createNativeQuery($sql, $rsm); return $query->getResult(); diff --git a/EventListener/BlockListener.php b/EventListener/BlockListener.php index c2dde8f..926b18e 100644 --- a/EventListener/BlockListener.php +++ b/EventListener/BlockListener.php @@ -31,13 +31,6 @@ public function onRequestTypes(RequestTypes $event) 'form' => new PageShortcutType($this->em), 'form_template' => 'SoloistCoreBundle:AdminBlock:configurePageShortcut.html.twig' )) - ->addBlockType('similar_content', array( - 'name' => 'Contenu similaire', - 'action' => 'SoloistCoreBundle:Default:similarContent', - 'settings' => array('type' => 'page'), - 'form' => new SimilarContentType(), - 'form_template' => 'SoloistCoreBundle:AdminBlock:configureSimilarContent.html.twig' - )) ; } } diff --git a/Form/Type/BlockSettings/SimilarContentType.php b/Form/Type/BlockSettings/SimilarContentType.php deleted file mode 100644 index 892e238..0000000 --- a/Form/Type/BlockSettings/SimilarContentType.php +++ /dev/null @@ -1,49 +0,0 @@ -em = $em; - } - - /** - * Build the form - * @param FormBuilderInterface $builder - * @param array $options - */ - public function buildForm(FormBuilderInterface $builder, array $options) - { - $builder - ->appendClientTransformer($this) - ->add('node', 'choice', array( - 'class' => 'SoloistCoreBundle:Page', - 'empty_value' => 'Choisissez un élément', - 'label' => 'Élément' - )) - ; - } -} diff --git a/Resources/views/Navigation/Similar/page.html.twig b/Resources/views/Navigation/Similar/page.html.twig new file mode 100644 index 0000000..bdd53dd --- /dev/null +++ b/Resources/views/Navigation/Similar/page.html.twig @@ -0,0 +1,7 @@ +
+ +
From 295a343fe2e80c7c3518e597ceb761eda46ee3d5 Mon Sep 17 00:00:00 2001 From: Nek Date: Mon, 16 Jul 2012 16:40:34 +0200 Subject: [PATCH 18/20] Fixed bad instruction --- Controller/NavigationController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controller/NavigationController.php b/Controller/NavigationController.php index 683e2a5..e144dd7 100644 --- a/Controller/NavigationController.php +++ b/Controller/NavigationController.php @@ -61,7 +61,7 @@ public function showSimilarAction(Taggable $node) } $tagManager = $this->get('fpn_tag.tag_manager'); - $tagManager->loadOrCreateTags($node); + $tagManager->loadTagging($node); $em = $this->getDoctrine()->getManager(); $repo = $em->getRepository('SoloistCoreBundle:Node'); From 56a30617b8bf917b879b60eee87d6331859a898c Mon Sep 17 00:00:00 2001 From: Nek Date: Wed, 1 Aug 2012 18:39:57 +0200 Subject: [PATCH 19/20] Added generation of a global id --- Entity/Node.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Entity/Node.php b/Entity/Node.php index 6ddec82..198ff99 100644 --- a/Entity/Node.php +++ b/Entity/Node.php @@ -225,6 +225,11 @@ public function postLoad() { } + public function getGlobalId() + { + return 'core-' . $this->getType() . '-' . $this->getId(); + } + /** * Get tag type * This method is a part of the Taggable interface From ac8bac55f293ab328af05e1ea7de6d541c8662e5 Mon Sep 17 00:00:00 2001 From: Nek Date: Wed, 1 Aug 2012 18:40:24 +0200 Subject: [PATCH 20/20] Added event listener --- EventListener/BlogListener.php | 32 ++++++++++++++++++++++++++++++++ Resources/config/services.xml | 6 ++++++ 2 files changed, 38 insertions(+) create mode 100644 EventListener/BlogListener.php diff --git a/EventListener/BlogListener.php b/EventListener/BlogListener.php new file mode 100644 index 0000000..a43600f --- /dev/null +++ b/EventListener/BlogListener.php @@ -0,0 +1,32 @@ +em = $em; + } + /** + * Listen to the RequestCategories event from blog bundle + * + * @param \Soloist\Bundle\BlogBundle\EventListener\Event\RequestCategories $event + */ + public function onRequestCategories(RequestCategories $event) + { + $repo = $this->em->getRepository('SoloistCoreBundle:Category'); + $categories = $repo->findAll(); + + foreach($categories as $category) { + $event->addCategory($category->getGlobalId(), $category->getTitle()); + } + } +} diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 3497255..5dd4eae 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -16,6 +16,7 @@ Soloist\Bundle\CoreBundle\Twig\Extension Soloist\Bundle\CoreBundle\Form\Type\JsonArrayType Soloist\Bundle\CoreBundle\Router + Soloist\Bundle\CoreBundle\EventListener\BlogListener @@ -48,6 +49,11 @@ + + + + +