1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Entity\Repository; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\CMSBundle\Entity\Node; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Benjamin HUBERT <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class NodeRepository extends EntityRepository |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param $locale |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
public function findAllWithLocale($locale) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
return $this |
20
|
|
|
->createQueryBuilder('n') |
21
|
|
|
->andWhere('n.published = true') |
22
|
|
|
->andWhere('n.locale = :locale') |
23
|
|
|
->setParameter('locale', $locale) |
24
|
|
|
->orderBy('n.position', 'ASC') |
25
|
|
|
->getQuery() |
26
|
|
|
->getResult(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $slug |
31
|
|
|
* @return \Alpixel\Bundle\CMSBundle\Entity\Node|null |
32
|
|
|
*/ |
33
|
|
|
public function findOnePublishedBySlug($slug) |
34
|
|
|
{ |
35
|
|
|
return $this |
36
|
|
|
->createQueryBuilder('n') |
37
|
|
|
->andWhere('n.published = true') |
38
|
|
|
->andWhere('n.slug = :slug') |
39
|
|
|
->setParameter('slug', $slug) |
40
|
|
|
->getQuery() |
41
|
|
|
->getOneOrNullResult(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $slug |
46
|
|
|
* @param $locale |
47
|
|
|
* @return \Alpixel\Bundle\CMSBundle\Entity\Node|null |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function findOnePublishedBySlugAndLocale($slug, $locale) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
return $this |
52
|
|
|
->createQueryBuilder('n') |
53
|
|
|
->andWhere('n.published = true') |
54
|
|
|
->andWhere('n.locale = :locale') |
55
|
|
|
->andWhere('n.slug = :slug') |
56
|
|
|
->setParameter('locale', $locale) |
57
|
|
|
->setParameter('slug', $slug) |
58
|
|
|
->getQuery() |
59
|
|
|
->getOneOrNullResult(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $slug |
64
|
|
|
* @param $locale |
65
|
|
|
* @return \Alpixel\Bundle\CMSBundle\Entity\Node|null |
66
|
|
|
*/ |
67
|
|
|
public function findOneBySlugAndLocale($slug, $locale) |
68
|
|
|
{ |
69
|
|
|
return $this |
70
|
|
|
->createQueryBuilder('n') |
71
|
|
|
->andWhere('n.locale = :locale') |
72
|
|
|
->andWhere('n.slug = :slug') |
73
|
|
|
->setParameter('locale', $locale) |
74
|
|
|
->setParameter('slug', $slug) |
75
|
|
|
->getQuery() |
76
|
|
|
->getOneOrNullResult(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param \Alpixel\Bundle\CMSBundle\Entity\Node $node |
81
|
|
|
* @param $locale |
82
|
|
|
* @return \Alpixel\Bundle\CMSBundle\Entity\Node|null |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function findTranslation(Node $node, $locale) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$nodeSource = null; |
|
|
|
|
87
|
|
|
|
88
|
|
|
// We are checking if the node is the translation provider or translated |
89
|
|
|
// from an other node |
90
|
|
|
if ($node->getTranslationSource() !== null) { |
91
|
|
|
$nodeSource = $node->getTranslationSource(); |
92
|
|
|
if ($nodeSource->getLocale() == $locale) { |
93
|
|
|
return $nodeSource; |
94
|
|
|
} |
95
|
|
|
} else { |
96
|
|
|
$nodeSource = $node; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->createQueryBuilder('n') |
100
|
|
|
->addSelect('n') |
101
|
|
|
->andWhere('n.translationSource = :source') |
102
|
|
|
->andWhere('n.locale = :locale') |
103
|
|
|
->setParameters( |
104
|
|
|
[ |
105
|
|
|
'source' => $nodeSource, |
106
|
|
|
'locale' => $locale, |
107
|
|
|
] |
108
|
|
|
) |
109
|
|
|
->getQuery() |
110
|
|
|
->getOneOrNullResult(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param \Alpixel\Bundle\CMSBundle\Entity\Node $node |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function findTranslations(Node $node) |
118
|
|
|
{ |
119
|
|
|
$nodeSource = null; |
|
|
|
|
120
|
|
|
|
121
|
|
|
// We are checking if the node is the translation provider or translated |
122
|
|
|
// from an other node |
123
|
|
|
if ($node->getTranslationSource() !== null) { |
124
|
|
|
$nodeSource = $node->getTranslationSource(); |
125
|
|
|
} else { |
126
|
|
|
$nodeSource = $node; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->createQueryBuilder('n') |
130
|
|
|
->addSelect('n') |
131
|
|
|
->orWhere('n.translationSource = :source') |
132
|
|
|
->orWhere('n.id = :id') |
133
|
|
|
->setParameters( |
134
|
|
|
[ |
135
|
|
|
'source' => $nodeSource, |
136
|
|
|
'id' => $nodeSource->getId(), |
137
|
|
|
] |
138
|
|
|
) |
139
|
|
|
->getQuery() |
140
|
|
|
->getResult(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.