TranslatableCustomEntityRepository::addSortOrder()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Pim\Bundle\CustomEntityBundle\Entity\Repository;
4
5
use Doctrine\ORM\QueryBuilder;
6
7
/**
8
 * Repository for translatable custom entities
9
 *
10
 * @author    Antoine Guigan <[email protected]>
11
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
12
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
13
 */
14
class TranslatableCustomEntityRepository extends CustomEntityRepository
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function createDatagridQueryBuilder()
20
    {
21
        return parent::createDatagridQueryBuilder()
22
            ->leftJoin(
23
                sprintf('%s.translations', $this->getAlias()),
24
                'translation',
25
                'WITH',
26
                'translation.locale=:localeCode'
27
            )
28
            ->addSelect('translation');
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function findBySearchQB($search, array $options)
35
    {
36
        $qb = parent::findBySearchQB($search, $options);
37
38
        $qb
39
            ->leftJoin(
40
                sprintf('%s.translations', $this->getAlias()),
41
                'translation',
42
                'WITH',
43
                'translation.locale=:localeCode'
44
            )
45
            ->setParameter('localeCode', $options['dataLocale']);
46
47
        return $qb;
48
    }
49
50
    /**
51
     * If the column defined as the sorting one belongs to the entity fields, we filter by this field
52
     * Otherwise, we consider that it's a translation one
53
     *
54
     * {@inheritdoc}
55
     */
56
    protected function addSortOrder(QueryBuilder $qb)
57
    {
58
        $sortOrder = $this->getSortOrderColumn();
59
60
        if ($this->getClassMetadata()->hasField($sortOrder)) {
61
            parent::addSortOrder($qb);
62
        } else {
63
            $qb
64
                ->orderBy(sprintf('translation.%s', $sortOrder))
65
                ->addOrderBy(sprintf('%s.code', $this->getAlias()));
66
        }
67
    }
68
69
    /**
70
     * Adds select in the findBySearch method
71
     * Used in products datagrid filtering and product edit form
72
     * This method is used by findBySearch method and it's not recommended to call it from elsewhere
73
     *
74
     * {@inheritdoc}
75
     */
76
    protected function selectFields(QueryBuilder $qb, array $options)
77
    {
78
        $labelProperty = $this->getReferenceDataLabelProperty();
79
80
        if ($this->getClassMetadata()->hasField($labelProperty)) {
81
            parent::selectFields($qb, $options);
82
        } else {
83
            $identifierField = isset($options['type']) && 'code' === $options['type'] ? 'code' : 'id';
84
85
            $qb
86
                ->select(
87
                    sprintf('%s.%s AS id', $this->getAlias(), $identifierField)
88
                )
89
                ->addSelect(
90
                    sprintf(
91
                        'CASE WHEN translation.%s IS NULL '.
92
                        'THEN CONCAT(\'[\', %s.code, \']\') ELSE translation.%s END AS text',
93
                        $labelProperty,
94
                        $this->getAlias(),
95
                        $labelProperty
96
                    )
97
                );
98
        }
99
    }
100
101
    /**
102
     * Adds search on label or code in the findBySearch method
103
     * Used in products datagrid filtering and product edit form
104
     * This method is used by findBySearch method and it's not recommended to call it from elsewhere
105
     *
106
     * {@inheritdoc}
107
     */
108
    protected function addSearchFilter(QueryBuilder $qb, $search)
109
    {
110
        $labelProperty = $this->getReferenceDataLabelProperty();
111
112
        if ($this->getClassMetadata()->hasField($labelProperty)) {
113
            parent::addSearchFilter($qb, $search);
114
        } else {
115
            $searchDql = sprintf(
116
                '%s.code LIKE :search OR translation.%s LIKE :search',
117
                $this->getAlias(),
118
                $labelProperty
119
            );
120
            $qb->andWhere($searchDql)->setParameter('search', "%$search%");
121
        }
122
    }
123
}
124