Passed
Pull Request — master (#5753)
by Angel Fernando Quiroz
07:49
created

ExtraFieldRepository::findByVariable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Repository;
8
9
use Chamilo\CoreBundle\Entity\ExtraField;
10
use Chamilo\CoreBundle\Entity\ExtraFieldOptions;
11
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
12
use Doctrine\Persistence\ManagerRegistry;
13
14
class ExtraFieldRepository extends ServiceEntityRepository
15
{
16
    public function __construct(ManagerRegistry $registry)
17
    {
18
        parent::__construct($registry, ExtraField::class);
19
    }
20
21
    /**
22
     * @return ExtraField[]
23
     */
24
    public function getExtraFields(int $type): array
25
    {
26
        $qb = $this->createQueryBuilder('f');
27
        $qb
28
            ->addSelect(
29
                'CASE WHEN f.fieldOrder IS NULL THEN -1 ELSE f.fieldOrder END AS HIDDEN list_order_is_null'
30
            )
31
            ->where(
32
                $qb->expr()->eq('f.visibleToSelf', true),
33
            )
34
            ->andWhere(
35
                $qb->expr()->eq('f.itemType', $type)
36
            )
37
            ->orderBy('list_order_is_null', 'ASC')
38
        ;
39
40
        return $qb->getQuery()->getResult();
41
    }
42
43
    public function getHandlerFieldInfoByFieldVariable(string $variable, int $itemType): bool|array
44
    {
45
        $extraField = $this->findOneBy([
46
            'variable' => $variable,
47
            'itemType' => $itemType,
48
        ]);
49
50
        if (!$extraField) {
51
            return false;
52
        }
53
54
        $fieldInfo = [
55
            'id' => $extraField->getId(),
56
            'variable' => $extraField->getVariable(),
57
            'display_text' => $extraField->getDisplayText(),
58
            'type' => $extraField->getValueType(),
59
            'options' => [],
60
        ];
61
62
        $options = $this->_em->getRepository(ExtraFieldOptions::class)->findBy([
63
            'field' => $extraField,
64
        ]);
65
66
        foreach ($options as $option) {
67
            $fieldInfo['options'][$option->getId()] = [
68
                'id' => $option->getId(),
69
                'value' => $option->getValue(),
70
                'display_text' => $option->getDisplayText(),
71
                'option_order' => $option->getOptionOrder(),
72
            ];
73
        }
74
75
        return $fieldInfo;
76
    }
77
78
    public function findByVariable(int $itemType, string $variable): ?ExtraField
79
    {
80
        return $this->findOneBy(['variable' => $variable, 'itemType' => $itemType]);
81
    }
82
}
83