-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathModule.php
More file actions
331 lines (312 loc) · 15.5 KB
/
Module.php
File metadata and controls
331 lines (312 loc) · 15.5 KB
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
<?php
namespace NumericDataTypes;
use Composer\Semver\Comparator;
use Doctrine\Common\Collections\Criteria;
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)
{
$sql = <<<'SQL'
CREATE TABLE numeric_data_types_integer (id INT AUTO_INCREMENT NOT NULL, resource_id INT NOT NULL, property_id INT NOT NULL, value NUMERIC(32, 16) 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;
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;
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;
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;
ALTER TABLE numeric_data_types_integer ADD CONSTRAINT FK_6D39C79089329D25 FOREIGN KEY (resource_id) REFERENCES resource (id) ON DELETE CASCADE;
ALTER TABLE numeric_data_types_integer ADD CONSTRAINT FK_6D39C790549213EC FOREIGN KEY (property_id) REFERENCES property (id) ON DELETE CASCADE;
ALTER TABLE numeric_data_types_duration ADD CONSTRAINT FK_E1B5FC6089329D25 FOREIGN KEY (resource_id) REFERENCES resource (id) ON DELETE CASCADE;
ALTER TABLE numeric_data_types_duration ADD CONSTRAINT FK_E1B5FC60549213EC FOREIGN KEY (property_id) REFERENCES property (id) ON DELETE CASCADE;
ALTER TABLE numeric_data_types_timestamp ADD CONSTRAINT FK_7367AFAA89329D25 FOREIGN KEY (resource_id) REFERENCES resource (id) ON DELETE CASCADE;
ALTER TABLE numeric_data_types_timestamp ADD CONSTRAINT FK_7367AFAA549213EC FOREIGN KEY (property_id) REFERENCES property (id) ON DELETE CASCADE;
ALTER TABLE numeric_data_types_interval ADD CONSTRAINT FK_7E2C936B89329D25 FOREIGN KEY (resource_id) REFERENCES resource (id) ON DELETE CASCADE;
ALTER TABLE numeric_data_types_interval ADD CONSTRAINT FK_7E2C936B549213EC FOREIGN KEY (property_id) REFERENCES property (id) ON DELETE CASCADE;
SQL;
$conn = $services->get('Omeka\Connection');
$conn->exec($sql);
}
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;');
}
if (Comparator::lessThan($oldVersion, '1.14.0')) {
// Changing from BIGINT to NUMERIC(32,16) should be safe because all
// existing values should fit within the specified precision and
// scale. This is because, prior to this update, the
// "numeric:integer" data type enforced a compatible minimum and
// maximum.
$conn->exec('ALTER TABLE numeric_data_types_integer CHANGE value value NUMERIC(32, 16) NOT NULL;');
}
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
$eventIds = [
'Omeka\Api\Adapter\ItemAdapter',
'Omeka\Api\Adapter\ItemSetAdapter',
'Omeka\Api\Adapter\MediaAdapter',
];
foreach ($eventIds as $eventId) {
$sharedEventManager->attach(
$eventId,
'api.search.query',
[$this, 'buildQueries']
);
$sharedEventManager->attach(
$eventId,
'api.search.query',
[$this, 'sortQueries']
);
$sharedEventManager->attach(
$eventId,
'api.hydrate.post',
[$this, 'saveNumericData']
);
}
$eventIds = [
'Omeka\Controller\Admin\Item',
'Omeka\Controller\Site\Item',
];
foreach ($eventIds as $eventId) {
$sharedEventManager->attach(
$eventId,
'view.sort-selector',
function (Event $event) {
$sortings = $this->getSortings('Omeka\Entity\Item');
$sortConfig = $event->getParam('sortConfig') ?: [];
$sortConfig = array_merge($sortConfig, $sortings);
$event->setParam('sortConfig', $sortConfig);
}
);
}
$sharedEventManager->attach(
'Omeka\Controller\Admin\ItemSet',
'view.sort-selector',
function (Event $event) {
$sortings = $this->getSortings('Omeka\Entity\ItemSet');
$sortConfig = $event->getParam('sortConfig') ?: [];
$sortConfig = array_merge($sortConfig, $sortings);
$event->setParam('sortConfig', $sortConfig);
}
);
$sharedEventManager->attach(
'Omeka\Controller\Admin\Media',
'view.sort-selector',
function (Event $event) {
$sortings = $this->getSortings('Omeka\Entity\Media');
$sortConfig = $event->getParam('sortConfig') ?: [];
$sortConfig = array_merge($sortConfig, $sortings);
$event->setParam('sortConfig', $sortConfig);
}
);
$eventIds = [
'Omeka\Controller\Admin\Item',
'Omeka\Controller\Admin\ItemSet',
'Omeka\Controller\Admin\Media',
'Omeka\Controller\Site\Item',
];
foreach ($eventIds as $eventId) {
$sharedEventManager->attach(
$eventId,
'view.advanced_search',
function (Event $event) {
$partials = $event->getParam('partials');
$partials[] = 'common/numeric-data-types-advanced-search';
$event->setParam('partials', $partials);
}
);
}
// Add JS to FacetedBrowse category form.
$sharedEventManager->attach(
'FacetedBrowse\Controller\SiteAdmin\Category',
'view.faceted_browse.category_form',
function (Event $event) {
$view = $event->getTarget();
$view->headScript()->appendFile($view->assetUrl('js/faceted-browse/category-form.js', 'NumericDataTypes'));
}
);
}
/**
* 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);
}
}
/**
* Get numeric sort options for sort by form.
*
* @param string $instanceOf Omeka\Entity\Item, Omeka\Entity\ItemSet, Omeka\Entity\Media
*/
public function getSortings($instanceOf)
{
$services = $this->getServiceLocator();
$entityManager = $services->get('Omeka\EntityManager');
$translator = $services->get('MvcTranslator');
$numericDataTypes = $this->getNumericDataTypes();
$sortings = [];
foreach ($numericDataTypes as $numericDataType) {
// Get only those properties that have corresponding numeric data
// type values that are assigned to resources of the passed instance.
$dql = sprintf('SELECT DISTINCT property.id, property.label
FROM %s ndt
JOIN ndt.property property
JOIN ndt.resource resource
WHERE resource INSTANCE OF %s',
$numericDataType->getEntityClass(),
$instanceOf
);
$query = $entityManager->createQuery($dql);
$properties = $query->getResult();
foreach ($properties as $property) {
$sortingKey = sprintf('%s:%s', $numericDataType->getName(), $property['id']);
$sortingValue = sprintf('%s (%s)', $translator->translate($property['label']), $numericDataType->getName());
$sortings[$sortingKey] = $sortingValue;
}
}
asort($sortings);
return $sortings;
}
/**
* 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;
}
}