1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Netgen\InformationCollection\Doctrine\Repository; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
8
|
|
|
use Doctrine\ORM\NonUniqueResultException; |
9
|
|
|
use Doctrine\ORM\NoResultException; |
10
|
|
|
use Doctrine\ORM\ORMException; |
11
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\Field; |
14
|
|
|
use Netgen\InformationCollection\API\Exception\RemoveAttributeFailedException; |
15
|
|
|
use Netgen\InformationCollection\API\Exception\RetrieveCountException; |
16
|
|
|
use Netgen\InformationCollection\API\Exception\StoringAttributeFailedException; |
17
|
|
|
use Netgen\InformationCollection\API\Value\Attribute; |
18
|
|
|
use Netgen\InformationCollection\API\Value\Filter\CollectionId; |
19
|
|
|
use Netgen\InformationCollection\API\Value\Legacy\FieldValue; |
20
|
|
|
use Netgen\InformationCollection\Doctrine\Entity\EzInfoCollection; |
21
|
|
|
use Netgen\InformationCollection\Doctrine\Entity\EzInfoCollectionAttribute; |
22
|
|
|
|
23
|
|
|
class EzInfoCollectionAttributeRepository extends EntityRepository |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Get new \Netgen\InformationCollection\Doctrine\Entity\EzInfoCollectionAttribute instance. |
27
|
|
|
* |
28
|
|
|
* @return \Netgen\InformationCollection\Doctrine\Entity\EzInfoCollectionAttribute |
29
|
|
|
*/ |
30
|
|
|
public function getInstance() |
31
|
|
|
{ |
32
|
|
|
return new EzInfoCollectionAttribute(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function createNewFromValues(Content $content, EzInfoCollection $collection, FieldValue $fieldValue, string $fieldDefIdentifier): EzInfoCollectionAttribute |
36
|
|
|
{ |
37
|
|
|
$ezInfoAttribute = $this->getInstance(); |
38
|
|
|
|
39
|
|
|
$ezInfoAttribute->setContentObjectId($content->contentInfo->id); |
40
|
|
|
$ezInfoAttribute->setInformationCollectionId($collection->getId()); |
41
|
|
|
$ezInfoAttribute->setContentClassAttributeId($fieldValue->getFieldDefinitionId()); |
42
|
|
|
|
43
|
|
|
$field = $content->getField($fieldDefIdentifier); |
44
|
|
|
if ($field instanceof Field) { |
45
|
|
|
$ezInfoAttribute->setContentObjectAttributeId($field->id); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$ezInfoAttribute->setDataInt($fieldValue->getDataInt()); |
49
|
|
|
$ezInfoAttribute->setDataFloat($fieldValue->getDataFloat()); |
50
|
|
|
$ezInfoAttribute->setDataText($fieldValue->getDataText()); |
51
|
|
|
|
52
|
|
|
return $ezInfoAttribute; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Save object. |
57
|
|
|
* |
58
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Entity\EzInfoCollectionAttribute $infoCollectionAttribute |
59
|
|
|
* |
60
|
|
|
* @throws StoringAttributeFailedException |
61
|
|
|
*/ |
62
|
|
|
public function save(EzInfoCollectionAttribute $infoCollectionAttribute) |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
|
|
$this->_em->persist($infoCollectionAttribute); |
66
|
|
|
$this->_em->flush($infoCollectionAttribute); |
67
|
|
|
} catch (ORMException | ORMInvalidArgumentException $exception) { |
68
|
|
|
throw new StoringAttributeFailedException('', ''); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param \Netgen\InformationCollection\Doctrine\Entity\EzInfoCollectionAttribute[] $attributes |
74
|
|
|
* |
75
|
|
|
* @throws ORMException |
76
|
|
|
*/ |
77
|
|
|
public function remove(array $attributes) |
78
|
|
|
{ |
79
|
|
|
try { |
80
|
|
|
foreach ($attributes as $attribute) { |
81
|
|
|
$this->_em->remove($attribute); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->_em->flush(); |
85
|
|
|
} catch (ORMException | ORMInvalidArgumentException $exception) { |
86
|
|
|
throw new RemoveAttributeFailedException('', ''); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function findByCollectionIdAndFieldDefinitionIds($collectionId, $fieldDefinitionIds) |
91
|
|
|
{ |
92
|
|
|
$qb = $this->createQueryBuilder('eica'); |
93
|
|
|
|
94
|
|
|
return $qb->select('eica') |
95
|
|
|
->where('eica.informationCollectionId = :collection-id') |
96
|
|
|
->setParameter('collection-id', $collectionId) |
97
|
|
|
->andWhere($qb->expr()->in('eica.contentClassAttributeId', ':fields')) |
98
|
|
|
->setParameter('fields', $fieldDefinitionIds) |
99
|
|
|
->getQuery() |
100
|
|
|
->getResult(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function updateByCollectionId(CollectionId $collectionId, Attribute $attribute) |
104
|
|
|
{ |
105
|
|
|
$entity = $this->findOneBy([ |
106
|
|
|
'informationCollectionId' => $collectionId->getCollectionId(), |
107
|
|
|
'id' => $attribute->getId(), |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
if (!$entity instanceof EzInfoCollectionAttribute) { |
111
|
|
|
throw new \InvalidArgumentException('Attribute not found.'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$entity->setDataFloat($attribute->getValue()->getDataFloat()); |
115
|
|
|
$entity->setDataInt($attribute->getValue()->getDataInt()); |
116
|
|
|
$entity->setDataText($attribute->getValue()->getDataText()); |
117
|
|
|
|
118
|
|
|
$this->_em->persist($entity); |
119
|
|
|
$this->_em->flush(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function findByCollectionId($collectionId) |
123
|
|
|
{ |
124
|
|
|
$qb = $this->createQueryBuilder('eica'); |
125
|
|
|
|
126
|
|
|
return $qb->select('eica') |
127
|
|
|
->where('eica.informationCollectionId = :collection-id') |
128
|
|
|
->setParameter('collection-id', $collectionId) |
129
|
|
|
->getQuery() |
130
|
|
|
->getResult(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getCountByContentId($contentId) |
134
|
|
|
{ |
135
|
|
|
try { |
136
|
|
|
return (int) $this->createQueryBuilder('eica') |
137
|
|
|
->andWhere('eica.contentObjectId = :contentId') |
138
|
|
|
->setParameter('contentId', $contentId) |
139
|
|
|
->select('COUNT(eica) as children_count') |
140
|
|
|
->getQuery() |
141
|
|
|
->getSingleScalarResult(); |
142
|
|
|
} catch (NonUniqueResultException | NoResultException $exception) { |
143
|
|
|
throw new RetrieveCountException('', ''); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function search($contentId, $searchText) |
148
|
|
|
{ |
149
|
|
|
$searchText = mb_strtolower($searchText); |
150
|
|
|
|
151
|
|
|
$qb = $this->createQueryBuilder('eica'); |
152
|
|
|
|
153
|
|
|
$result = $qb->select('eica.informationCollectionId') |
154
|
|
|
->where('eica.contentObjectId = :contentId') |
155
|
|
|
->setParameter('contentId', $contentId) |
156
|
|
|
->andWhere($qb->expr()->andX( |
157
|
|
|
$qb->expr()->like('LOWER(eica.dataText)', ':searchText') |
158
|
|
|
)) |
159
|
|
|
->setParameter('searchText', '%' . $searchText . '%') |
160
|
|
|
->getQuery() |
161
|
|
|
->getScalarResult(); |
162
|
|
|
|
163
|
|
|
return array_column($result, 'informationCollectionId'); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|