Skip to content

Commit

Permalink
[translatable] started implementation f personal translations for mon…
Browse files Browse the repository at this point in the history
…go ODM
  • Loading branch information
l3pp4rd committed Feb 1, 2012
1 parent 6ac6ffc commit 7052ee3
Show file tree
Hide file tree
Showing 11 changed files with 513 additions and 41 deletions.
147 changes: 147 additions & 0 deletions lib/Gedmo/Translatable/Document/AbstractPersonalTranslation.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Gedmo\Translatable\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations\MappedSuperclass;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;
use Doctrine\ODM\MongoDB\Mapping\Annotations\String;

/**
* Gedmo\Translatable\Document\AbstractPersonalTranslation
*
* @MappedSuperclass
*/
abstract class AbstractPersonalTranslation
{
/**
* @var integer $id
*
* @Id
*/
protected $id;

/**
* @var string $locale
*
* @String
*/
protected $locale;

/**
* Related entity with ManyToOne relation
* must be mapped by user
*/
protected $object;

/**
* @var string $field
*
* @String
*/
protected $field;

/**
* @var text $content
*
* @String
*/
protected $content;

/**
* Get id
*
* @return integer $id
*/
public function getId()
{
return $this->id;
}

/**
* Set locale
*
* @param string $locale
* @return AbstractPersonalTranslation
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}

/**
* Get locale
*
* @return string $locale
*/
public function getLocale()
{
return $this->locale;
}

/**
* Set field
*
* @param string $field
* @return AbstractPersonalTranslation
*/
public function setField($field)
{
$this->field = $field;
return $this;
}

/**
* Get field
*
* @return string $field
*/
public function getField()
{
return $this->field;
}

/**
* Set object related
*
* @param object $object
* @return AbstractPersonalTranslation
*/
public function setObject($object)
{
$this->object = $object;
return $this;
}

/**
* Get object related
*
* @return string $object
*/
public function getObject()
{
return $this->object;
}

/**
* Set content
*
* @param string $content
* @return AbstractPersonalTranslation
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}

/**
* Get content
*
* @return string $content
*/
public function getContent()
{
return $this->content;
}
}
4 changes: 2 additions & 2 deletions lib/Gedmo/Translatable/Entity/AbstractPersonalTranslation.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function getObject()
/** /**
* Set content * Set content
* *
* @param text $content * @param string $content
* @return AbstractTranslation * @return AbstractTranslation
*/ */
public function setContent($content) public function setContent($content)
Expand All @@ -141,7 +141,7 @@ public function setContent($content)
/** /**
* Get content * Get content
* *
* @return text $content * @return string $content
*/ */
public function getContent() public function getContent()
{ {
Expand Down
110 changes: 85 additions & 25 deletions lib/Gedmo/Translatable/Mapping/Event/Adapter/ODM.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@
*/ */
final class ODM extends BaseAdapterODM implements TranslatableAdapter final class ODM extends BaseAdapterODM implements TranslatableAdapter
{ {
/**
* {@inheritDoc}
*/
public function usesPersonalTranslation($translationClassName)
{
return $this
->getObjectManager()
->getClassMetadata($translationClassName)
->getReflectionClass()
->isSubclassOf('Gedmo\Translatable\Document\AbstractPersonalTranslation')
;
}

/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
Expand All @@ -37,57 +50,104 @@ public function loadTranslations($object, $translationClass, $locale)
{ {
$dm = $this->getObjectManager(); $dm = $this->getObjectManager();
$wrapped = AbstractWrapper::wrapp($object, $dm); $wrapped = AbstractWrapper::wrapp($object, $dm);
$result = array();


// load translated content for all translatable fields if ($this->usesPersonalTranslation($translationClass)) {
$identifier = $wrapped->getIdentifier(); // first try to load it using collection
// construct query die(print_r($wrapped->getMetadata()));
$qb = $dm->createQueryBuilder($translationClass); $found = false;
$q = $qb->field('foreignKey')->equals($identifier) foreach ($wrapped->getMetadata()->associationMappings as $assoc) {
->field('locale')->equals($locale) $isRightCollection = $assoc['targetEntity'] === $translationClass
->field('objectClass')->equals($wrapped->getMetadata()->name) && $assoc['mappedBy'] === 'object'
->getQuery(); && $assoc['type'] === ClassMetadataInfo::ONE_TO_MANY
;
if ($isRightCollection) {
$collection = $wrapped->getPropertyValue($assoc['fieldName']);
foreach ($collection as $trans) {
if ($trans->getLocale() === $locale) {
$result[] = array(
'field' => $trans->getField(),
'content' => $trans->getContent()
);
}
}
$found = true;
break;
}
}
// if collection is not set, fetch it through relation
if (!$found) {
$dql = 'SELECT t.content, t.field FROM ' . $translationClass . ' t';
$dql .= ' WHERE t.locale = :locale';
$dql .= ' AND t.object = :object';


$q->setHydrate(false); $q = $em->createQuery($dql);
$result = $q->execute(); $q->setParameters(compact('object', 'locale'));
if ($result instanceof Cursor) { $result = $q->getArrayResult();
$result = $result->toArray(); }
} else {
// load translated content for all translatable fields
$identifier = $wrapped->getIdentifier();
// construct query
$qb = $dm->createQueryBuilder($translationClass);
$q = $qb->field('foreignKey')->equals($identifier)
->field('locale')->equals($locale)
->field('objectClass')->equals($wrapped->getMetadata()->name)
->getQuery();

$q->setHydrate(false);
$result = $q->execute();
if ($result instanceof Cursor) {
$result = $result->toArray();
}
} }
return $result; return $result;
} }


/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function findTranslation($objectId, $objectClass, $locale, $field, $translationClass) public function findTranslation(AbstractWrapper $wrapped, $locale, $field, $translationClass)
{ {
$dm = $this->getObjectManager(); $dm = $this->getObjectManager();
$qb = $dm->createQueryBuilder($translationClass); $qb = $dm
$q = $qb->field('foreignKey')->equals($objectId) ->createQueryBuilder($translationClass)
->field('locale')->equals($locale) ->field('locale')->equals($locale)
->field('field')->equals($field) ->field('field')->equals($field)
->field('objectClass')->equals($objectClass) ->limit(1)
->getQuery(); ;

if ($this->usesPersonalTranslation($translationClass)) {
$qb->field('object')->equals($wrapped->getObject());
} else {
$qb->field('foreignKey')->equals($wrapped->getIdentifier());
$qb->field('objectClass')->equals($wrapped->getMetadata()->name);
}
$q = $qb->getQuery();
$q->setHydrate(false);
$result = $q->execute(); $result = $q->execute();
if ($result instanceof Cursor) { if ($result instanceof Cursor) {
$result = current($result->toArray()); $result = current($result->toArray());
} }
$q->setHydrate(false);
return $result; return $result;
} }


/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function removeAssociatedTranslations($objectId, $transClass, $targetClass) public function removeAssociatedTranslations(AbstractWrapper $wrapped, $transClass)
{ {
$dm = $this->getObjectManager(); $dm = $this->getObjectManager();
$qb = $dm->createQueryBuilder($transClass); $qb = $dm
$q = $qb->remove() ->createQueryBuilder($transClass)
->field('foreignKey')->equals($objectId) ->remove()
->field('objectClass')->equals($targetClass)
->getQuery()
; ;
if ($this->usesPersonalTranslation($transClass)) {
$qb->field('object')->equals($wrapped->getObject());
} else {
$qb->field('foreignKey')->equals($wrapped->getIdentifier());
$qb->field('objectClass')->equals($wrapped->getMetadata()->name);
}
$q = $qb->getQuery();
return $q->execute(); return $q->execute();
} }


Expand Down
62 changes: 50 additions & 12 deletions lib/Gedmo/Translatable/Mapping/Event/Adapter/ORM.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM; use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
use Doctrine\ORM\Query; use Doctrine\ORM\Query;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Gedmo\Translatable\Mapping\Event\TranslatableAdapter; use Gedmo\Translatable\Mapping\Event\TranslatableAdapter;
use Gedmo\Tool\Wrapper\AbstractWrapper; use Gedmo\Tool\Wrapper\AbstractWrapper;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
Expand Down Expand Up @@ -48,18 +49,54 @@ public function loadTranslations($object, $translationClass, $locale)
{ {
$em = $this->getObjectManager(); $em = $this->getObjectManager();
$wrapped = AbstractWrapper::wrapp($object, $em); $wrapped = AbstractWrapper::wrapp($object, $em);
// load translated content for all translatable fields $result = array();
$objectId = $wrapped->getIdentifier(); if ($this->usesPersonalTranslation($translationClass)) {
// construct query // first try to load it using collection
$dql = 'SELECT t.content, t.field FROM ' . $translationClass . ' t'; $found = false;
$dql .= ' WHERE t.foreignKey = :objectId'; foreach ($wrapped->getMetadata()->associationMappings as $assoc) {
$dql .= ' AND t.locale = :locale'; $isRightCollection = $assoc['targetEntity'] === $translationClass
$dql .= ' AND t.objectClass = :objectClass'; && $assoc['mappedBy'] === 'object'
// fetch results && $assoc['type'] === ClassMetadataInfo::ONE_TO_MANY
$objectClass = $wrapped->getMetadata()->name; ;
$q = $em->createQuery($dql); if ($isRightCollection) {
$q->setParameters(compact('objectId', 'locale', 'objectClass')); $collection = $wrapped->getPropertyValue($assoc['fieldName']);
return $q->getArrayResult(); foreach ($collection as $trans) {
if ($trans->getLocale() === $locale) {
$result[] = array(
'field' => $trans->getField(),
'content' => $trans->getContent()
);
}
}
$found = true;
break;
}
}
// if collection is not set, fetch it through relation
if (!$found) {
$dql = 'SELECT t.content, t.field FROM ' . $translationClass . ' t';
$dql .= ' WHERE t.locale = :locale';
$dql .= ' AND t.object = :object';

$q = $em->createQuery($dql);
$q->setParameters(compact('object', 'locale'));
$result = $q->getArrayResult();
}
} else {
// load translated content for all translatable fields
$objectId = $wrapped->getIdentifier();
// construct query
$dql = 'SELECT t.content, t.field FROM ' . $translationClass . ' t';
$dql .= ' WHERE t.foreignKey = :objectId';
$dql .= ' AND t.locale = :locale';
$dql .= ' AND t.objectClass = :objectClass';
// fetch results
$objectClass = $wrapped->getMetadata()->name;
$q = $em->createQuery($dql);
$q->setParameters(compact('objectId', 'locale', 'objectClass'));
$result = $q->getArrayResult();
}
return $result;
} }


/** /**
Expand Down Expand Up @@ -87,6 +124,7 @@ public function findTranslation(AbstractWrapper $wrapped, $locale, $field, $tran
$qb->setParameter('objectClass', $wrapped->getMetadata()->name); $qb->setParameter('objectClass', $wrapped->getMetadata()->name);
} }
$q = $qb->getQuery(); $q = $qb->getQuery();
$q->setMaxResults(1);
$result = $q->getResult(); $result = $q->getResult();


if ($result) { if ($result) {
Expand Down
Loading

0 comments on commit 7052ee3

Please sign in to comment.