TranslationRepository.php 6.3 KB

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