TranslationRepository.php 7.5 KB

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