1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2016 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\CoreBundle\Entity; |
||
13 | |||
14 | use Doctrine\Common\Collections\ArrayCollection; |
||
15 | use Doctrine\Common\Collections\Collection; |
||
16 | use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder; |
||
17 | |||
18 | trait TranslationEntityTrait |
||
19 | { |
||
20 | /** |
||
21 | * Set by AbstractCommonModel::getEntityBySlugs() if a language slug was used to fetch the entity. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | public $languageSlug; |
||
26 | |||
27 | /** |
||
28 | * @var ArrayCollection |
||
29 | **/ |
||
30 | private $translationChildren; |
||
31 | |||
32 | /** |
||
33 | * @var TranslationEntityInterface |
||
34 | **/ |
||
35 | private $translationParent; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $language = 'en'; |
||
41 | |||
42 | /** |
||
43 | * @param ClassMetadata $builder |
||
44 | * @param $entityClass |
||
45 | * @param string $languageColumnName |
||
46 | */ |
||
47 | protected static function addTranslationMetadata(ClassMetadataBuilder $builder, $entityClass, $languageColumnName = 'lang') |
||
48 | { |
||
49 | $builder->createOneToMany('translationChildren', $entityClass) |
||
50 | ->setIndexBy('id') |
||
51 | ->setOrderBy(['isPublished' => 'DESC']) |
||
52 | ->mappedBy('translationParent') |
||
53 | ->build(); |
||
54 | |||
55 | $builder->createManyToOne('translationParent', $entityClass) |
||
56 | ->inversedBy('translationChildren') |
||
57 | ->addJoinColumn('translation_parent_id', 'id', true, false, 'CASCADE') |
||
58 | ->build(); |
||
59 | |||
60 | $builder->createField('language', 'string') |
||
61 | ->columnName($languageColumnName) |
||
62 | ->build(); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Add translation. |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function addTranslationChild(TranslationEntityInterface $child) |
||
71 | { |
||
72 | if (!$this->translationChildren->contains($child)) { |
||
73 | $this->translationChildren[] = $child; |
||
74 | } |
||
75 | |||
76 | return $this; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Remove translation. |
||
81 | */ |
||
82 | public function removeTranslationChild(TranslationEntityInterface $child) |
||
83 | { |
||
84 | $this->translationChildren->removeElement($child); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Get translated items. |
||
89 | * |
||
90 | * @return \Doctrine\Common\Collections\Collection |
||
91 | */ |
||
92 | public function getTranslationChildren() |
||
93 | { |
||
94 | return $this->translationChildren; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Set translation parent. |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function setTranslationParent(TranslationEntityInterface $parent = null) |
||
103 | { |
||
104 | if (method_exists($this, 'isChanged')) { |
||
105 | $this->isChanged('translationParent', $parent); |
||
106 | } |
||
107 | |||
108 | $this->translationParent = $parent; |
||
109 | |||
110 | return $this; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get translation parent. |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function getTranslationParent() |
||
119 | { |
||
120 | return $this->translationParent; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Remove translation parent. |
||
125 | */ |
||
126 | public function removeTranslationParent() |
||
127 | { |
||
128 | if (method_exists($this, 'isChanged')) { |
||
129 | $this->isChanged('translationParent', ''); |
||
130 | } |
||
131 | |||
132 | $this->translationParent = null; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Set language. |
||
137 | * |
||
138 | * @param string $language |
||
139 | * |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function setLanguage($language) |
||
143 | { |
||
144 | if (method_exists($this, 'isChanged')) { |
||
145 | $this->isChanged('language', $language); |
||
146 | } |
||
147 | |||
148 | $this->language = $language; |
||
149 | |||
150 | return $this; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Get language. |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getLanguage() |
||
159 | { |
||
160 | return $this->language; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param bool $isChild True to return if the item is a translation of a parent |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function isTranslation($isChild = false) |
||
169 | { |
||
170 | $parent = $this->getTranslationParent(); |
||
171 | $children = $this->getTranslationChildren(); |
||
172 | |||
173 | if ($isChild) { |
||
174 | return (null === $parent) ? false : true; |
||
175 | } else { |
||
176 | return (!empty($parent) || count($children)) ? true : false; |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Check if this entity has translations. |
||
182 | * |
||
183 | * @return int |
||
184 | */ |
||
185 | public function hasTranslations() |
||
186 | { |
||
187 | $children = $this->getTranslationChildren(); |
||
188 | |||
189 | return count($children); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Clear translations. |
||
194 | */ |
||
195 | public function clearTranslations() |
||
196 | { |
||
197 | $this->translationChildren = new ArrayCollection(); |
||
198 | $this->translationParent = null; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Get translation parent/children. |
||
203 | * |
||
204 | * @param bool $onlyChildren |
||
205 | * |
||
206 | * @return array|\Doctrine\Common\Collections\ArrayCollection |
||
207 | */ |
||
208 | public function getTranslations($onlyChildren = false) |
||
209 | { |
||
210 | $parent = $this->getTranslationParent(); |
||
211 | |||
212 | if (empty($parent)) { |
||
213 | $parent = $this; |
||
214 | } |
||
215 | |||
216 | if ($children = $parent->getTranslationChildren()) { |
||
217 | if ($children instanceof Collection) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
218 | $children = $children->toArray(); |
||
219 | } |
||
220 | } |
||
221 | |||
222 | if (!is_array($children)) { |
||
223 | $children = []; |
||
224 | } |
||
225 | |||
226 | if ($onlyChildren) { |
||
227 | return $children; |
||
228 | } |
||
229 | |||
230 | return [$parent, $children]; |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @param $getter |
||
235 | * |
||
236 | * @return mixed |
||
237 | */ |
||
238 | protected function getAccumulativeTranslationCount($getter, $variantParent = null) |
||
239 | { |
||
240 | $count = 0; |
||
241 | |||
242 | list($parent, $children) = $this->getTranslations(); |
||
243 | if ($variantParent != $parent) { |
||
244 | $count = $parent->$getter(); |
||
245 | } |
||
246 | |||
247 | foreach ($children as $translation) { |
||
248 | if ($variantParent != $translation) { |
||
249 | $count += $translation->$getter(); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | return $count; |
||
254 | } |
||
255 | } |
||
256 |