-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathModule.php
388 lines (364 loc) · 17.7 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
namespace NumericDataTypes;
use Composer\Semver\Comparator;
use Doctrine\Common\Collections\Criteria;
use NumericDataTypes\Form\Element\ConvertToNumeric;
use Omeka\Module\AbstractModule;
use Laminas\EventManager\Event;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
class Module extends AbstractModule
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function install(ServiceLocatorInterface $services)
{
$conn = $services->get('Omeka\Connection');
$conn->exec('CREATE TABLE numeric_data_types_duration (id INT AUTO_INCREMENT NOT NULL, resource_id INT NOT NULL, property_id INT NOT NULL, value BIGINT NOT NULL, INDEX IDX_E1B5FC6089329D25 (resource_id), INDEX IDX_E1B5FC60549213EC (property_id), INDEX property_value (property_id, value), INDEX value (value), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;');
$conn->exec('CREATE TABLE numeric_data_types_integer (id INT AUTO_INCREMENT NOT NULL, resource_id INT NOT NULL, property_id INT NOT NULL, value BIGINT NOT NULL, INDEX IDX_6D39C79089329D25 (resource_id), INDEX IDX_6D39C790549213EC (property_id), INDEX property_value (property_id, value), INDEX value (value), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;');
$conn->exec('CREATE TABLE numeric_data_types_timestamp (id INT AUTO_INCREMENT NOT NULL, resource_id INT NOT NULL, property_id INT NOT NULL, value BIGINT NOT NULL, INDEX IDX_7367AFAA89329D25 (resource_id), INDEX IDX_7367AFAA549213EC (property_id), INDEX property_value (property_id, value), INDEX value (value), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;');
$conn->exec('CREATE TABLE numeric_data_types_interval (id INT AUTO_INCREMENT NOT NULL, resource_id INT NOT NULL, property_id INT NOT NULL, value BIGINT NOT NULL, value2 BIGINT NOT NULL, INDEX IDX_7E2C936B89329D25 (resource_id), INDEX IDX_7E2C936B549213EC (property_id), INDEX property_value (property_id, value), INDEX value (value), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;');
$conn->exec('ALTER TABLE numeric_data_types_duration ADD CONSTRAINT FK_E1B5FC6089329D25 FOREIGN KEY (resource_id) REFERENCES resource (id) ON DELETE CASCADE;');
$conn->exec('ALTER TABLE numeric_data_types_duration ADD CONSTRAINT FK_E1B5FC60549213EC FOREIGN KEY (property_id) REFERENCES property (id) ON DELETE CASCADE;');
$conn->exec('ALTER TABLE numeric_data_types_integer ADD CONSTRAINT FK_6D39C79089329D25 FOREIGN KEY (resource_id) REFERENCES resource (id) ON DELETE CASCADE;');
$conn->exec('ALTER TABLE numeric_data_types_integer ADD CONSTRAINT FK_6D39C790549213EC FOREIGN KEY (property_id) REFERENCES property (id) ON DELETE CASCADE;');
$conn->exec('ALTER TABLE numeric_data_types_timestamp ADD CONSTRAINT FK_7367AFAA89329D25 FOREIGN KEY (resource_id) REFERENCES resource (id) ON DELETE CASCADE;');
$conn->exec('ALTER TABLE numeric_data_types_timestamp ADD CONSTRAINT FK_7367AFAA549213EC FOREIGN KEY (property_id) REFERENCES property (id) ON DELETE CASCADE;');
$conn->exec('ALTER TABLE numeric_data_types_interval ADD CONSTRAINT FK_7E2C936B89329D25 FOREIGN KEY (resource_id) REFERENCES resource (id) ON DELETE CASCADE;');
$conn->exec('ALTER TABLE numeric_data_types_interval ADD CONSTRAINT FK_7E2C936B549213EC FOREIGN KEY (property_id) REFERENCES property (id) ON DELETE CASCADE;');
}
public function uninstall(ServiceLocatorInterface $services)
{
$conn = $services->get('Omeka\Connection');
$conn->exec('DROP TABLE IF EXISTS numeric_data_types_duration;');
$conn->exec('DROP TABLE IF EXISTS numeric_data_types_integer;');
$conn->exec('DROP TABLE IF EXISTS numeric_data_types_timestamp;');
$conn->exec('DROP TABLE IF EXISTS numeric_data_types_interval;');
}
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $services)
{
$conn = $services->get('Omeka\Connection');
if (Comparator::lessThan($oldVersion, '1.1.0-alpha')) {
$conn->exec('CREATE TABLE numeric_data_types_interval (id INT AUTO_INCREMENT NOT NULL, resource_id INT NOT NULL, property_id INT NOT NULL, value BIGINT NOT NULL, value2 BIGINT NOT NULL, INDEX IDX_7E2C936B89329D25 (resource_id), INDEX IDX_7E2C936B549213EC (property_id), INDEX property_value (property_id, value), INDEX value (value), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;');
$conn->exec('ALTER TABLE numeric_data_types_interval ADD CONSTRAINT FK_7E2C936B89329D25 FOREIGN KEY (resource_id) REFERENCES resource (id) ON DELETE CASCADE;');
$conn->exec('ALTER TABLE numeric_data_types_interval ADD CONSTRAINT FK_7E2C936B549213EC FOREIGN KEY (property_id) REFERENCES property (id) ON DELETE CASCADE;');
}
if (Comparator::lessThan($oldVersion, '1.2.0')) {
// The numeric_data_types_duration table was mistakenly not created
// in the previous upgrade. Create it now.
$conn->exec('CREATE TABLE numeric_data_types_duration (id INT AUTO_INCREMENT NOT NULL, resource_id INT NOT NULL, property_id INT NOT NULL, value BIGINT NOT NULL, INDEX IDX_E1B5FC6089329D25 (resource_id), INDEX IDX_E1B5FC60549213EC (property_id), INDEX property_value (property_id, value), INDEX value (value), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB;');
$conn->exec('ALTER TABLE numeric_data_types_duration ADD CONSTRAINT FK_E1B5FC6089329D25 FOREIGN KEY (resource_id) REFERENCES resource (id) ON DELETE CASCADE;');
$conn->exec('ALTER TABLE numeric_data_types_duration ADD CONSTRAINT FK_E1B5FC60549213EC FOREIGN KEY (property_id) REFERENCES property (id) ON DELETE CASCADE;');
}
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
$sharedEventManager->attach(
'Omeka\Api\Adapter\ItemAdapter',
'api.search.query',
[$this, 'buildQueries']
);
$sharedEventManager->attach(
'Omeka\Api\Adapter\ItemAdapter',
'api.search.query',
[$this, 'sortQueries']
);
$sharedEventManager->attach(
'Omeka\Api\Adapter\ItemAdapter',
'api.hydrate.post',
[$this, 'convertToNumeric'],
100 // Set a high priority so this runs before saveNumericData().
);
$sharedEventManager->attach(
'Omeka\Api\Adapter\ItemAdapter',
'api.hydrate.post',
[$this, 'saveNumericData']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.sort-selector',
[$this, 'addSortings']
);
$sharedEventManager->attach(
'Omeka\Controller\Site\Item',
'view.sort-selector',
[$this, 'addSortings']
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.advanced_search',
function (Event $event) {
$partials = $event->getParam('partials');
$partials[] = 'common/numeric-data-types-advanced-search';
$event->setParam('partials', $partials);
}
);
$sharedEventManager->attach(
'Omeka\Controller\Site\Item',
'view.advanced_search',
function (Event $event) {
$partials = $event->getParam('partials');
$partials[] = 'common/numeric-data-types-advanced-search';
$event->setParam('partials', $partials);
}
);
$sharedEventManager->attach(
'Omeka\Form\ResourceBatchUpdateForm',
'form.add_elements',
function (Event $event) {
$form = $event->getTarget();
$form->add([
'type' => ConvertToNumeric::class,
'name' => 'numeric_convert',
]);
}
);
$sharedEventManager->attach(
'Omeka\Api\Adapter\ItemAdapter',
'api.preprocess_batch_update',
function (Event $event) {
$data = $event->getParam('data');
$rawData = $event->getParam('request')->getContent();
if ($this->convertToNumericDataIsValid($rawData)) {
$data['numeric_convert'] = $rawData['numeric_convert'];
}
$event->setParam('data', $data);
}
);
}
/**
* Convert property values to the specified numeric data type.
*
* This will work for Item, ItemSet, and Media resources.
*
* @param Event $event
*/
public function convertToNumeric(Event $event)
{
$entity = $event->getParam('entity');
if ($entity instanceof \Omeka\Entity\Item) {
$resource = 'items';
} elseif ($entity instanceof \Omeka\Entity\ItemSet) {
$resource = 'item_sets';
} elseif ($entity instanceof \Omeka\Entity\Media) {
$resource = 'media';
} else {
return; // This is not a resource entity.
}
$data = $event->getParam('request')->getContent();
if (!$this->convertToNumericDataIsValid($data)) {
return; // This is not a convert-to-numeric request.
}
$propertyId = (int) $data['numeric_convert']['property'];
$type = $data['numeric_convert']['type'];
$services = $this->getServiceLocator();
$entityManager = $services->get('Omeka\EntityManager');
$dataType = $services->get('Omeka\DataTypeManager')->get($type);
$adapter = $services->get('Omeka\ApiAdapterManager')->get($resource);
$logger = $services->get('Omeka\Logger');
// Get the property entity.
$dql = 'SELECT p FROM Omeka\Entity\Property p WHERE p.id = :id';
$property = $entityManager->createQuery($dql)
->setParameter('id', $propertyId)
->getOneOrNullResult();
if (null === $property) {
return; // The property doesn't exist. Do nothing.
}
// Only convert literal values of the specified property.
$criteria = Criteria::create()
->where(Criteria::expr()->eq('property', $property))
->andWhere(Criteria::expr()->eq('type', 'literal'));
$values = $entity->getValues()->matching($criteria);
foreach ($values as $value) {
$valueObject = ['@value' => $value->getValue()];
if ($dataType->isValid($valueObject)) {
$value->setType($type);
$dataType->hydrate($valueObject, $value, $adapter);
} else {
$message = sprintf(
'NumericDataTypes - invalid %s value for ID %s - %s', // @translate
$type, $entity->getId(), $value->getValue()
);
$logger->notice($message);
}
}
}
/**
* Save numeric data to the corresponding number tables.
*
* This clears all existing numbers and (re)saves them during create and
* update operations for a resource (item, item set, media). We do this as
* an easy way to ensure that the numbers in the number tables are in sync
* with the numbers in the value table.
*
* This will work for Item, ItemSet, and Media resources.
*
* @param Event $event
*/
public function saveNumericData(Event $event)
{
$entity = $event->getParam('entity');
if (!$entity instanceof \Omeka\Entity\Resource) {
return; // This is not a resource entity.
}
$allValues = $entity->getValues();
foreach ($this->getNumericDataTypes() as $dataTypeName => $dataType) {
$criteria = Criteria::create()
->where(Criteria::expr()
->eq('type', $dataTypeName));
$matchingValues = $allValues->matching($criteria);
if (!$matchingValues) {
// This resource has no number values of this type.
continue;
}
$em = $this->getServiceLocator()->get('Omeka\EntityManager');
$existingNumbers = [];
if ($entity->getId()) {
$dql = sprintf(
'SELECT n FROM %s n WHERE n.resource = :resource',
$dataType->getEntityClass()
);
$query = $em->createQuery($dql);
$query->setParameter('resource', $entity);
$existingNumbers = $query->getResult();
}
foreach ($matchingValues as $value) {
// Avoid ID churn by reusing number rows.
$number = current($existingNumbers);
if ($number === false) {
// No more number rows to reuse. Create a new one.
$entityClass = $dataType->getEntityClass();
$number = new $entityClass;
$em->persist($number);
} else {
// Null out numbers as we reuse them. Note that existing
// numbers are already managed and will update during flush.
$existingNumbers[key($existingNumbers)] = null;
next($existingNumbers);
}
$number->setResource($entity);
$number->setProperty($value->getProperty());
$dataType->setEntityValues($number, $value);
}
// Remove any numbers that weren't reused.
foreach ($existingNumbers as $existingNumber) {
if (null !== $existingNumber) {
$em->remove($existingNumber);
}
}
}
}
/**
* Build numerical queries.
*
* @param Event $event
*/
public function buildQueries(Event $event)
{
$query = $event->getParam('request')->getContent();
if (!isset($query['numeric'])) {
return;
}
$adapter = $event->getTarget();
$qb = $event->getParam('queryBuilder');
foreach ($this->getNumericDataTypes() as $dataType) {
$dataType->buildQuery($adapter, $qb, $query);
}
}
/**
* Sort numerical queries.
*
* sort_by=numeric:<type>:<propertyId>
*
* @param Event $event
*/
public function sortQueries(Event $event)
{
$adapter = $event->getTarget();
$qb = $event->getParam('queryBuilder');
$query = $event->getParam('request')->getContent();
if (!isset($query['sort_by']) || !is_string($query['sort_by'])) {
return;
}
$sortBy = explode(':', $query['sort_by']);
if (3 !== count($sortBy)) {
return;
}
[$namespace, $type, $propertyId] = $sortBy;
if ('numeric' !== $namespace || !is_string($type) || !is_numeric($propertyId)) {
return;
}
foreach ($this->getNumericDataTypes() as $dataType) {
$dataType->sortQuery($adapter, $qb, $query, $type, $propertyId);
}
}
/**
* Add numeric sort options to sort by form.
*
* @param Event $event
*/
public function addSortings(Event $event)
{
$services = $this->getServiceLocator();
$translator = $services->get('MvcTranslator');
$entityManager = $services->get('Omeka\EntityManager');
$qb = $entityManager->createQueryBuilder();
$qb->select(['p.id', 'p.label', 'rtp.dataType'])
->from('Omeka\Entity\ResourceTemplateProperty', 'rtp')
->innerJoin('rtp.property', 'p');
$qb->andWhere($qb->expr()->isNotNull('rtp.dataType'));
$query = $qb->getQuery();
$numericDataTypes = $this->getNumericDataTypes();
$numericSortBy = [];
foreach ($query->getResult() as $templatePropertyData) {
$dataTypes = $templatePropertyData['dataType'] ?? [];
foreach ($dataTypes as $dataType) {
if (isset($numericDataTypes[$dataType])) {
$value = sprintf('%s:%s', $dataType, $templatePropertyData['id']);
if (!isset($numericSortBy[$value])) {
$numericSortBy[$value] = sprintf('%s (%s)', $translator->translate($templatePropertyData['label']), $dataType);
}
}
}
}
// Sort options alphabetically.
asort($numericSortBy);
$sortConfig = $event->getParam('sortConfig') ?: [];
$sortConfig = array_merge($sortConfig, $numericSortBy);
$event->setParam('sortConfig', $sortConfig);
}
/**
* Get all data types added by this module.
*
* @return array
*/
public function getNumericDataTypes()
{
$dataTypes = $this->getServiceLocator()->get('Omeka\DataTypeManager');
$numericDataTypes = [];
foreach ($dataTypes->getRegisteredNames() as $dataType) {
if (0 === strpos($dataType, 'numeric:')) {
$numericDataTypes[$dataType] = $dataTypes->get($dataType);
}
}
return $numericDataTypes;
}
/**
* Does the passed data contain valid convert-to-numeric data?
*
* @param array $data
* return bool
*/
public function convertToNumericDataIsValid(array $data)
{
$validTypes = array_keys($this->getNumericDataTypes());
return (
isset($data['numeric_convert']['property'])
&& is_numeric($data['numeric_convert']['property'])
&& isset($data['numeric_convert']['type'])
&& in_array($data['numeric_convert']['type'], $validTypes)
);
}
}