-
Notifications
You must be signed in to change notification settings - Fork 10
/
islandora_chem_sp_blocks.inc
190 lines (159 loc) · 5.32 KB
/
islandora_chem_sp_blocks.inc
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
<?php
/**
* @file
*/
function islandora_chem_sp_ingest_form($form_state) {
$form = array();
$form['#attributes'] = array('enctype' => "multipart/form-data");
$form['formats'] = array(
'#value' => l(t('Supported formats'), 'formats'),
'#suffix' => '<br/>',
);
$form['upload'] = array(
'#type' => 'file',
);
$form['structure_submit'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
'#name' => 'ingest_submit',
);
return $form;
}
function islandora_chem_sp_ingest_form_validate($form, &$form_state) {
$field_name = 'upload';
if (isset($_FILES['files']) && is_uploaded_file($_FILES['files']['tmp_name'][$field_name])) {
$file = file_save_upload($field_name);
if (!$file) {
form_set_error($field_name, 'Error uploading file.');
return;
}
$form_state['values']['file'] = $file;
}
else {
form_set_error($field_name, 'Error uploading file.');
return;
}
}
function islandora_chem_sp_ingest_form_submit($form, &$form_state) {
module_load_include('inc', 'islandora_chem_sp', 'FileConversion');
module_load_include('inc', 'islandora_content_model_forms', 'FOXML');
module_load_include('inc', 'fedora_repository', 'CollectionPolicy');
module_load_include('inc', 'islandora_chem_sp', 'chem');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'islandora_chem_sp', 'includes/islandora_chem_sp_batch');
global $user;
$file = $form_state['values']['file'];
$contents = file_get_contents($file->filepath);
$ext = strrchr($file->filename, '.');
$ext = substr($ext, 1);
$allowed_extensions = array(
'mol',
'xyz',
'pdb',
'cdx',
'c3d',
'chm',
'cif',
'cml',
'inp',
'gam',
'out',
'cub',
'gau',
'gal',
'jdx',
'dx',
'mop',
'in',
'zip',
);
if (!in_array($ext, $allowed_extensions) && $ext != NULL) {
drupal_set_message(t('The file you uploaded is not an allowed type!'), 'warning');
return;
}
if ($ext == 'zip') {
islandora_chem_sp_batch_ingest($file->filepath);
return;
}
if ($ext == NULL) {
$start = substr($contents, 0, 20);
switch ($start) {
case 'MacSPARTAN \'08 MECHA':
drupal_set_message(t('This is a MacSpartan output file'));
$file_name = islandora_chem_sp_convert_spartan_output($file->filepath);
break;
case ' OPT B3LYP 6-31G* CO':
drupal_set_message(t('This is a MacSpartan input file (smol)'));
$file_name = islandora_chem_sp_convert_spartan($file->filepath);
break;
default:
drupal_set_message(t('I have no idea what this file is!'), 'warning');
}
}
else {
$file_name = $file->filepath;
}
islandora_chem_sp_simple_ingest($file_name);
$form_state['redirect'] = $base_url . "/fedora/repository/{$collection_pid}";
}
function islandora_chem_sp_simple_ingest($file_name, $separate = TRUE) {
$file_name_array = array();
$file_name_array['OBJ'] = $file_name;
$title = basename($file_name);
$transform = drupal_get_path('module', 'islandora_content_model_forms') . "/transforms/mods_to_dc.xsl";
$content_model_pid = 'islandora:sp_chem_CM';
$collection_pid = 'quantumchem:sp_chem_calculations';
$dsid = 'MODS';
$pid = Fedora_Item::get_next_PID_in_namespace('quantumchem');
$username = $user->name;
$xml = "<mods xmlns=\"http://www.loc.gov/mods/v3\">
<titleInfo>
<title>$title</title>
</titleInfo>
<name type=\"personal\">
<namePart>$username</namePart>
<role>
<roleTerm authority=\"marcrelator\" type=\"text\">Author</roleTerm>
</role>
</name>
</mods>";
$document = new DOMDocument();
$document->loadXML($xml);
$document2 = $document->saveXML();
$document3 = new DOMDocument();
$document3->loadXML($document2);
$collection_policy = CollectionPolicy::loadFromCollection($collection_pid);
if ($collection_policy !== FALSE) {
$relationship = $collection_policy->getRelationship();
}
$ingest = new IslandoraChem($pid);
$mol = $ingest->chem2mol(NULL, 'MOL', $file_name, 'mol');
if ($mol != 1) {
drupal_set_message(t("Creating an archival version of $file_name failed!"), 'error');
return;
}
$file_name_array['MOL'] = $_SESSION['fedora_ingest_files']['MOL'];
$cml = $ingest->chem2cml(NULL, 'CML', $file_name, 'xml', $separate);
if ($cml != 1) {
drupal_set_message(t("Extracting metadata from $file_name failed!"), 'error');
return;
}
$file_name_array['CML'] = $_SESSION['fedora_ingest_files']['CML'];
$jpg = $ingest->chem2jpg(array('width' => '100', 'height' => '100'), 'TN', $file_name, 'jpg');
if ($jpg != 1) {
drupal_set_message(t("Creating the thumbnail for $file_name failed!"), 'error');
return;
}
$file_name_array['TN'] = $_SESSION['fedora_ingest_files']['TN'];
$medium = $ingest->chem2jpg(array('width' => '250', 'height' => '250'), 'MEDIUM', $file_name, 'jpg');
if ($medium != 1) {
drupal_set_message(t("Creating an image of $file_name failed!"), 'error');
return;
}
$file_name_array['MEDIUM'] = $_SESSION['fedora_ingest_files']['MEDIUM'];
$object = new FOXML($title, $pid, $dsid, $content_model_pid, $collection_pid, $relationship, $file_name_array, $document3, $transform);
$submit = $object->ingest();
if (!$submit) {
drupal_set_message(t("An error occurred when saving the file!"), 'error');
}
}