Completed
Push — master ( ef9fce...66659a )
by
unknown
14s
created

ViewRepository::joinSeoTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Victoire\Bundle\CoreBundle\Repository;
4
5
use Doctrine\ORM\Query;
6
use Doctrine\ORM\Query\Expr;
7
use Gedmo\Tree\Entity\Repository\NestedTreeRepository;
8
use Victoire\Bundle\CoreBundle\Entity\View;
9
use Victoire\Bundle\PageBundle\Entity\Page;
10
use Victoire\Bundle\PageBundle\Entity\PageStatus;
11
use Victoire\Bundle\ViewReferenceBundle\ViewReference\BusinessPageReference;
12
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
13
14
/**
15
 * The View repository.
16
 */
17
class ViewRepository extends NestedTreeRepository
18
{
19
    use StateFullRepositoryTrait;
20
21
    protected $mainAlias = 'view';
22
23
    /**
24
     * Filter the query by the sitemap index (=visibility).
25
     *
26
     * @param bool $indexed
27
     *
28
     * @return ViewRepository
29
     */
30
    public function filterBySitemapIndexed($indexed = true)
31
    {
32
        $qb = $this->getInstance();
33
        $qb->innerJoin($this->mainAlias.'.seo', 'seo')->addSelect('seo')
34
            ->andWhere('seo.sitemapIndexed = :sitemapIndexed')
35
            ->setParameter('sitemapIndexed', $indexed);
36
37
        return $this;
38
    }
39
40
    /**
41
     * Get all rentals in the repository.
42
     *
43
     * @param bool $excludeUnpublished Should we get only the published Views ?
44
     *
45
     * @return ViewRepository
46
     */
47
    public function getAll($excludeUnpublished = false)
48
    {
49
        $this->qb = $this->getInstance();
50
51
        //If $excludeUnpublished === true, we exclude the non published results
52
        if ($excludeUnpublished) {
53
            $this->qb
54
                ->andWhere($this->mainAlias.'.status = :status')
55
                ->orWhere($this->mainAlias.'.status = :scheduled_status AND '.$this->mainAlias.'.publishedAt > :publicationDate')
56
                ->setParameter('status', PageStatus::PUBLISHED)
57
                ->setParameter('scheduled_status', PageStatus::SCHEDULED)
58
                ->setParameter('publicationDate', new \DateTime());
59
        }
60
61
        return $this;
62
    }
63
64
    /**
65
     * Find a large amount of views by ViewReferences.
66
     *
67
     * @param ViewReference[] $viewReferences
68
     *
69
     * @return View[]|null The entity instance or NULL if the entities cannot be found.
70
     */
71
    public function findByViewReferences(array $viewReferences)
72
    {
73
        $pageIds = [];
74
        foreach ($viewReferences as $viewReference) {
75
            if ($viewReference instanceof BusinessPageReference) {
76
                $pageIds[] = $viewReference->getTemplateId();
77
            } else {
78
                $pageIds[] = $viewReference->getViewId();
79
            }
80
        }
81
82
        $qb = $this->createQueryBuilder($this->mainAlias);
83
        $qb->andWhere($this->mainAlias.'.id IN (:pageIds)')
84
            ->setParameter('pageIds', $pageIds);
85
86
        $pages = $qb->getQuery()->getResult();
87
88
        foreach ($pages as $page) {
89
            $pageId = $page->getId();
90
            $viewReference = array_filter(
91
                $viewReferences,
92
                function ($e) use ($pageId) {
93
                    return $e->getViewId() == $pageId;
94
                });
95
            if (!empty($viewReference[0])) {
96
                $page->setCurrentLocale($viewReference[0]->getLocale());
97
            }
98
        }
99
100
        return $pages;
101
    }
102
103
    /**
104
     * Get the the view that is a aage and a published one.
105
     *
106
     * @param string $locale
107
     *
108
     * @return Page
109
     */
110
    public function findOneByHomepage($locale = 'fr')
111
    {
112
        //the query builder
113
        $qb = $this->createQueryBuilder($this->mainAlias);
114
        $qb
115
            ->where($this->mainAlias.'.homepage = true')
116
            ->andWhere($this->mainAlias.'.status = :status')
117
            ->setMaxResults(1)
118
            ->setParameter('status', PageStatus::PUBLISHED);
119
        // Use Translation Walker
120
        $query = $qb->getQuery();
121
        $view = $query->getOneOrNullResult();
122
        $view->translate($locale);
123
124
        return $view;
125
    }
126
127
    /**
128
     * Get PageSeo.
129
     *
130
     * @param string $method leftJoin|innerJoin
131
     *
132
     * @return ViewRepository
133
     */
134
    public function joinSeo($method = 'leftJoin')
135
    {
136
        $this->getInstance()->$method($this->mainAlias.'.seo', 'seo')->addSelect('seo');
137
138
        return $this;
139
    }
140
141
    /**
142
     * Join PageSeoTranslation to PageSeo for a locale.
143
     *
144
     * @param string $locale
145
     *
146
     * @return ViewRepository
147
     */
148
    public function joinSeoTranslations($locale)
149
    {
150
        $this->getInstance()
151
            ->leftJoin('seo.translations', 'translation', Expr\Join::WITH, 'translation.locale = :locale')
152
            ->setParameter('locale', $locale);
153
154
        return $this;
155
    }
156
157
    /**
158
     * Join ViewTranslation for a locale.
159
     *
160
     * @param string $locale
161
     *
162
     * @return ViewRepository
163
     */
164
    public function joinTranslations($locale)
165
    {
166
        $this->getInstance()
167
            ->innerJoin($this->mainAlias.'.translations', 'translation', Expr\Join::WITH, 'translation.locale = :locale')
168
            ->setParameter('locale', $locale);
169
170
        return $this;
171
    }
172
173
    /**
174
     * Filter the query by the sitemap index (=visibility).
175
     *
176
     * @param array $ids
177
     *
178
     * @return ViewRepository
179
     */
180
    public function filterByIds($ids)
181
    {
182
        $this->getInstance()->andWhere($this->mainAlias.'.id IN (:ids)')->setParameter('ids', $ids);
183
184
        return $this;
185
    }
186
}
187