TranslationRepository.php 5.8 KB

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