-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModule.php
113 lines (101 loc) · 5.61 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
<?php
namespace DspaceConnector;
use Omeka\Module\AbstractModule;
use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\EventManager\SharedEventManagerInterface;
use Laminas\Mvc\MvcEvent;
use Composer\Semver\Comparator;
class Module extends AbstractModule
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function onBootstrap(MvcEvent $event)
{
parent::onBootstrap($event);
$acl = $this->getServiceLocator()->get('Omeka\Acl');
$acl->allow(
null,
['DspaceConnector\Api\Adapter\DspaceItemAdapter'],
['search', 'read']
);
}
public function install(ServiceLocatorInterface $serviceLocator)
{
$connection = $serviceLocator->get('Omeka\Connection');
$connection->exec("CREATE TABLE dspace_item (id INT AUTO_INCREMENT NOT NULL, item_id INT NOT NULL, job_id INT NOT NULL, api_url VARCHAR(255) NOT NULL, remote_id VARCHAR(36) NOT NULL, handle VARCHAR(255) NOT NULL, last_modified DATETIME NOT NULL, UNIQUE INDEX UNIQ_1C6D63B4126F525E (item_id), INDEX IDX_1C6D63B4BE04EA9 (job_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;");
$connection->exec("ALTER TABLE dspace_item ADD CONSTRAINT FK_1C6D63B4126F525E FOREIGN KEY (item_id) REFERENCES item (id) ON DELETE CASCADE;");
$connection->exec("ALTER TABLE dspace_item ADD CONSTRAINT FK_1C6D63B4BE04EA9 FOREIGN KEY (job_id) REFERENCES job (id);");
$connection->exec("CREATE TABLE dspace_import (id INT AUTO_INCREMENT NOT NULL, job_id INT NOT NULL, undo_job_id INT DEFAULT NULL, rerun_job_id INT DEFAULT NULL, added_count INT NOT NULL, updated_count INT NOT NULL, comment VARCHAR(255) DEFAULT NULL, UNIQUE INDEX UNIQ_56197DADBE04EA9 (job_id), UNIQUE INDEX UNIQ_56197DAD4C276F75 (undo_job_id), UNIQUE INDEX UNIQ_56197DAD7071F49C (rerun_job_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB;");
$connection->exec("ALTER TABLE dspace_import ADD CONSTRAINT FK_56197DADBE04EA9 FOREIGN KEY (job_id) REFERENCES job (id);");
$connection->exec("ALTER TABLE dspace_import ADD CONSTRAINT FK_56197DAD4C276F75 FOREIGN KEY (undo_job_id) REFERENCES job (id);");
$connection->exec("ALTER TABLE dspace_import ADD CONSTRAINT FK_56197DAD7071F49C FOREIGN KEY (rerun_job_id) REFERENCES job (id);");
}
public function uninstall(ServiceLocatorInterface $serviceLocator)
{
$connection = $serviceLocator->get('Omeka\Connection');
$connection->exec("ALTER TABLE dspace_item DROP FOREIGN KEY FK_1C6D63B4126F525E;");
$connection->exec("ALTER TABLE dspace_item DROP FOREIGN KEY FK_1C6D63B4BE04EA9;");
$connection->exec('DROP TABLE dspace_item');
$connection->exec("ALTER TABLE dspace_import DROP FOREIGN KEY FK_56197DADBE04EA9");
$connection->exec("ALTER TABLE dspace_import DROP FOREIGN KEY FK_56197DAD4C276F75");
$connection->exec("ALTER TABLE dspace_import DROP FOREIGN KEY FK_56197DAD7071F49C");
$connection->exec('DROP TABLE dspace_import');
}
public function upgrade($oldVersion, $newVersion, ServiceLocatorInterface $serviceLocator)
{
$connection = $serviceLocator->get('Omeka\Connection');
if (Comparator::lessThan($oldVersion, '0.4.0-alpha')) {
$connection->exec("ALTER TABLE `dspace_item` CHANGE `remote_id` `remote_id` VARCHAR(36) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL;");
}
if (Comparator::lessThan($oldVersion, '1.6.1')) {
$connection->exec("ALTER TABLE dspace_import ADD rerun_job_id INT DEFAULT NULL AFTER undo_job_id;");
$connection->exec("ALTER TABLE dspace_import ADD CONSTRAINT FK_56197DAD7071F49C FOREIGN KEY (rerun_job_id) REFERENCES job (id);");
}
}
public function attachListeners(SharedEventManagerInterface $sharedEventManager)
{
$sharedEventManager->attach(
'Omeka\Controller\Admin\Item',
'view.show.after',
[$this, 'showSource']
);
$sharedEventManager->attach(
\Omeka\Api\Adapter\ItemAdapter::class,
'api.search.query',
[$this, 'importSearch']
);
}
public function showSource($event)
{
$view = $event->getTarget();
$item = $view->item;
$api = $this->getServiceLocator()->get('Omeka\ApiManager');
$response = $api->search('dspace_items', ['item_id' => $item->id()]);
$dspaceItems = $response->getContent();
if ($dspaceItems) {
$dspaceItem = $dspaceItems[0];
$url = 'http://hdl.handle.net/' . $dspaceItem->handle();
echo '<h3>' . $view->translate('Original') . '</h3>';
echo '<p>' . $view->translate('Last Modified') . ' ' . $view->i18n()->dateFormat($dspaceItem->lastModified()) . '</p>';
echo '<p><a href="' . $url . '">' . $view->translate('Link') . '</a></p>';
}
}
public function importSearch($event)
{
$query = $event->getParam('request')->getContent();
if (isset($query['dspace_import_id'])) {
$qb = $event->getParam('queryBuilder');
$adapter = $event->getTarget();
$importItemAlias = $adapter->createAlias();
$qb->innerJoin(
\DspaceConnector\Entity\DspaceItem::class, $importItemAlias,
'WITH', "$importItemAlias.item = omeka_root.id"
)->andWhere($qb->expr()->eq(
"$importItemAlias.job",
$adapter->createNamedParameter($qb, $query['dspace_import_id'])
));
}
}
}