forked from annewootton/PBCore-Element-Set
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPBCoreElementSetPlugin.php
More file actions
243 lines (213 loc) · 7.68 KB
/
Copy pathPBCoreElementSetPlugin.php
File metadata and controls
243 lines (213 loc) · 7.68 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
<?php
/**
* PBCore Element Set plugin
*
* Creates element set for PBCore (Public Broadcasting Metadata Dictionary), a
* standard for digitalized documents (see http://pbcore.org).
*
* @copyright Pop Up Archive, 2012
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/
/**
* The PBCore Element Set plugin.
* @package Omeka\Plugins\PBCoreElementSet
*/
class PBCoreElementSetPlugin extends Omeka_Plugin_AbstractPlugin
{
private $_elementSetName = 'PBCore';
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array(
'install',
'uninstall',
'config_form',
'config',
'after_save_item',
'public_theme_header',
// BeamMeUpToInternetArchive hook used to get list of metadata.
'beamia_set_settings',
);
/**
* @var array Filters for the plugin.
*/
protected $_filters = array(
'response_contexts',
'action_contexts',
);
/**
* @var array This plugin's options.
*/
protected $_options = array(
'pbcore_element_set_add_url_as_identifier' => false,
);
/**
* Install the plugin.
*/
public function hookInstall()
{
$this->_installOptions();
// Load elements to add.
require_once('elements.php');
// Don't install if an element set already exists.
if ($this->_getElementSet($this->_elementSetName)) {
throw new Omeka_Plugin_Installer_Exception('An element set by the name "' . $this->_elementSetName . '" already exists. You must delete that element set to install this plugin.');
}
insert_element_set($elementSetMetadata, $elements);
}
/**
* Uninstall the plugin.
*/
public function hookUninstall()
{
$this->_deleteElementSet($this->_elementSetName);
$this->_uninstallOptions();
}
/**
* Displays configuration form.
*
* @return void
*/
public function hookConfigForm()
{
include('config_form.php');
}
/**
* Saves plugin configuration.
*
* @return void
*/
public function hookConfig($args)
{
$post = $args['post'];
set_option('pbcore_element_set_add_url_as_identifier', $post['PBCoreElementSetAddUrlAsIdentifier']);
}
/**
* The recommended pbcore identifier is to use the omeka url with current
* id, so we need to update the item once it is saved for the first time
* (insert). An option is added to disable this feature.
*/
public function hookAfterSaveItem($args)
{
$post = $args['post'];
$item = $args['record'];
// Add the default identifier if wished.
if ($args['insert'] && (boolean) get_option('pbcore_element_set_add_url_as_identifier')) {
$text = WEB_ROOT . '/items/show/' . $item->id;
// Check if this url already exists as an identifier.
$elementTexts = $item->getElementTexts('PBCore', 'Identifier');
foreach ($elementTexts as $key => $elementText) {
$elementTexts[$key] = $elementText->text;
}
if (!in_array($text, $elementTexts)) {
//// We use direct sql to avoid some problems with this update, in
//// particular when there are attached files.
// $elementTexts = array($this->_elementSetName => array('Identifier' => array(array(
// 'text' => $text,
// 'html' => false,
// ))));
// update_item($item, array(), $elementTexts);
static $elementId;
if (!$elementId) {
$elementId = $this->_db->getTable('Element')->findByElementSetNameAndElementName('PBCore', 'Identifier')->id;
}
$sql = "
INSERT INTO {$this->_db->ElementText} (record_id, record_type, element_id, html, text)
VALUES ('{$item->id}', 'Item', '{$elementId}', '0', '$text')
";
$this->_db->query($sql);
}
}
// Update or remove (see status).
else {
// Nothing, because the user should be able to remove or update the
// default identifier manually.
}
}
// TODO To check.
public function hookPublicThemeHeader()
{
$request = Zend_Controller_Front::getInstance()->getRequest();
if ($request->getControllerName() == 'items' && $request->getActionName() == 'show') {
echo '<link rel="alternate" type="application/rss+xml" href="' . record_url(get_current_record('item')) . '?output=pbcore" id="pbcore"/>' . PHP_EOL;
}
if ($request->getControllerName() == 'items' && $request->getActionName() == 'browse') {
echo '<link rel="alternate" type="application/rss+xml" href="' . record_url(get_current_record('item')) . '?output=pbcore" id="pbcore"/>' . PHP_EOL;
}
}
/**
* BeamMeUpToInternetArchive hook used to get list of metadata.
*
* Note that default Dublin Core metadata are removed.
*/
public function hookBeamiaSetSettings($args)
{
// Don't use Dublin Core metadata that are created by default.
$settings = array();
$record = $args['record'];
$elementSetName = $this->_elementSetName;
// Add existing elements.
$options = array(
'show_empty_elements' => false,
'return_type' => 'array',
);
if ($elementSetName) {
$options['show_element_sets'] = $elementSetName;
}
$elementTexts = all_element_texts($record, $options);
// Don't add "Dublin Core" in the header, because this is the standard
// on Internet Archive.
$cleanElementSetName = ($elementSetName == 'Dublin Core') ?
'' :
preg_replace('#[^a-z0-9]+#', '-', strtolower($elementSetName)) . '-';
foreach ($elementTexts[$elementSetName] as $element => $texts) {
// Replace unique or serie of non-alphanumeric character by "-".
$meta = preg_replace('#[^a-z0-9]+#', '-', strtolower($element));
foreach ($texts as $key => $text) {
$base = (count($texts) == 1) ?
'x-archive-meta-' :
'x-archive-meta' . sprintf('%02d', $key) . '-';
$settings[] = $base . $cleanElementSetName . $meta . ':' . $text;
}
// Add default title if it exists. If none, a generic name will be
// added automatically.
if ($element == 'Title') {
$settings[] = 'x-archive-meta-title:' . $texts[0];
}
}
$args['settings'] = $settings;
}
public function filterResponseContexts($contexts)
{
$contexts['pbcore'] = array(
'suffix' => 'pbcore',
'headers' => array('Content-Type' => 'text/xml'),
);
return $contexts;
}
public function filterActionContexts($contexts, $controller)
{
if ($controller['controller'] instanceof ItemsController) {
$contexts['show'][] = 'pbcore';
$contexts['browse'][] = 'pbcore';
}
return $contexts;
}
private function _getElementSet($elementSetName)
{
return $this->_db
->getTable('ElementSet')
->findByName($elementSetName);
}
private function _deleteElementSet($elementSetName)
{
$elementSet = $this->_getElementSet($elementSetName);
if ($elementSet) {
$elements = $elementSet->getElements();
foreach ($elements as $element) {
$element->delete();
}
$elementSet->delete();
}
}
}