-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathMain.php
More file actions
495 lines (448 loc) · 18.4 KB
/
Main.php
File metadata and controls
495 lines (448 loc) · 18.4 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
/**
* CsvImport_Form_Main class - represents the form on csv-import/index/index.
*
* @copyright Copyright 2007-2012 Roy Rosenzweig Center for History and New Media
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU GPLv3
* @package CsvImport
*/
class CsvImport_Form_Main extends Omeka_Form
{
private $_columnDelimiter;
private $_fileDelimiter;
private $_tagDelimiter;
private $_elementDelimiter;
private $_fileDestinationDir;
private $_maxFileSize;
/**
* Initialize the form.
*/
public function init()
{
parent::init();
$this->_columnDelimiter = CsvImport_RowIterator::getDefaultColumnDelimiter();
$this->_fileDelimiter = CsvImport_ColumnMap_File::getDefaultFileDelimiter();
$this->_tagDelimiter = CsvImport_ColumnMap_Tag::getDefaultTagDelimiter();
$this->_elementDelimiter = CsvImport_ColumnMap_Element::getDefaultElementDelimiter();
$this->setAttrib('id', 'csvimport');
$this->setMethod('post');
$this->_addFileElement();
$values = get_db()->getTable('ItemType')->findPairsForSelectForm();
$values = array('' => __('Select Item Type')) + $values;
$this->addElement('checkbox', 'omeka_csv_export', array(
'label' => __('Use an export from Omeka CSV Report'),
'description'=> __('Selecting this will override the options below.'))
);
$this->addElement('checkbox', 'automap_columns_names_to_elements', array(
'label' => __('Automap Column Names to Elements'),
'description'=> __('Automatically maps columns to elements based on their column names. The column name must be in the form: <br/> {ElementSetName}:{ElementName}'),
'value' => true)
);
$this->addElement('select', 'item_type_id', array(
'label' => __('Select Item Type'),
'multiOptions' => $values,
));
$values = get_db()->getTable('Collection')->findPairsForSelectForm();
$values = array('' => __('Select Collection')) + $values;
$this->addElement('select', 'collection_id', array(
'label' => __('Select Collection'),
'multiOptions' => $values,
));
$this->addElement('checkbox', 'items_are_public', array(
'label' => __('Make All Items Public?'),
));
$this->addElement('checkbox', 'items_are_featured', array(
'label' => __('Feature All Items?'),
));
$this->addElement('checkbox', 'remove_local_files', array(
'label' => __('Remove local files after successful import?'),
));
$this->_addIdentifierElementIdsElement();
$this->_addColumnDelimiterElement();
$this->_addTagDelimiterElement();
$this->_addFileDelimiterElement();
$this->_addElementDelimiterElement();
$this->applyOmekaStyles();
$this->setAutoApplyOmekaStyles(false);
$submit = $this->createElement('submit',
'submit',
array('label' => __('Next'),
'class' => 'submit submit-medium'));
$submit->setDecorators(array('ViewHelper',
array('HtmlTag',
array('tag' => 'div',
'class' => 'csvimportnext'))));
$this->addElement($submit);
}
protected function _getElementsOptions()
{
$db = get_db();
$sql = "
SELECT
es.name AS element_set_name,
e.id AS element_id,
e.name AS element_name,
it.name AS item_type_name
FROM {$db->ElementSet} es
JOIN {$db->Element} e ON es.id = e.element_set_id
LEFT JOIN {$db->ItemTypesElements} ite ON e.id = ite.element_id
LEFT JOIN {$db->ItemType} it ON ite.item_type_id = it.id
WHERE es.record_type IS NULL
OR (es.record_type = 'Item' AND it.name IS NOT NULL)
ORDER BY es.name, it.name, e.name
";
$elements = $db->fetchAll($sql);
$result = array();
foreach ($elements as $element) {
$group = $element['item_type_name']
? __('Item Type') . ': ' . __($element['item_type_name'])
: __($element['element_set_name']);
$result[$group][$element['element_id']] = $element['element_name'];
}
return $result;
}
protected function _addIdentifierElementIdsElement()
{
$this->addElement('multiselect', 'identifier_element_ids', array(
'label' => __('Choose Identifier Elements'),
'description' => __('Those elements will be compared to detect if an item already exists in database. If an item already exists, it will be skipped.'),
'size' => '6',
'multioptions' => $this->_getElementsOptions(),
));
}
/**
* Return the human readable word for a delimiter
*
* @param string $delimiter The delimiter
* @return string The human readable word for the delimiter
*/
protected function _getHumanDelimiterText($delimiter)
{
$delimiterText = $delimiter;
switch ($delimiter) {
case ',':
$delimiterText = __('comma');
break;
case ';':
$delimiterText = __('semi-colon');
break;
case '':
$delimiterText = __('empty');
break;
}
return $delimiterText;
}
/**
* Add the column delimiter element to the form
*/
protected function _addColumnDelimiterElement()
{
$delimiter = $this->_columnDelimiter;
$humanDelimiterText = $this->_getHumanDelimiterText($delimiter);
$this->addElement('text', 'column_delimiter', array(
'label' => __('Choose Column Delimiter'),
'description' => __('A single character that will be used to '
. 'separate columns in the file (%s by default).'
. ' Note that spaces, tabs, and other whitespace are not accepted.', $humanDelimiterText),
'value' => $delimiter,
'required' => true,
'size' => '1',
'validators' => array(
array('validator' => 'NotEmpty',
'breakChainOnFailure' => true,
'options' => array('messages' => array(
Zend_Validate_NotEmpty::IS_EMPTY =>
__('Column delimiter cannot be whitespace and must be one character long.'),
)),
),
array('validator' => 'StringLength', 'options' => array(
'min' => 1,
'max' => 1,
'messages' => array(
Zend_Validate_StringLength::TOO_SHORT =>
__('Column delimiter cannot be whitespace and must be one character long.'),
Zend_Validate_StringLength::TOO_LONG =>
__('Column delimiter cannot be whitespace and must be one character long.'),
),
)),
),
));
}
/**
* Add the file delimiter element to the form
*/
protected function _addFileDelimiterElement()
{
$delimiter = $this->_fileDelimiter;
$humanDelimiterText = $this->_getHumanDelimiterText($delimiter);
$this->addElement('text', 'file_delimiter', array(
'label' => __('Choose File Delimiter'),
'description' => __('A single character that will be used to '
. 'separate file paths or URLs within a cell (%s by default).'
. ' If the delimiter is empty, then the whole text will be used as the file path or URL. Note that spaces, tabs, and other whitespace are not accepted.', $humanDelimiterText),
'value' => $delimiter,
'required' => false,
'size' => '1',
'validators' => array(
array('validator' => 'NotEmpty',
'breakChainOnFailure' => true,
'options' => array('type' => 'space', 'messages' => array(
Zend_Validate_NotEmpty::IS_EMPTY =>
__('File delimiter cannot be whitespace, and must be empty or one character long.'),
)),
),
array('validator' => 'StringLength', 'options' => array(
'min' => 0,
'max' => 1,
'messages' => array(
Zend_Validate_StringLength::TOO_SHORT =>
__('File delimiter cannot be whitespace, and must be empty or one character long.'),
Zend_Validate_StringLength::TOO_LONG =>
__('File delimiter cannot be whitespace, and must be empty or one character long.'),
),
)),
),
));
}
/**
* Add the tag delimiter element to the form
*/
protected function _addTagDelimiterElement()
{
$delimiter = $this->_tagDelimiter;
$humanDelimiterText = $this->_getHumanDelimiterText($delimiter);
$this->addElement('text', 'tag_delimiter', array(
'label' => __('Choose Tag Delimiter'),
'description' => __('A single character that will be used to '
. 'separate tags within a cell (%s by default).'
. ' Note that spaces, tabs, and other whitespace are not accepted.', $humanDelimiterText),
'value' => $delimiter,
'required' => true,
'size' => '1',
'validators' => array(
array('validator' => 'NotEmpty',
'breakChainOnFailure' => true,
'options' => array('messages' => array(
Zend_Validate_NotEmpty::IS_EMPTY =>
__('Tag delimiter cannot be whitespace and must be one character long.'),
)),
),
array('validator' => 'StringLength', 'options' => array(
'min' => 1,
'max' => 1,
'messages' => array(
Zend_Validate_StringLength::TOO_SHORT =>
__('Tag delimiter cannot be whitespace and must be one character long.'),
Zend_Validate_StringLength::TOO_LONG =>
__('Tag delimiter cannot be whitespace and must be one character long.'),
),
)),
),
));
}
/**
* Add the element delimiter element to the form
*/
protected function _addElementDelimiterElement()
{
$delimiter = $this->_elementDelimiter;
$humanDelimiterText = $this->_getHumanDelimiterText($delimiter);
$this->addElement('text', 'element_delimiter', array(
'label' => __('Choose Element Delimiter'),
'description' => __('A single character that will be used to '
. 'separate metadata elements within a cell (%s by default).'
. ' If the delimiter is empty, then the whole text will be used as the element text. Note that spaces, tabs, and other whitespace are not accepted.', $humanDelimiterText),
'value' => $delimiter,
'required' => false,
'size' => '1',
'validators' => array(
array('validator' => 'NotEmpty',
'breakChainOnFailure' => true,
'options' => array('type' => 'space', 'messages' => array(
Zend_Validate_NotEmpty::IS_EMPTY =>
__('Element delimiter cannot be whitespace, and must be empty or one character long.'),
)),
),
array('validator' => 'StringLength', 'options' => array(
'min' => 0,
'max' => 1,
'messages' => array(
Zend_Validate_StringLength::TOO_SHORT =>
__('Element delimiter cannot be whitespace, and must be empty or one character long.'),
Zend_Validate_StringLength::TOO_LONG =>
__('Element delimiter cannot be whitespace, and must be empty or one character long.'),
),
)),
),
));
}
/**
* Add the file element to the form
*/
protected function _addFileElement()
{
$size = $this->getMaxFileSize();
$byteSize = clone $this->getMaxFileSize();
$byteSize->setType(Zend_Measure_Binary::BYTE);
$fileValidators = array(
new Zend_Validate_File_Size(array(
'max' => $byteSize->getValue())),
new Zend_Validate_File_Count(1),
);
if ($this->_requiredExtensions) {
$fileValidators[] =
new Omeka_Validate_File_Extension($this->_requiredExtensions);
}
if ($this->_requiredMimeTypes) {
$fileValidators[] =
new Omeka_Validate_File_MimeType($this->_requiredMimeTypes);
}
// Random filename in the temporary directory.
// Prevents race condition.
$filter = new Zend_Filter_File_Rename($this->_fileDestinationDir
. '/' . md5(mt_rand() + microtime(true)));
$this->addElement('file', 'csv_file', array(
'label' => __('Upload CSV File'),
'required' => true,
'validators' => $fileValidators,
'destination' => $this->_fileDestinationDir,
'description' => __("Maximum file size is %s.", $size->toString())
));
$this->csv_file->addFilter($filter);
}
/**
* Validate the form post
*/
public function isValid($post)
{
// Too much POST data, return with an error.
if (empty($post) && (int)$_SERVER['CONTENT_LENGTH'] > 0) {
$maxSize = $this->getMaxFileSize()->toString();
$this->csv_file->addError(
__('The file you have uploaded exceeds the maximum post size '
. 'allowed by the server. Please upload a file smaller '
. 'than %s.', $maxSize));
return false;
}
return parent::isValid($post);
}
/**
* Set the column delimiter for the form.
*
* @param string $delimiter The column delimiter
*/
public function setColumnDelimiter($delimiter)
{
$this->_columnDelimiter = $delimiter;
}
/**
* Set the file delimiter for the form.
*
* @param string $delimiter The file delimiter
*/
public function setFileDelimiter($delimiter)
{
$this->_fileDelimiter = $delimiter;
}
/**
* Set the tag delimiter for the form.
*
* @param string $delimiter The tag delimiter
*/
public function setTagDelimiter($delimiter)
{
$this->_tagDelimiter = $delimiter;
}
/**
* Set the element delimiter for the form.
*
* @param string $delimiter The element delimiter
*/
public function setElementDelimiter($delimiter)
{
$this->_elementDelimiter = $delimiter;
}
/**
* Set the file destination for the form.
*
* @param string $dest The file destination
*/
public function setFileDestination($dest)
{
$this->_fileDestinationDir = $dest;
}
/**
* Set the maximum size for an uploaded CSV file.
*
* If this is not set in the plugin configuration,
* defaults to the smaller of 'upload_max_filesize' and 'post_max_size'
* settings in php.
*
* If this is set but it exceeds the aforementioned php setting, the size
* will be reduced to that lower setting.
*
* @param string|null $size The maximum file size
*/
public function setMaxFileSize($size = null)
{
$postMaxSize = $this->_getBinarySize(ini_get('post_max_size'));
$fileMaxSize = $this->_getBinarySize(ini_get('upload_max_filesize'));
// Start with the max size as the lower of the two php ini settings.
$strictMaxSize = $postMaxSize->compare($fileMaxSize) > 0
? $fileMaxSize
: $postMaxSize;
// If the plugin max file size setting is lower, choose it as the strict max size
$pluginMaxSizeRaw = trim(get_option(CsvImportPlugin::MEMORY_LIMIT_OPTION_NAME));
if ($pluginMaxSizeRaw != '') {
if ($pluginMaxSize = $this->_getBinarySize($pluginMaxSizeRaw)) {
$strictMaxSize = $strictMaxSize->compare($pluginMaxSize) > 0
? $pluginMaxSize
: $strictMaxSize;
}
}
if ($size === null) {
$maxSize = $this->_maxFileSize;
} else {
$maxSize = $this->_getBinarySize($size);
}
if ($maxSize === false ||
$maxSize === null ||
$maxSize->compare($strictMaxSize) > 0) {
$maxSize = $strictMaxSize;
}
$this->_maxFileSize = $maxSize;
}
/**
* Return the max file size
*
* @return string The max file size
*/
public function getMaxFileSize()
{
if (!$this->_maxFileSize) {
$this->setMaxFileSize();
}
return $this->_maxFileSize;
}
/**
* Return the binary size measure
*
* @return Zend_Measure_Binary The binary size
*/
protected function _getBinarySize($size)
{
if (!preg_match('/(\d+)([KMG]?)/i', $size, $matches)) {
return false;
}
$sizeType = Zend_Measure_Binary::BYTE;
$sizeTypes = array(
'K' => Zend_Measure_Binary::KILOBYTE,
'M' => Zend_Measure_Binary::MEGABYTE,
'G' => Zend_Measure_Binary::GIGABYTE,
);
if (count($matches) == 3 && array_key_exists($matches[2], $sizeTypes)) {
$sizeType = $sizeTypes[$matches[2]];
}
return new Zend_Measure_Binary($matches[1], $sizeType);
}
}