Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway;
8
9 use Doctrine\DBAL\Connection;
10 use eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway;
11 use eZ\Publish\SPI\Persistence\Content\Field;
12 use eZ\Publish\SPI\Persistence\Content\VersionInfo;
13 use PDO;
14
15 class DoctrineStorage extends Gateway
16 {
17 const MAP_LOCATION_TABLE = 'ezgmaplocation';
18
20 protected $connection;
21
22 public function __construct(Connection $connection)
23 {
24 $this->connection = $connection;
25 }
26
38 public function storeFieldData(VersionInfo $versionInfo, Field $field)
39 {
40 if ($field->value->externalData === null) {
41 // Store empty value and return
42 $this->deleteFieldData($versionInfo, [$field->id]);
43 $field->value->data = [
44 'sortKey' => null,
45 'hasData' => false,
46 ];
47
48 return false;
49 }
50
51 if ($this->hasFieldData($field->id, $versionInfo->versionNo)) {
52 $this->updateFieldData($versionInfo, $field);
53 } else {
54 $this->storeNewFieldData($versionInfo, $field);
55 }
56
57 $field->value->data = [
58 'sortKey' => $field->value->externalData['address'],
59 'hasData' => true,
60 ];
61
62 return true;
63 }
64
73 protected function updateFieldData(VersionInfo $versionInfo, Field $field)
74 {
75 $updateQuery = $this->connection->createQueryBuilder();
76 $updateQuery->update($this->connection->quoteIdentifier(self::MAP_LOCATION_TABLE))
77 ->set($this->connection->quoteIdentifier('latitude'), ':latitude')
78 ->set($this->connection->quoteIdentifier('longitude'), ':longitude')
79 ->set($this->connection->quoteIdentifier('address'), ':address')
80 ->where(
81 $updateQuery->expr()->andX(
82 $updateQuery->expr()->eq(
83 $this->connection->quoteIdentifier('contentobject_attribute_id'),
84 ':fieldId'
85 ),
86 $updateQuery->expr()->eq(
87 $this->connection->quoteIdentifier('contentobject_version'),
88 ':versionNo'
89 )
90 )
91 )
92 ->setParameter(':latitude', $field->value->externalData['latitude'])
93 ->setParameter(':longitude', $field->value->externalData['longitude'])
94 ->setParameter(':address', $field->value->externalData['address'])
95 ->setParameter(':fieldId', $field->id, PDO::PARAM_INT)
96 ->setParameter(':versionNo', $versionInfo->versionNo, PDO::PARAM_INT)
97 ;
98
99 $updateQuery->execute();
100 }
101
108 protected function storeNewFieldData(VersionInfo $versionInfo, Field $field)
109 {
110 $insertQuery = $this->connection->createQueryBuilder();
111 $insertQuery
112 ->insert($this->connection->quoteIdentifier(self::MAP_LOCATION_TABLE))
113 ->values([
114 'latitude' => ':latitude',
115 'longitude' => ':longitude',
116 'address' => ':address',
117 'contentobject_attribute_id' => ':fieldId',
118 'contentobject_version' => ':versionNo',
119 ])
120 ->setParameter(':latitude', $field->value->externalData['latitude'])
121 ->setParameter(':longitude', $field->value->externalData['longitude'])
122 ->setParameter(':address', $field->value->externalData['address'])
123 ->setParameter(':fieldId', $field->id)
124 ->setParameter(':versionNo', $versionInfo->versionNo)
125 ;
126
127 $insertQuery->execute();
128 }
129
138 public function getFieldData(VersionInfo $versionInfo, Field $field)
139 {
140 $field->value->externalData = $this->loadFieldData($field->id, $versionInfo->versionNo);
141 }
142
153 protected function loadFieldData($fieldId, $versionNo)
154 {
155 $selectQuery = $this->connection->createQueryBuilder();
156 $selectQuery
157 ->select(
158 $this->connection->quoteIdentifier('latitude'),
159 $this->connection->quoteIdentifier('longitude'),
160 $this->connection->quoteIdentifier('address')
161 )
162 ->from($this->connection->quoteIdentifier('ezgmaplocation'))
163 ->where(
164 $selectQuery->expr()->andX(
165 $selectQuery->expr()->eq(
166 $this->connection->quoteIdentifier('contentobject_attribute_id'),
167 ':fieldId'
168 ),
169 $selectQuery->expr()->eq(
170 $this->connection->quoteIdentifier('contentobject_version'),
171 ':versionNo'
172 )
173 )
174 )
175 ->setParameter(':fieldId', $fieldId, PDO::PARAM_INT)
176 ->setParameter(':versionNo', $versionNo, PDO::PARAM_INT)
177 ;
178
179 $statement = $selectQuery->execute();
180
181 $rows = $statement->fetchAll(PDO::FETCH_ASSOC);
182 if (!isset($rows[0])) {
183 return null;
184 }
185
186 // Cast coordinates as the DB can return them as strings
187 $rows[0]['latitude'] = (float)$rows[0]['latitude'];
188 $rows[0]['longitude'] = (float)$rows[0]['longitude'];
189
190 return $rows[0];
191 }
192
201 protected function hasFieldData($fieldId, $versionNo)
202 {
203 return $this->loadFieldData($fieldId, $versionNo) !== null;
204 }
205
212 public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds)
213 {
214 if (empty($fieldIds)) {
215 // Nothing to do
216 return;
217 }
218
219 $deleteQuery = $this->connection->createQueryBuilder();
220 $deleteQuery
221 ->delete($this->connection->quoteIdentifier(self::MAP_LOCATION_TABLE))
222 ->where(
223 $deleteQuery->expr()->andX(
224 $deleteQuery->expr()->in(
225 $this->connection->quoteIdentifier('contentobject_attribute_id'),
226 ':fieldIds'
227 ),
228 $deleteQuery->expr()->eq(
229 $this->connection->quoteIdentifier('contentobject_version'),
230 ':versionNo'
231 )
232 )
233 )
234 ->setParameter(':fieldIds', $fieldIds, Connection::PARAM_INT_ARRAY)
235 ->setParameter(':versionNo', $versionInfo->versionNo, PDO::PARAM_INT)
236 ;
237
238 $deleteQuery->execute();
239 }
240 }
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway\DoctrineStorage
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:16
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway\DoctrineStorage\updateFieldData
updateFieldData(VersionInfo $versionInfo, Field $field)
Perform an update on the field data.
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:73
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway\DoctrineStorage\loadFieldData
loadFieldData($fieldId, $versionNo)
Return the data for the given $fieldId.
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:153
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway\DoctrineStorage\hasFieldData
hasFieldData($fieldId, $versionNo)
Return if field data exists for $fieldId.
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:201
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway\DoctrineStorage\getFieldData
getFieldData(VersionInfo $versionInfo, Field $field)
Set the loaded field data into $field->externalData.
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:138
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway\DoctrineStorage\$connection
$connection
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:20
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway\DoctrineStorage\deleteFieldData
deleteFieldData(VersionInfo $versionInfo, array $fieldIds)
Delete the data for all given $fieldIds.
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:212
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway\DoctrineStorage\storeNewFieldData
storeNewFieldData(VersionInfo $versionInfo, Field $field)
Store new field data.
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:108
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway\DoctrineStorage\storeFieldData
storeFieldData(VersionInfo $versionInfo, Field $field)
Store the data stored in the given $field.
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:38
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway\DoctrineStorage\MAP_LOCATION_TABLE
const MAP_LOCATION_TABLE
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:17
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway\DoctrineStorage\__construct
__construct(Connection $connection)
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:22
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway
MapLocation Field Type external storage gateway.
Definition: FieldType/MapLocation/MapLocationStorage/Gateway.php:17
eZ\Publish\SPI\Persistence\Content\Field
Definition: SPI/Persistence/Content/Field.php:12
eZ\Publish\SPI\Persistence\Content\VersionInfo
This class holds version information data.
Definition: SPI/Persistence/Content/VersionInfo.php:15
eZ\Publish\Core\FieldType\MapLocation\MapLocationStorage\Gateway
Definition: MapLocation/MapLocationStorage/Gateway/DoctrineStorage.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.