-
Notifications
You must be signed in to change notification settings - Fork 0
/
BookCorePlugin.php
302 lines (266 loc) · 11.9 KB
/
BookCorePlugin.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* BookCore Plugin
*
* @copyright Marcus Burkhardt
* @license http://www.apache.org/licenses/LICENSE-2.0.html
*/
/**
* The BookCore Plugin
* @package Omeka\Plugins\BookCorePlugin
*/
class BookCorePlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array(
'install',
'uninstall',
'after_save_item'
);
/**
* @var array Filters for the plugin.
*/
protected $_filters = array(
'admin_items_form_tabs'
);
/**
* Install the plugin.
*/
public function hookInstall()
{
$elementSetMetadata = array(
'name' => 'Book',
'description' => 'This metadata set allows to add book metadata'
);
$elements = array(
array(
'name' => 'Title'
),
array(
'name' => 'Subtitle',
'_refines' => 'Title'
),
array(
'name' => 'Author/Editor',
'description' => 'Name of the Authors or Editors to be added in this field: Last Name, First Name'
),
array(
'name' => 'Publisher'
),
array(
'name' => 'Year Published',
'description' => 'The year the book has been published to be added in this field'
),
array(
'name' => 'Blurb'
),
array(
'name' => 'Keywords'
),
array(
'name' => 'Series'
),
array(
'name' => 'ISBN Print'
),
array(
'name' => 'ISBN PDF'
),
array(
'name' => 'ISBN EPUB'
),
array(
'name' => 'DOI'
),
array(
'name' => 'Rights',
'description' => 'The license or copyright notice is to be added in this field'
),
array(
'name' => 'Language'
),
array(
'name' => 'Type',
'description' => 'The type of publication is to be added in this field: Usually it will be just Book, Peer-reviewed Book, Edited Book'
),
array(
'name' => 'Format',
'description' => 'Add available formats in this field'
),
);
insert_element_set($elementSetMetadata, $elements);
}
/**
* Uninstall the plugin
*/
public function hookUninstall()
{
$db = get_db();
$elementSet = get_db()->getTable('ElementSet')->findByName('Book');
$elementSet->delete();
}
/**
* After saving an item with data in the Book Element Set values are automatically
* mapped on the Dublin Core Element Set. Preexisting DC values are deleted
*/
public function hookAfterSaveItem($args)
{
$item = $args['record'];
$id = $item['id'];
$db = get_db();
$DCElementSetIDSelect = $db -> select() -> from (array('omeka_element_sets'), array('id')) -> where ('name = ?', 'Dublin Core');
$DCElementSetID = $db -> fetchOne($DCElementSetIDSelect);
$DCElementIDsSelect = $db -> select() -> from (array('omeka_elements'), array('id')) -> where ('element_set_id = ?', $DCElementSetID);
$DCElementIDs= $db -> fetchCol($DCElementIDsSelect);
/**
* Removes the content of DC fields if Book Element Set has values
*/
$hasBookElements = $this->has_book_element_texts($item);
if ($hasBookElements == true)
{
foreach ($DCElementIDs as $DCElementID) {
$db->delete('omeka_element_texts', array('record_id =' . $id, 'element_id =' . $DCElementID));
}
}
else {}
/**
* Mapping rules for fields from the Book Element Set to the Dublin Core Fields
*/
$mapping = array('Title' => 'Title', 'Author/Editor' => 'Creator', 'Publisher' => 'Publisher',
'Year Published' => 'Date', 'Blurb' => 'Description', 'Keywords' => 'Subject', 'Series' => 'Description',
'ISBN Print' => 'Identifier', 'ISBN PDF' => 'Identifier', 'ISBN EPUB' => 'Identifier',
'DOI' => 'Identifier', 'Rights' => 'Rights', 'Language' => 'Language', 'Type' => 'Type',
'Format' => 'Format');
$bookElementSetIDSelect = $db -> select() -> from (array('omeka_element_sets'), array('id')) -> where ('name = ?', 'Book');
$bookElementSetID = $db -> fetchOne($bookElementSetIDSelect);
$bookElementSubtitleIDSelect = $db -> select() -> from (array('omeka_elements'), array('id'))
-> where ('element_set_id = ?', $bookElementSetID)
-> where ('name = ?', 'Subtitle');
$bookElementSubtitleID = $db -> fetchOne($bookElementSubtitleIDSelect);
foreach ($mapping as $bookField => $DCField) {
$bookElementTexts = $item->getElementTexts('Book', $bookField);
foreach ($bookElementTexts as $bookElementText){
$dcElementIDSelect = $db -> select() -> from (array('omeka_elements'), array('id'))
-> where ('element_set_id = ?', $DCElementSetID)
-> where ('name = ?', $DCField);
$dcID = $db -> fetchOne($dcElementIDSelect);
$bookElementIDSelect = $db -> select() -> from (array('omeka_elements'), array('id'))
-> where ('element_set_id = ?', $bookElementSetID)
-> where ('name = ?', $bookField);
$bookID = $db -> fetchOne($bookElementIDSelect);
$htmlSelect = $db -> select() -> from (array('omeka_element_texts'), array('html'))
-> where ('element_id = ?', $bookID)
-> where ('record_id = ?', $id)
-> where ('text = ?', $bookElementText);
$html = $db -> fetchOne($htmlSelect);
/**
* Combines Title and Subtitle Fields to DC:title = Title : Subtitle 1 : Subtitle 2
* and checks if either Title or Subtitle has been marked up as HTML
*/
if ($bookField == 'Title') {
$bookSubtitles = $item->getElementTexts('Book', 'Subtitle');
foreach ($bookSubtitles as $bookSubtitleElement) {
$bookSubtitle = $bookSubtitle . ' : ' . $bookSubtitleElement;
$htmlSubtitleSelect = $db -> select() -> from (array('omeka_element_texts'), array('html'))
-> where ('element_id = ?', $bookElementSubtitleID)
-> where ('record_id = ?', $id)
-> where ('text = ?', $bookSubtitleElement);
$htmlTemp = $db -> fetchOne($htmlSubtitleSelect);
if ($htmlTemp == '1'){
$html= '1';
}
}
$dcElementValues = array(
'record_id' => $id, // 'record_id'
'record_type' => 'Item',
'element_id' => $dcID, // 'element_id'
'html' => $html, // 'html'
'text' => $bookElementText . $bookSubtitle);
$db->insert('element texts', $dcElementValues);
}
/**
* Mapping rules for all other fields from Book Element Set to DC
* > Identifier are amended with a description of their type.
* > All other Fields are mapped according to the rules defined in the array $mapping
*/
elseif ($bookField == 'ISBN Print') {
$dcElementValues = array(
'record_id' => $id, // 'record_id'
'record_type' => 'Item',
'element_id' => $dcID, // 'element_id'
'html' => $html, // 'html'
'text' => 'ISBN Print: ' . $bookElementText);
$db->insert('element texts', $dcElementValues);
}
elseif ($bookField == 'ISBN PDF') {
$dcElementValues = array(
'record_id' => $id, // 'record_id'
'record_type' => 'Item',
'element_id' => $dcID, // 'element_id'
'html' => $html, // 'html'
'text' => 'ISBN PDF: ' . $bookElementText);
$db->insert('element texts', $dcElementValues);
}
elseif ($bookField == 'ISBN EPUB') {
$dcElementValues = array(
'record_id' => $id, // 'record_id'
'record_type' => 'Item',
'element_id' => $dcID, // 'element_id'
'html' => $html, // 'html'
'text' => 'ISBN EPUB: ' . $bookElementText);
$db->insert('element texts', $dcElementValues);
}
elseif ($bookField == 'DOI') {
$dcElementValues = array(
'record_id' => $id, // 'record_id'
'record_type' => 'Item',
'element_id' => $dcID, // 'element_id'
'html' => $html, // 'html'
'text' => 'DOI: ' . $bookElementText);
$db->insert('element texts', $dcElementValues);
}
elseif ($bookField == 'Series') {
$dcElementValues = array(
'record_id' => $id, // 'record_id'
'record_type' => 'Item',
'element_id' => $dcID, // 'element_id'
'html' => $html, // 'html'
'text' => 'Series: ' . $bookElementText);
$db->insert('element texts', $dcElementValues);
}
else {
$dcElementValues = array(
'record_id' => $id, // 'record_id'
'record_type' => 'Item',
'element_id' => $dcID, // 'element_id'
'html' => $html, // 'html'
'text' => $bookElementText);
$db->insert('element texts', $dcElementValues);
}
}
}
}
public function filterAdminItemsFormTabs($tabs, $args)
{
$ItemAdminOrder = array('Book' => '', 'Files' => '', 'Tags' => '', 'Item Type Metadata' => '', 'Dublin Core' => '');
return (array_merge ($ItemAdminOrder, $tabs));
// return ($tabs);
}
/**
* Checks if any field of the Book Element Set has been filled out. If yes, true is returned.
*/
public function has_book_element_texts($item){
$bookElementFields = array('Title', 'Subtitle', 'Author/Editor', 'Publisher', 'Year Published', 'Blurb',
'Keywords', 'ISBN Print', 'ISBN PDF', 'ISBN EPUB', 'DOI',
'Rights', 'Language', 'Type', 'Format');
foreach ($bookElementFields as $bookElementField){
$bookElementsTest = array($item->getElementTexts('Book', $bookElementField));
if ($bookElementsTest[0] != NULL){
return true;
}
}
return false;
}
}