TranslationRepository.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace Gedmo\Translatable\Document\Repository;
  3. use Gedmo\Translatable\TranslatableListener;
  4. use Doctrine\ODM\MongoDB\DocumentRepository;
  5. use Doctrine\ODM\MongoDB\Cursor;
  6. use Doctrine\ODM\MongoDB\DocumentManager;
  7. use Doctrine\ODM\MongoDB\UnitOfWork;
  8. use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
  9. use Gedmo\Tool\Wrapper\MongoDocumentWrapper;
  10. use Gedmo\Translatable\Mapping\Event\Adapter\ODM as TranslatableAdapterODM;
  11. use Doctrine\ODM\MongoDB\Mapping\Types\Type;
  12. /**
  13. * The TranslationRepository has some useful functions
  14. * to interact with translations.
  15. *
  16. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  17. * @package Gedmo.Translatable.Document.Repository
  18. * @subpackage TranslationRepository
  19. * @link http://www.gediminasm.org
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. class TranslationRepository extends DocumentRepository
  23. {
  24. /**
  25. * Current TranslatableListener instance used
  26. * in EntityManager
  27. *
  28. * @var TranslatableListener
  29. */
  30. private $listener;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function __construct(DocumentManager $dm, UnitOfWork $uow, ClassMetadata $class)
  35. {
  36. if ($class->getReflectionClass()->isSubclassOf('Gedmo\Translatable\Document\MappedSuperclass\AbstractPersonalTranslation')) {
  37. throw new \Gedmo\Exception\UnexpectedValueException('This repository is useless for personal translations');
  38. }
  39. parent::__construct($dm, $uow, $class);
  40. }
  41. /**
  42. * Makes additional translation of $document $field into $locale
  43. * using $value
  44. *
  45. * @param object $document
  46. * @param string $field
  47. * @param string $locale
  48. * @param mixed $value
  49. * @return TranslationRepository
  50. */
  51. public function translate($document, $field, $locale, $value)
  52. {
  53. $meta = $this->dm->getClassMetadata(get_class($document));
  54. $listener = $this->getTranslatableListener();
  55. $config = $listener->getConfiguration($this->dm, $meta->name);
  56. if (!isset($config['fields']) || !in_array($field, $config['fields'])) {
  57. throw new \Gedmo\Exception\InvalidArgumentException("Document: {$meta->name} does not translate field - {$field}");
  58. }
  59. if (in_array($locale, array($listener->getDefaultLocale(), $listener->getTranslatableLocale($document, $meta)))) {
  60. $meta->getReflectionProperty($field)->setValue($document, $value);
  61. $this->dm->persist($document);
  62. } else {
  63. $ea = new TranslatableAdapterODM();
  64. $foreignKey = $meta->getReflectionProperty($meta->identifier)->getValue($document);
  65. $objectClass = $meta->name;
  66. $class = $listener->getTranslationClass($ea, $meta->name);
  67. $transMeta = $this->dm->getClassMetadata($class);
  68. $trans = $this->findOneBy(compact('locale', 'field', 'objectClass', 'foreignKey'));
  69. if (!$trans) {
  70. $trans = $transMeta->newInstance();
  71. $transMeta->getReflectionProperty('foreignKey')->setValue($trans, $foreignKey);
  72. $transMeta->getReflectionProperty('objectClass')->setValue($trans, $objectClass);
  73. $transMeta->getReflectionProperty('field')->setValue($trans, $field);
  74. $transMeta->getReflectionProperty('locale')->setValue($trans, $locale);
  75. }
  76. $mapping = $meta->getFieldMapping($field);
  77. $type = Type::getType($mapping['type']);
  78. $transformed = $type->convertToDatabaseValue($value);
  79. $transMeta->getReflectionProperty('content')->setValue($trans, $transformed);
  80. if ($this->dm->getUnitOfWork()->isInIdentityMap($document)) {
  81. $this->dm->persist($trans);
  82. } else {
  83. $oid = spl_object_hash($document);
  84. $listener->addPendingTranslationInsert($oid, $trans);
  85. }
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Loads all translations with all translatable
  91. * fields from the given entity
  92. *
  93. * @param object $document
  94. * @return array list of translations in locale groups
  95. */
  96. public function findTranslations($document)
  97. {
  98. $result = array();
  99. $wrapped = new MongoDocumentWrapper($document, $this->dm);
  100. if ($wrapped->hasValidIdentifier()) {
  101. $documentId = $wrapped->getIdentifier();
  102. $translationMeta = $this->getClassMetadata();
  103. $qb = $this->createQueryBuilder();
  104. $q = $qb->field('foreignKey')->equals($documentId)
  105. ->field('objectClass')->equals($wrapped->getMetadata()->name)
  106. ->sort('locale', 'asc')
  107. ->getQuery();
  108. $q->setHydrate(false);
  109. $data = $q->execute();
  110. if ($data instanceof Cursor) {
  111. $data = $data->toArray();
  112. }
  113. if ($data && is_array($data) && count($data)) {
  114. foreach ($data as $row) {
  115. $result[$row['locale']][$row['field']] = $row['content'];
  116. }
  117. }
  118. }
  119. return $result;
  120. }
  121. /**
  122. * Find the object $class by the translated field.
  123. * Result is the first occurence of translated field.
  124. * Query can be slow, since there are no indexes on such
  125. * columns
  126. *
  127. * @param string $field
  128. * @param string $value
  129. * @param string $class
  130. * @return object - instance of $class or null if not found
  131. */
  132. public function findObjectByTranslatedField($field, $value, $class)
  133. {
  134. $document = null;
  135. $meta = $this->dm->getClassMetadata($class);
  136. $translationMeta = $this->getClassMetadata();
  137. if ($meta->hasField($field)) {
  138. $qb = $this->createQueryBuilder();
  139. $q = $qb->field('field')->equals($field)
  140. ->field('objectClass')->equals($meta->name)
  141. ->field('content')->equals($value)
  142. ->getQuery();
  143. $q->setHydrate(false);
  144. $result = $q->execute();
  145. if ($result instanceof Cursor) {
  146. $result = $result->toArray();
  147. }
  148. $id = count($result) ? $result[0]['foreignKey'] : null;
  149. if ($id) {
  150. $document = $this->dm->find($class, $id);
  151. }
  152. }
  153. return $document;
  154. }
  155. /**
  156. * Loads all translations with all translatable
  157. * fields by a given document primary key
  158. *
  159. * @param mixed $id - primary key value of document
  160. * @return array
  161. */
  162. public function findTranslationsByObjectId($id)
  163. {
  164. $result = array();
  165. if ($id) {
  166. $translationMeta = $this->getClassMetadata();
  167. $qb = $this->createQueryBuilder();
  168. $q = $qb->field('foreignKey')->equals($id)
  169. ->sort('locale', 'asc')
  170. ->getQuery();
  171. $q->setHydrate(false);
  172. $data = $q->execute();
  173. if ($data instanceof Cursor) {
  174. $data = $data->toArray();
  175. }
  176. if ($data && is_array($data) && count($data)) {
  177. foreach ($data as $row) {
  178. $result[$row['locale']][$row['field']] = $row['content'];
  179. }
  180. }
  181. }
  182. return $result;
  183. }
  184. /**
  185. * Get the currently used TranslatableListener
  186. *
  187. * @throws \Gedmo\Exception\RuntimeException - if listener is not found
  188. * @return TranslatableListener
  189. */
  190. private function getTranslatableListener()
  191. {
  192. if (!$this->listener) {
  193. foreach ($this->dm->getEventManager()->getListeners() as $event => $listeners) {
  194. foreach ($listeners as $hash => $listener) {
  195. if ($listener instanceof TranslatableListener) {
  196. $this->listener = $listener;
  197. break;
  198. }
  199. }
  200. if ($this->listener) {
  201. break;
  202. }
  203. }
  204. if (is_null($this->listener)) {
  205. throw new \Gedmo\Exception\RuntimeException('The translation listener could not be found');
  206. }
  207. }
  208. return $this->listener;
  209. }
  210. }