TranslationRepository.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Gedmo\Translatable\Document\Repository;
  3. use Doctrine\ODM\MongoDB\DocumentRepository,
  4. Doctrine\ODM\MongoDB\Cursor;
  5. /**
  6. * The TranslationRepository has some useful functions
  7. * to interact with translations.
  8. *
  9. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  10. * @package Gedmo.Translatable.Document.Repository
  11. * @subpackage TranslationRepository
  12. * @link http://www.gediminasm.org
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class TranslationRepository extends DocumentRepository
  16. {
  17. /**
  18. * Loads all translations with all translatable
  19. * fields from the given entity
  20. *
  21. * @param object $document
  22. * @return array list of translations in locale groups
  23. */
  24. public function findTranslations($document)
  25. {
  26. $result = array();
  27. if ($document) {
  28. $meta = $this->dm->getClassMetadata(get_class($document));
  29. $identifier = $meta->identifier;
  30. $documentId = $meta->getReflectionProperty($identifier)->getValue($document);
  31. $translationMeta = $this->getClassMetadata();
  32. $qb = $this->createQueryBuilder();
  33. $q = $qb->field('foreignKey')->equals($documentId)
  34. ->field('objectClass')->equals($meta->name)
  35. ->sort('locale', 'asc')
  36. ->getQuery();
  37. $q->setHydrate(false);
  38. $data = $q->execute();
  39. if ($data instanceof Cursor) {
  40. $data = $data->toArray();
  41. }
  42. if ($data && is_array($data) && count($data)) {
  43. foreach ($data as $row) {
  44. $result[$row['locale']][$row['field']] = $row['content'];
  45. }
  46. }
  47. }
  48. return $result;
  49. }
  50. /**
  51. * Find the object $class by the translated field.
  52. * Result is the first occurence of translated field.
  53. * Query can be slow, since there are no indexes on such
  54. * columns
  55. *
  56. * @param string $field
  57. * @param string $value
  58. * @param string $class
  59. * @return object - instance of $class or null if not found
  60. */
  61. public function findObjectByTranslatedField($field, $value, $class)
  62. {
  63. $document = null;
  64. $meta = $this->dm->getClassMetadata($class);
  65. $translationMeta = $this->getClassMetadata();
  66. if ($meta->hasField($field)) {
  67. $qb = $this->createQueryBuilder();
  68. $q = $qb->field('field')->equals($field)
  69. ->field('objectClass')->equals($meta->name)
  70. ->field('content')->equals($value)
  71. ->getQuery();
  72. $q->setHydrate(false);
  73. $result = $q->execute();
  74. if ($result instanceof Cursor) {
  75. $result = $data->toArray();
  76. }
  77. $id = count($result) ? $result[0]['foreignKey'] : null;
  78. if ($id) {
  79. $document = $this->dm->find($class, $id);
  80. }
  81. }
  82. return $entity;
  83. }
  84. /**
  85. * Loads all translations with all translatable
  86. * fields by a given document primary key
  87. *
  88. * @param mixed $id - primary key value of document
  89. * @return array
  90. */
  91. public function findTranslationsByObjectId($id)
  92. {
  93. $result = array();
  94. if ($id) {
  95. $translationMeta = $this->getClassMetadata();
  96. $qb = $this->createQueryBuilder();
  97. $q = $qb->field('foreignKey')->equals($id)
  98. ->sort('locale', 'asc')
  99. ->getQuery();
  100. $q->setHydrate(false);
  101. $data = $q->execute();
  102. if ($data instanceof Cursor) {
  103. $data = $data->toArray();
  104. }
  105. if ($data && is_array($data) && count($data)) {
  106. foreach ($data as $row) {
  107. $result[$row['locale']][$row['field']] = $row['content'];
  108. }
  109. }
  110. }
  111. return $result;
  112. }
  113. }