-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
380 lines (336 loc) · 13.6 KB
/
plugin.php
File metadata and controls
380 lines (336 loc) · 13.6 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* CsvImport plugin
*
* @copyright Center for History and New Media, 2008
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @version $Id:$
* @package CsvImport
* @author CHNM
**/
define('CSV_IMPORT_DIRECTORY', dirname(__FILE__));
define('CSV_IMPORT_CSV_FILES_DIRECTORY', CSV_IMPORT_DIRECTORY . DIRECTORY_SEPARATOR . 'csv_files');
define('CSV_IMPORT_BACKGROUND_SCRIPTS_DIRECTORY', CSV_IMPORT_DIRECTORY . DIRECTORY_SEPARATOR . 'background_scripts');
define('CSV_IMPORT_COLUMN_MAP_TAG_CHECKBOX_PREFIX', 'csv_import_column_map_tag_');
define('CSV_IMPORT_COLUMN_MAP_FILE_CHECKBOX_PREFIX', 'csv_import_column_map_file_');
define('CSV_IMPORT_COLUMN_MAP_HTML_CHECKBOX_PREFIX', 'csv_import_column_map_html_');
define('CSV_IMPORT_COLUMN_MAP_ELEMENTS_LIST_PREFIX', 'csv_import_column_map_elements_list_');
define('CSV_IMPORT_COLUMN_MAP_ELEMENTS_DROPDOWN_PREFIX', CSV_IMPORT_COLUMN_MAP_ELEMENTS_LIST_PREFIX . 'dropdown_');
define('CSV_IMPORT_COLUMN_MAP_ELEMENTS_HIDDEN_INPUT_PREFIX', CSV_IMPORT_COLUMN_MAP_ELEMENTS_LIST_PREFIX . 'hidden_input_');
add_plugin_hook('install', 'csv_import_install');
add_plugin_hook('uninstall', 'csv_import_uninstall');
add_plugin_hook('upgrade', 'csv_import_upgrade');
add_plugin_hook('config_form', 'csv_import_config_form');
add_plugin_hook('config', 'csv_import_config');
add_plugin_hook('admin_theme_header', 'csv_import_admin_header');
add_plugin_hook('define_acl', 'csv_import_define_acl');
add_filter('admin_navigation_main', 'csv_import_admin_navigation');
/**
* Install the plugin.
*
* @return void
*/
function csv_import_install()
{
$db = get_db();
// create csv imports table
$db->exec("CREATE TABLE IF NOT EXISTS `{$db->prefix}csv_import_imports` (
`id` int(10) unsigned NOT NULL auto_increment,
`item_type_id` int(10) unsigned NOT NULL,
`collection_id` int(10) unsigned NOT NULL,
`csv_file_name` text collate utf8_unicode_ci NOT NULL,
`status` varchar(255) collate utf8_unicode_ci,
`error_details` TEXT collate utf8_unicode_ci,
`item_count` int(10) unsigned NOT NULL,
`is_public` tinyint(1) default '0',
`is_featured` tinyint(1) default '0',
`stop_import_if_file_download_error` tinyint(1) default '0',
`serialized_column_maps` text collate utf8_unicode_ci NOT NULL,
`added` timestamp NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
// create csv imported items table
$db->exec("CREATE TABLE IF NOT EXISTS `{$db->prefix}csv_import_imported_items` (
`id` int(10) unsigned NOT NULL auto_increment,
`item_id` int(10) unsigned NOT NULL,
`import_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY (`import_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
}
/**
* Uninstall the plugin.
*
* @return void
*/
function csv_import_uninstall()
{
// delete the plugin options
delete_option('csv_import_memory_limit');
delete_option('csv_import_php_path');
// drop the tables
$db = get_db();
$sql = "DROP TABLE IF EXISTS `{$db->prefix}csv_import_imports`";
$db->query($sql);
$sql = "DROP TABLE IF EXISTS `{$db->prefix}csv_import_imported_items`";
$db->query($sql);
}
/** Upgrade the plugin.
*
* @return void
*/
function csv_import_upgrade($oldVersion, $newVersion)
{
}
/**
* Defines the ACL for the reports controllers.
*
* @param Omeka_Acl $acl Access control list
*/
function csv_import_define_acl($acl)
{
// only allow super users and admins to import csv files
$acl->loadResourceList(array(
'CsvImport_Index' => array('index', 'map-columns', 'undo-import', 'clear-history', 'status')
));
}
/**
* Add the admin navigation for the plugin.
*
* @return array
*/
function csv_import_admin_navigation($tabs)
{
if (get_acl()->checkUserPermission('CsvImport_Index', 'index')) {
$tabs['CSV Import'] = uri('csv-import');
}
return $tabs;
}
function csv_import_admin_header($request)
{
if ($request->getModuleName() == 'csv-import') {
queue_css('csv_import_main');
queue_js('csv-import');
}
}
/**
* Get the default value for an element.
* If the user has already submitted the value, then use that as the default,
* else return null
*
* @param string Element input name
* @param string Default value of the element
* @return string
*/
function csv_import_get_default_value($htmlInputElementName, $defaultValue = null)
{
// set the default file if the form is already submitted
if (isset($_POST[$htmlInputElementName])) {
$defaultValue = $_POST[$htmlInputElementName];
}
return $defaultValue;
}
/**
* Get the dropdown html code for csv files.
*
* @return string
*/
function csv_import_get_file_drop_down($dropDownName, $dropDownLabel)
{
$ht = '';
$csvFiles = CsvImport_File::getFiles();
foreach ($csvFiles as $csvFile) {
$values[$csvFile->getFileName()] = $csvFile->getFileName();
}
$ht .= '<div class="field">';
$ht .= select(array('name' => $dropDownName, 'id' => $dropDownName), $values, csv_import_get_default_value($dropDownName), $dropDownLabel);
$ht .= '</div>';
return $ht;
}
/**
* Get the dropdown box html code for item types
*
* @return string
*/
function csv_import_get_item_types_drop_down($dropDownName, $dropDownLabel)
{
$ht = '';
$ht .= '<div class="field">';
$ht .= select_item_type(array('name' => $dropDownName, 'id' => $dropDownName), csv_import_get_default_value($dropDownName), $dropDownLabel);
$ht .= '</div>';
return $ht;
}
/**
* Get the html code for mapping columns in the csv file to elements
*
* @return string
*/
function csv_import_get_column_mappings($csvImportFile, $csvImportItemTypeId)
{
$ht = '';
$colNames = $csvImportFile->getColumnNames();
$colExamples = $csvImportFile->getColumnExamples();
$itemElementIdsToNames = array();
$ht .= '<table id="csv-import-column-mappings-table" class="simple" cellspacing="0" cellpadding="0">';
$ht .= '<thead>';
$ht .= '<tr>';
$ht .= '<th>Column</th>';
$ht .= '<th>Example from CSV File</th>';
$ht .= '<th>Map To Element</th>';
$ht .= '<th>Use HTML?</th>';
$ht .= '<th>Tags?</th>';
$ht .= '<th>File?</th>';
$ht .= '</tr>';
$ht .= '</thead>';
$ht .= '<tbody>';
for($i = 0; $i < count($colNames); $i++) {
$ht .= '<tr>';
$ht .= '<td><strong>'.$colNames[$i].'</strong></td>';
$ht .= '<td>"' . $colExamples[$i] . '"</td>';
$ht .= '<td>'.csv_import_get_elements_for_column_mapping($i, $csvImportItemTypeId).'</td>';
$ht .= '<td>'.csv_import_checkbox(CSV_IMPORT_COLUMN_MAP_HTML_CHECKBOX_PREFIX . $i).'</td>';
$ht .= '<td>'.csv_import_checkbox(CSV_IMPORT_COLUMN_MAP_TAG_CHECKBOX_PREFIX . $i).'</td>';
$ht .= '<td>'.csv_import_checkbox(CSV_IMPORT_COLUMN_MAP_FILE_CHECKBOX_PREFIX . $i).'</td>';
$ht .= '</tr>';
}
$ht .= '</tbody>';
$ht .= '</table>';
return $ht;
}
/**
* Gets a div that allows users to add and remove elements for an column mapping
*
* @todo Fix the hidden helper function so it does not echo the output and then use it.
* @return string
*/
function csv_import_get_elements_for_column_mapping($columnIndex, $itemTypeId)
{
$elementsDropDownName = CSV_IMPORT_COLUMN_MAP_ELEMENTS_DROPDOWN_PREFIX . $columnIndex;
$elementsHiddenInputName = CSV_IMPORT_COLUMN_MAP_ELEMENTS_HIDDEN_INPUT_PREFIX . $columnIndex;
$elementsListName = CSV_IMPORT_COLUMN_MAP_ELEMENTS_LIST_PREFIX . $columnIndex;
$ht = '';
$ht .= '<div>';
$ht .= csv_import_get_item_elements_drop_down($elementsDropDownName, $itemTypeId, $elementsListName, $elementsHiddenInputName);
$ht .= '<input type="hidden" value="' . csv_import_get_default_value($elementsHiddenInputName) . '" name="' . $elementsHiddenInputName . '" id="' . $elementsHiddenInputName .'" />';
$ht .= '<span id="' . $elementsListName . '"></span>';
$ht .= '</div>';
return $ht;
}
/**
* Get the drop down html code that includes item elements from all of the item element sets,
* except for the "Item Type Metadata" element set, only get the elements for the item type
*
* @return string
*/
function csv_import_get_item_elements_drop_down($elementsDropDownName, $itemTypeId, $elementsListName, $elementsHiddenInputName)
{
$ht = '';
// get an associative array of elements where the key is the element set name and the value is the array of elements associated with the element set
// order the element sets by: Dublin Core, item type, and then all other element sets
$elementsByElementSetName = csv_import_get_elements_by_element_set_name($itemTypeId);
$onChange .= "csvImportAddElementToColumnMap('" . $elementsListName . "', '" . $elementsDropDownName ."', '" . $elementsHiddenInputName . "');this.selectedIndex=0;";
// get the select dropdown box
$ht .= select( array('name' => $elementsDropDownName, 'id' => $elementsDropDownName, 'class'=>'csv-import-element-select'), $elementsByElementSetName, csv_import_get_default_value($elementsDropDownName), null);
return $ht;
}
/**
* Get an associative array of elements where the key is the element set name and the value is an array of elements.
* The associative array will include the following sets of elements in the following order:
* Dublin Core element set,
* the set of elements associated with the item type,
* and then every other element set. Assumes that Dublin Core element set is the first element set in the database.
*
* @return string
*/
function csv_import_get_elements_by_element_set_name($itemTypeId)
{
$db = get_db();
$es = $db->getTable('ElementSet');
$elementSets = $es->findByRecordType('Item');
$elementsByElementSetName = array(); // associative array that maps element set name to arrays of item elements
foreach($elementSets as $elementSet) {
switch(trim($elementSet['name'])) {
// get the elements for the item type
case 'Item Type Metadata':
if (!empty($itemTypeId)) {
$sql = "SELECT e.id, e.name FROM `{$db->prefix}item_types_elements` AS ite, `{$db->prefix}elements` AS e
WHERE `ite`.`item_type_id` = ? AND `e`.`id` = `ite`.`element_id`";
$query = $db->query($sql, array($itemTypeId));
$itElementIdsToElementNames = array();
while ($itElement = $query->fetch()) {
$itElementIdsToElementNames[$itElement['id']] = $itElement['name'];
}
$itt = $db->getTable('ItemType');
$itemType = $itt->find($itemTypeId);
$elementsByElementSetName[$elementSet['name'] . ' - ' . $itemType['name']] = $itElementIdsToElementNames;
}
break;
// get the elements from the Dublin Core and each of the other element sets
default:
$oElementIdsToElementNames = array();
$oElements = $elementSet->getElements();
foreach($oElements as $oElement) {
$oElementIdsToElementNames[$oElement['id']] = $oElement['name'];
}
$elementsByElementSetName[$elementSet['name']] = $oElementIdsToElementNames;
break;
}
}
return $elementsByElementSetName;
}
/**
* Get the drop down html code for the collections
*
* @return string
*/
function csv_import_get_collections_drop_down($dropDownName, $dropDownLabel)
{
$ht = '';
$ht .= '<div class="field">';
$ht .= select_collection( array('name' => $dropDownName, 'id' => $dropDownName), csv_import_get_default_value($dropDownName), $dropDownLabel);
$ht .= '</div>';
return $ht;
}
/**
* Get the checkbox html code. Used for specifying whether items are public or featured
*
* @param string $checkBoxName
* @param string $checkBoxLabel
* @param string $isCheckedByDefault
* @return string
*/
function csv_import_checkbox($checkBoxName, $checkBoxLabel='', $divClass = '', $isCheckedByDefault=false)
{
$ht = '';
$ht .= '<div' . (!empty($divClass) ? (' class="' . html_escape($divClass) . '" ') : '' ) . '>';
$checked = (bool) csv_import_get_default_value($checkBoxName, $isCheckedByDefault);
if ($checkBoxLabel) {
$ht .= '<label for="' . html_escape($checkBoxName) . '">' . html_escape($checkBoxLabel) . '</label>';
}
$ht .= checkbox($attributes = array('name' => $checkBoxName, 'id' => $checkBoxName), $checked, null);
$ht .= '</div>';
return $ht;
}
function csv_import_config_form()
{
if (!$memoryLimit = get_option('csv_import_memory_limit')) {
$memoryLimit = ini_get('memory_limit');
}
?>
<div class="field">
<label for="csv_import_memory_limit">Memory Limit</label>
<?php echo __v()->formText('csv_import_memory_limit', $memoryLimit, null);?>
<p class="explanation">Set a high memory limit to avoid memory allocation
issues during harvesting. Examples include 128M, 1G, and -1. The available
options are K (for Kilobytes), M (for Megabytes) and G (for Gigabytes).
Anything else assumes bytes. Set to -1 for an infinite limit. Be advised
that many web hosts set a maximum memory limit, so this setting may be
ignored if it exceeds the maximum allowable limit. Check with your web host
for more information.</p>
</div>
<?php
}
function csv_import_config()
{
set_option('csv_import_memory_limit', $_POST['csv_import_memory_limit']);
}