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 = null; |
||
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 | * @param TranslationEntityInterface $translationChildren |
||
69 | * |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function addTranslationChild(TranslationEntityInterface $child) |
||
73 | { |
||
74 | if (!$this->translationChildren->contains($child)) { |
||
75 | $this->translationChildren[] = $child; |
||
76 | } |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Remove translation. |
||
83 | * |
||
84 | * @param TranslationEntityInterface $child |
||
85 | */ |
||
86 | public function removeTranslationChild(TranslationEntityInterface $child) |
||
87 | { |
||
88 | $this->translationChildren->removeElement($child); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Get translated items. |
||
93 | * |
||
94 | * @return \Doctrine\Common\Collections\Collection |
||
95 | */ |
||
96 | public function getTranslationChildren() |
||
97 | { |
||
98 | return $this->translationChildren; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Set translation parent. |
||
103 | * |
||
104 | * @param TranslationEntityInterface $translationParent |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function setTranslationParent(TranslationEntityInterface $parent = null) |
||
109 | { |
||
110 | if (method_exists($this, 'isChanged')) { |
||
111 | $this->isChanged('translationParent', $parent); |
||
112 | } |
||
113 | |||
114 | $this->translationParent = $parent; |
||
115 | |||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get translation parent. |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function getTranslationParent() |
||
125 | { |
||
126 | return $this->translationParent; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Remove translation parent. |
||
131 | */ |
||
132 | public function removeTranslationParent() |
||
133 | { |
||
134 | if (method_exists($this, 'isChanged')) { |
||
135 | $this->isChanged('translationParent', ''); |
||
136 | } |
||
137 | |||
138 | $this->translationParent = null; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Set language. |
||
143 | * |
||
144 | * @param string $language |
||
145 | * |
||
146 | * @return $this |
||
147 | */ |
||
148 | public function setLanguage($language) |
||
149 | { |
||
150 | if (method_exists($this, 'isChanged')) { |
||
151 | $this->isChanged('language', $language); |
||
152 | } |
||
153 | |||
154 | $this->language = $language; |
||
155 | |||
156 | return $this; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get language. |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | public function getLanguage() |
||
165 | { |
||
166 | return $this->language; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param bool $isChild True to return if the item is a translation of a parent |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | public function isTranslation($isChild = false) |
||
175 | { |
||
176 | $parent = $this->getTranslationParent(); |
||
177 | $children = $this->getTranslationChildren(); |
||
178 | |||
179 | if ($isChild) { |
||
180 | return ($parent === null) ? false : true; |
||
181 | } else { |
||
182 | return (!empty($parent) || count($children)) ? true : false; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Check if this entity has translations. |
||
188 | * |
||
189 | * @return int |
||
190 | */ |
||
191 | public function hasTranslations() |
||
192 | { |
||
193 | $children = $this->getTranslationChildren(); |
||
194 | |||
195 | return count($children); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Clear translations. |
||
200 | */ |
||
201 | public function clearTranslations() |
||
202 | { |
||
203 | $this->translationChildren = new ArrayCollection(); |
||
204 | $this->translationParent = null; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Get translation parent/children. |
||
209 | * |
||
210 | * @param bool $onlyChildren |
||
211 | * |
||
212 | * @return array|\Doctrine\Common\Collections\ArrayCollection |
||
213 | */ |
||
214 | public function getTranslations($onlyChildren = false) |
||
215 | { |
||
216 | $parent = $this->getTranslationParent(); |
||
217 | |||
218 | if (empty($parent)) { |
||
219 | $parent = $this; |
||
220 | } |
||
221 | |||
222 | if ($children = $parent->getTranslationChildren()) { |
||
223 | if ($children instanceof Collection) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
224 | $children = $children->toArray(); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | if (!is_array($children)) { |
||
229 | $children = []; |
||
230 | } |
||
231 | |||
232 | if ($onlyChildren) { |
||
233 | return $children; |
||
234 | } |
||
235 | |||
236 | return [$parent, $children]; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param $getter |
||
241 | * |
||
242 | * @return mixed |
||
243 | */ |
||
244 | protected function getAccumulativeTranslationCount($getter, $variantParent = null) |
||
245 | { |
||
246 | $count = 0; |
||
247 | |||
248 | list($parent, $children) = $this->getTranslations(); |
||
249 | if ($variantParent != $parent) { |
||
250 | $count = $parent->$getter(); |
||
251 | } |
||
252 | |||
253 | foreach ($children as $translation) { |
||
254 | if ($variantParent != $translation) { |
||
255 | $count += $translation->$getter(); |
||
256 | } |
||
257 | } |
||
258 | |||
259 | return $count; |
||
260 | } |
||
261 | } |
||
262 |