/library/Zly/Doctrine/Translatable/Entity/Repository/TranslationRepository.php

https://github.com/x3ak/zly · PHP · 180 lines · 111 code · 12 blank · 57 comment · 16 complexity · 2a06d711f9ad816ffd5fed6566b4ba2e MD5 · raw file

  1. <?php
  2. namespace Zly\Doctrine\Translatable\Entity\Repository;
  3. use Zly\Doctrine\Translatable\TranslationListener;
  4. use Doctrine\ORM\EntityRepository;
  5. use Doctrine\ORM\Query;
  6. use Zly\Doctrine\Tool\Wrapper\EntityWrapper;
  7. /**
  8. * The TranslationRepository has some useful functions
  9. * to interact with translations.
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Zly\Doctrine.Translatable.Entity.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 EntityRepository
  18. {
  19. /**
  20. * Current TranslationListener instance used
  21. * in EntityManager
  22. *
  23. * @var TranslationListener
  24. */
  25. private $listener;
  26. /**
  27. * Makes additional translation of $entity $field into $locale
  28. * using $value
  29. *
  30. * @param object $entity
  31. * @param string $field
  32. * @param string $locale
  33. * @param mixed $value
  34. * @return TranslationRepository
  35. */
  36. public function translate($entity, $field, $locale, $value)
  37. {
  38. $meta = $this->_em->getClassMetadata(get_class($entity));
  39. $config = $this->getTranslationListener()->getConfiguration($this->_em, $meta->name);
  40. if (!isset($config['fields']) || !in_array($field, $config['fields'])) {
  41. throw new \Zly\Doctrine\Exception\InvalidArgumentException("Entity: {$meta->name} does not translate - {$field}");
  42. }
  43. $oid = spl_object_hash($entity);
  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 $entity Must implement Translatable
  52. * @return array list of translations in locale groups
  53. */
  54. public function findTranslations($entity)
  55. {
  56. $result = array();
  57. $wrapped = new EntityWrapper($entity, $this->_em);
  58. if ($wrapped->hasValidIdentifier()) {
  59. $entityId = $wrapped->getIdentifier();
  60. $entityClass = $wrapped->getMetadata()->name;
  61. $translationMeta = $this->getClassMetadata(); // table inheritance support
  62. $qb = $this->_em->createQueryBuilder();
  63. $qb->select('trans.content, trans.field, trans.locale')
  64. ->from($translationMeta->rootEntityName, 'trans')
  65. ->where('trans.foreignKey = :entityId', 'trans.objectClass = :entityClass')
  66. ->orderBy('trans.locale');
  67. $q = $qb->getQuery();
  68. $data = $q->execute(
  69. compact('entityId', 'entityClass'),
  70. Query::HYDRATE_ARRAY
  71. );
  72. if ($data && is_array($data) && count($data)) {
  73. foreach ($data as $row) {
  74. $result[$row['locale']][$row['field']] = $row['content'];
  75. }
  76. }
  77. }
  78. return $result;
  79. }
  80. /**
  81. * Find the entity $class by the translated field.
  82. * Result is the first occurence of translated field.
  83. * Query can be slow, since there are no indexes on such
  84. * columns
  85. *
  86. * @param string $field
  87. * @param string $value
  88. * @param string $class
  89. * @return object - instance of $class or null if not found
  90. */
  91. public function findObjectByTranslatedField($field, $value, $class)
  92. {
  93. $entity = null;
  94. $meta = $this->_em->getClassMetadata($class);
  95. $translationMeta = $this->getClassMetadata(); // table inheritance support
  96. if ($meta->hasField($field)) {
  97. $dql = "SELECT trans.foreignKey FROM {$translationMeta->rootEntityName} trans";
  98. $dql .= ' WHERE trans.objectClass = :class';
  99. $dql .= ' AND trans.field = :field';
  100. $dql .= ' AND trans.content = :value';
  101. $q = $this->_em->createQuery($dql);
  102. $q->setParameters(compact('class', 'field', 'value'));
  103. $q->setMaxResults(1);
  104. $result = $q->getArrayResult();
  105. $id = count($result) ? $result[0]['foreignKey'] : null;
  106. if ($id) {
  107. $entity = $this->_em->find($class, $id);
  108. }
  109. }
  110. return $entity;
  111. }
  112. /**
  113. * Loads all translations with all translatable
  114. * fields by a given entity primary key
  115. *
  116. * @param mixed $id - primary key value of an entity
  117. * @return array
  118. */
  119. public function findTranslationsByObjectId($id)
  120. {
  121. $result = array();
  122. if ($id) {
  123. $translationMeta = $this->getClassMetadata(); // table inheritance support
  124. $qb = $this->_em->createQueryBuilder();
  125. $qb->select('trans.content, trans.field, trans.locale')
  126. ->from($translationMeta->rootEntityName, 'trans')
  127. ->where('trans.foreignKey = :entityId')
  128. ->orderBy('trans.locale');
  129. $q = $qb->getQuery();
  130. $data = $q->execute(
  131. array('entityId' => $id),
  132. Query::HYDRATE_ARRAY
  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 \Zly\Doctrine\Exception\RuntimeException - if listener is not found
  146. * @return TranslationListener
  147. */
  148. private function getTranslationListener()
  149. {
  150. if (!$this->listener) {
  151. foreach ($this->_em->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 \Zly\Doctrine\Exception\RuntimeException('The translation listener could not be found');
  164. }
  165. }
  166. return $this->listener;
  167. }
  168. }