This repository has been archived by the owner on Oct 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
FedoraConnectorPlugin.php
279 lines (220 loc) · 6.85 KB
/
FedoraConnectorPlugin.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
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 cc=80; */
/**
* @package omeka
* @subpackage fedora-connector
* @copyright 2012 Rector and Board of Visitors, University of Virginia
* @license http://www.apache.org/licenses/LICENSE-2.0.html
*/
class FedoraConnectorPlugin extends Omeka_Plugin_AbstractPlugin
{
protected $_hooks = array(
'install',
'uninstall',
'define_routes',
'after_save_item',
'admin_head',
'admin_items_show',
'public_items_show'
);
protected $_filters = array(
'admin_items_form_tabs',
'admin_navigation_main',
'exhibit_attachment_markup'
);
/**
* Load the objects table.
*/
public function __construct()
{
parent::__construct();
$this->_objects = $this->_db->getTable('FedoraConnectorObject');
}
/**
* Create servers and objects tables.
*/
public function hookInstall()
{
$this->_db->query(<<<SQL
CREATE TABLE IF NOT EXISTS
{$this->_db->prefix}fedora_connector_servers (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
name TINYTEXT NOT NULL,
url TINYTEXT NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SQL
);
$this->_db->query(<<<SQL
CREATE TABLE IF NOT EXISTS
{$this->_db->prefix}fedora_connector_objects (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
item_id INT(10) UNSIGNED NOT NULL,
server_id INT(10) UNSIGNED NOT NULL,
pid TINYTEXT NOT NULL,
dsids TINYTEXT NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SQL
);
}
/**
* Drop tables.
*/
public function hookUninstall()
{
$this->_db->query(<<<SQL
DROP TABLE {$this->_db->prefix}fedora_connector_servers
SQL
);
$this->_db->query(<<<SQL
DROP TABLE {$this->_db->prefix}fedora_connector_objects
SQL
);
}
/**
* Register routes.
*
* @param array $args Contains: `router` (Zend_Config).
*/
public function hookDefineRoutes($args)
{
$args['router']->addConfig(new Zend_Config_Ini(
FEDORA_DIR . '/routes.ini'
));
}
/**
* Add plugin static assets.
*
* @param Zend_Controller_Request_Http $request The request.
*/
public function hookAdminHead($args)
{
// Get request module and action.
$controller = Zend_Controller_Front::getInstance();
$cname = $controller->getRequest()->getControllerName();
$module = $controller->getRequest()->getModuleName();
$action = $controller->getRequest()->getActionName();
// queue_js_string("console.log('hookAdminHead: $cname/$module/$action');");
// Server browse CSS:
if ($module == 'fedora-connector' && $action == 'browse') {
queue_css_file('browse');
}
// Datastreams form JS:
if ($cname != 'users' && $module == 'default'
&& ($action == 'add' || $action == 'edit')) {
queue_js_file('payloads/datastreams');
queue_js_file('fedora-bootstrap');
}
}
/**
* Add Fedora tab to the Items interface.
*
* @param array $tabs Array of tab names => markup.
*
* @return array Updated $tabs array.
*/
public function filterAdminItemsFormTabs($tabs)
{
// Get the form markup.
$form = new FedoraConnector_Form_Object();
$form->removeDecorator('form');
// Get the item.
$item = get_current_record('item');
if ($item->exists()) {
// Try to get a datastream.
$object = $this->_objects->findByItem($item);
// Populate fields.
if ($object) {
$form->populate(array(
'server' => $object->server_id,
'pid' => $object->pid,
'saved-dsids' => $object->dsids
));
}
}
// Add tab.
$tabs['Fedora'] = $form;
return $tabs;
}
/**
* Save / update datastream, import from Fedora.
*
* @param array $args
*/
public function hookAfterSaveItem($args)
{
$item = $args['record'];
$post = $args['post'];
// TODO|refactor
// Only try to run the import if the Item form is being saved, and a
// POST array is defined. Is there no clearer way to do this?
if (!$post) return;
// Create or update the datastream.
$object = $this->_objects->createOrUpdate(
$item, (int) $post['server'], $post['pid'], $post['dsids']
);
// Perform the import.
if ((bool) $post['import']) {
$importer = new FedoraConnector_Import();
$importer->import($object);
}
}
/**
* Add link to admin menu bar.
*
* @param array $tabs Tabs, <LABEL> => <URI> pairs.
* @return array The tab array with the "Fedora Connector" tab.
*/
public function filterAdminNavigationMain($tabs)
{
$tabs[] = array(
'label' => 'Fedora Connector', 'uri' => url('fedora-connector')
);
return $tabs;
}
/**
* Render the datastream on admin show page.
*/
public function hookAdminItemsShow($args)
{
$dom = fc_displayObject($args['item']);
if (!is_null($dom)) {
echo $dom->saveHTML();
}
}
/**
* Render the datastream on public show page.
*/
public function hookPublicItemsShow($args)
{
if (array_key_exists('item', $args)) {
$dom = fc_displayObject($args['item']);
if (!is_null($dom)) {
echo $dom->saveHTML();
}
}
}
public function filterExhibitAttachmentMarkup($html, $options)
{
$item = $options['attachment']->getItem();
if ($item === null) {
return;
}
if (fc_isFedoraStream($item)) {
$uri = exhibit_builder_exhibit_item_uri($item);
$dom = fc_displayObject($item);
if ($dom->documentElement->tagName == 'img') {
$caption = $options['attachment']->caption;
$caption = strip_tags($caption, '<br>');
$el = $dom->documentElement;
$el->setAttribute('alt', $caption);
$el->setAttribute('title', $caption);
}
$img = $dom->saveHTML();
$html = "<a href=\"$uri\" class=\"exhibit-item-link\">$img</a>"
. get_view()->exhibitAttachmentCaption($options['attachment']);
}
return $html;
}
}