Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Tag entity configurable #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
21 changes: 19 additions & 2 deletions AdminList/TagAdminListConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
class TagAdminListConfigurator extends AbstractDoctrineORMAdminListConfigurator
{

protected $repositoryName;

/**
* @param EntityManager $em The entity manager
* @param AclHelper $aclHelper The acl helper
*/
public function __construct(EntityManager $em, AclHelper $aclHelper = null)
public function __construct(EntityManager $em, AclHelper $aclHelper = null, $repositoryName)
{
parent::__construct($em, $aclHelper);
$this->setAdminType(new TagAdminType());
$this->repositoryName = $repositoryName;
}

/**
Expand Down Expand Up @@ -60,4 +62,19 @@ public function getEntityName()
return 'Tag';
}

/**
* Return default repository name.
*
* @return string
*/
public function getRepositoryName()
{
return $this->repositoryName;
}

public function getAdminType($entity)
{
return parent::getAdminType($entity); // TODO: Change the autogenerated stub
}

}
4 changes: 2 additions & 2 deletions Controller/TagAdminListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TagAdminListController extends AdminListController
public function getAdminListConfigurator()
{
if (!isset($this->configurator)) {
$this->configurator = new TagAdminListConfigurator($this->getEntityManager());
$this->configurator = new TagAdminListConfigurator($this->getEntityManager(), null, $this->getParameter('kuma_tagging.tag.repository'));
}

return $this->configurator;
Expand Down Expand Up @@ -81,7 +81,7 @@ public function autocompleteAction(Request $request)
{
$search = $request->get('term');
$em = $this->getDoctrine()->getManager();
$qb = $em->getRepository('KunstmaanTaggingBundle:Tag')->createQueryBuilder('n')
$qb = $em->getRepository($this->getParameter('kuma_tagging.tag.repository'))->createQueryBuilder('n')
->where('n.name LIKE :search')
->orderBy('n.name', 'ASC')
->setParameter('search', '%' . $search . '%');
Expand Down
3 changes: 1 addition & 2 deletions Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

use Kunstmaan\TaggingBundle\Form\TagAdminType;

/**
Expand Down Expand Up @@ -42,7 +41,7 @@ class Tag extends BaseTag
protected $updatedAt;

/**
* @ORM\OneToMany(targetEntity="Kunstmaan\TaggingBundle\Entity\Tagging", mappedBy="tag", fetch="LAZY")
* Mapping happens in TagRelationSubscriber
*/
protected $tagging;

Expand Down
82 changes: 76 additions & 6 deletions Entity/TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ class TagManager extends BaseTagManager

const TAGGING_HYDRATOR = 'taggingHydrator';

public function copyTags($origin, $destination)
{
$tags = $this->getTagging($origin);
$this->replaceTags($tags, $destination);
$this->saveTagging($destination);
}

public function loadTagging(BaseTaggable $resource)
{
if ($resource instanceof LazyLoadingTaggableInterface) {
Expand Down Expand Up @@ -41,6 +48,43 @@ public function saveTagging(BaseTaggable $resource)
}


/**
* @param $tag
* @return array
*/
public function getResourcesByTag($tag, $entityName, $type)
{
$em = $this->em;

$config = $em->getConfiguration();
if (is_null($config->getCustomHydrationMode(TagManager::TAGGING_HYDRATOR))) {
$config->addCustomHydrationMode(TagManager::TAGGING_HYDRATOR, 'Doctrine\ORM\Internal\Hydration\ObjectHydrator');
}

if ($type) {
$joinCriteria = 't2.resourceType = :type and t2.resourceId = p.id';
}
else {
$joinCriteria = 't2.resourceId = p.id';
}

$qb = $em
->createQueryBuilder()
->select('p')
->from($entityName, 'p')
->innerJoin($this->taggingClass, 't2', Expr\Join::WITH, $joinCriteria)
->innerJoin('t2.tag', 't', Expr\Join::WITH, 't.name = :tag')
->setParameter('tag', $tag);

if($type) {
$qb->setParameter('type', $type);
}

$query = $qb->getQuery();
$pages = $query->getResult(TagManager::TAGGING_HYDRATOR);

return $pages;
}
/**
* Gets all tags for the given taggable resource
*
Expand All @@ -56,20 +100,39 @@ public function getTagging(BaseTaggable $resource)
$config->addCustomHydrationMode(self::TAGGING_HYDRATOR, 'Doctrine\ORM\Internal\Hydration\ObjectHydrator');
}

return $em
$qb = $em
->createQueryBuilder()

->select('t')
->from($this->tagClass, 't')

->innerJoin('t.tagging', 't2', Expr\Join::WITH, 't2.resourceId = :id AND t2.resourceType = :type')
->setParameter('id', $resource->getTaggableId())
->setParameter('type', $resource->getTaggableType())
->setParameter('type', $resource->getTaggableType());

->getQuery()
$query = $qb
->getQuery();
$result = $query
->getResult(self::TAGGING_HYDRATOR);

return $result;
}

public function getTagsByResourceType($type)
{

$qb = $this->em->createQueryBuilder()
->select('t.name, COUNT(t2) as cnt')
->from($this->tagClass, 't')
->leftJoin('t.tagging', 't2', Expr\Join::WITH, 't2.resourceType = \'' . $type . '\'')
->groupBy('t')
->orderBy('cnt', 'desc')
->having('cnt > 0');

$query = $qb->getQuery();
$results = $query->execute();
return $results;
}


public function findById($id)
{

Expand All @@ -92,11 +155,18 @@ public function findById($id)

public function findAll()
{
$tagsRepo = $this->em->getRepository('KunstmaanTaggingBundle:Tag');
$tagsRepo = $this->em->getRepository($this->tagClass);

return $tagsRepo->findAll();
}

public function findByName($name)
{
$tagsRepo = $this->em->getRepository($this->tagClass);

return $tagsRepo->findOneBy(['name' => $name]);
}

public function findRelatedItems(Taggable $item, $class, $locale, $nbOfItems=1)
{
$instance = new $class();
Expand Down
4 changes: 0 additions & 4 deletions Entity/Tagging.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ class Tagging extends BaseTagging
*/
protected $id;

/**
* @ORM\ManyToOne(targetEntity="Kunstmaan\TaggingBundle\Entity\Tag", inversedBy="tagging")
* @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $tag;

/**
Expand Down
9 changes: 5 additions & 4 deletions EventListener/CloneListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
use Doctrine\ORM\EntityManager;
use DoctrineExtensions\Taggable\Taggable;
use Kunstmaan\AdminBundle\Event\DeepCloneAndSaveEvent;
use Kunstmaan\TaggingBundle\Entity\TagManager;

/**
* This listener will make sure the tags are copied as well
*/
class CloneListener
{

protected $em;
protected $tagManager;

public function __construct(EntityManager $em)
public function __construct(TagManager $tagManager)
{
$this->em = $em;
$this->tagManager = $tagManager;
}

/**
Expand All @@ -28,7 +29,7 @@ public function postDeepCloneAndSave(DeepCloneAndSaveEvent $event)

if ($originalEntity instanceof Taggable) {
$targetEntity = $event->getClonedEntity();
$this->em->getRepository('KunstmaanTaggingBundle:Tag')->copyTags($originalEntity, $targetEntity);
$this->tagManager->copyTags($originalEntity, $targetEntity);
}
}

Expand Down
77 changes: 77 additions & 0 deletions EventListener/TagRelationSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Created by PhpStorm.
* User: hpenny
* Date: 10/04/17
* Time: 12:02 PM
*/

namespace Kunstmaan\TaggingBundle\EventListener;


use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Events;

class TagRelationSubscriber implements EventSubscriber
{
protected $tagClass = 'Kunstmaan\TaggingBundle\Entity\Tag';
protected $taggingClass = 'Kunstmaan\TaggingBundle\Entity\Tagging';

public function __construct($tagClass, $taggingClass)
{
$this->tagClass = $tagClass;
$this->taggingClass = $taggingClass;
}

/**
* {@inheritDoc}
*/
public function getSubscribedEvents()
{
return array(
Events::loadClassMetadata,
);
}

/**
* @param LoadClassMetadataEventArgs $eventArgs
*/
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
// the $metadata is the whole mapping info for this class
$metadata = $eventArgs->getClassMetadata();

if ($metadata->getName() == $this->taggingClass) {

$metadata->mapManyToOne(array(
'targetEntity' => $this->tagClass,
'fieldName' => 'tag',
'cascade' => [],
'inversedBy' => 'tagging',
'JoinColumn' => array(
'name' => 'tag_id',
'unique' => false,
'nullable' => true,
'referencedColumnName' => 'id',
'columnDefinition' => null,
'onDelete' => 'CASCADE'
),
'fetch' => 2,
'type' => 2,

));

}
else if ($metadata->getName() == $this->tagClass) {

$metadata->mapOneToMany(array(
'targetEntity' => $this->taggingClass,
'fieldName' => 'tagging',
'mappedBy' => 'tag',
'fetch' => "LAZY",
));

}
}
}
17 changes: 0 additions & 17 deletions Repository/TagRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,4 @@

class TagRepository extends EntityRepository
{

protected $tagManager;

public function __construct($em, ClassMetadata $class)
{
parent::__construct($em, $class);
$this->tagManager = new TagManager($em, 'Kunstmaan\TaggingBundle\Entity\Tag', 'Kunstmaan\TaggingBundle\Entity\Tagging');
}

public function copyTags($origin, $destination)
{
$tagManager = $this->tagManager;
$tags = $tagManager->getTagging($origin);
$tagManager->replaceTags($tags, $destination);
$tagManager->saveTagging($destination);
}

}
14 changes: 12 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
parameters:
kuma_tagging.tag.class: 'Kunstmaan\TaggingBundle\Entity\Tag'
kuma_tagging.tag.repository: 'KunstmaanTaggingBundle:Tag'
kuma_tagging.tagging.class: 'Kunstmaan\TaggingBundle\Entity\Tagging'
kuma_tagging.event_listener.tag_relation_subscriber.class: 'Kunstmaan\TaggingBundle\EventListener\TagRelationSubscriber'

services:
kuma_tagging.tag_manager:
class: Kunstmaan\TaggingBundle\Entity\TagManager
arguments: ['@doctrine.orm.entity_manager', 'Kunstmaan\TaggingBundle\Entity\Tag', 'Kunstmaan\TaggingBundle\Entity\Tagging']
arguments: ['@doctrine.orm.entity_manager', '%kuma_tagging.tag.class%', '%kuma_tagging.tagging.class%']

kuma_tagging.listener:
class: Kunstmaan\TaggingBundle\EventListener\TagsListener
Expand All @@ -16,7 +20,7 @@ services:

kuma_tagging.clone.listener:
class: Kunstmaan\TaggingBundle\EventListener\CloneListener
arguments: ['@doctrine.orm.entity_manager']
arguments: ['@kuma_tagging.tag_manager']
tags:
- { name: kernel.event_listener, event: kunstmaan_admin.postDeepCloneAndSave, method: postDeepCloneAndSave }

Expand All @@ -36,3 +40,9 @@ services:
arguments: ['@kuma_tagging.tag_manager']
tags:
- { name: form.type }

kuma_tagging.event_listener.tag_relation_subscriber:
class: '%kuma_tagging.event_listener.tag_relation_subscriber.class%'
arguments: ['%kuma_tagging.tag.class%', '%kuma_tagging.tagging.class%']
tags:
- { name: doctrine.event_subscriber }