-
Notifications
You must be signed in to change notification settings - Fork 2
/
OmekaApiImportPlugin.php
122 lines (109 loc) · 4.04 KB
/
OmekaApiImportPlugin.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
<?php
class OmekaApiImportPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'install',
'uninstall',
'upgrade',
'after_delete_record',
'define_acl'
);
protected $_filters = array('admin_navigation_main');
public function hookInstall()
{
$db = get_db();
$sql = "
CREATE TABLE IF NOT EXISTS `$db->OmekaApiImportRecordIdMap` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`local_id` int(11) NOT NULL,
`record_type` varchar(50) COLLATE latin1_general_ci NOT NULL,
`external_id` int(11) NOT NULL,
`endpoint_uri` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `external_id` (`record_type`,`external_id`,`endpoint_uri`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
";
$db->query($sql);
$sql = "
CREATE TABLE IF NOT EXISTS `$db->OmekaApiImport` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`endpoint_uri` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`status` tinytext NOT NULL,
`date` timestamp,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
";
$db->query($sql);
}
public function hookUninstall()
{
$db = get_db();
$sql = "DROP TABLE IF EXISTS `$db->OmekaApiImportRecordIdMap`";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `$db->OmekaApiImport`";
$db->query($sql);
}
public function hookUpgrade($args)
{
$oldVersion = $args['old_version'];
$newVersion = $args['new_version'];
if (version_compare($oldVersion, '1.1', '<')) {
$db = get_db();
$sql = "
CREATE TABLE IF NOT EXISTS `$db->OmekaApiImport` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`endpoint_uri` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`status` tinytext NOT NULL,
`date` timestamp,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
";
$db->query($sql);
$sql = "
SELECT DISTINCT `endpoint_uri`
FROM `$db->OmekaApiImportRecordIdMap`
WHERE 1
";
$uris = $db->fetchCol($sql);
if (! empty($uris)) {
$values = array();
foreach ($uris as $uri) {
$values[] = "(NULL, '$uri', '', CURRENT_TIMESTAMP)";
}
$sql = "
INSERT INTO `$db->OmekaApiImport` (`id`, `endpoint_uri`, `status`, `date`) VALUES
" . join(", ", $values);
$db->query($sql);
}
}
}
public function hookDefineAcl($args)
{
$acl = $args['acl'];
$acl->addResource('OmekaApiImport_Index');
$acl->allow('super', 'OmekaApiImport_Index');
$acl->deny(array('admin', 'contributor', 'researcher'), 'OmekaApiImport_Index');
}
public function filterAdminNavigationMain($nav)
{
if (is_allowed('OmekaApiImport_Index', 'index')) {
$nav[] = array('label' => __('Omeka API Import'),
'uri' => url('omeka-api-import/index/index')
);
}
return $nav;
}
public function hookAfterDeleteRecord($args)
{
$record = $args['record'];
if ($record instanceof Plugin) {
return;
}
$apiRecordMap = get_db()->getTable('OmekaApiImportRecordIdMap')->findBy(array('local_id' => $record->id,
'record_type' => get_class($record)));
if(!empty($apiRecordMap)) {
$apiRecordMap = $apiRecordMap[0];
$apiRecordMap->delete();
}
}
}