ExportOds.class.php 12 KB

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