TranslationRepository.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. $modRecordValue = (!$listener->getPersistDefaultLocaleTranslation() && $locale === $listener->getDefaultLocale())
  60. || $listener->getTranslatableLocale($document, $meta) === $locale
  61. ;
  62. if ($modRecordValue) {
  63. $meta->getReflectionProperty($field)->setValue($document, $value);
  64. $this->dm->persist($document);
  65. } else {
  66. if (isset($config['translationClass'])) {
  67. $class = $config['translationClass'];
  68. } else {
  69. $ea = new TranslatableAdapterODM();
  70. $class = $listener->getTranslationClass($ea, $config['useObjectClass']);
  71. }
  72. $foreignKey = $meta->getReflectionProperty($meta->identifier)->getValue($document);
  73. $objectClass = $config['useObjectClass'];
  74. $transMeta = $this->dm->getClassMetadata($class);
  75. $trans = $this->findOneBy(compact('locale', 'field', 'objectClass', 'foreignKey'));
  76. if (!$trans) {
  77. $trans = $transMeta->newInstance();
  78. $transMeta->getReflectionProperty('foreignKey')->setValue($trans, $foreignKey);
  79. $transMeta->getReflectionProperty('objectClass')->setValue($trans, $objectClass);
  80. $transMeta->getReflectionProperty('field')->setValue($trans, $field);
  81. $transMeta->getReflectionProperty('locale')->setValue($trans, $locale);
  82. }
  83. $mapping = $meta->getFieldMapping($field);
  84. $type = Type::getType($mapping['type']);
  85. $transformed = $type->convertToDatabaseValue($value);
  86. $transMeta->getReflectionProperty('content')->setValue($trans, $transformed);
  87. if ($this->dm->getUnitOfWork()->isInIdentityMap($document)) {
  88. $this->dm->persist($trans);
  89. } else {
  90. $oid = spl_object_hash($document);
  91. $listener->addPendingTranslationInsert($oid, $trans);
  92. }
  93. }
  94. return $this;
  95. }
  96. /**
  97. * Loads all translations with all translatable
  98. * fields from the given entity
  99. *
  100. * @param object $document
  101. * @return array list of translations in locale groups
  102. */
  103. public function findTranslations($document)
  104. {
  105. $result = array();
  106. $wrapped = new MongoDocumentWrapper($document, $this->dm);
  107. if ($wrapped->hasValidIdentifier()) {
  108. $documentId = $wrapped->getIdentifier();
  109. $translationMeta = $this->getClassMetadata();
  110. $qb = $this->createQueryBuilder();
  111. $q = $qb->field('foreignKey')->equals($documentId)
  112. ->field('objectClass')->equals($wrapped->getMetadata()->rootDocumentName)
  113. ->sort('locale', 'asc')
  114. ->getQuery();
  115. $q->setHydrate(false);
  116. $data = $q->execute();
  117. if ($data instanceof Cursor) {
  118. $data = $data->toArray();
  119. }
  120. if ($data && is_array($data) && count($data)) {
  121. foreach ($data as $row) {
  122. $result[$row['locale']][$row['field']] = $row['content'];
  123. }
  124. }
  125. }
  126. return $result;
  127. }
  128. /**
  129. * Find the object $class by the translated field.
  130. * Result is the first occurence of translated field.
  131. * Query can be slow, since there are no indexes on such
  132. * columns
  133. *
  134. * @param string $field
  135. * @param string $value
  136. * @param string $class
  137. * @return object - instance of $class or null if not found
  138. */
  139. public function findObjectByTranslatedField($field, $value, $class)
  140. {
  141. $document = null;
  142. $meta = $this->dm->getClassMetadata($class);
  143. $translationMeta = $this->getClassMetadata();
  144. if ($meta->hasField($field)) {
  145. $qb = $this->createQueryBuilder();
  146. $q = $qb->field('field')->equals($field)
  147. ->field('objectClass')->equals($meta->rootDocumentName)
  148. ->field('content')->equals($value)
  149. ->getQuery();
  150. $q->setHydrate(false);
  151. $result = $q->execute();
  152. if ($result instanceof Cursor) {
  153. $result = $result->toArray();
  154. }
  155. $id = count($result) ? $result[0]['foreignKey'] : null;
  156. if ($id) {
  157. $document = $this->dm->find($class, $id);
  158. }
  159. }
  160. return $document;
  161. }
  162. /**
  163. * Loads all translations with all translatable
  164. * fields by a given document primary key
  165. *
  166. * @param mixed $id - primary key value of document
  167. * @return array
  168. */
  169. public function findTranslationsByObjectId($id)
  170. {
  171. $result = array();
  172. if ($id) {
  173. $translationMeta = $this->getClassMetadata();
  174. $qb = $this->createQueryBuilder();
  175. $q = $qb->field('foreignKey')->equals($id)
  176. ->sort('locale', 'asc')
  177. ->getQuery();
  178. $q->setHydrate(false);
  179. $data = $q->execute();
  180. if ($data instanceof Cursor) {
  181. $data = $data->toArray();
  182. }
  183. if ($data && is_array($data) && count($data)) {
  184. foreach ($data as $row) {
  185. $result[$row['locale']][$row['field']] = $row['content'];
  186. }
  187. }
  188. }
  189. return $result;
  190. }
  191. /**
  192. * Get the currently used TranslatableListener
  193. *
  194. * @throws \Gedmo\Exception\RuntimeException - if listener is not found
  195. * @return TranslatableListener
  196. */
  197. private function getTranslatableListener()
  198. {
  199. if (!$this->listener) {
  200. foreach ($this->dm->getEventManager()->getListeners() as $event => $listeners) {
  201. foreach ($listeners as $hash => $listener) {
  202. if ($listener instanceof TranslatableListener) {
  203. $this->listener = $listener;
  204. break;
  205. }
  206. }
  207. if ($this->listener) {
  208. break;
  209. }
  210. }
  211. if (is_null($this->listener)) {
  212. throw new \Gedmo\Exception\RuntimeException('The translation listener could not be found');
  213. }
  214. }
  215. return $this->listener;
  216. }
  217. }