Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 207 lines (197 sloc) 7.61 kb
39b3df0 @rwincewicz Most functionality is working now
rwincewicz authored
1 <?php
2
3 /**
4 * @file
5 *
6 * Functions for exporting citations from the Bibliography.
7 */
8
9 /**
10 * Export many citations in a particular format and redirect the user to another page.
11 */
12 function islandora_bibliography_export() {
13 $export = isset($_SESSION['bibliography']['export']) ? $_SESSION['bibliography']['export'] : NULL;
14 $filename = isset($export['filename']) ? $export['filename'] : NULL;
15 $mime_type = isset($export['mime_type']) ? $export['mime_type'] : NULL;
16 $download_filename = isset($export['download_filename']) ? $export['download_filename'] : $filename;
17 $redirect = isset($export['redirect']) ? $export['redirect'] : NULL;
18 unset($_SESSION['bibliography']['export']);
19 if (file_exists($filename)) {
20 $file_size = filesize($filename);
21 header("Content-type: $mime_type");
22 header("Content-length: $file_size");
23 header("Content-Disposition: attachment; filename=\"$download_filename\"");
24 header("Cache-control: private");
25 $curl_handle = curl_init();
26 if ($curl_handle !== FALSE) {
27 global $base_url;
28 $url = $base_url . '/' . $filename;
29 curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
30 curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, FALSE);
31 curl_setopt($curl_handle, CURLOPT_FAILONERROR, 1); // Fail on errors
32 curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
33 curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/4.0 pp(compatible; MSIE 5.01; Windows NT 5.0)");
34 curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 0); // return into a variable
35 curl_setopt($curl_handle, CURLOPT_URL, $url);
36 }
37 curl_exec($curl_handle);
38 curl_close($curl_handle);
39 }
40 else {
41 drupal_goto($redirect);
42 exit();
43 }
44 }
45
46 /**
47 *
48 * @param array $pids
49 * @param array $context
50 *
51 function _islandora_bibliography_batch_combine_mods($filename, $pid, array &$context) {
52 module_load_include('inc', 'fedora_repository', 'api/fedora_item');
53 $mods = new DOMDocument();
54 $mods->load($filename);
55 $item = new Fedora_Item($pid);
56 if ($item->exists() && isset($item->datastreams['MODS'])) {
57 $datastream = $item->get_datastream_dissemination('MODS');
58 $datastream = trim($datastream);
59 if (!empty($mods)) {
60 $doc = new DOMDocument();
61 $doc->loadXML($datastream);
62 $node = $mods->importNode($doc->documentElement, TRUE);
63 $mods->documentElement->appendChild($node);
64 $mods->save($filename);
65 }
66 }
67 }
68
69 /**
70 *
71 * @param array $pids
72 * @param string $redirect
73 * @param array $context
74 *
75 function _islandora_bibliography_convert_mods_to_ris($filename, array &$context) {
76 $context['results']['filename'] = bibutils_mods_file_to_ris_document($filename);
77 $context['results']['mime_type'] = 'application/x-Research-Info-Systems';
78 $context['results']['download_filename'] = 'export.ris';
79 }
80
81 /**
82 *
83 * @param type $redirect
84 * @param array $context
85 *
86 function _islandora_bibliography_redirect($redirect, array &$context) {
87 $context['results']['redirect'] = $redirect;
88 }
89
90 /**
91 *
92 * @param type $pid
93 *
94 function _islandora_bibliography_batch_convert_mods_to_json($pid, array &$context) {
95 module_load_include('inc', 'fedora_repository', 'api/fedora_item');
96 module_load_include('php', 'citeproc', 'generators/converter');
97 $item = new Fedora_Item($pid);
98 if ($item->exists() && isset($item->datastreams['MODS'])) {
99 $mods = $item->get_datastream_dissemination('MODS');
100 $mods = trim($mods);
101 if (!empty($mods)) {
102 $context['results']['data'][] = convert_mods_to_citeproc_jsons($mods);
103 }
104 }
105 }
106
107 function _islandora_bibliography_convert_json_to_rtf($style, array &$context) {
108 module_load_include('php', 'islandora_bibliography', 'class_rtf');
109 module_load_include('php', 'CiteProc', 'lib/citeproc-php/CiteProc');
110 /* Style *
111 $csl = new DomDocument();
112 $csl->load(drupal_get_path('module', 'CiteProc') . '/data/' . $style);
113 /* Citations *
114 $citeproc = new citeproc($csl->saveXML());
115 $rtf = new rtf();
116 foreach ($context['results']['data'] as $citation) {
117 $citation = (object) $citation;
118 $citation = json_decode(json_encode($citation)); // HACKKKKK!
119 $text = $citeproc->render($citation, 'bibliography');
120 $rtf->addText($text . '<br/>');
121 }
122 /* File *
123 $temp_file_dir = file_directory_temp();
124 $temp_file_name = file_create_filename("export.rtf", $temp_file_dir);
125 $file = fopen($temp_file_name, 'w');
126 if ($file !== FALSE) {
127 $document = $rtf->getDocument();
128 fwrite($file, $document);
129 fclose($file);
130 }
131 /* Batch Values *
132 $context['results']['filename'] = $temp_file_name;
133 $context['results']['download_filename'] = 'export.rtf';
134 $context['results']['mime_type'] = 'application/rtf';
135 }
136
137 function _islandora_bibliography_convert_rtf_to_pdf(array &$context) {
138 $output = array();
139 $return_value = FALSE;
140 $filename = $context['results']['filename'];
141 exec('soffice.bin --headless -convert-to pdf ' . $filename, $output, $return_value);
142 $filename = preg_replace('/\.rtf$/', '.pdf', $context['results']['filename']);
143 $context['results']['filename'] = $filename;
144 $context['results']['download_filename'] = 'export.pdf';
145 $context['results']['mime_type'] = 'application/pdf';
146 }
147
148 /**
149 *
150 * @param boolean $successful
151 * @param array $results
152 * @param array $operations
153 *
154 function _islandora_bibliography_export_citations($successful, array $results, array $operations) {
155 $_SESSION['bibliography']['export']['filename'] = $results['filename'];
156 $_SESSION['bibliography']['export']['download_filename'] = $results['download_filename'];
157 $_SESSION['bibliography']['export']['mime_type'] = $results['mime_type'];
158 $_SESSION['bibliography']['export']['redirect'] = $results['redirect'];
159 }
160
161 /**
162 *
163 * @param array $pids
164 * @param type $style
165 * @param string $file_format
166 * Either pdf, rtf, ris, if null display to screen.
167 *
168 function islandora_bibliography_export_citations(array $pids, $style, $file_format = NULL) {
169 $operations = array();
170 switch ($file_format) {
171 case 'RIS':
172 $temp_file_dir = file_directory_temp();
173 $temp_file = file_create_filename("mods.xml", $temp_file_dir);
174 $mods = new DOMDocument();
175 $mods->loadXML('<modsCollection xmlns="http://www.loc.gov/mods/v3" />');
176 $mods->save($temp_file);
177 foreach ($pids as $pid) {
178 $operations[] = array('_islandora_bibliography_batch_combine_mods', array($temp_file, $pid));
179 }
180 $operations[] = array('_islandora_bibliography_convert_mods_to_ris', array($temp_file));
181 $operations[] = array('_islandora_bibliography_redirect', array($_GET['q']));
182 break;
183 case 'RTF':
184 foreach ($pids as $pid) {
185 $operations[] = array('_islandora_bibliography_batch_convert_mods_to_json', array($pid));
186 }
187 $operations[] = array('_islandora_bibliography_convert_json_to_rtf', array($style));
188 $operations[] = array('_islandora_bibliography_redirect', array($_GET['q']));
189 break;
190 case 'PDF':
191 foreach ($pids as $pid) {
192 $operations[] = array('_islandora_bibliography_batch_convert_mods_to_json', array($pid));
193 }
194 $operations[] = array('_islandora_bibliography_convert_json_to_rtf', array($style));
195 $operations[] = array('_islandora_bibliography_convert_rtf_to_pdf', array());
196 $operations[] = array('_islandora_bibliography_redirect', array($_GET['q']));
197 break;
198 }
199 if (count($operations) > 0) {
200 $batch = array(
201 'title' => t('Exporting File'),
202 'operations' => $operations,
203 'finished' => '_islandora_bibliography_export_citations'
204 );
205 batch_set($batch);
206 }
207 }*/
Something went wrong with that request. Please try again.