Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / plugins / export / ExportOds.class.php
blobb1e9f91e729adc86e718cc84c940f29a99528dc7
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used to build OpenDocument Spreadsheet dumps of tables
6 * @package PhpMyAdmin-Export
7 * @subpackage ODS
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
16 $GLOBALS['ods_buffer'] = '';
17 require_once 'libraries/opendocument.lib.php';
19 /**
20 * Handles the export for the ODS class
22 * @package PhpMyAdmin-Export
23 * @subpackage ODS
25 class ExportOds extends ExportPlugin
27 /**
28 * Constructor
30 public function __construct()
32 $this->setProperties();
35 /**
36 * Sets the export ODS properties
38 * @return void
40 protected function setProperties()
42 $props = 'libraries/properties/';
43 include_once "$props/plugins/ExportPluginProperties.class.php";
44 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
45 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
46 include_once "$props/options/items/TextPropertyItem.class.php";
47 include_once "$props/options/items/BoolPropertyItem.class.php";
48 include_once "$props/options/items/HiddenPropertyItem.class.php";
50 $exportPluginProperties = new ExportPluginProperties();
51 $exportPluginProperties->setText('OpenDocument Spreadsheet');
52 $exportPluginProperties->setExtension('ods');
53 $exportPluginProperties->setMimeType(
54 'application/vnd.oasis.opendocument.spreadsheet'
56 $exportPluginProperties->setForceFile(true);
57 $exportPluginProperties->setOptionsText(__('Options'));
59 // create the root group that will be the options field for
60 // $exportPluginProperties
61 // this will be shown as "Format specific options"
62 $exportSpecificOptions = new OptionsPropertyRootGroup();
63 $exportSpecificOptions->setName("Format Specific Options");
65 // general options main group
66 $generalOptions = new OptionsPropertyMainGroup();
67 $generalOptions->setName("general_opts");
68 // create primary items and add them to the group
69 $leaf = new TextPropertyItem();
70 $leaf->setName("null");
71 $leaf->setText(__('Replace NULL with:'));
72 $generalOptions->addProperty($leaf);
73 $leaf = new BoolPropertyItem();
74 $leaf->setName("columns");
75 $leaf->setText(__('Put columns names in the first row'));
76 $generalOptions->addProperty($leaf);
77 $leaf = new HiddenPropertyItem();
78 $leaf->setName("structure_or_data");
79 $generalOptions->addProperty($leaf);
80 // add the main group to the root group
81 $exportSpecificOptions->addProperty($generalOptions);
83 // set the options for the export plugin property item
84 $exportPluginProperties->setOptions($exportSpecificOptions);
85 $this->properties = $exportPluginProperties;
88 /**
89 * This method is called when any PluginManager to which the observer
90 * is attached calls PluginManager::notify()
92 * @param SplSubject $subject The PluginManager notifying the observer
93 * of an update.
95 * @return void
97 public function update (SplSubject $subject)
102 * Outputs export header
104 * @return bool Whether it succeeded
106 public function exportHeader ()
108 $GLOBALS['ods_buffer'] .= '<?xml version="1.0" encoding="utf-8"?' . '>'
109 . '<office:document-content '
110 . $GLOBALS['OpenDocumentNS'] . 'office:version="1.0">'
111 . '<office:automatic-styles>'
112 . '<number:date-style style:name="N37"'
113 .' number:automatic-order="true">'
114 . '<number:month number:style="long"/>'
115 . '<number:text>/</number:text>'
116 . '<number:day number:style="long"/>'
117 . '<number:text>/</number:text>'
118 . '<number:year/>'
119 . '</number:date-style>'
120 . '<number:time-style style:name="N43">'
121 . '<number:hours number:style="long"/>'
122 . '<number:text>:</number:text>'
123 . '<number:minutes number:style="long"/>'
124 . '<number:text>:</number:text>'
125 . '<number:seconds number:style="long"/>'
126 . '<number:text> </number:text>'
127 . '<number:am-pm/>'
128 . '</number:time-style>'
129 . '<number:date-style style:name="N50"'
130 . ' number:automatic-order="true"'
131 . ' number:format-source="language">'
132 . '<number:month/>'
133 . '<number:text>/</number:text>'
134 . '<number:day/>'
135 . '<number:text>/</number:text>'
136 . '<number:year/>'
137 . '<number:text> </number:text>'
138 . '<number:hours number:style="long"/>'
139 . '<number:text>:</number:text>'
140 . '<number:minutes number:style="long"/>'
141 . '<number:text> </number:text>'
142 . '<number:am-pm/>'
143 . '</number:date-style>'
144 . '<style:style style:name="DateCell" style:family="table-cell"'
145 . ' style:parent-style-name="Default" style:data-style-name="N37"/>'
146 . '<style:style style:name="TimeCell" style:family="table-cell"'
147 . ' style:parent-style-name="Default" style:data-style-name="N43"/>'
148 . '<style:style style:name="DateTimeCell" style:family="table-cell"'
149 . ' style:parent-style-name="Default" style:data-style-name="N50"/>'
150 . '</office:automatic-styles>'
151 . '<office:body>'
152 . '<office:spreadsheet>';
153 return true;
157 * Outputs export footer
159 * @return bool Whether it succeeded
161 public function exportFooter ()
163 $GLOBALS['ods_buffer'] .= '</office:spreadsheet>'
164 . '</office:body>'
165 . '</office:document-content>';
166 if (! PMA_exportOutputHandler(
167 PMA_createOpenDocument(
168 'application/vnd.oasis.opendocument.spreadsheet',
169 $GLOBALS['ods_buffer']
171 )) {
172 return false;
174 return true;
178 * Outputs database header
180 * @param string $db Database name
182 * @return bool Whether it succeeded
184 public function exportDBHeader ($db)
186 return true;
190 * Outputs database footer
192 * @param string $db Database name
194 * @return bool Whether it succeeded
196 public function exportDBFooter ($db)
198 return true;
202 * Outputs CREATE DATABASE statement
204 * @param string $db Database name
206 * @return bool Whether it succeeded
208 public function exportDBCreate($db)
210 return true;
214 * Outputs the content of a table in NHibernate format
216 * @param string $db database name
217 * @param string $table table name
218 * @param string $crlf the end of line sequence
219 * @param string $error_url the url to go back in case of error
220 * @param string $sql_query SQL query for obtaining data
222 * @return bool Whether it succeeded
224 public function exportData($db, $table, $crlf, $error_url, $sql_query)
226 global $what;
228 // Gets the data from the database
229 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED);
230 $fields_cnt = PMA_DBI_num_fields($result);
231 $fields_meta = PMA_DBI_get_fields_meta($result);
232 $field_flags = array();
233 for ($j = 0; $j < $fields_cnt; $j++) {
234 $field_flags[$j] = PMA_DBI_field_flags($result, $j);
237 $GLOBALS['ods_buffer'] .=
238 '<table:table table:name="' . htmlspecialchars($table) . '">';
240 // If required, get fields name at the first line
241 if (isset($GLOBALS[$what . '_columns'])) {
242 $GLOBALS['ods_buffer'] .= '<table:table-row>';
243 for ($i = 0; $i < $fields_cnt; $i++) {
244 $GLOBALS['ods_buffer'] .=
245 '<table:table-cell office:value-type="string">'
246 . '<text:p>'
247 . htmlspecialchars(
248 stripslashes(PMA_DBI_field_name($result, $i))
250 . '</text:p>'
251 . '</table:table-cell>';
252 } // end for
253 $GLOBALS['ods_buffer'] .= '</table:table-row>';
254 } // end if
256 // Format the data
257 while ($row = PMA_DBI_fetch_row($result)) {
258 $GLOBALS['ods_buffer'] .= '<table:table-row>';
259 for ($j = 0; $j < $fields_cnt; $j++) {
260 if (! isset($row[$j]) || is_null($row[$j])) {
261 $GLOBALS['ods_buffer'] .=
262 '<table:table-cell office:value-type="string">'
263 . '<text:p>'
264 . htmlspecialchars($GLOBALS[$what . '_null'])
265 . '</text:p>'
266 . '</table:table-cell>';
267 } elseif (stristr($field_flags[$j], 'BINARY')
268 && $fields_meta[$j]->blob
270 // ignore BLOB
271 $GLOBALS['ods_buffer'] .=
272 '<table:table-cell office:value-type="string">'
273 . '<text:p></text:p>'
274 . '</table:table-cell>';
275 } elseif ($fields_meta[$j]->type == "date") {
276 $GLOBALS['ods_buffer'] .=
277 '<table:table-cell office:value-type="date"'
278 . ' office:date-value="'
279 . date("Y-m-d", strtotime($row[$j]))
280 . '" table:style-name="DateCell">'
281 . '<text:p>'
282 . htmlspecialchars($row[$j])
283 . '</text:p>'
284 . '</table:table-cell>';
285 } elseif ($fields_meta[$j]->type == "time") {
286 $GLOBALS['ods_buffer'] .=
287 '<table:table-cell office:value-type="time"'
288 . ' office:time-value="'
289 . date("\P\TH\Hi\Ms\S", strtotime($row[$j]))
290 . '" table:style-name="TimeCell">'
291 . '<text:p>'
292 . htmlspecialchars($row[$j])
293 . '</text:p>'
294 . '</table:table-cell>';
295 } elseif ($fields_meta[$j]->type == "datetime") {
296 $GLOBALS['ods_buffer'] .=
297 '<table:table-cell office:value-type="date"'
298 . ' office:date-value="'
299 . date("Y-m-d\TH:i:s", strtotime($row[$j]))
300 . '" table:style-name="DateTimeCell">'
301 . '<text:p>'
302 . htmlspecialchars($row[$j])
303 . '</text:p>'
304 . '</table:table-cell>';
305 } elseif (($fields_meta[$j]->numeric
306 && $fields_meta[$j]->type != 'timestamp'
307 && ! $fields_meta[$j]->blob) || $fields_meta[$j]->type == 'real'
309 $GLOBALS['ods_buffer'] .=
310 '<table:table-cell office:value-type="float"'
311 . ' office:value="' . $row[$j] . '" >'
312 . '<text:p>'
313 . htmlspecialchars($row[$j])
314 . '</text:p>'
315 . '</table:table-cell>';
316 } else {
317 $GLOBALS['ods_buffer'] .=
318 '<table:table-cell office:value-type="string">'
319 . '<text:p>'
320 . htmlspecialchars($row[$j])
321 . '</text:p>'
322 . '</table:table-cell>';
324 } // end for
325 $GLOBALS['ods_buffer'] .= '</table:table-row>';
326 } // end while
327 PMA_DBI_free_result($result);
329 $GLOBALS['ods_buffer'] .= '</table:table>';
331 return true;